Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/solaris/native/sun/awt/list.c
32287 views
/*1* Copyright (c) 1999, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/24/** ------------------------------------------------------------------------25This file contains routines for manipulating generic lists.26Lists are implemented with a "harness". In other words, each27node in the list consists of two pointers, one to the data item28and one to the next node in the list. The head of the list is29the same struct as each node, but the "item" ptr is used to point30to the current member of the list (used by the first_in_list and31next_in_list functions).3233This file is available under and governed by the GNU General Public34License version 2 only, as published by the Free Software Foundation.35However, the following notice accompanied the original version of this36file:3738Copyright 1994 Hewlett-Packard Co.39Copyright 1996, 1998 The Open Group4041Permission to use, copy, modify, distribute, and sell this software and its42documentation for any purpose is hereby granted without fee, provided that43the above copyright notice appear in all copies and that both that44copyright notice and this permission notice appear in supporting45documentation.4647The above copyright notice and this permission notice shall be included48in all copies or substantial portions of the Software.4950THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS51OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF52MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.53IN NO EVENT SHALL THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR54OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,55ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR56OTHER DEALINGS IN THE SOFTWARE.5758Except as contained in this notice, the name of The Open Group shall59not be used in advertising or otherwise to promote the sale, use or60other dealings in this Software without prior written authorization61from The Open Group.6263----------------------------------------------------------------------- **/6465#include <stdio.h>66#include <stdlib.h>6768#include "list.h"697071/** ------------------------------------------------------------------------72Sets the pointers of the specified list to NULL.73--------------------------------------------------------------------- **/74void zero_list(list_ptr lp)75{76lp->next = NULL;77lp->ptr.item = NULL;78}798081/** ------------------------------------------------------------------------82Adds item to the list pointed to by lp. Finds the end of the83list, then mallocs a new list node onto the end of the list.84The item pointer in the new node is set to "item" passed in,85and the next pointer in the new node is set to NULL.86Returns 1 if successful, 0 if the malloc failed.87-------------------------------------------------------------------- **/88int add_to_list(list_ptr lp, void *item)89{90while (lp->next) {91lp = lp->next;92}93if ((lp->next = (list_ptr) malloc( sizeof( list_item))) == NULL) {9495return 0;96}97lp->next->ptr.item = item;98lp->next->next = NULL;99100return 1;101}102103104/** ------------------------------------------------------------------------105Creates a new list and sets its pointers to NULL.106Returns a pointer to the new list.107-------------------------------------------------------------------- **/108list_ptr new_list (void)109{110list_ptr lp;111112if ((lp = (list_ptr) malloc( sizeof( list_item)))) {113lp->next = NULL;114lp->ptr.item = NULL;115}116117return lp;118}119120121/** ------------------------------------------------------------------------122Creates a new list head, pointing to the same list as the one123passed in. If start_at_curr is TRUE, the new list's first item124is the "current" item (as set by calls to first/next_in_list()).125If start_at_curr is FALSE, the first item in the new list is the126same as the first item in the old list. In either case, the127curr pointer in the new list is the same as in the old list.128Returns a pointer to the new list head.129-------------------------------------------------------------------- **/130list_ptr dup_list_head(list_ptr lp, int start_at_curr)131{132list_ptr new_listp;133134if ((new_listp = (list_ptr) malloc( sizeof( list_item))) == NULL) {135136return (list_ptr)NULL;137}138new_listp->next = start_at_curr ? lp->ptr.curr : lp->next;139new_listp->ptr.curr = lp->ptr.curr;140141return new_listp;142}143144145/** ------------------------------------------------------------------------146Returns the number of items in the list.147-------------------------------------------------------------------- **/148unsigned int list_length(list_ptr lp)149{150unsigned int count = 0;151152while (lp->next) {153count++;154lp = lp->next;155}156157return count;158}159160161/** ------------------------------------------------------------------------162Scans thru list, looking for a node whose ptr.item is equal to163the "item" passed in. "Equal" here means the same address - no164attempt is made to match equivalent values stored in different165locations. If a match is found, that node is deleted from the166list. Storage for the node is freed, but not for the item itself.167Returns a pointer to the item, so the caller can free it if it168so desires. If a match is not found, returns NULL.169-------------------------------------------------------------------- **/170void *delete_from_list(list_ptr lp, void *item)171{172list_ptr new_next;173174while (lp->next) {175if (lp->next->ptr.item == item) {176new_next = lp->next->next;177free (lp->next);178lp->next = new_next;179180return item;181}182lp = lp->next;183}184185return NULL;186}187188189/** ------------------------------------------------------------------------190Deletes each node in the list *except the head*. This allows191the deletion of lists where the head is not malloced or created192with new_list(). If free_items is true, each item pointed to193from the node is freed, in addition to the node itself.194-------------------------------------------------------------------- **/195void delete_list(list_ptr lp, int free_items)196{197list_ptr del_node;198void *item;199200while (lp->next) {201del_node = lp->next;202item = del_node->ptr.item;203lp->next = del_node->next;204free (del_node);205if (free_items) {206free( item);207}208}209}210211void delete_list_destroying(list_ptr lp, void destructor(void *item))212{213list_ptr del_node;214void *item;215216while (lp->next) {217del_node = lp->next;218item = del_node->ptr.item;219lp->next = del_node->next;220free( del_node);221if (destructor) {222destructor( item);223}224}225}226227228/** ------------------------------------------------------------------------229Returns a ptr to the first *item* (not list node) in the list.230Sets the list head node's curr ptr to the first node in the list.231Returns NULL if the list is empty.232-------------------------------------------------------------------- **/233void * first_in_list(list_ptr lp)234{235if (! lp) {236237return NULL;238}239lp->ptr.curr = lp->next;240241return lp->ptr.curr ? lp->ptr.curr->ptr.item : NULL;242}243244/** ------------------------------------------------------------------------245Returns a ptr to the next *item* (not list node) in the list.246Sets the list head node's curr ptr to the next node in the list.247first_in_list must have been called prior.248Returns NULL if no next item.249-------------------------------------------------------------------- **/250void * next_in_list(list_ptr lp)251{252if (! lp) {253254return NULL;255}256if (lp->ptr.curr) {257lp->ptr.curr = lp->ptr.curr->next;258}259260return lp->ptr.curr ? lp->ptr.curr->ptr.item : NULL;261}262263int list_is_empty(list_ptr lp)264{265return (lp == NULL || lp->next == NULL);266}267268269270