1 /*
2  * Copyright (c) 1991, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  *	@(#)queue.h	8.5 (Berkeley) 8/20/94
34  * $FreeBSD: src/sys/sys/queue.h,v 1.44 2001/09/28 00:05:11 luigi Exp $
35  */
36 
37 #ifndef _SYS_QUEUE_H_
38 #define	_SYS_QUEUE_H_
39 
40 #include <stddef.h>	/* for offsetof() */
41 
42 /*
43  * This file defines five types of data structures: singly-linked lists,
44  * singly-linked tail queues, lists, tail queues, and circular queues.
45  *
46  * A singly-linked list is headed by a single forward pointer. The elements
47  * are singly linked for minimum space and pointer manipulation overhead at
48  * the expense of O(n) removal for arbitrary elements. New elements can be
49  * added to the list after an existing element or at the head of the list.
50  * Elements being removed from the head of the list should use the explicit
51  * macro for this purpose for optimum efficiency. A singly-linked list may
52  * only be traversed in the forward direction.  Singly-linked lists are ideal
53  * for applications with large datasets and few or no removals or for
54  * implementing a LIFO queue.
55  *
56  * A singly-linked tail queue is headed by a pair of pointers, one to the
57  * head of the list and the other to the tail of the list. The elements are
58  * singly linked for minimum space and pointer manipulation overhead at the
59  * expense of O(n) removal for arbitrary elements. New elements can be added
60  * to the list after an existing element, at the head of the list, or at the
61  * end of the list. Elements being removed from the head of the tail queue
62  * should use the explicit macro for this purpose for optimum efficiency.
63  * A singly-linked tail queue may only be traversed in the forward direction.
64  * Singly-linked tail queues are ideal for applications with large datasets
65  * and few or no removals or for implementing a FIFO queue.
66  *
67  * A list is headed by a single forward pointer (or an array of forward
68  * pointers for a hash table header). The elements are doubly linked
69  * so that an arbitrary element can be removed without a need to
70  * traverse the list. New elements can be added to the list before
71  * or after an existing element or at the head of the list. A list
72  * may only be traversed in the forward direction.
73  *
74  * A tail queue is headed by a pair of pointers, one to the head of the
75  * list and the other to the tail of the list. The elements are doubly
76  * linked so that an arbitrary element can be removed without a need to
77  * traverse the list. New elements can be added to the list before or
78  * after an existing element, at the head of the list, or at the end of
79  * the list. A tail queue may be traversed in either direction.
80  *
81  * For details on the use of these macros, see the queue(3) manual page.
82  *
83  *
84  *			SLIST	LIST	STAILQ	TAILQ
85  * _HEAD		+	+	+	+
86  * _HEAD_INITIALIZER	+	+	+	+
87  * _ENTRY		+	+	+	+
88  * _INIT		+	+	+	+
89  * _EMPTY		+	+	+	+
90  * _FIRST		+	+	+	+
91  * _NEXT		+	+	+	+
92  * _PREV		-	-	-	+
93  * _LAST		-	-	+	+
94  * _FOREACH		+	+	+	+
95  * _FOREACH_REVERSE	-	-	-	+
96  * _INSERT_HEAD		+	+	+	+
97  * _INSERT_BEFORE	-	+	-	+
98  * _INSERT_AFTER	+	+	+	+
99  * _INSERT_TAIL		-	-	+	+
100  * _REMOVE_HEAD		+	-	+	-
101  * _REMOVE		+	+	+	+
102  *
103  */
104 
105 /*
106  * Singly-linked List declarations.
107  */
108 #define	SLIST_HEAD(name, type)						\
109 struct name {								\
110 	struct type *slh_first;	/* first element */			\
111 }
112 
113 #define	SLIST_HEAD_INITIALIZER(head)					\
114 	{ NULL }
115 
116 #define	SLIST_ENTRY(type)						\
117 struct {								\
118 	struct type *sle_next;	/* next element */			\
119 }
120 
121 /*
122  * Singly-linked List functions.
123  */
124 #define	SLIST_EMPTY(head)	((head)->slh_first == NULL)
125 
126 #define	SLIST_FIRST(head)	((head)->slh_first)
127 
128 #define	SLIST_FOREACH(var, head, field)					\
129 	for ((var) = SLIST_FIRST((head));				\
130 	    (var);							\
131 	    (var) = SLIST_NEXT((var), field))
132 
133 #define	SLIST_INIT(head) do {						\
134 	SLIST_FIRST((head)) = NULL;					\
135 } while (0)
136 
137 #define	SLIST_INSERT_AFTER(slistelm, elm, field) do {			\
138 	SLIST_NEXT((elm), field) = SLIST_NEXT((slistelm), field);	\
139 	SLIST_NEXT((slistelm), field) = (elm);				\
140 } while (0)
141 
142 #define	SLIST_INSERT_HEAD(head, elm, field) do {			\
143 	SLIST_NEXT((elm), field) = SLIST_FIRST((head));			\
144 	SLIST_FIRST((head)) = (elm);					\
145 } while (0)
146 
147 #define	SLIST_NEXT(elm, field)	((elm)->field.sle_next)
148 
149 #define	SLIST_REMOVE(head, elm, type, field) do {			\
150 	if (SLIST_FIRST((head)) == (elm)) {				\
151 		SLIST_REMOVE_HEAD((head), field);			\
152 	}								\
153 	else {								\
154 		struct type *curelm = SLIST_FIRST((head));		\
155 		while (SLIST_NEXT(curelm, field) != (elm))		\
156 			curelm = SLIST_NEXT(curelm, field);		\
157 		SLIST_NEXT(curelm, field) =				\
158 		    SLIST_NEXT(SLIST_NEXT(curelm, field), field);	\
159 	}								\
160 } while (0)
161 
162 #define	SLIST_REMOVE_HEAD(head, field) do {				\
163 	SLIST_FIRST((head)) = SLIST_NEXT(SLIST_FIRST((head)), field);	\
164 } while (0)
165 
166 /*
167  * Singly-linked Tail queue declarations.
168  */
169 #define	STAILQ_HEAD(name, type)						\
170 struct name {								\
171 	struct type *stqh_first;/* first element */			\
172 	struct type **stqh_last;/* addr of last next element */		\
173 }
174 
175 #define	STAILQ_HEAD_INITIALIZER(head)					\
176 	{ NULL, &(head).stqh_first }
177 
178 #define	STAILQ_ENTRY(type)						\
179 struct {								\
180 	struct type *stqe_next;	/* next element */			\
181 }
182 
183 /*
184  * Singly-linked Tail queue functions.
185  */
186 #define	STAILQ_EMPTY(head)	((head)->stqh_first == NULL)
187 
188 #define	STAILQ_FIRST(head)	((head)->stqh_first)
189 
190 #define	STAILQ_FOREACH(var, head, field)				\
191 	for((var) = STAILQ_FIRST((head));				\
192 	   (var);							\
193 	   (var) = STAILQ_NEXT((var), field))
194 
195 #define	STAILQ_INIT(head) do {						\
196 	STAILQ_FIRST((head)) = NULL;					\
197 	(head)->stqh_last = &STAILQ_FIRST((head));			\
198 } while (0)
199 
200 #define	STAILQ_INSERT_AFTER(head, tqelm, elm, field) do {		\
201 	if ((STAILQ_NEXT((elm), field) = STAILQ_NEXT((tqelm), field)) == NULL)\
202 		(head)->stqh_last = &STAILQ_NEXT((elm), field);		\
203 	STAILQ_NEXT((tqelm), field) = (elm);				\
204 } while (0)
205 
206 #define	STAILQ_INSERT_HEAD(head, elm, field) do {			\
207 	if ((STAILQ_NEXT((elm), field) = STAILQ_FIRST((head))) == NULL)	\
208 		(head)->stqh_last = &STAILQ_NEXT((elm), field);		\
209 	STAILQ_FIRST((head)) = (elm);					\
210 } while (0)
211 
212 #define	STAILQ_INSERT_TAIL(head, elm, field) do {			\
213 	STAILQ_NEXT((elm), field) = NULL;				\
214 	*(head)->stqh_last = (elm);					\
215 	(head)->stqh_last = &STAILQ_NEXT((elm), field);			\
216 } while (0)
217 
218 #define	STAILQ_LAST(head, type, field)					\
219 	(STAILQ_EMPTY(head) ?						\
220 		NULL :							\
221 	        ((struct type *)					\
222 		((char *)((head)->stqh_last) - offsetof(struct type, field))))
223 
224 #define	STAILQ_NEXT(elm, field)	((elm)->field.stqe_next)
225 
226 #define	STAILQ_REMOVE(head, elm, type, field) do {			\
227 	if (STAILQ_FIRST((head)) == (elm)) {				\
228 		STAILQ_REMOVE_HEAD(head, field);			\
229 	}								\
230 	else {								\
231 		struct type *curelm = STAILQ_FIRST((head));		\
232 		while (STAILQ_NEXT(curelm, field) != (elm))		\
233 			curelm = STAILQ_NEXT(curelm, field);		\
234 		if ((STAILQ_NEXT(curelm, field) =			\
235 		     STAILQ_NEXT(STAILQ_NEXT(curelm, field), field)) == NULL)\
236 			(head)->stqh_last = &STAILQ_NEXT((curelm), field);\
237 	}								\
238 } while (0)
239 
240 #define	STAILQ_REMOVE_HEAD(head, field) do {				\
241 	if ((STAILQ_FIRST((head)) =					\
242 	     STAILQ_NEXT(STAILQ_FIRST((head)), field)) == NULL)		\
243 		(head)->stqh_last = &STAILQ_FIRST((head));		\
244 } while (0)
245 
246 #define	STAILQ_REMOVE_HEAD_UNTIL(head, elm, field) do {			\
247 	if ((STAILQ_FIRST((head)) = STAILQ_NEXT((elm), field)) == NULL)	\
248 		(head)->stqh_last = &STAILQ_FIRST((head));		\
249 } while (0)
250 
251 /*
252  * List declarations.
253  */
254 #define	LIST_HEAD(name, type)						\
255 struct name {								\
256 	struct type *lh_first;	/* first element */			\
257 }
258 
259 #define	LIST_HEAD_INITIALIZER(head)					\
260 	{ NULL }
261 
262 #define	LIST_ENTRY(type)						\
263 struct {								\
264 	struct type *le_next;	/* next element */			\
265 	struct type **le_prev;	/* address of previous next element */	\
266 }
267 
268 /*
269  * List functions.
270  */
271 
272 #define	LIST_EMPTY(head)	((head)->lh_first == NULL)
273 
274 #define	LIST_FIRST(head)	((head)->lh_first)
275 
276 #define	LIST_FOREACH(var, head, field)					\
277 	for ((var) = LIST_FIRST((head));				\
278 	    (var);							\
279 	    (var) = LIST_NEXT((var), field))
280 
281 #define	LIST_INIT(head) do {						\
282 	LIST_FIRST((head)) = NULL;					\
283 } while (0)
284 
285 #define	LIST_INSERT_AFTER(listelm, elm, field) do {			\
286 	if ((LIST_NEXT((elm), field) = LIST_NEXT((listelm), field)) != NULL)\
287 		LIST_NEXT((listelm), field)->field.le_prev =		\
288 		    &LIST_NEXT((elm), field);				\
289 	LIST_NEXT((listelm), field) = (elm);				\
290 	(elm)->field.le_prev = &LIST_NEXT((listelm), field);		\
291 } while (0)
292 
293 #define	LIST_INSERT_BEFORE(listelm, elm, field) do {			\
294 	(elm)->field.le_prev = (listelm)->field.le_prev;		\
295 	LIST_NEXT((elm), field) = (listelm);				\
296 	*(listelm)->field.le_prev = (elm);				\
297 	(listelm)->field.le_prev = &LIST_NEXT((elm), field);		\
298 } while (0)
299 
300 #define	LIST_INSERT_HEAD(head, elm, field) do {				\
301 	if ((LIST_NEXT((elm), field) = LIST_FIRST((head))) != NULL)	\
302 		LIST_FIRST((head))->field.le_prev = &LIST_NEXT((elm), field);\
303 	LIST_FIRST((head)) = (elm);					\
304 	(elm)->field.le_prev = &LIST_FIRST((head));			\
305 } while (0)
306 
307 #define	LIST_NEXT(elm, field)	((elm)->field.le_next)
308 
309 #define	LIST_REMOVE(elm, field) do {					\
310 	if (LIST_NEXT((elm), field) != NULL)				\
311 		LIST_NEXT((elm), field)->field.le_prev = 		\
312 		    (elm)->field.le_prev;				\
313 	*(elm)->field.le_prev = LIST_NEXT((elm), field);		\
314 } while (0)
315 
316 /*
317  * Tail queue declarations.
318  */
319 #define	TAILQ_HEAD(name, type)						\
320 struct name {								\
321 	struct type *tqh_first;	/* first element */			\
322 	struct type **tqh_last;	/* addr of last next element */		\
323 }
324 
325 #define	TAILQ_HEAD_INITIALIZER(head)					\
326 	{ NULL, &(head).tqh_first }
327 
328 #define	TAILQ_ENTRY(type)						\
329 struct {								\
330 	struct type *tqe_next;	/* next element */			\
331 	struct type **tqe_prev;	/* address of previous next element */	\
332 }
333 
334 /*
335  * Tail queue functions.
336  */
337 #define	TAILQ_EMPTY(head)	((head)->tqh_first == NULL)
338 
339 #define	TAILQ_FIRST(head)	((head)->tqh_first)
340 
341 #define	TAILQ_FOREACH(var, head, field)					\
342 	for ((var) = TAILQ_FIRST((head));				\
343 	    (var);							\
344 	    (var) = TAILQ_NEXT((var), field))
345 
346 #define	TAILQ_FOREACH_REVERSE(var, head, headname, field)		\
347 	for ((var) = TAILQ_LAST((head), headname);			\
348 	    (var);							\
349 	    (var) = TAILQ_PREV((var), headname, field))
350 
351 #define	TAILQ_INIT(head) do {						\
352 	TAILQ_FIRST((head)) = NULL;					\
353 	(head)->tqh_last = &TAILQ_FIRST((head));			\
354 } while (0)
355 
356 #define	TAILQ_INSERT_AFTER(head, listelm, elm, field) do {		\
357 	if ((TAILQ_NEXT((elm), field) = TAILQ_NEXT((listelm), field)) != NULL)\
358 		TAILQ_NEXT((elm), field)->field.tqe_prev = 		\
359 		    &TAILQ_NEXT((elm), field);				\
360 	else								\
361 		(head)->tqh_last = &TAILQ_NEXT((elm), field);		\
362 	TAILQ_NEXT((listelm), field) = (elm);				\
363 	(elm)->field.tqe_prev = &TAILQ_NEXT((listelm), field);		\
364 } while (0)
365 
366 #define	TAILQ_INSERT_BEFORE(listelm, elm, field) do {			\
367 	(elm)->field.tqe_prev = (listelm)->field.tqe_prev;		\
368 	TAILQ_NEXT((elm), field) = (listelm);				\
369 	*(listelm)->field.tqe_prev = (elm);				\
370 	(listelm)->field.tqe_prev = &TAILQ_NEXT((elm), field);		\
371 } while (0)
372 
373 #define	TAILQ_INSERT_HEAD(head, elm, field) do {			\
374 	if ((TAILQ_NEXT((elm), field) = TAILQ_FIRST((head))) != NULL)	\
375 		TAILQ_FIRST((head))->field.tqe_prev =			\
376 		    &TAILQ_NEXT((elm), field);				\
377 	else								\
378 		(head)->tqh_last = &TAILQ_NEXT((elm), field);		\
379 	TAILQ_FIRST((head)) = (elm);					\
380 	(elm)->field.tqe_prev = &TAILQ_FIRST((head));			\
381 } while (0)
382 
383 #define	TAILQ_INSERT_TAIL(head, elm, field) do {			\
384 	TAILQ_NEXT((elm), field) = NULL;				\
385 	(elm)->field.tqe_prev = (head)->tqh_last;			\
386 	*(head)->tqh_last = (elm);					\
387 	(head)->tqh_last = &TAILQ_NEXT((elm), field);			\
388 } while (0)
389 
390 #define	TAILQ_LAST(head, headname)					\
391 	(*(((struct headname *)((head)->tqh_last))->tqh_last))
392 
393 #define	TAILQ_NEXT(elm, field) ((elm)->field.tqe_next)
394 
395 #define	TAILQ_PREV(elm, headname, field)				\
396 	(*(((struct headname *)((elm)->field.tqe_prev))->tqh_last))
397 
398 #define	TAILQ_REMOVE(head, elm, field) do {				\
399 	if ((TAILQ_NEXT((elm), field)) != NULL)				\
400 		TAILQ_NEXT((elm), field)->field.tqe_prev = 		\
401 		    (elm)->field.tqe_prev;				\
402 	else								\
403 		(head)->tqh_last = (elm)->field.tqe_prev;		\
404 	*(elm)->field.tqe_prev = TAILQ_NEXT((elm), field);		\
405 } while (0)
406 
407 
408 #ifdef _KERNEL
409 
410 /*
411  * XXX insque() and remque() are an old way of handling certain queues.
412  * They bogusly assumes that all queue heads look alike.
413  */
414 
415 struct quehead {
416 	struct quehead *qh_link;
417 	struct quehead *qh_rlink;
418 };
419 
420 #ifdef	__GNUC__
421 
422 static __inline void
insque(void * a,void * b)423 insque(void *a, void *b)
424 {
425 	struct quehead *element = (struct quehead *)a,
426 		 *head = (struct quehead *)b;
427 
428 	element->qh_link = head->qh_link;
429 	element->qh_rlink = head;
430 	head->qh_link = element;
431 	element->qh_link->qh_rlink = element;
432 }
433 
434 static __inline void
remque(void * a)435 remque(void *a)
436 {
437 	struct quehead *element = (struct quehead *)a;
438 
439 	element->qh_link->qh_rlink = element->qh_rlink;
440 	element->qh_rlink->qh_link = element->qh_link;
441 	element->qh_rlink = 0;
442 }
443 
444 #else /* !__GNUC__ */
445 
446 void	insque __P((void *a, void *b));
447 void	remque __P((void *a));
448 
449 #endif /* __GNUC__ */
450 
451 #endif /* _KERNEL */
452 
453 #endif /* !_SYS_QUEUE_H_ */
454