8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / tools / ctf / common / list.c
blobc01de9b9241d40c9eb1ae454a979d3fb7e95fcc0
1 /*
2 * CDDL HEADER START
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
7 * with the License.
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]
20 * CDDL HEADER END
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
33 #include <stdio.h>
34 #include <assert.h>
35 #include <stdlib.h>
37 #include "list.h"
38 #include "memory.h"
40 struct list {
41 void *l_data;
42 struct list *l_next;
45 /* Add an element to a list */
46 void
47 list_add(list_t **list, void *data)
49 list_t *le;
51 le = xmalloc(sizeof (list_t));
52 le->l_data = data;
53 le->l_next = *list;
54 *list = le;
57 /* Add an element to a sorted list */
58 void
59 slist_add(list_t **list, void *data, int (*cmp)(void *, void *))
61 list_t **nextp;
63 for (nextp = list; *nextp; nextp = &((*nextp)->l_next)) {
64 if (cmp((*nextp)->l_data, data) > 0)
65 break;
68 list_add(nextp, data);
71 /*ARGSUSED2*/
72 static int
73 list_defcmp(void *d1, void *d2, void *private)
75 return (d1 != d2);
78 void *
79 list_remove(list_t **list, void *data, int (*cmp)(void *, void *, void *),
80 void *private)
82 list_t *le, **le2;
83 void *led;
85 if (!cmp)
86 cmp = list_defcmp;
88 for (le = *list, le2 = list; le; le2 = &le->l_next, le = le->l_next) {
89 if (cmp(le->l_data, data, private) == 0) {
90 *le2 = le->l_next;
91 led = le->l_data;
92 free(le);
93 return (led);
97 return (NULL);
100 void
101 list_free(list_t *list, void (*datafree)(void *, void *), void *private)
103 list_t *le;
105 while (list) {
106 le = list;
107 list = list->l_next;
108 if (le->l_data && datafree)
109 datafree(le->l_data, private);
110 free(le);
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)
121 list_t *lnext;
122 int cumrc = 0;
123 int cbrc;
125 while (list) {
126 lnext = list->l_next;
127 if ((cbrc = func(list->l_data, private)) < 0)
128 return (cbrc);
129 cumrc += cbrc;
130 list = lnext;
133 return (cumrc);
136 /*ARGSUSED*/
137 static int
138 list_count_cb(void *data, void *private)
140 return (1);
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);
155 void *
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);
163 return (NULL);
166 void *
167 list_first(list_t *list)
169 return (list ? list->l_data : NULL);
172 void
173 list_concat(list_t **list1, list_t *list2)
175 list_t *l, *last;
177 for (l = *list1, last = NULL; l; last = l, l = l->l_next)
178 continue;
180 if (last == NULL)
181 *list1 = list2;
182 else
183 last->l_next = list2;
187 * Merges two sorted lists. Equal nodes (as determined by cmp) are retained.
189 void
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) {
196 *list1p = list2;
197 return;
200 list1 = *list1p;
201 while (list2 != NULL) {
202 if (cmp(list1->l_data, list2->l_data) > 0) {
203 next2 = list2->l_next;
205 if (last1 == NULL) {
206 /* Insert at beginning */
207 *list1p = last1 = list2;
208 list2->l_next = list1;
209 } else {
210 list2->l_next = list1;
211 last1->l_next = list2;
212 last1 = list2;
215 list2 = next2;
216 } else {
218 last1 = list1;
219 list1 = list1->l_next;
221 if (list1 == NULL) {
222 /* Add the rest to the end of list1 */
223 last1->l_next = list2;
224 list2 = NULL;