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. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#)queue.h 8.5 (Berkeley) 8/20/94
36 #include <sys/cdefs.h>
39 * This file defines four types of data structures: singly-linked lists,
40 * singly-linked tail queues, lists and tail queues.
42 * A singly-linked list is headed by a single forward pointer. The elements
43 * are singly linked for minimum space and pointer manipulation overhead at
44 * the expense of O(n) removal for arbitrary elements. New elements can be
45 * added to the list after an existing element or at the head of the list.
46 * Elements being removed from the head of the list should use the explicit
47 * macro for this purpose for optimum efficiency. A singly-linked list may
48 * only be traversed in the forward direction. Singly-linked lists are ideal
49 * for applications with large datasets and few or no removals or for
50 * implementing a LIFO queue.
52 * A singly-linked tail queue is headed by a pair of pointers, one to the
53 * head of the list and the other to the tail of the list. The elements are
54 * singly linked for minimum space and pointer manipulation overhead at the
55 * expense of O(n) removal for arbitrary elements. New elements can be added
56 * to the list after an existing element, at the head of the list, or at the
57 * end of the list. Elements being removed from the head of the tail queue
58 * should use the explicit macro for this purpose for optimum efficiency.
59 * A singly-linked tail queue may only be traversed in the forward direction.
60 * Singly-linked tail queues are ideal for applications with large datasets
61 * and few or no removals or for implementing a FIFO queue.
63 * A list is headed by a single forward pointer (or an array of forward
64 * pointers for a hash table header). The elements are doubly linked
65 * so that an arbitrary element can be removed without a need to
66 * traverse the list. New elements can be added to the list before
67 * or after an existing element or at the head of the list. A list
68 * may be traversed in either direction.
70 * A tail queue is headed by a pair of pointers, one to the head of the
71 * list and the other to the tail of the list. The elements are doubly
72 * linked 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 or
74 * after an existing element, at the head of the list, or at the end of
75 * the list. A tail queue may be traversed in either direction.
77 * For details on the use of these macros, see the queue(3) manual page.
79 * Below is a summary of implemented functions where:
80 * + means the macro is available
81 * - means the macro is not available
82 * s means the macro is available but is slow (runs in O(n) time)
84 * SLIST LIST STAILQ TAILQ
87 * _HEAD_INITIALIZER + + + +
89 * _CLASS_ENTRY + + + +
98 * _FOREACH_FROM + + + +
99 * _FOREACH_SAFE + + + +
100 * _FOREACH_FROM_SAFE + + + +
101 * _FOREACH_REVERSE - - - +
102 * _FOREACH_REVERSE_FROM - - - +
103 * _FOREACH_REVERSE_SAFE - - - +
104 * _FOREACH_REVERSE_FROM_SAFE - - - +
105 * _INSERT_HEAD + + + +
106 * _INSERT_BEFORE - + - +
107 * _INSERT_AFTER + + + +
108 * _INSERT_TAIL - - + +
110 * _REMOVE_AFTER + - + -
111 * _REMOVE_HEAD + - + -
116 #ifdef QUEUE_MACRO_DEBUG
117 #warn Use QUEUE_MACRO_DEBUG_TRACE and/or QUEUE_MACRO_DEBUG_TRASH
118 #define QUEUE_MACRO_DEBUG_TRACE
119 #define QUEUE_MACRO_DEBUG_TRASH
122 #ifdef QUEUE_MACRO_DEBUG_TRACE
123 /* Store the last 2 places the queue element or head was altered */
125 unsigned long lastline
;
126 unsigned long prevline
;
127 const char *lastfile
;
128 const char *prevfile
;
131 #define TRACEBUF struct qm_trace trace;
132 #define TRACEBUF_INITIALIZER { __LINE__, 0, __FILE__, NULL } ,
134 #define QMD_TRACE_HEAD(head) do { \
135 (head)->trace.prevline = (head)->trace.lastline; \
136 (head)->trace.prevfile = (head)->trace.lastfile; \
137 (head)->trace.lastline = __LINE__; \
138 (head)->trace.lastfile = __FILE__; \
141 #define QMD_TRACE_ELEM(elem) do { \
142 (elem)->trace.prevline = (elem)->trace.lastline; \
143 (elem)->trace.prevfile = (elem)->trace.lastfile; \
144 (elem)->trace.lastline = __LINE__; \
145 (elem)->trace.lastfile = __FILE__; \
148 #else /* !QUEUE_MACRO_DEBUG_TRACE */
149 #define QMD_TRACE_ELEM(elem)
150 #define QMD_TRACE_HEAD(head)
152 #define TRACEBUF_INITIALIZER
153 #endif /* QUEUE_MACRO_DEBUG_TRACE */
155 #ifdef QUEUE_MACRO_DEBUG_TRASH
156 #define TRASHIT(x) do {(x) = (void *)-1;} while (0)
157 #define QMD_IS_TRASHED(x) ((x) == (void *)(intptr_t)-1)
158 #else /* !QUEUE_MACRO_DEBUG_TRASH */
160 #define QMD_IS_TRASHED(x) 0
161 #endif /* QUEUE_MACRO_DEBUG_TRASH */
163 #if defined(QUEUE_MACRO_DEBUG_TRACE) || defined(QUEUE_MACRO_DEBUG_TRASH)
164 #define QMD_SAVELINK(name, link) void **name = (void *)&(link)
165 #else /* !QUEUE_MACRO_DEBUG_TRACE && !QUEUE_MACRO_DEBUG_TRASH */
166 #define QMD_SAVELINK(name, link)
167 #endif /* QUEUE_MACRO_DEBUG_TRACE || QUEUE_MACRO_DEBUG_TRASH */
171 * In C++ there can be structure lists and class lists:
173 #define QUEUE_TYPEOF(type) type
175 #define QUEUE_TYPEOF(type) struct type
179 * Singly-linked List declarations.
181 #define SLIST_HEAD(name, type) \
183 struct type *slh_first; /* first element */ \
186 #define SLIST_CLASS_HEAD(name, type) \
188 class type *slh_first; /* first element */ \
191 #define SLIST_HEAD_INITIALIZER(head) \
194 #define SLIST_ENTRY(type) \
196 struct type *sle_next; /* next element */ \
199 #define SLIST_CLASS_ENTRY(type) \
201 class type *sle_next; /* next element */ \
205 * Singly-linked List functions.
207 #if (defined(_KERNEL) && defined(INVARIANTS))
208 #define QMD_SLIST_CHECK_PREVPTR(prevp, elm) do { \
209 if (*(prevp) != (elm)) \
210 panic("Bad prevptr *(%p) == %p != %p", \
211 (prevp), *(prevp), (elm)); \
214 #define QMD_SLIST_CHECK_PREVPTR(prevp, elm)
217 #define SLIST_CONCAT(head1, head2, type, field) do { \
218 QUEUE_TYPEOF(type) *curelm = SLIST_FIRST(head1); \
219 if (curelm == NULL) { \
220 if ((SLIST_FIRST(head1) = SLIST_FIRST(head2)) != NULL) \
222 } else if (SLIST_FIRST(head2) != NULL) { \
223 while (SLIST_NEXT(curelm, field) != NULL) \
224 curelm = SLIST_NEXT(curelm, field); \
225 SLIST_NEXT(curelm, field) = SLIST_FIRST(head2); \
230 #define SLIST_EMPTY(head) ((head)->slh_first == NULL)
232 #define SLIST_FIRST(head) ((head)->slh_first)
234 #define SLIST_FOREACH(var, head, field) \
235 for ((var) = SLIST_FIRST((head)); \
237 (var) = SLIST_NEXT((var), field))
239 #define SLIST_FOREACH_FROM(var, head, field) \
240 for ((var) = ((var) ? (var) : SLIST_FIRST((head))); \
242 (var) = SLIST_NEXT((var), field))
244 #define SLIST_FOREACH_SAFE(var, head, field, tvar) \
245 for ((var) = SLIST_FIRST((head)); \
246 (var) && ((tvar) = SLIST_NEXT((var), field), 1); \
249 #define SLIST_FOREACH_FROM_SAFE(var, head, field, tvar) \
250 for ((var) = ((var) ? (var) : SLIST_FIRST((head))); \
251 (var) && ((tvar) = SLIST_NEXT((var), field), 1); \
254 #define SLIST_FOREACH_PREVPTR(var, varp, head, field) \
255 for ((varp) = &SLIST_FIRST((head)); \
256 ((var) = *(varp)) != NULL; \
257 (varp) = &SLIST_NEXT((var), field))
259 #define SLIST_INIT(head) do { \
260 SLIST_FIRST((head)) = NULL; \
263 #define SLIST_INSERT_AFTER(slistelm, elm, field) do { \
264 SLIST_NEXT((elm), field) = SLIST_NEXT((slistelm), field); \
265 SLIST_NEXT((slistelm), field) = (elm); \
268 #define SLIST_INSERT_HEAD(head, elm, field) do { \
269 SLIST_NEXT((elm), field) = SLIST_FIRST((head)); \
270 SLIST_FIRST((head)) = (elm); \
273 #define SLIST_NEXT(elm, field) ((elm)->field.sle_next)
275 #define SLIST_REMOVE(head, elm, type, field) do { \
276 QMD_SAVELINK(oldnext, (elm)->field.sle_next); \
277 if (SLIST_FIRST((head)) == (elm)) { \
278 SLIST_REMOVE_HEAD((head), field); \
281 QUEUE_TYPEOF(type) *curelm = SLIST_FIRST(head); \
282 while (SLIST_NEXT(curelm, field) != (elm)) \
283 curelm = SLIST_NEXT(curelm, field); \
284 SLIST_REMOVE_AFTER(curelm, field); \
289 #define SLIST_REMOVE_AFTER(elm, field) do { \
290 SLIST_NEXT(elm, field) = \
291 SLIST_NEXT(SLIST_NEXT(elm, field), field); \
294 #define SLIST_REMOVE_HEAD(head, field) do { \
295 SLIST_FIRST((head)) = SLIST_NEXT(SLIST_FIRST((head)), field); \
298 #define SLIST_REMOVE_PREVPTR(prevp, elm, field) do { \
299 QMD_SLIST_CHECK_PREVPTR(prevp, elm); \
300 *(prevp) = SLIST_NEXT(elm, field); \
301 TRASHIT((elm)->field.sle_next); \
304 #define SLIST_SWAP(head1, head2, type) do { \
305 QUEUE_TYPEOF(type) *swap_first = SLIST_FIRST(head1); \
306 SLIST_FIRST(head1) = SLIST_FIRST(head2); \
307 SLIST_FIRST(head2) = swap_first; \
311 * Singly-linked Tail queue declarations.
313 #define STAILQ_HEAD(name, type) \
315 struct type *stqh_first;/* first element */ \
316 struct type **stqh_last;/* addr of last next element */ \
319 #define STAILQ_CLASS_HEAD(name, type) \
321 class type *stqh_first; /* first element */ \
322 class type **stqh_last; /* addr of last next element */ \
325 #define STAILQ_HEAD_INITIALIZER(head) \
326 { NULL, &(head).stqh_first }
328 #define STAILQ_ENTRY(type) \
330 struct type *stqe_next; /* next element */ \
333 #define STAILQ_CLASS_ENTRY(type) \
335 class type *stqe_next; /* next element */ \
339 * Singly-linked Tail queue functions.
341 #define STAILQ_CONCAT(head1, head2) do { \
342 if (!STAILQ_EMPTY((head2))) { \
343 *(head1)->stqh_last = (head2)->stqh_first; \
344 (head1)->stqh_last = (head2)->stqh_last; \
345 STAILQ_INIT((head2)); \
349 #define STAILQ_EMPTY(head) ((head)->stqh_first == NULL)
351 #define STAILQ_FIRST(head) ((head)->stqh_first)
353 #define STAILQ_FOREACH(var, head, field) \
354 for((var) = STAILQ_FIRST((head)); \
356 (var) = STAILQ_NEXT((var), field))
358 #define STAILQ_FOREACH_FROM(var, head, field) \
359 for ((var) = ((var) ? (var) : STAILQ_FIRST((head))); \
361 (var) = STAILQ_NEXT((var), field))
363 #define STAILQ_FOREACH_SAFE(var, head, field, tvar) \
364 for ((var) = STAILQ_FIRST((head)); \
365 (var) && ((tvar) = STAILQ_NEXT((var), field), 1); \
368 #define STAILQ_FOREACH_FROM_SAFE(var, head, field, tvar) \
369 for ((var) = ((var) ? (var) : STAILQ_FIRST((head))); \
370 (var) && ((tvar) = STAILQ_NEXT((var), field), 1); \
373 #define STAILQ_INIT(head) do { \
374 STAILQ_FIRST((head)) = NULL; \
375 (head)->stqh_last = &STAILQ_FIRST((head)); \
378 #define STAILQ_INSERT_AFTER(head, tqelm, elm, field) do { \
379 if ((STAILQ_NEXT((elm), field) = STAILQ_NEXT((tqelm), field)) == NULL)\
380 (head)->stqh_last = &STAILQ_NEXT((elm), field); \
381 STAILQ_NEXT((tqelm), field) = (elm); \
384 #define STAILQ_INSERT_HEAD(head, elm, field) do { \
385 if ((STAILQ_NEXT((elm), field) = STAILQ_FIRST((head))) == NULL) \
386 (head)->stqh_last = &STAILQ_NEXT((elm), field); \
387 STAILQ_FIRST((head)) = (elm); \
390 #define STAILQ_INSERT_TAIL(head, elm, field) do { \
391 STAILQ_NEXT((elm), field) = NULL; \
392 *(head)->stqh_last = (elm); \
393 (head)->stqh_last = &STAILQ_NEXT((elm), field); \
396 #define STAILQ_LAST(head, type, field) \
397 (STAILQ_EMPTY((head)) ? NULL : \
398 __containerof((head)->stqh_last, \
399 QUEUE_TYPEOF(type), field.stqe_next))
401 #define STAILQ_NEXT(elm, field) ((elm)->field.stqe_next)
403 #define STAILQ_REMOVE(head, elm, type, field) do { \
404 QMD_SAVELINK(oldnext, (elm)->field.stqe_next); \
405 if (STAILQ_FIRST((head)) == (elm)) { \
406 STAILQ_REMOVE_HEAD((head), field); \
409 QUEUE_TYPEOF(type) *curelm = STAILQ_FIRST(head); \
410 while (STAILQ_NEXT(curelm, field) != (elm)) \
411 curelm = STAILQ_NEXT(curelm, field); \
412 STAILQ_REMOVE_AFTER(head, curelm, field); \
417 #define STAILQ_REMOVE_AFTER(head, elm, field) do { \
418 if ((STAILQ_NEXT(elm, field) = \
419 STAILQ_NEXT(STAILQ_NEXT(elm, field), field)) == NULL) \
420 (head)->stqh_last = &STAILQ_NEXT((elm), field); \
423 #define STAILQ_REMOVE_HEAD(head, field) do { \
424 if ((STAILQ_FIRST((head)) = \
425 STAILQ_NEXT(STAILQ_FIRST((head)), field)) == NULL) \
426 (head)->stqh_last = &STAILQ_FIRST((head)); \
429 #define STAILQ_REMOVE_HEAD_UNTIL(head, elm, field) do { \
430 if ((STAILQ_FIRST((head)) = STAILQ_NEXT((elm), field)) == NULL) \
431 (head)->stqh_last = &STAILQ_FIRST((head)); \
434 #define STAILQ_SWAP(head1, head2, type) do { \
435 QUEUE_TYPEOF(type) *swap_first = STAILQ_FIRST(head1); \
436 QUEUE_TYPEOF(type) **swap_last = (head1)->stqh_last; \
437 STAILQ_FIRST(head1) = STAILQ_FIRST(head2); \
438 (head1)->stqh_last = (head2)->stqh_last; \
439 STAILQ_FIRST(head2) = swap_first; \
440 (head2)->stqh_last = swap_last; \
441 if (STAILQ_EMPTY(head1)) \
442 (head1)->stqh_last = &STAILQ_FIRST(head1); \
443 if (STAILQ_EMPTY(head2)) \
444 (head2)->stqh_last = &STAILQ_FIRST(head2); \
451 #define LIST_HEAD(name, type) \
453 struct type *lh_first; /* first element */ \
456 #define LIST_CLASS_HEAD(name, type) \
458 class type *lh_first; /* first element */ \
461 #define LIST_HEAD_INITIALIZER(head) \
464 #define LIST_ENTRY(type) \
466 struct type *le_next; /* next element */ \
467 struct type **le_prev; /* address of previous next element */ \
470 #define LIST_CLASS_ENTRY(type) \
472 class type *le_next; /* next element */ \
473 class type **le_prev; /* address of previous next element */ \
480 #if (defined(_KERNEL) && defined(INVARIANTS))
482 * QMD_LIST_CHECK_HEAD(LIST_HEAD *head, LIST_ENTRY NAME)
484 * If the list is non-empty, validates that the first element of the list
485 * points back at 'head.'
487 #define QMD_LIST_CHECK_HEAD(head, field) do { \
488 if (LIST_FIRST((head)) != NULL && \
489 LIST_FIRST((head))->field.le_prev != \
490 &LIST_FIRST((head))) \
491 panic("Bad list head %p first->prev != head", (head)); \
495 * QMD_LIST_CHECK_NEXT(TYPE *elm, LIST_ENTRY NAME)
497 * If an element follows 'elm' in the list, validates that the next element
498 * points back at 'elm.'
500 #define QMD_LIST_CHECK_NEXT(elm, field) do { \
501 if (LIST_NEXT((elm), field) != NULL && \
502 LIST_NEXT((elm), field)->field.le_prev != \
503 &((elm)->field.le_next)) \
504 panic("Bad link elm %p next->prev != elm", (elm)); \
508 * QMD_LIST_CHECK_PREV(TYPE *elm, LIST_ENTRY NAME)
510 * Validates that the previous element (or head of the list) points to 'elm.'
512 #define QMD_LIST_CHECK_PREV(elm, field) do { \
513 if (*(elm)->field.le_prev != (elm)) \
514 panic("Bad link elm %p prev->next != elm", (elm)); \
517 #define QMD_LIST_CHECK_HEAD(head, field)
518 #define QMD_LIST_CHECK_NEXT(elm, field)
519 #define QMD_LIST_CHECK_PREV(elm, field)
520 #endif /* (_KERNEL && INVARIANTS) */
522 #define LIST_CONCAT(head1, head2, type, field) do { \
523 QUEUE_TYPEOF(type) *curelm = LIST_FIRST(head1); \
524 if (curelm == NULL) { \
525 if ((LIST_FIRST(head1) = LIST_FIRST(head2)) != NULL) { \
526 LIST_FIRST(head2)->field.le_prev = \
527 &LIST_FIRST((head1)); \
530 } else if (LIST_FIRST(head2) != NULL) { \
531 while (LIST_NEXT(curelm, field) != NULL) \
532 curelm = LIST_NEXT(curelm, field); \
533 LIST_NEXT(curelm, field) = LIST_FIRST(head2); \
534 LIST_FIRST(head2)->field.le_prev = &LIST_NEXT(curelm, field); \
539 #define LIST_EMPTY(head) ((head)->lh_first == NULL)
541 #define LIST_FIRST(head) ((head)->lh_first)
543 #define LIST_FOREACH(var, head, field) \
544 for ((var) = LIST_FIRST((head)); \
546 (var) = LIST_NEXT((var), field))
548 #define LIST_FOREACH_FROM(var, head, field) \
549 for ((var) = ((var) ? (var) : LIST_FIRST((head))); \
551 (var) = LIST_NEXT((var), field))
553 #define LIST_FOREACH_SAFE(var, head, field, tvar) \
554 for ((var) = LIST_FIRST((head)); \
555 (var) && ((tvar) = LIST_NEXT((var), field), 1); \
558 #define LIST_FOREACH_FROM_SAFE(var, head, field, tvar) \
559 for ((var) = ((var) ? (var) : LIST_FIRST((head))); \
560 (var) && ((tvar) = LIST_NEXT((var), field), 1); \
563 #define LIST_INIT(head) do { \
564 LIST_FIRST((head)) = NULL; \
567 #define LIST_INSERT_AFTER(listelm, elm, field) do { \
568 QMD_LIST_CHECK_NEXT(listelm, field); \
569 if ((LIST_NEXT((elm), field) = LIST_NEXT((listelm), field)) != NULL)\
570 LIST_NEXT((listelm), field)->field.le_prev = \
571 &LIST_NEXT((elm), field); \
572 LIST_NEXT((listelm), field) = (elm); \
573 (elm)->field.le_prev = &LIST_NEXT((listelm), field); \
576 #define LIST_INSERT_BEFORE(listelm, elm, field) do { \
577 QMD_LIST_CHECK_PREV(listelm, field); \
578 (elm)->field.le_prev = (listelm)->field.le_prev; \
579 LIST_NEXT((elm), field) = (listelm); \
580 *(listelm)->field.le_prev = (elm); \
581 (listelm)->field.le_prev = &LIST_NEXT((elm), field); \
584 #define LIST_INSERT_HEAD(head, elm, field) do { \
585 QMD_LIST_CHECK_HEAD((head), field); \
586 if ((LIST_NEXT((elm), field) = LIST_FIRST((head))) != NULL) \
587 LIST_FIRST((head))->field.le_prev = &LIST_NEXT((elm), field);\
588 LIST_FIRST((head)) = (elm); \
589 (elm)->field.le_prev = &LIST_FIRST((head)); \
592 #define LIST_NEXT(elm, field) ((elm)->field.le_next)
594 #define LIST_PREV(elm, head, type, field) \
595 ((elm)->field.le_prev == &LIST_FIRST((head)) ? NULL : \
596 __containerof((elm)->field.le_prev, \
597 QUEUE_TYPEOF(type), field.le_next))
599 #define LIST_REMOVE(elm, field) do { \
600 QMD_SAVELINK(oldnext, (elm)->field.le_next); \
601 QMD_SAVELINK(oldprev, (elm)->field.le_prev); \
602 QMD_LIST_CHECK_NEXT(elm, field); \
603 QMD_LIST_CHECK_PREV(elm, field); \
604 if (LIST_NEXT((elm), field) != NULL) \
605 LIST_NEXT((elm), field)->field.le_prev = \
606 (elm)->field.le_prev; \
607 *(elm)->field.le_prev = LIST_NEXT((elm), field); \
612 #define LIST_SWAP(head1, head2, type, field) do { \
613 QUEUE_TYPEOF(type) *swap_tmp = LIST_FIRST(head1); \
614 LIST_FIRST((head1)) = LIST_FIRST((head2)); \
615 LIST_FIRST((head2)) = swap_tmp; \
616 if ((swap_tmp = LIST_FIRST((head1))) != NULL) \
617 swap_tmp->field.le_prev = &LIST_FIRST((head1)); \
618 if ((swap_tmp = LIST_FIRST((head2))) != NULL) \
619 swap_tmp->field.le_prev = &LIST_FIRST((head2)); \
623 * Tail queue declarations.
625 #define TAILQ_HEAD(name, type) \
627 struct type *tqh_first; /* first element */ \
628 struct type **tqh_last; /* addr of last next element */ \
632 #define TAILQ_CLASS_HEAD(name, type) \
634 class type *tqh_first; /* first element */ \
635 class type **tqh_last; /* addr of last next element */ \
639 #define TAILQ_HEAD_INITIALIZER(head) \
640 { NULL, &(head).tqh_first, TRACEBUF_INITIALIZER }
642 #define TAILQ_ENTRY(type) \
644 struct type *tqe_next; /* next element */ \
645 struct type **tqe_prev; /* address of previous next element */ \
649 #define TAILQ_CLASS_ENTRY(type) \
651 class type *tqe_next; /* next element */ \
652 class type **tqe_prev; /* address of previous next element */ \
657 * Tail queue functions.
659 #if (defined(_KERNEL) && defined(INVARIANTS))
661 * QMD_TAILQ_CHECK_HEAD(TAILQ_HEAD *head, TAILQ_ENTRY NAME)
663 * If the tailq is non-empty, validates that the first element of the tailq
664 * points back at 'head.'
666 #define QMD_TAILQ_CHECK_HEAD(head, field) do { \
667 if (!TAILQ_EMPTY(head) && \
668 TAILQ_FIRST((head))->field.tqe_prev != \
669 &TAILQ_FIRST((head))) \
670 panic("Bad tailq head %p first->prev != head", (head)); \
674 * QMD_TAILQ_CHECK_TAIL(TAILQ_HEAD *head, TAILQ_ENTRY NAME)
676 * Validates that the tail of the tailq is a pointer to pointer to NULL.
678 #define QMD_TAILQ_CHECK_TAIL(head, field) do { \
679 if (*(head)->tqh_last != NULL) \
680 panic("Bad tailq NEXT(%p->tqh_last) != NULL", (head)); \
684 * QMD_TAILQ_CHECK_NEXT(TYPE *elm, TAILQ_ENTRY NAME)
686 * If an element follows 'elm' in the tailq, validates that the next element
687 * points back at 'elm.'
689 #define QMD_TAILQ_CHECK_NEXT(elm, field) do { \
690 if (TAILQ_NEXT((elm), field) != NULL && \
691 TAILQ_NEXT((elm), field)->field.tqe_prev != \
692 &((elm)->field.tqe_next)) \
693 panic("Bad link elm %p next->prev != elm", (elm)); \
697 * QMD_TAILQ_CHECK_PREV(TYPE *elm, TAILQ_ENTRY NAME)
699 * Validates that the previous element (or head of the tailq) points to 'elm.'
701 #define QMD_TAILQ_CHECK_PREV(elm, field) do { \
702 if (*(elm)->field.tqe_prev != (elm)) \
703 panic("Bad link elm %p prev->next != elm", (elm)); \
706 #define QMD_TAILQ_CHECK_HEAD(head, field)
707 #define QMD_TAILQ_CHECK_TAIL(head, headname)
708 #define QMD_TAILQ_CHECK_NEXT(elm, field)
709 #define QMD_TAILQ_CHECK_PREV(elm, field)
710 #endif /* (_KERNEL && INVARIANTS) */
712 #define TAILQ_CONCAT(head1, head2, field) do { \
713 if (!TAILQ_EMPTY(head2)) { \
714 *(head1)->tqh_last = (head2)->tqh_first; \
715 (head2)->tqh_first->field.tqe_prev = (head1)->tqh_last; \
716 (head1)->tqh_last = (head2)->tqh_last; \
717 TAILQ_INIT((head2)); \
718 QMD_TRACE_HEAD(head1); \
719 QMD_TRACE_HEAD(head2); \
723 #define TAILQ_EMPTY(head) ((head)->tqh_first == NULL)
725 #define TAILQ_FIRST(head) ((head)->tqh_first)
727 #define TAILQ_FOREACH(var, head, field) \
728 for ((var) = TAILQ_FIRST((head)); \
730 (var) = TAILQ_NEXT((var), field))
732 #define TAILQ_FOREACH_FROM(var, head, field) \
733 for ((var) = ((var) ? (var) : TAILQ_FIRST((head))); \
735 (var) = TAILQ_NEXT((var), field))
737 #define TAILQ_FOREACH_SAFE(var, head, field, tvar) \
738 for ((var) = TAILQ_FIRST((head)); \
739 (var) && ((tvar) = TAILQ_NEXT((var), field), 1); \
742 #define TAILQ_FOREACH_FROM_SAFE(var, head, field, tvar) \
743 for ((var) = ((var) ? (var) : TAILQ_FIRST((head))); \
744 (var) && ((tvar) = TAILQ_NEXT((var), field), 1); \
747 #define TAILQ_FOREACH_REVERSE(var, head, headname, field) \
748 for ((var) = TAILQ_LAST((head), headname); \
750 (var) = TAILQ_PREV((var), headname, field))
752 #define TAILQ_FOREACH_REVERSE_FROM(var, head, headname, field) \
753 for ((var) = ((var) ? (var) : TAILQ_LAST((head), headname)); \
755 (var) = TAILQ_PREV((var), headname, field))
757 #define TAILQ_FOREACH_REVERSE_SAFE(var, head, headname, field, tvar) \
758 for ((var) = TAILQ_LAST((head), headname); \
759 (var) && ((tvar) = TAILQ_PREV((var), headname, field), 1); \
762 #define TAILQ_FOREACH_REVERSE_FROM_SAFE(var, head, headname, field, tvar) \
763 for ((var) = ((var) ? (var) : TAILQ_LAST((head), headname)); \
764 (var) && ((tvar) = TAILQ_PREV((var), headname, field), 1); \
767 #define TAILQ_INIT(head) do { \
768 TAILQ_FIRST((head)) = NULL; \
769 (head)->tqh_last = &TAILQ_FIRST((head)); \
770 QMD_TRACE_HEAD(head); \
773 #define TAILQ_INSERT_AFTER(head, listelm, elm, field) do { \
774 QMD_TAILQ_CHECK_NEXT(listelm, field); \
775 if ((TAILQ_NEXT((elm), field) = TAILQ_NEXT((listelm), field)) != NULL)\
776 TAILQ_NEXT((elm), field)->field.tqe_prev = \
777 &TAILQ_NEXT((elm), field); \
779 (head)->tqh_last = &TAILQ_NEXT((elm), field); \
780 QMD_TRACE_HEAD(head); \
782 TAILQ_NEXT((listelm), field) = (elm); \
783 (elm)->field.tqe_prev = &TAILQ_NEXT((listelm), field); \
784 QMD_TRACE_ELEM(&(elm)->field); \
785 QMD_TRACE_ELEM(&(listelm)->field); \
788 #define TAILQ_INSERT_BEFORE(listelm, elm, field) do { \
789 QMD_TAILQ_CHECK_PREV(listelm, field); \
790 (elm)->field.tqe_prev = (listelm)->field.tqe_prev; \
791 TAILQ_NEXT((elm), field) = (listelm); \
792 *(listelm)->field.tqe_prev = (elm); \
793 (listelm)->field.tqe_prev = &TAILQ_NEXT((elm), field); \
794 QMD_TRACE_ELEM(&(elm)->field); \
795 QMD_TRACE_ELEM(&(listelm)->field); \
798 #define TAILQ_INSERT_HEAD(head, elm, field) do { \
799 QMD_TAILQ_CHECK_HEAD(head, field); \
800 if ((TAILQ_NEXT((elm), field) = TAILQ_FIRST((head))) != NULL) \
801 TAILQ_FIRST((head))->field.tqe_prev = \
802 &TAILQ_NEXT((elm), field); \
804 (head)->tqh_last = &TAILQ_NEXT((elm), field); \
805 TAILQ_FIRST((head)) = (elm); \
806 (elm)->field.tqe_prev = &TAILQ_FIRST((head)); \
807 QMD_TRACE_HEAD(head); \
808 QMD_TRACE_ELEM(&(elm)->field); \
811 #define TAILQ_INSERT_TAIL(head, elm, field) do { \
812 QMD_TAILQ_CHECK_TAIL(head, field); \
813 TAILQ_NEXT((elm), field) = NULL; \
814 (elm)->field.tqe_prev = (head)->tqh_last; \
815 *(head)->tqh_last = (elm); \
816 (head)->tqh_last = &TAILQ_NEXT((elm), field); \
817 QMD_TRACE_HEAD(head); \
818 QMD_TRACE_ELEM(&(elm)->field); \
821 #define TAILQ_LAST(head, headname) \
822 (*(((struct headname *)((head)->tqh_last))->tqh_last))
825 * The FAST function is fast in that it causes no data access other
826 * then the access to the head. The standard LAST function above
827 * will cause a data access of both the element you want and
828 * the previous element. FAST is very useful for instances when
829 * you may want to prefetch the last data element.
831 #define TAILQ_LAST_FAST(head, type, field) \
832 (TAILQ_EMPTY(head) ? NULL : __containerof((head)->tqh_last, QUEUE_TYPEOF(type), field.tqe_next))
834 #define TAILQ_NEXT(elm, field) ((elm)->field.tqe_next)
836 #define TAILQ_PREV(elm, headname, field) \
837 (*(((struct headname *)((elm)->field.tqe_prev))->tqh_last))
839 #define TAILQ_REMOVE(head, elm, field) do { \
840 QMD_SAVELINK(oldnext, (elm)->field.tqe_next); \
841 QMD_SAVELINK(oldprev, (elm)->field.tqe_prev); \
842 QMD_TAILQ_CHECK_NEXT(elm, field); \
843 QMD_TAILQ_CHECK_PREV(elm, field); \
844 if ((TAILQ_NEXT((elm), field)) != NULL) \
845 TAILQ_NEXT((elm), field)->field.tqe_prev = \
846 (elm)->field.tqe_prev; \
848 (head)->tqh_last = (elm)->field.tqe_prev; \
849 QMD_TRACE_HEAD(head); \
851 *(elm)->field.tqe_prev = TAILQ_NEXT((elm), field); \
854 QMD_TRACE_ELEM(&(elm)->field); \
857 #define TAILQ_SWAP(head1, head2, type, field) do { \
858 QUEUE_TYPEOF(type) *swap_first = (head1)->tqh_first; \
859 QUEUE_TYPEOF(type) **swap_last = (head1)->tqh_last; \
860 (head1)->tqh_first = (head2)->tqh_first; \
861 (head1)->tqh_last = (head2)->tqh_last; \
862 (head2)->tqh_first = swap_first; \
863 (head2)->tqh_last = swap_last; \
864 if ((swap_first = (head1)->tqh_first) != NULL) \
865 swap_first->field.tqe_prev = &(head1)->tqh_first; \
867 (head1)->tqh_last = &(head1)->tqh_first; \
868 if ((swap_first = (head2)->tqh_first) != NULL) \
869 swap_first->field.tqe_prev = &(head2)->tqh_first; \
871 (head2)->tqh_last = &(head2)->tqh_first; \
877 * XXX insque() and remque() are an old way of handling certain queues.
878 * They bogusly assumes that all queue heads look alike.
882 struct quehead
*qh_link
;
883 struct quehead
*qh_rlink
;
889 insque(void *a
, void *b
)
891 struct quehead
*element
= (struct quehead
*)a
,
892 *head
= (struct quehead
*)b
;
894 element
->qh_link
= head
->qh_link
;
895 element
->qh_rlink
= head
;
896 head
->qh_link
= element
;
897 element
->qh_link
->qh_rlink
= element
;
903 struct quehead
*element
= (struct quehead
*)a
;
905 element
->qh_link
->qh_rlink
= element
->qh_rlink
;
906 element
->qh_rlink
->qh_link
= element
->qh_link
;
907 element
->qh_rlink
= 0;
910 #else /* !__GNUC__ */
912 void insque(void *a
, void *b
);
913 void remque(void *a
);
915 #endif /* __GNUC__ */
919 #endif /* !_SYS_QUEUE_H_ */