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 * 4. 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.
80 * SLIST LIST STAILQ TAILQ
83 * _HEAD_INITIALIZER + + + +
85 * _CLASS_ENTRY + + + +
93 * _FOREACH_FROM + + + +
94 * _FOREACH_SAFE + + + +
95 * _FOREACH_FROM_SAFE + + + +
96 * _FOREACH_REVERSE - - - +
97 * _FOREACH_REVERSE_FROM - - - +
98 * _FOREACH_REVERSE_SAFE - - - +
99 * _FOREACH_REVERSE_FROM_SAFE - - - +
100 * _INSERT_HEAD + + + +
101 * _INSERT_BEFORE - + - +
102 * _INSERT_AFTER + + + +
103 * _INSERT_TAIL - - + +
105 * _REMOVE_AFTER + - + -
106 * _REMOVE_HEAD + - + -
111 #ifdef QUEUE_MACRO_DEBUG
112 /* Store the last 2 places the queue element or head was altered */
120 #define TRACEBUF struct qm_trace trace;
121 #define TRASHIT(x) do {(x) = (void *)-1;} while (0)
122 #define QMD_SAVELINK(name, link) void **name = (void *)&(link)
124 #define QMD_TRACE_HEAD(head) do { \
125 (head)->trace.prevline = (head)->trace.lastline; \
126 (head)->trace.prevfile = (head)->trace.lastfile; \
127 (head)->trace.lastline = __LINE__; \
128 (head)->trace.lastfile = __FILE__; \
131 #define QMD_TRACE_ELEM(elem) do { \
132 (elem)->trace.prevline = (elem)->trace.lastline; \
133 (elem)->trace.prevfile = (elem)->trace.lastfile; \
134 (elem)->trace.lastline = __LINE__; \
135 (elem)->trace.lastfile = __FILE__; \
139 #define QMD_TRACE_ELEM(elem)
140 #define QMD_TRACE_HEAD(head)
141 #define QMD_SAVELINK(name, link)
144 #endif /* QUEUE_MACRO_DEBUG */
148 * In C++ there can be structure lists and class lists:
150 #define QUEUE_TYPEOF(type) type
152 #define QUEUE_TYPEOF(type) struct type
156 * Singly-linked List declarations.
158 #define SLIST_HEAD(name, type) \
160 struct type *slh_first; /* first element */ \
163 #define SLIST_CLASS_HEAD(name, type) \
165 class type *slh_first; /* first element */ \
168 #define SLIST_HEAD_INITIALIZER(head) \
171 #define SLIST_ENTRY(type) \
173 struct type *sle_next; /* next element */ \
176 #define SLIST_CLASS_ENTRY(type) \
178 class type *sle_next; /* next element */ \
182 * Singly-linked List functions.
184 #define SLIST_EMPTY(head) ((head)->slh_first == NULL)
186 #define SLIST_FIRST(head) ((head)->slh_first)
188 #define SLIST_FOREACH(var, head, field) \
189 for ((var) = SLIST_FIRST((head)); \
191 (var) = SLIST_NEXT((var), field))
193 #define SLIST_FOREACH_FROM(var, head, field) \
194 for ((var) = ((var) ? (var) : SLIST_FIRST((head))); \
196 (var) = SLIST_NEXT((var), field))
198 #define SLIST_FOREACH_SAFE(var, head, field, tvar) \
199 for ((var) = SLIST_FIRST((head)); \
200 (var) && ((tvar) = SLIST_NEXT((var), field), 1); \
203 #define SLIST_FOREACH_FROM_SAFE(var, head, field, tvar) \
204 for ((var) = ((var) ? (var) : SLIST_FIRST((head))); \
205 (var) && ((tvar) = SLIST_NEXT((var), field), 1); \
208 #define SLIST_FOREACH_PREVPTR(var, varp, head, field) \
209 for ((varp) = &SLIST_FIRST((head)); \
210 ((var) = *(varp)) != NULL; \
211 (varp) = &SLIST_NEXT((var), field))
213 #define SLIST_INIT(head) do { \
214 SLIST_FIRST((head)) = NULL; \
217 #define SLIST_INSERT_AFTER(slistelm, elm, field) do { \
218 SLIST_NEXT((elm), field) = SLIST_NEXT((slistelm), field); \
219 SLIST_NEXT((slistelm), field) = (elm); \
222 #define SLIST_INSERT_HEAD(head, elm, field) do { \
223 SLIST_NEXT((elm), field) = SLIST_FIRST((head)); \
224 SLIST_FIRST((head)) = (elm); \
227 #define SLIST_NEXT(elm, field) ((elm)->field.sle_next)
229 #define SLIST_REMOVE(head, elm, type, field) do { \
230 QMD_SAVELINK(oldnext, (elm)->field.sle_next); \
231 if (SLIST_FIRST((head)) == (elm)) { \
232 SLIST_REMOVE_HEAD((head), field); \
235 QUEUE_TYPEOF(type) *curelm = SLIST_FIRST(head); \
236 while (SLIST_NEXT(curelm, field) != (elm)) \
237 curelm = SLIST_NEXT(curelm, field); \
238 SLIST_REMOVE_AFTER(curelm, field); \
243 #define SLIST_REMOVE_AFTER(elm, field) do { \
244 SLIST_NEXT(elm, field) = \
245 SLIST_NEXT(SLIST_NEXT(elm, field), field); \
248 #define SLIST_REMOVE_HEAD(head, field) do { \
249 SLIST_FIRST((head)) = SLIST_NEXT(SLIST_FIRST((head)), field); \
252 #define SLIST_SWAP(head1, head2, type) do { \
253 QUEUE_TYPEOF(type) *swap_first = SLIST_FIRST(head1); \
254 SLIST_FIRST(head1) = SLIST_FIRST(head2); \
255 SLIST_FIRST(head2) = swap_first; \
259 * Singly-linked Tail queue declarations.
261 #define STAILQ_HEAD(name, type) \
263 struct type *stqh_first;/* first element */ \
264 struct type **stqh_last;/* addr of last next element */ \
267 #define STAILQ_CLASS_HEAD(name, type) \
269 class type *stqh_first; /* first element */ \
270 class type **stqh_last; /* addr of last next element */ \
273 #define STAILQ_HEAD_INITIALIZER(head) \
274 { NULL, &(head).stqh_first }
276 #define STAILQ_ENTRY(type) \
278 struct type *stqe_next; /* next element */ \
281 #define STAILQ_CLASS_ENTRY(type) \
283 class type *stqe_next; /* next element */ \
287 * Singly-linked Tail queue functions.
289 #define STAILQ_CONCAT(head1, head2) do { \
290 if (!STAILQ_EMPTY((head2))) { \
291 *(head1)->stqh_last = (head2)->stqh_first; \
292 (head1)->stqh_last = (head2)->stqh_last; \
293 STAILQ_INIT((head2)); \
297 #define STAILQ_EMPTY(head) ((head)->stqh_first == NULL)
299 #define STAILQ_FIRST(head) ((head)->stqh_first)
301 #define STAILQ_FOREACH(var, head, field) \
302 for((var) = STAILQ_FIRST((head)); \
304 (var) = STAILQ_NEXT((var), field))
306 #define STAILQ_FOREACH_FROM(var, head, field) \
307 for ((var) = ((var) ? (var) : STAILQ_FIRST((head))); \
309 (var) = STAILQ_NEXT((var), field))
311 #define STAILQ_FOREACH_SAFE(var, head, field, tvar) \
312 for ((var) = STAILQ_FIRST((head)); \
313 (var) && ((tvar) = STAILQ_NEXT((var), field), 1); \
316 #define STAILQ_FOREACH_FROM_SAFE(var, head, field, tvar) \
317 for ((var) = ((var) ? (var) : STAILQ_FIRST((head))); \
318 (var) && ((tvar) = STAILQ_NEXT((var), field), 1); \
321 #define STAILQ_INIT(head) do { \
322 STAILQ_FIRST((head)) = NULL; \
323 (head)->stqh_last = &STAILQ_FIRST((head)); \
326 #define STAILQ_INSERT_AFTER(head, tqelm, elm, field) do { \
327 if ((STAILQ_NEXT((elm), field) = STAILQ_NEXT((tqelm), field)) == NULL)\
328 (head)->stqh_last = &STAILQ_NEXT((elm), field); \
329 STAILQ_NEXT((tqelm), field) = (elm); \
332 #define STAILQ_INSERT_HEAD(head, elm, field) do { \
333 if ((STAILQ_NEXT((elm), field) = STAILQ_FIRST((head))) == NULL) \
334 (head)->stqh_last = &STAILQ_NEXT((elm), field); \
335 STAILQ_FIRST((head)) = (elm); \
338 #define STAILQ_INSERT_TAIL(head, elm, field) do { \
339 STAILQ_NEXT((elm), field) = NULL; \
340 *(head)->stqh_last = (elm); \
341 (head)->stqh_last = &STAILQ_NEXT((elm), field); \
344 #define STAILQ_LAST(head, type, field) \
345 (STAILQ_EMPTY((head)) ? NULL : \
346 __containerof((head)->stqh_last, \
347 QUEUE_TYPEOF(type), field.stqe_next))
349 #define STAILQ_NEXT(elm, field) ((elm)->field.stqe_next)
351 #define STAILQ_REMOVE(head, elm, type, field) do { \
352 QMD_SAVELINK(oldnext, (elm)->field.stqe_next); \
353 if (STAILQ_FIRST((head)) == (elm)) { \
354 STAILQ_REMOVE_HEAD((head), field); \
357 QUEUE_TYPEOF(type) *curelm = STAILQ_FIRST(head); \
358 while (STAILQ_NEXT(curelm, field) != (elm)) \
359 curelm = STAILQ_NEXT(curelm, field); \
360 STAILQ_REMOVE_AFTER(head, curelm, field); \
365 #define STAILQ_REMOVE_AFTER(head, elm, field) do { \
366 if ((STAILQ_NEXT(elm, field) = \
367 STAILQ_NEXT(STAILQ_NEXT(elm, field), field)) == NULL) \
368 (head)->stqh_last = &STAILQ_NEXT((elm), field); \
371 #define STAILQ_REMOVE_HEAD(head, field) do { \
372 if ((STAILQ_FIRST((head)) = \
373 STAILQ_NEXT(STAILQ_FIRST((head)), field)) == NULL) \
374 (head)->stqh_last = &STAILQ_FIRST((head)); \
377 #define STAILQ_SWAP(head1, head2, type) do { \
378 QUEUE_TYPEOF(type) *swap_first = STAILQ_FIRST(head1); \
379 QUEUE_TYPEOF(type) **swap_last = (head1)->stqh_last; \
380 STAILQ_FIRST(head1) = STAILQ_FIRST(head2); \
381 (head1)->stqh_last = (head2)->stqh_last; \
382 STAILQ_FIRST(head2) = swap_first; \
383 (head2)->stqh_last = swap_last; \
384 if (STAILQ_EMPTY(head1)) \
385 (head1)->stqh_last = &STAILQ_FIRST(head1); \
386 if (STAILQ_EMPTY(head2)) \
387 (head2)->stqh_last = &STAILQ_FIRST(head2); \
394 #define LIST_HEAD(name, type) \
396 struct type *lh_first; /* first element */ \
399 #define LIST_CLASS_HEAD(name, type) \
401 class type *lh_first; /* first element */ \
404 #define LIST_HEAD_INITIALIZER(head) \
407 #define LIST_ENTRY(type) \
409 struct type *le_next; /* next element */ \
410 struct type **le_prev; /* address of previous next element */ \
413 #define LIST_CLASS_ENTRY(type) \
415 class type *le_next; /* next element */ \
416 class type **le_prev; /* address of previous next element */ \
423 #if (defined(_KERNEL) && defined(INVARIANTS))
424 #define QMD_LIST_CHECK_HEAD(head, field) do { \
425 if (LIST_FIRST((head)) != NULL && \
426 LIST_FIRST((head))->field.le_prev != \
427 &LIST_FIRST((head))) \
428 panic("Bad list head %p first->prev != head", (head)); \
431 #define QMD_LIST_CHECK_NEXT(elm, field) do { \
432 if (LIST_NEXT((elm), field) != NULL && \
433 LIST_NEXT((elm), field)->field.le_prev != \
434 &((elm)->field.le_next)) \
435 panic("Bad link elm %p next->prev != elm", (elm)); \
438 #define QMD_LIST_CHECK_PREV(elm, field) do { \
439 if (*(elm)->field.le_prev != (elm)) \
440 panic("Bad link elm %p prev->next != elm", (elm)); \
443 #define QMD_LIST_CHECK_HEAD(head, field)
444 #define QMD_LIST_CHECK_NEXT(elm, field)
445 #define QMD_LIST_CHECK_PREV(elm, field)
446 #endif /* (_KERNEL && INVARIANTS) */
448 #define LIST_EMPTY(head) ((head)->lh_first == NULL)
450 #define LIST_FIRST(head) ((head)->lh_first)
452 #define LIST_FOREACH(var, head, field) \
453 for ((var) = LIST_FIRST((head)); \
455 (var) = LIST_NEXT((var), field))
457 #define LIST_FOREACH_FROM(var, head, field) \
458 for ((var) = ((var) ? (var) : LIST_FIRST((head))); \
460 (var) = LIST_NEXT((var), field))
462 #define LIST_FOREACH_SAFE(var, head, field, tvar) \
463 for ((var) = LIST_FIRST((head)); \
464 (var) && ((tvar) = LIST_NEXT((var), field), 1); \
467 #define LIST_FOREACH_FROM_SAFE(var, head, field, tvar) \
468 for ((var) = ((var) ? (var) : LIST_FIRST((head))); \
469 (var) && ((tvar) = LIST_NEXT((var), field), 1); \
472 #define LIST_INIT(head) do { \
473 LIST_FIRST((head)) = NULL; \
476 #define LIST_INSERT_AFTER(listelm, elm, field) do { \
477 QMD_LIST_CHECK_NEXT(listelm, field); \
478 if ((LIST_NEXT((elm), field) = LIST_NEXT((listelm), field)) != NULL)\
479 LIST_NEXT((listelm), field)->field.le_prev = \
480 &LIST_NEXT((elm), field); \
481 LIST_NEXT((listelm), field) = (elm); \
482 (elm)->field.le_prev = &LIST_NEXT((listelm), field); \
485 #define LIST_INSERT_BEFORE(listelm, elm, field) do { \
486 QMD_LIST_CHECK_PREV(listelm, field); \
487 (elm)->field.le_prev = (listelm)->field.le_prev; \
488 LIST_NEXT((elm), field) = (listelm); \
489 *(listelm)->field.le_prev = (elm); \
490 (listelm)->field.le_prev = &LIST_NEXT((elm), field); \
493 #define LIST_INSERT_HEAD(head, elm, field) do { \
494 QMD_LIST_CHECK_HEAD((head), field); \
495 if ((LIST_NEXT((elm), field) = LIST_FIRST((head))) != NULL) \
496 LIST_FIRST((head))->field.le_prev = &LIST_NEXT((elm), field);\
497 LIST_FIRST((head)) = (elm); \
498 (elm)->field.le_prev = &LIST_FIRST((head)); \
501 #define LIST_NEXT(elm, field) ((elm)->field.le_next)
503 #define LIST_PREV(elm, head, type, field) \
504 ((elm)->field.le_prev == &LIST_FIRST((head)) ? NULL : \
505 __containerof((elm)->field.le_prev, \
506 QUEUE_TYPEOF(type), field.le_next))
508 #define LIST_REMOVE(elm, field) do { \
509 QMD_SAVELINK(oldnext, (elm)->field.le_next); \
510 QMD_SAVELINK(oldprev, (elm)->field.le_prev); \
511 QMD_LIST_CHECK_NEXT(elm, field); \
512 QMD_LIST_CHECK_PREV(elm, field); \
513 if (LIST_NEXT((elm), field) != NULL) \
514 LIST_NEXT((elm), field)->field.le_prev = \
515 (elm)->field.le_prev; \
516 *(elm)->field.le_prev = LIST_NEXT((elm), field); \
521 #define LIST_SWAP(head1, head2, type, field) do { \
522 QUEUE_TYPEOF(type) *swap_tmp = LIST_FIRST(head1); \
523 LIST_FIRST((head1)) = LIST_FIRST((head2)); \
524 LIST_FIRST((head2)) = swap_tmp; \
525 if ((swap_tmp = LIST_FIRST((head1))) != NULL) \
526 swap_tmp->field.le_prev = &LIST_FIRST((head1)); \
527 if ((swap_tmp = LIST_FIRST((head2))) != NULL) \
528 swap_tmp->field.le_prev = &LIST_FIRST((head2)); \
532 * Tail queue declarations.
534 #define TAILQ_HEAD(name, type) \
536 struct type *tqh_first; /* first element */ \
537 struct type **tqh_last; /* addr of last next element */ \
541 #define TAILQ_CLASS_HEAD(name, type) \
543 class type *tqh_first; /* first element */ \
544 class type **tqh_last; /* addr of last next element */ \
548 #define TAILQ_HEAD_INITIALIZER(head) \
549 { NULL, &(head).tqh_first }
551 #define TAILQ_ENTRY(type) \
553 struct type *tqe_next; /* next element */ \
554 struct type **tqe_prev; /* address of previous next element */ \
558 #define TAILQ_CLASS_ENTRY(type) \
560 class type *tqe_next; /* next element */ \
561 class type **tqe_prev; /* address of previous next element */ \
566 * Tail queue functions.
568 #if (defined(_KERNEL) && defined(INVARIANTS))
569 #define QMD_TAILQ_CHECK_HEAD(head, field) do { \
570 if (!TAILQ_EMPTY(head) && \
571 TAILQ_FIRST((head))->field.tqe_prev != \
572 &TAILQ_FIRST((head))) \
573 panic("Bad tailq head %p first->prev != head", (head)); \
576 #define QMD_TAILQ_CHECK_TAIL(head, field) do { \
577 if (*(head)->tqh_last != NULL) \
578 panic("Bad tailq NEXT(%p->tqh_last) != NULL", (head)); \
581 #define QMD_TAILQ_CHECK_NEXT(elm, field) do { \
582 if (TAILQ_NEXT((elm), field) != NULL && \
583 TAILQ_NEXT((elm), field)->field.tqe_prev != \
584 &((elm)->field.tqe_next)) \
585 panic("Bad link elm %p next->prev != elm", (elm)); \
588 #define QMD_TAILQ_CHECK_PREV(elm, field) do { \
589 if (*(elm)->field.tqe_prev != (elm)) \
590 panic("Bad link elm %p prev->next != elm", (elm)); \
593 #define QMD_TAILQ_CHECK_HEAD(head, field)
594 #define QMD_TAILQ_CHECK_TAIL(head, headname)
595 #define QMD_TAILQ_CHECK_NEXT(elm, field)
596 #define QMD_TAILQ_CHECK_PREV(elm, field)
597 #endif /* (_KERNEL && INVARIANTS) */
599 #define TAILQ_CONCAT(head1, head2, field) do { \
600 if (!TAILQ_EMPTY(head2)) { \
601 *(head1)->tqh_last = (head2)->tqh_first; \
602 (head2)->tqh_first->field.tqe_prev = (head1)->tqh_last; \
603 (head1)->tqh_last = (head2)->tqh_last; \
604 TAILQ_INIT((head2)); \
605 QMD_TRACE_HEAD(head1); \
606 QMD_TRACE_HEAD(head2); \
610 #define TAILQ_EMPTY(head) ((head)->tqh_first == NULL)
612 #define TAILQ_FIRST(head) ((head)->tqh_first)
614 #define TAILQ_FOREACH(var, head, field) \
615 for ((var) = TAILQ_FIRST((head)); \
617 (var) = TAILQ_NEXT((var), field))
619 #define TAILQ_FOREACH_FROM(var, head, field) \
620 for ((var) = ((var) ? (var) : TAILQ_FIRST((head))); \
622 (var) = TAILQ_NEXT((var), field))
624 #define TAILQ_FOREACH_SAFE(var, head, field, tvar) \
625 for ((var) = TAILQ_FIRST((head)); \
626 (var) && ((tvar) = TAILQ_NEXT((var), field), 1); \
629 #define TAILQ_FOREACH_FROM_SAFE(var, head, field, tvar) \
630 for ((var) = ((var) ? (var) : TAILQ_FIRST((head))); \
631 (var) && ((tvar) = TAILQ_NEXT((var), field), 1); \
634 #define TAILQ_FOREACH_REVERSE(var, head, headname, field) \
635 for ((var) = TAILQ_LAST((head), headname); \
637 (var) = TAILQ_PREV((var), headname, field))
639 #define TAILQ_FOREACH_REVERSE_FROM(var, head, headname, field) \
640 for ((var) = ((var) ? (var) : TAILQ_LAST((head), headname)); \
642 (var) = TAILQ_PREV((var), headname, field))
644 #define TAILQ_FOREACH_REVERSE_SAFE(var, head, headname, field, tvar) \
645 for ((var) = TAILQ_LAST((head), headname); \
646 (var) && ((tvar) = TAILQ_PREV((var), headname, field), 1); \
649 #define TAILQ_FOREACH_REVERSE_FROM_SAFE(var, head, headname, field, tvar) \
650 for ((var) = ((var) ? (var) : TAILQ_LAST((head), headname)); \
651 (var) && ((tvar) = TAILQ_PREV((var), headname, field), 1); \
654 #define TAILQ_INIT(head) do { \
655 TAILQ_FIRST((head)) = NULL; \
656 (head)->tqh_last = &TAILQ_FIRST((head)); \
657 QMD_TRACE_HEAD(head); \
660 #define TAILQ_INSERT_AFTER(head, listelm, elm, field) do { \
661 QMD_TAILQ_CHECK_NEXT(listelm, field); \
662 if ((TAILQ_NEXT((elm), field) = TAILQ_NEXT((listelm), field)) != NULL)\
663 TAILQ_NEXT((elm), field)->field.tqe_prev = \
664 &TAILQ_NEXT((elm), field); \
666 (head)->tqh_last = &TAILQ_NEXT((elm), field); \
667 QMD_TRACE_HEAD(head); \
669 TAILQ_NEXT((listelm), field) = (elm); \
670 (elm)->field.tqe_prev = &TAILQ_NEXT((listelm), field); \
671 QMD_TRACE_ELEM(&(elm)->field); \
672 QMD_TRACE_ELEM(&(listelm)->field); \
675 #define TAILQ_INSERT_BEFORE(listelm, elm, field) do { \
676 QMD_TAILQ_CHECK_PREV(listelm, field); \
677 (elm)->field.tqe_prev = (listelm)->field.tqe_prev; \
678 TAILQ_NEXT((elm), field) = (listelm); \
679 *(listelm)->field.tqe_prev = (elm); \
680 (listelm)->field.tqe_prev = &TAILQ_NEXT((elm), field); \
681 QMD_TRACE_ELEM(&(elm)->field); \
682 QMD_TRACE_ELEM(&(listelm)->field); \
685 #define TAILQ_INSERT_HEAD(head, elm, field) do { \
686 QMD_TAILQ_CHECK_HEAD(head, field); \
687 if ((TAILQ_NEXT((elm), field) = TAILQ_FIRST((head))) != NULL) \
688 TAILQ_FIRST((head))->field.tqe_prev = \
689 &TAILQ_NEXT((elm), field); \
691 (head)->tqh_last = &TAILQ_NEXT((elm), field); \
692 TAILQ_FIRST((head)) = (elm); \
693 (elm)->field.tqe_prev = &TAILQ_FIRST((head)); \
694 QMD_TRACE_HEAD(head); \
695 QMD_TRACE_ELEM(&(elm)->field); \
698 #define TAILQ_INSERT_TAIL(head, elm, field) do { \
699 QMD_TAILQ_CHECK_TAIL(head, field); \
700 TAILQ_NEXT((elm), field) = NULL; \
701 (elm)->field.tqe_prev = (head)->tqh_last; \
702 *(head)->tqh_last = (elm); \
703 (head)->tqh_last = &TAILQ_NEXT((elm), field); \
704 QMD_TRACE_HEAD(head); \
705 QMD_TRACE_ELEM(&(elm)->field); \
708 #define TAILQ_LAST(head, headname) \
709 (*(((struct headname *)((head)->tqh_last))->tqh_last))
711 #define TAILQ_NEXT(elm, field) ((elm)->field.tqe_next)
713 #define TAILQ_PREV(elm, headname, field) \
714 (*(((struct headname *)((elm)->field.tqe_prev))->tqh_last))
716 #define TAILQ_REMOVE(head, elm, field) do { \
717 QMD_SAVELINK(oldnext, (elm)->field.tqe_next); \
718 QMD_SAVELINK(oldprev, (elm)->field.tqe_prev); \
719 QMD_TAILQ_CHECK_NEXT(elm, field); \
720 QMD_TAILQ_CHECK_PREV(elm, field); \
721 if ((TAILQ_NEXT((elm), field)) != NULL) \
722 TAILQ_NEXT((elm), field)->field.tqe_prev = \
723 (elm)->field.tqe_prev; \
725 (head)->tqh_last = (elm)->field.tqe_prev; \
726 QMD_TRACE_HEAD(head); \
728 *(elm)->field.tqe_prev = TAILQ_NEXT((elm), field); \
731 QMD_TRACE_ELEM(&(elm)->field); \
734 #define TAILQ_SWAP(head1, head2, type, field) do { \
735 QUEUE_TYPEOF(type) *swap_first = (head1)->tqh_first; \
736 QUEUE_TYPEOF(type) **swap_last = (head1)->tqh_last; \
737 (head1)->tqh_first = (head2)->tqh_first; \
738 (head1)->tqh_last = (head2)->tqh_last; \
739 (head2)->tqh_first = swap_first; \
740 (head2)->tqh_last = swap_last; \
741 if ((swap_first = (head1)->tqh_first) != NULL) \
742 swap_first->field.tqe_prev = &(head1)->tqh_first; \
744 (head1)->tqh_last = &(head1)->tqh_first; \
745 if ((swap_first = (head2)->tqh_first) != NULL) \
746 swap_first->field.tqe_prev = &(head2)->tqh_first; \
748 (head2)->tqh_last = &(head2)->tqh_first; \
751 #endif /* !_SYS_QUEUE_H_ */