1 /* $OpenBSD: queue.h,v 1.23 2003/06/02 23:28:21 millert Exp $ */
2 /* $NetBSD: queue.h,v 1.11 1996/05/16 05:17:14 mycroft Exp $ */
5 * Copyright (c) 1991, 1993
6 * The Regents of the University of California. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * @(#)queue.h 8.5 (Berkeley) 8/20/94
35 #ifndef _FAKE_QUEUE_H_
36 #define _FAKE_QUEUE_H_
39 * Ignore all <sys/queue.h> since older platforms have broken/incomplete
40 * <sys/queue.h> that are too hard to work around.
43 #undef SLIST_HEAD_INITIALIZER
51 #undef SLIST_INSERT_AFTER
52 #undef SLIST_INSERT_HEAD
53 #undef SLIST_REMOVE_HEAD
56 #undef LIST_HEAD_INITIALIZER
64 #undef LIST_INSERT_AFTER
65 #undef LIST_INSERT_BEFORE
66 #undef LIST_INSERT_HEAD
70 #undef SIMPLEQ_HEAD_INITIALIZER
76 #undef SIMPLEQ_FOREACH
78 #undef SIMPLEQ_INSERT_HEAD
79 #undef SIMPLEQ_INSERT_TAIL
80 #undef SIMPLEQ_INSERT_AFTER
81 #undef SIMPLEQ_REMOVE_HEAD
83 #undef TAILQ_HEAD_INITIALIZER
92 #undef TAILQ_FOREACH_REVERSE
94 #undef TAILQ_INSERT_HEAD
95 #undef TAILQ_INSERT_TAIL
96 #undef TAILQ_INSERT_AFTER
97 #undef TAILQ_INSERT_BEFORE
101 #undef CIRCLEQ_HEAD_INITIALIZER
109 #undef CIRCLEQ_FOREACH
110 #undef CIRCLEQ_FOREACH_REVERSE
112 #undef CIRCLEQ_INSERT_AFTER
113 #undef CIRCLEQ_INSERT_BEFORE
114 #undef CIRCLEQ_INSERT_HEAD
115 #undef CIRCLEQ_INSERT_TAIL
116 #undef CIRCLEQ_REMOVE
117 #undef CIRCLEQ_REPLACE
120 * This file defines five types of data structures: singly-linked lists,
121 * lists, simple queues, tail queues, and circular queues.
124 * A singly-linked list is headed by a single forward pointer. The elements
125 * are singly linked for minimum space and pointer manipulation overhead at
126 * the expense of O(n) removal for arbitrary elements. New elements can be
127 * added to the list after an existing element or at the head of the list.
128 * Elements being removed from the head of the list should use the explicit
129 * macro for this purpose for optimum efficiency. A singly-linked list may
130 * only be traversed in the forward direction. Singly-linked lists are ideal
131 * for applications with large datasets and few or no removals or for
132 * implementing a LIFO queue.
134 * A list is headed by a single forward pointer (or an array of forward
135 * pointers for a hash table header). The elements are doubly linked
136 * so that an arbitrary element can be removed without a need to
137 * traverse the list. New elements can be added to the list before
138 * or after an existing element or at the head of the list. A list
139 * may only be traversed in the forward direction.
141 * A simple queue is headed by a pair of pointers, one the head of the
142 * list and the other to the tail of the list. The elements are singly
143 * linked to save space, so elements can only be removed from the
144 * head of the list. New elements can be added to the list before or after
145 * an existing element, at the head of the list, or at the end of the
146 * list. A simple queue may only be traversed in the forward direction.
148 * A tail queue is headed by a pair of pointers, one to the head of the
149 * list and the other to the tail of the list. The elements are doubly
150 * linked so that an arbitrary element can be removed without a need to
151 * traverse the list. New elements can be added to the list before or
152 * after an existing element, at the head of the list, or at the end of
153 * the list. A tail queue may be traversed in either direction.
155 * A circle queue is headed by a pair of pointers, one to the head of the
156 * list and the other to the tail of the list. The elements are doubly
157 * linked so that an arbitrary element can be removed without a need to
158 * traverse the list. New elements can be added to the list before or after
159 * an existing element, at the head of the list, or at the end of the list.
160 * A circle queue may be traversed in either direction, but has a more
161 * complex end of list detection.
163 * For details on the use of these macros, see the queue(3) manual page.
167 * Singly-linked List definitions.
169 #define SLIST_HEAD(name, type) \
171 struct type *slh_first; /* first element */ \
174 #define SLIST_HEAD_INITIALIZER(head) \
177 #define SLIST_ENTRY(type) \
179 struct type *sle_next; /* next element */ \
183 * Singly-linked List access methods.
185 #define SLIST_FIRST(head) ((head)->slh_first)
186 #define SLIST_END(head) NULL
187 #define SLIST_EMPTY(head) (SLIST_FIRST(head) == SLIST_END(head))
188 #define SLIST_NEXT(elm, field) ((elm)->field.sle_next)
190 #define SLIST_FOREACH(var, head, field) \
191 for((var) = SLIST_FIRST(head); \
192 (var) != SLIST_END(head); \
193 (var) = SLIST_NEXT(var, field))
196 * Singly-linked List functions.
198 #define SLIST_INIT(head) { \
199 SLIST_FIRST(head) = SLIST_END(head); \
202 #define SLIST_INSERT_AFTER(slistelm, elm, field) do { \
203 (elm)->field.sle_next = (slistelm)->field.sle_next; \
204 (slistelm)->field.sle_next = (elm); \
207 #define SLIST_INSERT_HEAD(head, elm, field) do { \
208 (elm)->field.sle_next = (head)->slh_first; \
209 (head)->slh_first = (elm); \
212 #define SLIST_REMOVE_HEAD(head, field) do { \
213 (head)->slh_first = (head)->slh_first->field.sle_next; \
216 #define SLIST_REMOVE(head, elm, type, field) do { \
217 if ((head)->slh_first == (elm)) { \
218 SLIST_REMOVE_HEAD((head), field); \
221 struct type *curelm = (head)->slh_first; \
222 while( curelm->field.sle_next != (elm) ) \
223 curelm = curelm->field.sle_next; \
224 curelm->field.sle_next = \
225 curelm->field.sle_next->field.sle_next; \
232 #define LIST_HEAD(name, type) \
234 struct type *lh_first; /* first element */ \
237 #define LIST_HEAD_INITIALIZER(head) \
240 #define LIST_ENTRY(type) \
242 struct type *le_next; /* next element */ \
243 struct type **le_prev; /* address of previous next element */ \
247 * List access methods
249 #define LIST_FIRST(head) ((head)->lh_first)
250 #define LIST_END(head) NULL
251 #define LIST_EMPTY(head) (LIST_FIRST(head) == LIST_END(head))
252 #define LIST_NEXT(elm, field) ((elm)->field.le_next)
254 #define LIST_FOREACH(var, head, field) \
255 for((var) = LIST_FIRST(head); \
256 (var)!= LIST_END(head); \
257 (var) = LIST_NEXT(var, field))
262 #define LIST_INIT(head) do { \
263 LIST_FIRST(head) = LIST_END(head); \
266 #define LIST_INSERT_AFTER(listelm, elm, field) do { \
267 if (((elm)->field.le_next = (listelm)->field.le_next) != NULL) \
268 (listelm)->field.le_next->field.le_prev = \
269 &(elm)->field.le_next; \
270 (listelm)->field.le_next = (elm); \
271 (elm)->field.le_prev = &(listelm)->field.le_next; \
274 #define LIST_INSERT_BEFORE(listelm, elm, field) do { \
275 (elm)->field.le_prev = (listelm)->field.le_prev; \
276 (elm)->field.le_next = (listelm); \
277 *(listelm)->field.le_prev = (elm); \
278 (listelm)->field.le_prev = &(elm)->field.le_next; \
281 #define LIST_INSERT_HEAD(head, elm, field) do { \
282 if (((elm)->field.le_next = (head)->lh_first) != NULL) \
283 (head)->lh_first->field.le_prev = &(elm)->field.le_next;\
284 (head)->lh_first = (elm); \
285 (elm)->field.le_prev = &(head)->lh_first; \
288 #define LIST_REMOVE(elm, field) do { \
289 if ((elm)->field.le_next != NULL) \
290 (elm)->field.le_next->field.le_prev = \
291 (elm)->field.le_prev; \
292 *(elm)->field.le_prev = (elm)->field.le_next; \
295 #define LIST_REPLACE(elm, elm2, field) do { \
296 if (((elm2)->field.le_next = (elm)->field.le_next) != NULL) \
297 (elm2)->field.le_next->field.le_prev = \
298 &(elm2)->field.le_next; \
299 (elm2)->field.le_prev = (elm)->field.le_prev; \
300 *(elm2)->field.le_prev = (elm2); \
304 * Simple queue definitions.
306 #define SIMPLEQ_HEAD(name, type) \
308 struct type *sqh_first; /* first element */ \
309 struct type **sqh_last; /* addr of last next element */ \
312 #define SIMPLEQ_HEAD_INITIALIZER(head) \
313 { NULL, &(head).sqh_first }
315 #define SIMPLEQ_ENTRY(type) \
317 struct type *sqe_next; /* next element */ \
321 * Simple queue access methods.
323 #define SIMPLEQ_FIRST(head) ((head)->sqh_first)
324 #define SIMPLEQ_END(head) NULL
325 #define SIMPLEQ_EMPTY(head) (SIMPLEQ_FIRST(head) == SIMPLEQ_END(head))
326 #define SIMPLEQ_NEXT(elm, field) ((elm)->field.sqe_next)
328 #define SIMPLEQ_FOREACH(var, head, field) \
329 for((var) = SIMPLEQ_FIRST(head); \
330 (var) != SIMPLEQ_END(head); \
331 (var) = SIMPLEQ_NEXT(var, field))
334 * Simple queue functions.
336 #define SIMPLEQ_INIT(head) do { \
337 (head)->sqh_first = NULL; \
338 (head)->sqh_last = &(head)->sqh_first; \
341 #define SIMPLEQ_INSERT_HEAD(head, elm, field) do { \
342 if (((elm)->field.sqe_next = (head)->sqh_first) == NULL) \
343 (head)->sqh_last = &(elm)->field.sqe_next; \
344 (head)->sqh_first = (elm); \
347 #define SIMPLEQ_INSERT_TAIL(head, elm, field) do { \
348 (elm)->field.sqe_next = NULL; \
349 *(head)->sqh_last = (elm); \
350 (head)->sqh_last = &(elm)->field.sqe_next; \
353 #define SIMPLEQ_INSERT_AFTER(head, listelm, elm, field) do { \
354 if (((elm)->field.sqe_next = (listelm)->field.sqe_next) == NULL)\
355 (head)->sqh_last = &(elm)->field.sqe_next; \
356 (listelm)->field.sqe_next = (elm); \
359 #define SIMPLEQ_REMOVE_HEAD(head, elm, field) do { \
360 if (((head)->sqh_first = (elm)->field.sqe_next) == NULL) \
361 (head)->sqh_last = &(head)->sqh_first; \
365 * Tail queue definitions.
367 #define TAILQ_HEAD(name, type) \
369 struct type *tqh_first; /* first element */ \
370 struct type **tqh_last; /* addr of last next element */ \
373 #define TAILQ_HEAD_INITIALIZER(head) \
374 { NULL, &(head).tqh_first }
376 #define TAILQ_ENTRY(type) \
378 struct type *tqe_next; /* next element */ \
379 struct type **tqe_prev; /* address of previous next element */ \
383 * tail queue access methods
385 #define TAILQ_FIRST(head) ((head)->tqh_first)
386 #define TAILQ_END(head) NULL
387 #define TAILQ_NEXT(elm, field) ((elm)->field.tqe_next)
388 #define TAILQ_LAST(head, headname) \
389 (*(((struct headname *)((head)->tqh_last))->tqh_last))
391 #define TAILQ_PREV(elm, headname, field) \
392 (*(((struct headname *)((elm)->field.tqe_prev))->tqh_last))
393 #define TAILQ_EMPTY(head) \
394 (TAILQ_FIRST(head) == TAILQ_END(head))
396 #define TAILQ_FOREACH(var, head, field) \
397 for((var) = TAILQ_FIRST(head); \
398 (var) != TAILQ_END(head); \
399 (var) = TAILQ_NEXT(var, field))
401 #define TAILQ_FOREACH_REVERSE(var, head, field, headname) \
402 for((var) = TAILQ_LAST(head, headname); \
403 (var) != TAILQ_END(head); \
404 (var) = TAILQ_PREV(var, headname, field))
407 * Tail queue functions.
409 #define TAILQ_INIT(head) do { \
410 (head)->tqh_first = NULL; \
411 (head)->tqh_last = &(head)->tqh_first; \
414 #define TAILQ_INSERT_HEAD(head, elm, field) do { \
415 if (((elm)->field.tqe_next = (head)->tqh_first) != NULL) \
416 (head)->tqh_first->field.tqe_prev = \
417 &(elm)->field.tqe_next; \
419 (head)->tqh_last = &(elm)->field.tqe_next; \
420 (head)->tqh_first = (elm); \
421 (elm)->field.tqe_prev = &(head)->tqh_first; \
424 #define TAILQ_INSERT_TAIL(head, elm, field) do { \
425 (elm)->field.tqe_next = NULL; \
426 (elm)->field.tqe_prev = (head)->tqh_last; \
427 *(head)->tqh_last = (elm); \
428 (head)->tqh_last = &(elm)->field.tqe_next; \
431 #define TAILQ_INSERT_AFTER(head, listelm, elm, field) do { \
432 if (((elm)->field.tqe_next = (listelm)->field.tqe_next) != NULL)\
433 (elm)->field.tqe_next->field.tqe_prev = \
434 &(elm)->field.tqe_next; \
436 (head)->tqh_last = &(elm)->field.tqe_next; \
437 (listelm)->field.tqe_next = (elm); \
438 (elm)->field.tqe_prev = &(listelm)->field.tqe_next; \
441 #define TAILQ_INSERT_BEFORE(listelm, elm, field) do { \
442 (elm)->field.tqe_prev = (listelm)->field.tqe_prev; \
443 (elm)->field.tqe_next = (listelm); \
444 *(listelm)->field.tqe_prev = (elm); \
445 (listelm)->field.tqe_prev = &(elm)->field.tqe_next; \
448 #define TAILQ_REMOVE(head, elm, field) do { \
449 if (((elm)->field.tqe_next) != NULL) \
450 (elm)->field.tqe_next->field.tqe_prev = \
451 (elm)->field.tqe_prev; \
453 (head)->tqh_last = (elm)->field.tqe_prev; \
454 *(elm)->field.tqe_prev = (elm)->field.tqe_next; \
457 #define TAILQ_REPLACE(head, elm, elm2, field) do { \
458 if (((elm2)->field.tqe_next = (elm)->field.tqe_next) != NULL) \
459 (elm2)->field.tqe_next->field.tqe_prev = \
460 &(elm2)->field.tqe_next; \
462 (head)->tqh_last = &(elm2)->field.tqe_next; \
463 (elm2)->field.tqe_prev = (elm)->field.tqe_prev; \
464 *(elm2)->field.tqe_prev = (elm2); \
468 * Circular queue definitions.
470 #define CIRCLEQ_HEAD(name, type) \
472 struct type *cqh_first; /* first element */ \
473 struct type *cqh_last; /* last element */ \
476 #define CIRCLEQ_HEAD_INITIALIZER(head) \
477 { CIRCLEQ_END(&head), CIRCLEQ_END(&head) }
479 #define CIRCLEQ_ENTRY(type) \
481 struct type *cqe_next; /* next element */ \
482 struct type *cqe_prev; /* previous element */ \
486 * Circular queue access methods
488 #define CIRCLEQ_FIRST(head) ((head)->cqh_first)
489 #define CIRCLEQ_LAST(head) ((head)->cqh_last)
490 #define CIRCLEQ_END(head) ((void *)(head))
491 #define CIRCLEQ_NEXT(elm, field) ((elm)->field.cqe_next)
492 #define CIRCLEQ_PREV(elm, field) ((elm)->field.cqe_prev)
493 #define CIRCLEQ_EMPTY(head) \
494 (CIRCLEQ_FIRST(head) == CIRCLEQ_END(head))
496 #define CIRCLEQ_FOREACH(var, head, field) \
497 for((var) = CIRCLEQ_FIRST(head); \
498 (var) != CIRCLEQ_END(head); \
499 (var) = CIRCLEQ_NEXT(var, field))
501 #define CIRCLEQ_FOREACH_REVERSE(var, head, field) \
502 for((var) = CIRCLEQ_LAST(head); \
503 (var) != CIRCLEQ_END(head); \
504 (var) = CIRCLEQ_PREV(var, field))
507 * Circular queue functions.
509 #define CIRCLEQ_INIT(head) do { \
510 (head)->cqh_first = CIRCLEQ_END(head); \
511 (head)->cqh_last = CIRCLEQ_END(head); \
514 #define CIRCLEQ_INSERT_AFTER(head, listelm, elm, field) do { \
515 (elm)->field.cqe_next = (listelm)->field.cqe_next; \
516 (elm)->field.cqe_prev = (listelm); \
517 if ((listelm)->field.cqe_next == CIRCLEQ_END(head)) \
518 (head)->cqh_last = (elm); \
520 (listelm)->field.cqe_next->field.cqe_prev = (elm); \
521 (listelm)->field.cqe_next = (elm); \
524 #define CIRCLEQ_INSERT_BEFORE(head, listelm, elm, field) do { \
525 (elm)->field.cqe_next = (listelm); \
526 (elm)->field.cqe_prev = (listelm)->field.cqe_prev; \
527 if ((listelm)->field.cqe_prev == CIRCLEQ_END(head)) \
528 (head)->cqh_first = (elm); \
530 (listelm)->field.cqe_prev->field.cqe_next = (elm); \
531 (listelm)->field.cqe_prev = (elm); \
534 #define CIRCLEQ_INSERT_HEAD(head, elm, field) do { \
535 (elm)->field.cqe_next = (head)->cqh_first; \
536 (elm)->field.cqe_prev = CIRCLEQ_END(head); \
537 if ((head)->cqh_last == CIRCLEQ_END(head)) \
538 (head)->cqh_last = (elm); \
540 (head)->cqh_first->field.cqe_prev = (elm); \
541 (head)->cqh_first = (elm); \
544 #define CIRCLEQ_INSERT_TAIL(head, elm, field) do { \
545 (elm)->field.cqe_next = CIRCLEQ_END(head); \
546 (elm)->field.cqe_prev = (head)->cqh_last; \
547 if ((head)->cqh_first == CIRCLEQ_END(head)) \
548 (head)->cqh_first = (elm); \
550 (head)->cqh_last->field.cqe_next = (elm); \
551 (head)->cqh_last = (elm); \
554 #define CIRCLEQ_REMOVE(head, elm, field) do { \
555 if ((elm)->field.cqe_next == CIRCLEQ_END(head)) \
556 (head)->cqh_last = (elm)->field.cqe_prev; \
558 (elm)->field.cqe_next->field.cqe_prev = \
559 (elm)->field.cqe_prev; \
560 if ((elm)->field.cqe_prev == CIRCLEQ_END(head)) \
561 (head)->cqh_first = (elm)->field.cqe_next; \
563 (elm)->field.cqe_prev->field.cqe_next = \
564 (elm)->field.cqe_next; \
567 #define CIRCLEQ_REPLACE(head, elm, elm2, field) do { \
568 if (((elm2)->field.cqe_next = (elm)->field.cqe_next) == \
570 (head).cqh_last = (elm2); \
572 (elm2)->field.cqe_next->field.cqe_prev = (elm2); \
573 if (((elm2)->field.cqe_prev = (elm)->field.cqe_prev) == \
575 (head).cqh_first = (elm2); \
577 (elm2)->field.cqe_prev->field.cqe_next = (elm2); \
580 #endif /* !_FAKE_QUEUE_H_ */