Path: blob/main/cddl/contrib/opensolaris/tools/ctf/common/list.c
39562 views
/*1* CDDL HEADER START2*3* The contents of this file are subject to the terms of the4* Common Development and Distribution License, Version 1.0 only5* (the "License"). You may not use this file except in compliance6* with the License.7*8* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE9* or http://www.opensolaris.org/os/licensing.10* See the License for the specific language governing permissions11* and limitations under the License.12*13* When distributing Covered Code, include this CDDL HEADER in each14* file and include the License file at usr/src/OPENSOLARIS.LICENSE.15* If applicable, add the following below this CDDL HEADER, with the16* fields enclosed by brackets "[]" replaced with your own identifying17* information: Portions Copyright [yyyy] [name of copyright owner]18*19* CDDL HEADER END20*/21/*22* Copyright 2005 Sun Microsystems, Inc. All rights reserved.23* Use is subject to license terms.24*/2526#pragma ident "%Z%%M% %I% %E% SMI"2728/*29* Routines for manipulating linked lists30*/3132#include <stdio.h>33#include <assert.h>34#include <stdlib.h>3536#include "list.h"37#include "memory.h"3839struct list {40void *l_data;41struct list *l_next;42};4344/* Add an element to a list */45void46list_add(list_t **list, void *data)47{48list_t *le;4950le = xmalloc(sizeof (list_t));51le->l_data = data;52le->l_next = *list;53*list = le;54}5556/* Add an element to a sorted list */57void58slist_add(list_t **list, void *data, int (*cmp)(void *, void *))59{60list_t **nextp;6162for (nextp = list; *nextp; nextp = &((*nextp)->l_next)) {63if (cmp((*nextp)->l_data, data) > 0)64break;65}6667list_add(nextp, data);68}6970/*ARGSUSED2*/71static int72list_defcmp(void *d1, void *d2, void *private __unused)73{74return (d1 != d2);75}7677void *78list_remove(list_t **list, void *data, int (*cmp)(void *, void *, void *),79void *private)80{81list_t *le, **le2;82void *led;8384if (!cmp)85cmp = list_defcmp;8687for (le = *list, le2 = list; le; le2 = &le->l_next, le = le->l_next) {88if (cmp(le->l_data, data, private) == 0) {89*le2 = le->l_next;90led = le->l_data;91free(le);92return (led);93}94}9596return (NULL);97}9899void100list_free(list_t *list, void (*datafree)(void *, void *), void *private)101{102list_t *le;103104while (list) {105le = list;106list = list->l_next;107if (le->l_data && datafree)108datafree(le->l_data, private);109free(le);110}111}112113/*114* This iterator is specifically designed to tolerate the deletion of the115* node being iterated over.116*/117int118list_iter(list_t *list, int (*func)(void *, void *), void *private)119{120list_t *lnext;121int cumrc = 0;122int cbrc;123124while (list) {125lnext = list->l_next;126if ((cbrc = func(list->l_data, private)) < 0)127return (cbrc);128cumrc += cbrc;129list = lnext;130}131132return (cumrc);133}134135/*ARGSUSED*/136static int137list_count_cb(void *data __unused, void *private __unused)138{139return (1);140}141142int143list_count(list_t *list)144{145return (list_iter(list, list_count_cb, NULL));146}147148int149list_empty(list_t *list)150{151return (list == NULL);152}153154void *155list_find(list_t *list, void *tmpl, int (*cmp)(void *, void *))156{157for (; list; list = list->l_next) {158if (cmp(list->l_data, tmpl) == 0)159return (list->l_data);160}161162return (NULL);163}164165void *166list_first(list_t *list)167{168return (list ? list->l_data : NULL);169}170171void172list_concat(list_t **list1, list_t *list2)173{174list_t *l, *last;175176for (l = *list1, last = NULL; l; last = l, l = l->l_next)177continue;178179if (last == NULL)180*list1 = list2;181else182last->l_next = list2;183}184185/*186* Merges two sorted lists. Equal nodes (as determined by cmp) are retained.187*/188void189slist_merge(list_t **list1p, list_t *list2, int (*cmp)(void *, void *))190{191list_t *list1, *next2;192list_t *last1 = NULL;193194if (*list1p == NULL) {195*list1p = list2;196return;197}198199list1 = *list1p;200while (list2 != NULL) {201if (cmp(list1->l_data, list2->l_data) > 0) {202next2 = list2->l_next;203204if (last1 == NULL) {205/* Insert at beginning */206*list1p = last1 = list2;207list2->l_next = list1;208} else {209list2->l_next = list1;210last1->l_next = list2;211last1 = list2;212}213214list2 = next2;215} else {216217last1 = list1;218list1 = list1->l_next;219220if (list1 == NULL) {221/* Add the rest to the end of list1 */222last1->l_next = list2;223list2 = NULL;224}225}226}227}228229230