1*3c112a2bSEric Taylor /*
2*3c112a2bSEric Taylor * CDDL HEADER START
3*3c112a2bSEric Taylor *
4*3c112a2bSEric Taylor * The contents of this file are subject to the terms of the
5*3c112a2bSEric Taylor * Common Development and Distribution License (the "License").
6*3c112a2bSEric Taylor * You may not use this file except in compliance with the License.
7*3c112a2bSEric Taylor *
8*3c112a2bSEric Taylor * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*3c112a2bSEric Taylor * or http://www.opensolaris.org/os/licensing.
10*3c112a2bSEric Taylor * See the License for the specific language governing permissions
11*3c112a2bSEric Taylor * and limitations under the License.
12*3c112a2bSEric Taylor *
13*3c112a2bSEric Taylor * When distributing Covered Code, include this CDDL HEADER in each
14*3c112a2bSEric Taylor * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*3c112a2bSEric Taylor * If applicable, add the following below this CDDL HEADER, with the
16*3c112a2bSEric Taylor * fields enclosed by brackets "[]" replaced with your own identifying
17*3c112a2bSEric Taylor * information: Portions Copyright [yyyy] [name of copyright owner]
18*3c112a2bSEric Taylor *
19*3c112a2bSEric Taylor * CDDL HEADER END
20*3c112a2bSEric Taylor */
21*3c112a2bSEric Taylor /*
22*3c112a2bSEric Taylor * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
23*3c112a2bSEric Taylor */
24*3c112a2bSEric Taylor
25*3c112a2bSEric Taylor /*
26*3c112a2bSEric Taylor * Generic doubly-linked list implementation
27*3c112a2bSEric Taylor */
28*3c112a2bSEric Taylor
29*3c112a2bSEric Taylor #include <sys/list.h>
30*3c112a2bSEric Taylor #include <sys/list_impl.h>
31*3c112a2bSEric Taylor #include <sys/types.h>
32*3c112a2bSEric Taylor #include <sys/sysmacros.h>
33*3c112a2bSEric Taylor #ifdef _KERNEL
34*3c112a2bSEric Taylor #include <sys/debug.h>
35*3c112a2bSEric Taylor #else
36*3c112a2bSEric Taylor #include <assert.h>
37*3c112a2bSEric Taylor #define ASSERT(a) assert(a)
38*3c112a2bSEric Taylor #endif
39*3c112a2bSEric Taylor
40*3c112a2bSEric Taylor #ifdef lint
41*3c112a2bSEric Taylor extern list_node_t *list_d2l(list_t *list, void *obj);
42*3c112a2bSEric Taylor #else
43*3c112a2bSEric Taylor #define list_d2l(a, obj) ((list_node_t *)(((char *)obj) + (a)->list_offset))
44*3c112a2bSEric Taylor #endif
45*3c112a2bSEric Taylor #define list_object(a, node) ((void *)(((char *)node) - (a)->list_offset))
46*3c112a2bSEric Taylor #define list_empty(a) ((a)->list_head.list_next == &(a)->list_head)
47*3c112a2bSEric Taylor
48*3c112a2bSEric Taylor #define list_insert_after_node(list, node, object) { \
49*3c112a2bSEric Taylor list_node_t *lnew = list_d2l(list, object); \
50*3c112a2bSEric Taylor lnew->list_prev = (node); \
51*3c112a2bSEric Taylor lnew->list_next = (node)->list_next; \
52*3c112a2bSEric Taylor (node)->list_next->list_prev = lnew; \
53*3c112a2bSEric Taylor (node)->list_next = lnew; \
54*3c112a2bSEric Taylor }
55*3c112a2bSEric Taylor
56*3c112a2bSEric Taylor #define list_insert_before_node(list, node, object) { \
57*3c112a2bSEric Taylor list_node_t *lnew = list_d2l(list, object); \
58*3c112a2bSEric Taylor lnew->list_next = (node); \
59*3c112a2bSEric Taylor lnew->list_prev = (node)->list_prev; \
60*3c112a2bSEric Taylor (node)->list_prev->list_next = lnew; \
61*3c112a2bSEric Taylor (node)->list_prev = lnew; \
62*3c112a2bSEric Taylor }
63*3c112a2bSEric Taylor
64*3c112a2bSEric Taylor #define list_remove_node(node) \
65*3c112a2bSEric Taylor (node)->list_prev->list_next = (node)->list_next; \
66*3c112a2bSEric Taylor (node)->list_next->list_prev = (node)->list_prev; \
67*3c112a2bSEric Taylor (node)->list_next = (node)->list_prev = NULL
68*3c112a2bSEric Taylor
69*3c112a2bSEric Taylor void
list_create(list_t * list,size_t size,size_t offset)70*3c112a2bSEric Taylor list_create(list_t *list, size_t size, size_t offset)
71*3c112a2bSEric Taylor {
72*3c112a2bSEric Taylor ASSERT(list);
73*3c112a2bSEric Taylor ASSERT(size > 0);
74*3c112a2bSEric Taylor ASSERT(size >= offset + sizeof (list_node_t));
75*3c112a2bSEric Taylor
76*3c112a2bSEric Taylor list->list_size = size;
77*3c112a2bSEric Taylor list->list_offset = offset;
78*3c112a2bSEric Taylor list->list_head.list_next = list->list_head.list_prev =
79*3c112a2bSEric Taylor &list->list_head;
80*3c112a2bSEric Taylor }
81*3c112a2bSEric Taylor
82*3c112a2bSEric Taylor void
list_destroy(list_t * list)83*3c112a2bSEric Taylor list_destroy(list_t *list)
84*3c112a2bSEric Taylor {
85*3c112a2bSEric Taylor list_node_t *node = &list->list_head;
86*3c112a2bSEric Taylor
87*3c112a2bSEric Taylor ASSERT(list);
88*3c112a2bSEric Taylor ASSERT(list->list_head.list_next == node);
89*3c112a2bSEric Taylor ASSERT(list->list_head.list_prev == node);
90*3c112a2bSEric Taylor
91*3c112a2bSEric Taylor node->list_next = node->list_prev = NULL;
92*3c112a2bSEric Taylor }
93*3c112a2bSEric Taylor
94*3c112a2bSEric Taylor void
list_insert_after(list_t * list,void * object,void * nobject)95*3c112a2bSEric Taylor list_insert_after(list_t *list, void *object, void *nobject)
96*3c112a2bSEric Taylor {
97*3c112a2bSEric Taylor if (object == NULL) {
98*3c112a2bSEric Taylor list_insert_head(list, nobject);
99*3c112a2bSEric Taylor } else {
100*3c112a2bSEric Taylor list_node_t *lold = list_d2l(list, object);
101*3c112a2bSEric Taylor list_insert_after_node(list, lold, nobject);
102*3c112a2bSEric Taylor }
103*3c112a2bSEric Taylor }
104*3c112a2bSEric Taylor
105*3c112a2bSEric Taylor void
list_insert_before(list_t * list,void * object,void * nobject)106*3c112a2bSEric Taylor list_insert_before(list_t *list, void *object, void *nobject)
107*3c112a2bSEric Taylor {
108*3c112a2bSEric Taylor if (object == NULL) {
109*3c112a2bSEric Taylor list_insert_tail(list, nobject);
110*3c112a2bSEric Taylor } else {
111*3c112a2bSEric Taylor list_node_t *lold = list_d2l(list, object);
112*3c112a2bSEric Taylor list_insert_before_node(list, lold, nobject);
113*3c112a2bSEric Taylor }
114*3c112a2bSEric Taylor }
115*3c112a2bSEric Taylor
116*3c112a2bSEric Taylor void
list_insert_head(list_t * list,void * object)117*3c112a2bSEric Taylor list_insert_head(list_t *list, void *object)
118*3c112a2bSEric Taylor {
119*3c112a2bSEric Taylor list_node_t *lold = &list->list_head;
120*3c112a2bSEric Taylor list_insert_after_node(list, lold, object);
121*3c112a2bSEric Taylor }
122*3c112a2bSEric Taylor
123*3c112a2bSEric Taylor void
list_insert_tail(list_t * list,void * object)124*3c112a2bSEric Taylor list_insert_tail(list_t *list, void *object)
125*3c112a2bSEric Taylor {
126*3c112a2bSEric Taylor list_node_t *lold = &list->list_head;
127*3c112a2bSEric Taylor list_insert_before_node(list, lold, object);
128*3c112a2bSEric Taylor }
129*3c112a2bSEric Taylor
130*3c112a2bSEric Taylor void
list_remove(list_t * list,void * object)131*3c112a2bSEric Taylor list_remove(list_t *list, void *object)
132*3c112a2bSEric Taylor {
133*3c112a2bSEric Taylor list_node_t *lold = list_d2l(list, object);
134*3c112a2bSEric Taylor ASSERT(!list_empty(list));
135*3c112a2bSEric Taylor ASSERT(lold->list_next != NULL);
136*3c112a2bSEric Taylor list_remove_node(lold);
137*3c112a2bSEric Taylor }
138*3c112a2bSEric Taylor
139*3c112a2bSEric Taylor void *
list_remove_head(list_t * list)140*3c112a2bSEric Taylor list_remove_head(list_t *list)
141*3c112a2bSEric Taylor {
142*3c112a2bSEric Taylor list_node_t *head = list->list_head.list_next;
143*3c112a2bSEric Taylor if (head == &list->list_head)
144*3c112a2bSEric Taylor return (NULL);
145*3c112a2bSEric Taylor list_remove_node(head);
146*3c112a2bSEric Taylor return (list_object(list, head));
147*3c112a2bSEric Taylor }
148*3c112a2bSEric Taylor
149*3c112a2bSEric Taylor void *
list_remove_tail(list_t * list)150*3c112a2bSEric Taylor list_remove_tail(list_t *list)
151*3c112a2bSEric Taylor {
152*3c112a2bSEric Taylor list_node_t *tail = list->list_head.list_prev;
153*3c112a2bSEric Taylor if (tail == &list->list_head)
154*3c112a2bSEric Taylor return (NULL);
155*3c112a2bSEric Taylor list_remove_node(tail);
156*3c112a2bSEric Taylor return (list_object(list, tail));
157*3c112a2bSEric Taylor }
158*3c112a2bSEric Taylor
159*3c112a2bSEric Taylor void *
list_head(list_t * list)160*3c112a2bSEric Taylor list_head(list_t *list)
161*3c112a2bSEric Taylor {
162*3c112a2bSEric Taylor if (list_empty(list))
163*3c112a2bSEric Taylor return (NULL);
164*3c112a2bSEric Taylor return (list_object(list, list->list_head.list_next));
165*3c112a2bSEric Taylor }
166*3c112a2bSEric Taylor
167*3c112a2bSEric Taylor void *
list_tail(list_t * list)168*3c112a2bSEric Taylor list_tail(list_t *list)
169*3c112a2bSEric Taylor {
170*3c112a2bSEric Taylor if (list_empty(list))
171*3c112a2bSEric Taylor return (NULL);
172*3c112a2bSEric Taylor return (list_object(list, list->list_head.list_prev));
173*3c112a2bSEric Taylor }
174*3c112a2bSEric Taylor
175*3c112a2bSEric Taylor void *
list_next(list_t * list,void * object)176*3c112a2bSEric Taylor list_next(list_t *list, void *object)
177*3c112a2bSEric Taylor {
178*3c112a2bSEric Taylor list_node_t *node = list_d2l(list, object);
179*3c112a2bSEric Taylor
180*3c112a2bSEric Taylor if (node->list_next != &list->list_head)
181*3c112a2bSEric Taylor return (list_object(list, node->list_next));
182*3c112a2bSEric Taylor
183*3c112a2bSEric Taylor return (NULL);
184*3c112a2bSEric Taylor }
185*3c112a2bSEric Taylor
186*3c112a2bSEric Taylor void *
list_prev(list_t * list,void * object)187*3c112a2bSEric Taylor list_prev(list_t *list, void *object)
188*3c112a2bSEric Taylor {
189*3c112a2bSEric Taylor list_node_t *node = list_d2l(list, object);
190*3c112a2bSEric Taylor
191*3c112a2bSEric Taylor if (node->list_prev != &list->list_head)
192*3c112a2bSEric Taylor return (list_object(list, node->list_prev));
193*3c112a2bSEric Taylor
194*3c112a2bSEric Taylor return (NULL);
195*3c112a2bSEric Taylor }
196*3c112a2bSEric Taylor
197*3c112a2bSEric Taylor /*
198*3c112a2bSEric Taylor * Insert src list after dst list. Empty src list thereafter.
199*3c112a2bSEric Taylor */
200*3c112a2bSEric Taylor void
list_move_tail(list_t * dst,list_t * src)201*3c112a2bSEric Taylor list_move_tail(list_t *dst, list_t *src)
202*3c112a2bSEric Taylor {
203*3c112a2bSEric Taylor list_node_t *dstnode = &dst->list_head;
204*3c112a2bSEric Taylor list_node_t *srcnode = &src->list_head;
205*3c112a2bSEric Taylor
206*3c112a2bSEric Taylor ASSERT(dst->list_size == src->list_size);
207*3c112a2bSEric Taylor ASSERT(dst->list_offset == src->list_offset);
208*3c112a2bSEric Taylor
209*3c112a2bSEric Taylor if (list_empty(src))
210*3c112a2bSEric Taylor return;
211*3c112a2bSEric Taylor
212*3c112a2bSEric Taylor dstnode->list_prev->list_next = srcnode->list_next;
213*3c112a2bSEric Taylor srcnode->list_next->list_prev = dstnode->list_prev;
214*3c112a2bSEric Taylor dstnode->list_prev = srcnode->list_prev;
215*3c112a2bSEric Taylor srcnode->list_prev->list_next = dstnode;
216*3c112a2bSEric Taylor
217*3c112a2bSEric Taylor /* empty src list */
218*3c112a2bSEric Taylor srcnode->list_next = srcnode->list_prev = srcnode;
219*3c112a2bSEric Taylor }
220*3c112a2bSEric Taylor
221*3c112a2bSEric Taylor void
list_link_replace(list_node_t * lold,list_node_t * lnew)222*3c112a2bSEric Taylor list_link_replace(list_node_t *lold, list_node_t *lnew)
223*3c112a2bSEric Taylor {
224*3c112a2bSEric Taylor ASSERT(list_link_active(lold));
225*3c112a2bSEric Taylor ASSERT(!list_link_active(lnew));
226*3c112a2bSEric Taylor
227*3c112a2bSEric Taylor lnew->list_next = lold->list_next;
228*3c112a2bSEric Taylor lnew->list_prev = lold->list_prev;
229*3c112a2bSEric Taylor lold->list_prev->list_next = lnew;
230*3c112a2bSEric Taylor lold->list_next->list_prev = lnew;
231*3c112a2bSEric Taylor lold->list_next = lold->list_prev = NULL;
232*3c112a2bSEric Taylor }
233*3c112a2bSEric Taylor
234*3c112a2bSEric Taylor void
list_link_init(list_node_t * link)235*3c112a2bSEric Taylor list_link_init(list_node_t *link)
236*3c112a2bSEric Taylor {
237*3c112a2bSEric Taylor link->list_next = NULL;
238*3c112a2bSEric Taylor link->list_prev = NULL;
239*3c112a2bSEric Taylor }
240*3c112a2bSEric Taylor
241*3c112a2bSEric Taylor int
list_link_active(list_node_t * link)242*3c112a2bSEric Taylor list_link_active(list_node_t *link)
243*3c112a2bSEric Taylor {
244*3c112a2bSEric Taylor return (link->list_next != NULL);
245*3c112a2bSEric Taylor }
246*3c112a2bSEric Taylor
247*3c112a2bSEric Taylor int
list_is_empty(list_t * list)248*3c112a2bSEric Taylor list_is_empty(list_t *list)
249*3c112a2bSEric Taylor {
250*3c112a2bSEric Taylor return (list_empty(list));
251*3c112a2bSEric Taylor }
252