2 * Copyright (c) 1991, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
33 * @(#)queue.h 8.5 (Berkeley) 8/20/94
34 * $FreeBSD: src/sys/sys/queue.h,v 1.32.2.7 2002/04/17 14:21:02 des Exp $
35 * $DragonFly: src/sys/sys/queue.h,v 1.4 2003/11/09 02:22:37 dillon Exp $
41 #ifndef _MACHINE_STDINT_H_
42 #include <machine/stdint.h> /* for __offsetof */
46 * This file defines five types of data structures: singly-linked lists,
47 * singly-linked tail queues, lists, tail queues, and circular queues.
49 * A singly-linked list is headed by a single forward pointer. The elements
50 * are singly linked for minimum space and pointer manipulation overhead at
51 * the expense of O(n) removal for arbitrary elements. New elements can be
52 * added to the list after an existing element or at the head of the list.
53 * Elements being removed from the head of the list should use the explicit
54 * macro for this purpose for optimum efficiency. A singly-linked list may
55 * only be traversed in the forward direction. Singly-linked lists are ideal
56 * for applications with large datasets and few or no removals or for
57 * implementing a LIFO queue.
59 * A singly-linked tail queue is headed by a pair of pointers, one to the
60 * head of the list and the other to the tail of the list. The elements are
61 * singly linked for minimum space and pointer manipulation overhead at the
62 * expense of O(n) removal for arbitrary elements. New elements can be added
63 * to the list after an existing element, at the head of the list, or at the
64 * end of the list. Elements being removed from the head of the tail queue
65 * should use the explicit macro for this purpose for optimum efficiency.
66 * A singly-linked tail queue may only be traversed in the forward direction.
67 * Singly-linked tail queues are ideal for applications with large datasets
68 * and few or no removals or for implementing a FIFO queue.
70 * A list is headed by a single forward pointer (or an array of forward
71 * pointers for a hash table header). The elements are doubly linked
72 * so that an arbitrary element can be removed without a need to
73 * traverse the list. New elements can be added to the list before
74 * or after an existing element or at the head of the list. A list
75 * may only be traversed in the forward direction.
77 * A tail queue is headed by a pair of pointers, one to the head of the
78 * list and the other to the tail of the list. The elements are doubly
79 * linked so that an arbitrary element can be removed without a need to
80 * traverse the list. New elements can be added to the list before or
81 * after an existing element, at the head of the list, or at the end of
82 * the list. A tail queue may be traversed in either direction.
84 * A circle queue is headed by a pair of pointers, one to the head of the
85 * list and the other to the tail of the list. The elements are doubly
86 * linked so that an arbitrary element can be removed without a need to
87 * traverse the list. New elements can be added to the list before or after
88 * an existing element, at the head of the list, or at the end of the list.
89 * A circle queue may be traversed in either direction, but has a more
90 * complex end of list detection.
92 * For details on the use of these macros, see the queue(3) manual page.
95 * SLIST LIST STAILQ TAILQ CIRCLEQ
97 * _HEAD_INITIALIZER + + + + +
106 * _FOREACH_REVERSE - - - + +
107 * _INSERT_HEAD + + + + +
108 * _INSERT_BEFORE - + - + +
109 * _INSERT_AFTER + + + + +
110 * _INSERT_TAIL - - + + +
111 * _REMOVE_HEAD + - + - -
117 * Singly-linked List declarations.
119 #define SLIST_HEAD(name, type) \
121 struct type *slh_first; /* first element */ \
124 #define SLIST_HEAD_INITIALIZER(head) \
127 #define SLIST_ENTRY(type) \
129 struct type *sle_next; /* next element */ \
133 * Singly-linked List functions.
135 #define SLIST_EMPTY(head) ((head)->slh_first == NULL)
137 #define SLIST_FIRST(head) ((head)->slh_first)
139 #define SLIST_FOREACH(var, head, field) \
140 for ((var) = SLIST_FIRST((head)); \
142 (var) = SLIST_NEXT((var), field))
144 #define SLIST_INIT(head) do { \
145 SLIST_FIRST((head)) = NULL; \
148 #define SLIST_INSERT_AFTER(slistelm, elm, field) do { \
149 SLIST_NEXT((elm), field) = SLIST_NEXT((slistelm), field); \
150 SLIST_NEXT((slistelm), field) = (elm); \
153 #define SLIST_INSERT_HEAD(head, elm, field) do { \
154 SLIST_NEXT((elm), field) = SLIST_FIRST((head)); \
155 SLIST_FIRST((head)) = (elm); \
158 #define SLIST_NEXT(elm, field) ((elm)->field.sle_next)
160 #define SLIST_REMOVE(head, elm, type, field) do { \
161 if (SLIST_FIRST((head)) == (elm)) { \
162 SLIST_REMOVE_HEAD((head), field); \
165 struct type *curelm = SLIST_FIRST((head)); \
166 while (SLIST_NEXT(curelm, field) != (elm)) \
167 curelm = SLIST_NEXT(curelm, field); \
168 SLIST_NEXT(curelm, field) = \
169 SLIST_NEXT(SLIST_NEXT(curelm, field), field); \
173 #define SLIST_REMOVE_HEAD(head, field) do { \
174 SLIST_FIRST((head)) = SLIST_NEXT(SLIST_FIRST((head)), field); \
178 * Singly-linked Tail queue declarations.
180 #define STAILQ_HEAD(name, type) \
182 struct type *stqh_first;/* first element */ \
183 struct type **stqh_last;/* addr of last next element */ \
186 #define STAILQ_HEAD_INITIALIZER(head) \
187 { NULL, &(head).stqh_first }
189 #define STAILQ_ENTRY(type) \
191 struct type *stqe_next; /* next element */ \
195 * Singly-linked Tail queue functions.
197 #define STAILQ_EMPTY(head) ((head)->stqh_first == NULL)
199 #define STAILQ_FIRST(head) ((head)->stqh_first)
201 #define STAILQ_FOREACH(var, head, field) \
202 for((var) = STAILQ_FIRST((head)); \
204 (var) = STAILQ_NEXT((var), field))
206 #define STAILQ_INIT(head) do { \
207 STAILQ_FIRST((head)) = NULL; \
208 (head)->stqh_last = &STAILQ_FIRST((head)); \
211 #define STAILQ_INSERT_AFTER(head, tqelm, elm, field) do { \
212 if ((STAILQ_NEXT((elm), field) = STAILQ_NEXT((tqelm), field)) == NULL)\
213 (head)->stqh_last = &STAILQ_NEXT((elm), field); \
214 STAILQ_NEXT((tqelm), field) = (elm); \
217 #define STAILQ_INSERT_HEAD(head, elm, field) do { \
218 if ((STAILQ_NEXT((elm), field) = STAILQ_FIRST((head))) == NULL) \
219 (head)->stqh_last = &STAILQ_NEXT((elm), field); \
220 STAILQ_FIRST((head)) = (elm); \
223 #define STAILQ_INSERT_TAIL(head, elm, field) do { \
224 STAILQ_NEXT((elm), field) = NULL; \
225 *(head)->stqh_last = (elm); \
226 (head)->stqh_last = &STAILQ_NEXT((elm), field); \
229 #define STAILQ_LAST(head, type, field) \
230 (STAILQ_EMPTY(head) ? \
233 ((char *)((head)->stqh_last) - __offsetof(struct type, field))))
235 #define STAILQ_NEXT(elm, field) ((elm)->field.stqe_next)
237 #define STAILQ_REMOVE(head, elm, type, field) do { \
238 if (STAILQ_FIRST((head)) == (elm)) { \
239 STAILQ_REMOVE_HEAD(head, field); \
242 struct type *curelm = STAILQ_FIRST((head)); \
243 while (STAILQ_NEXT(curelm, field) != (elm)) \
244 curelm = STAILQ_NEXT(curelm, field); \
245 if ((STAILQ_NEXT(curelm, field) = \
246 STAILQ_NEXT(STAILQ_NEXT(curelm, field), field)) == NULL)\
247 (head)->stqh_last = &STAILQ_NEXT((curelm), field);\
251 #define STAILQ_REMOVE_HEAD(head, field) do { \
252 if ((STAILQ_FIRST((head)) = \
253 STAILQ_NEXT(STAILQ_FIRST((head)), field)) == NULL) \
254 (head)->stqh_last = &STAILQ_FIRST((head)); \
257 #define STAILQ_REMOVE_HEAD_UNTIL(head, elm, field) do { \
258 if ((STAILQ_FIRST((head)) = STAILQ_NEXT((elm), field)) == NULL) \
259 (head)->stqh_last = &STAILQ_FIRST((head)); \
265 #define LIST_HEAD(name, type) \
267 struct type *lh_first; /* first element */ \
270 #define LIST_HEAD_INITIALIZER(head) \
273 #define LIST_ENTRY(type) \
275 struct type *le_next; /* next element */ \
276 struct type **le_prev; /* address of previous next element */ \
283 #define LIST_EMPTY(head) ((head)->lh_first == NULL)
285 #define LIST_FIRST(head) ((head)->lh_first)
287 #define LIST_FOREACH(var, head, field) \
288 for ((var) = LIST_FIRST((head)); \
290 (var) = LIST_NEXT((var), field))
292 #define LIST_INIT(head) do { \
293 LIST_FIRST((head)) = NULL; \
296 #define LIST_INSERT_AFTER(listelm, elm, field) do { \
297 if ((LIST_NEXT((elm), field) = LIST_NEXT((listelm), field)) != NULL)\
298 LIST_NEXT((listelm), field)->field.le_prev = \
299 &LIST_NEXT((elm), field); \
300 LIST_NEXT((listelm), field) = (elm); \
301 (elm)->field.le_prev = &LIST_NEXT((listelm), field); \
304 #define LIST_INSERT_BEFORE(listelm, elm, field) do { \
305 (elm)->field.le_prev = (listelm)->field.le_prev; \
306 LIST_NEXT((elm), field) = (listelm); \
307 *(listelm)->field.le_prev = (elm); \
308 (listelm)->field.le_prev = &LIST_NEXT((elm), field); \
311 #define LIST_INSERT_HEAD(head, elm, field) do { \
312 if ((LIST_NEXT((elm), field) = LIST_FIRST((head))) != NULL) \
313 LIST_FIRST((head))->field.le_prev = &LIST_NEXT((elm), field);\
314 LIST_FIRST((head)) = (elm); \
315 (elm)->field.le_prev = &LIST_FIRST((head)); \
318 #define LIST_NEXT(elm, field) ((elm)->field.le_next)
320 #define LIST_REMOVE(elm, field) do { \
321 if (LIST_NEXT((elm), field) != NULL) \
322 LIST_NEXT((elm), field)->field.le_prev = \
323 (elm)->field.le_prev; \
324 *(elm)->field.le_prev = LIST_NEXT((elm), field); \
328 * Tail queue declarations.
330 #define TAILQ_HEAD(name, type) \
332 struct type *tqh_first; /* first element */ \
333 struct type **tqh_last; /* addr of last next element */ \
336 #define TAILQ_HEAD_INITIALIZER(head) \
337 { NULL, &(head).tqh_first }
339 #define TAILQ_ENTRY(type) \
341 struct type *tqe_next; /* next element */ \
342 struct type **tqe_prev; /* address of previous next element */ \
346 * Tail queue functions.
348 #define TAILQ_EMPTY(head) ((head)->tqh_first == NULL)
350 #define TAILQ_FIRST(head) ((head)->tqh_first)
352 #define TAILQ_FOREACH(var, head, field) \
353 for ((var) = TAILQ_FIRST((head)); \
355 (var) = TAILQ_NEXT((var), field))
357 #define TAILQ_FOREACH_REVERSE(var, head, headname, field) \
358 for ((var) = TAILQ_LAST((head), headname); \
360 (var) = TAILQ_PREV((var), headname, field))
362 #define TAILQ_INIT(head) do { \
363 TAILQ_FIRST((head)) = NULL; \
364 (head)->tqh_last = &TAILQ_FIRST((head)); \
367 #define TAILQ_INSERT_AFTER(head, listelm, elm, field) do { \
368 if ((TAILQ_NEXT((elm), field) = TAILQ_NEXT((listelm), field)) != NULL)\
369 TAILQ_NEXT((elm), field)->field.tqe_prev = \
370 &TAILQ_NEXT((elm), field); \
372 (head)->tqh_last = &TAILQ_NEXT((elm), field); \
373 TAILQ_NEXT((listelm), field) = (elm); \
374 (elm)->field.tqe_prev = &TAILQ_NEXT((listelm), field); \
377 #define TAILQ_INSERT_BEFORE(listelm, elm, field) do { \
378 (elm)->field.tqe_prev = (listelm)->field.tqe_prev; \
379 TAILQ_NEXT((elm), field) = (listelm); \
380 *(listelm)->field.tqe_prev = (elm); \
381 (listelm)->field.tqe_prev = &TAILQ_NEXT((elm), field); \
384 #define TAILQ_INSERT_HEAD(head, elm, field) do { \
385 if ((TAILQ_NEXT((elm), field) = TAILQ_FIRST((head))) != NULL) \
386 TAILQ_FIRST((head))->field.tqe_prev = \
387 &TAILQ_NEXT((elm), field); \
389 (head)->tqh_last = &TAILQ_NEXT((elm), field); \
390 TAILQ_FIRST((head)) = (elm); \
391 (elm)->field.tqe_prev = &TAILQ_FIRST((head)); \
394 #define TAILQ_INSERT_TAIL(head, elm, field) do { \
395 TAILQ_NEXT((elm), field) = NULL; \
396 (elm)->field.tqe_prev = (head)->tqh_last; \
397 *(head)->tqh_last = (elm); \
398 (head)->tqh_last = &TAILQ_NEXT((elm), field); \
401 #define TAILQ_LAST(head, headname) \
402 (*(((struct headname *)((head)->tqh_last))->tqh_last))
404 #define TAILQ_NEXT(elm, field) ((elm)->field.tqe_next)
406 #define TAILQ_PREV(elm, headname, field) \
407 (*(((struct headname *)((elm)->field.tqe_prev))->tqh_last))
409 #define TAILQ_REMOVE(head, elm, field) do { \
410 if ((TAILQ_NEXT((elm), field)) != NULL) \
411 TAILQ_NEXT((elm), field)->field.tqe_prev = \
412 (elm)->field.tqe_prev; \
414 (head)->tqh_last = (elm)->field.tqe_prev; \
415 *(elm)->field.tqe_prev = TAILQ_NEXT((elm), field); \
419 * Circular queue declarations.
421 #define CIRCLEQ_HEAD(name, type) \
423 struct type *cqh_first; /* first element */ \
424 struct type *cqh_last; /* last element */ \
427 #define CIRCLEQ_HEAD_INITIALIZER(head) \
428 { (void *)&(head), (void *)&(head) }
430 #define CIRCLEQ_ENTRY(type) \
432 struct type *cqe_next; /* next element */ \
433 struct type *cqe_prev; /* previous element */ \
437 * Circular queue functions.
439 #define CIRCLEQ_EMPTY(head) ((head)->cqh_first == (void *)(head))
441 #define CIRCLEQ_FIRST(head) ((head)->cqh_first)
443 #define CIRCLEQ_FOREACH(var, head, field) \
444 for ((var) = CIRCLEQ_FIRST((head)); \
445 (var) != (void *)(head) || ((var) = NULL); \
446 (var) = CIRCLEQ_NEXT((var), field))
448 #define CIRCLEQ_FOREACH_REVERSE(var, head, field) \
449 for ((var) = CIRCLEQ_LAST((head)); \
450 (var) != (void *)(head) || ((var) = NULL); \
451 (var) = CIRCLEQ_PREV((var), field))
453 #define CIRCLEQ_INIT(head) do { \
454 CIRCLEQ_FIRST((head)) = (void *)(head); \
455 CIRCLEQ_LAST((head)) = (void *)(head); \
458 #define CIRCLEQ_INSERT_AFTER(head, listelm, elm, field) do { \
459 CIRCLEQ_NEXT((elm), field) = CIRCLEQ_NEXT((listelm), field); \
460 CIRCLEQ_PREV((elm), field) = (listelm); \
461 if (CIRCLEQ_NEXT((listelm), field) == (void *)(head)) \
462 CIRCLEQ_LAST((head)) = (elm); \
464 CIRCLEQ_PREV(CIRCLEQ_NEXT((listelm), field), field) = (elm);\
465 CIRCLEQ_NEXT((listelm), field) = (elm); \
468 #define CIRCLEQ_INSERT_BEFORE(head, listelm, elm, field) do { \
469 CIRCLEQ_NEXT((elm), field) = (listelm); \
470 CIRCLEQ_PREV((elm), field) = CIRCLEQ_PREV((listelm), field); \
471 if (CIRCLEQ_PREV((listelm), field) == (void *)(head)) \
472 CIRCLEQ_FIRST((head)) = (elm); \
474 CIRCLEQ_NEXT(CIRCLEQ_PREV((listelm), field), field) = (elm);\
475 CIRCLEQ_PREV((listelm), field) = (elm); \
478 #define CIRCLEQ_INSERT_HEAD(head, elm, field) do { \
479 CIRCLEQ_NEXT((elm), field) = CIRCLEQ_FIRST((head)); \
480 CIRCLEQ_PREV((elm), field) = (void *)(head); \
481 if (CIRCLEQ_LAST((head)) == (void *)(head)) \
482 CIRCLEQ_LAST((head)) = (elm); \
484 CIRCLEQ_PREV(CIRCLEQ_FIRST((head)), field) = (elm); \
485 CIRCLEQ_FIRST((head)) = (elm); \
488 #define CIRCLEQ_INSERT_TAIL(head, elm, field) do { \
489 CIRCLEQ_NEXT((elm), field) = (void *)(head); \
490 CIRCLEQ_PREV((elm), field) = CIRCLEQ_LAST((head)); \
491 if (CIRCLEQ_FIRST((head)) == (void *)(head)) \
492 CIRCLEQ_FIRST((head)) = (elm); \
494 CIRCLEQ_NEXT(CIRCLEQ_LAST((head)), field) = (elm); \
495 CIRCLEQ_LAST((head)) = (elm); \
498 #define CIRCLEQ_LAST(head) ((head)->cqh_last)
500 #define CIRCLEQ_NEXT(elm,field) ((elm)->field.cqe_next)
502 #define CIRCLEQ_PREV(elm,field) ((elm)->field.cqe_prev)
504 #define CIRCLEQ_REMOVE(head, elm, field) do { \
505 if (CIRCLEQ_NEXT((elm), field) == (void *)(head)) \
506 CIRCLEQ_LAST((head)) = CIRCLEQ_PREV((elm), field); \
508 CIRCLEQ_PREV(CIRCLEQ_NEXT((elm), field), field) = \
509 CIRCLEQ_PREV((elm), field); \
510 if (CIRCLEQ_PREV((elm), field) == (void *)(head)) \
511 CIRCLEQ_FIRST((head)) = CIRCLEQ_NEXT((elm), field); \
513 CIRCLEQ_NEXT(CIRCLEQ_PREV((elm), field), field) = \
514 CIRCLEQ_NEXT((elm), field); \
520 * XXX insque() and remque() are an old way of handling certain queues.
521 * They bogusly assumes that all queue heads look alike.
525 struct quehead *qh_link;
526 struct quehead *qh_rlink;
532 insque(void *a, void *b)
534 struct quehead *element = (struct quehead *)a,
535 *head = (struct quehead *)b;
537 element->qh_link = head->qh_link;
538 element->qh_rlink = head;
539 head->qh_link = element;
540 element->qh_link->qh_rlink = element;
546 struct quehead *element = (struct quehead *)a;
548 element->qh_link->qh_rlink = element->qh_rlink;
549 element->qh_rlink->qh_link = element->qh_link;
550 element->qh_rlink = 0;
553 #else /* !__GNUC__ */
555 void insque (void *a, void *b);
556 void remque (void *a);
558 #endif /* __GNUC__ */
562 #endif /* !_SYS_QUEUE_H_ */