4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
30 * Routines for manipulating linked lists
45 /* Add an element to a list */
47 list_add(list_t **list, void *data)
51 le = xmalloc(sizeof (list_t));
57 /* Add an element to a sorted list */
59 slist_add(list_t **list, void *data, int (*cmp)(void *, void *))
63 for (nextp = list; *nextp; nextp = &((*nextp)->l_next)) {
64 if (cmp((*nextp)->l_data, data) > 0)
68 list_add(nextp, data);
73 list_defcmp(void *d1, void *d2, void *private __unused)
79 list_remove(list_t **list, void *data, int (*cmp)(void *, void *, void *),
88 for (le = *list, le2 = list; le; le2 = &le->l_next, le = le->l_next) {
89 if (cmp(le->l_data, data, private) == 0) {
101 list_free(list_t *list, void (*datafree)(void *, void *), void *private)
108 if (le->l_data && datafree)
109 datafree(le->l_data, private);
115 * This iterator is specifically designed to tolerate the deletion of the
116 * node being iterated over.
119 list_iter(list_t *list, int (*func)(void *, void *), void *private)
126 lnext = list->l_next;
127 if ((cbrc = func(list->l_data, private)) < 0)
138 list_count_cb(void *data __unused, void *private __unused)
144 list_count(list_t *list)
146 return (list_iter(list, list_count_cb, NULL));
150 list_empty(list_t *list)
152 return (list == NULL);
156 list_find(list_t *list, void *tmpl, int (*cmp)(void *, void *))
158 for (; list; list = list->l_next) {
159 if (cmp(list->l_data, tmpl) == 0)
160 return (list->l_data);
167 list_first(list_t *list)
169 return (list ? list->l_data : NULL);
173 list_concat(list_t **list1, list_t *list2)
177 for (l = *list1, last = NULL; l; last = l, l = l->l_next)
183 last->l_next = list2;
187 * Merges two sorted lists. Equal nodes (as determined by cmp) are retained.
190 slist_merge(list_t **list1p, list_t *list2, int (*cmp)(void *, void *))
192 list_t *list1, *next2;
193 list_t *last1 = NULL;
195 if (*list1p == NULL) {
201 while (list2 != NULL) {
202 if (cmp(list1->l_data, list2->l_data) > 0) {
203 next2 = list2->l_next;
206 /* Insert at beginning */
207 *list1p = last1 = list2;
208 list2->l_next = list1;
210 list2->l_next = list1;
211 last1->l_next = list2;
219 list1 = list1->l_next;
222 /* Add the rest to the end of list1 */
223 last1->l_next = list2;