115#ifdef QUEUE_MACRO_DEBUG
116#warn Use QUEUE_MACRO_DEBUG_TRACE and/or QUEUE_MACRO_DEBUG_TRASH
117#define QUEUE_MACRO_DEBUG_TRACE
118#define QUEUE_MACRO_DEBUG_TRASH
121#ifdef QUEUE_MACRO_DEBUG_TRACE
124 unsigned long lastline;
125 unsigned long prevline;
126 const char *lastfile;
127 const char *prevfile;
130#define TRACEBUF struct qm_trace trace;
131#define TRACEBUF_INITIALIZER { __LINE__, 0, __FILE__, NULL } ,
133#define QMD_TRACE_HEAD(head) do { \
134 (head)->trace.prevline = (head)->trace.lastline; \
135 (head)->trace.prevfile = (head)->trace.lastfile; \
136 (head)->trace.lastline = __LINE__; \
137 (head)->trace.lastfile = __FILE__; \
140#define QMD_TRACE_ELEM(elem) do { \
141 (elem)->trace.prevline = (elem)->trace.lastline; \
142 (elem)->trace.prevfile = (elem)->trace.lastfile; \
143 (elem)->trace.lastline = __LINE__; \
144 (elem)->trace.lastfile = __FILE__; \
148#define QMD_TRACE_ELEM(elem)
149#define QMD_TRACE_HEAD(head)
151#define TRACEBUF_INITIALIZER
154#ifdef QUEUE_MACRO_DEBUG_TRASH
155#define QMD_SAVELINK(name, link) void **name = (void *)&(link)
156#define TRASHIT(x) do {(x) = (void *)-1;} while (0)
157#define QMD_IS_TRASHED(x) ((x) == (void *)(intptr_t)-1)
159#define QMD_SAVELINK(name, link)
161#define QMD_IS_TRASHED(x) 0
168#define QUEUE_TYPEOF(type) type
170#define QUEUE_TYPEOF(type) struct type
176#define SLIST_HEAD(name, type) \
178 struct type *slh_first; \
181#define SLIST_CLASS_HEAD(name, type) \
183 class type *slh_first; \
186#define SLIST_HEAD_INITIALIZER(head) \
189#define SLIST_ENTRY(type) \
191 struct type *sle_next; \
194#define SLIST_CLASS_ENTRY(type) \
196 class type *sle_next; \
202#if (defined(_KERNEL) && defined(INVARIANTS))
203#define QMD_SLIST_CHECK_PREVPTR(prevp, elm) do { \
204 if (*(prevp) != (elm)) \
205 panic("Bad prevptr *(%p) == %p != %p", \
206 (prevp), *(prevp), (elm)); \
209#define QMD_SLIST_CHECK_PREVPTR(prevp, elm)
212#define SLIST_CONCAT(head1, head2, type, field) do { \
213 QUEUE_TYPEOF(type) *curelm = SLIST_FIRST(head1); \
214 if (curelm == NULL) { \
215 if ((SLIST_FIRST(head1) = SLIST_FIRST(head2)) != NULL) \
217 } else if (SLIST_FIRST(head2) != NULL) { \
218 while (SLIST_NEXT(curelm, field) != NULL) \
219 curelm = SLIST_NEXT(curelm, field); \
220 SLIST_NEXT(curelm, field) = SLIST_FIRST(head2); \
225#define SLIST_EMPTY(head) ((head)->slh_first == NULL)
227#define SLIST_FIRST(head) ((head)->slh_first)
229#define SLIST_FOREACH(var, head, field) \
230 for ((var) = SLIST_FIRST((head)); \
232 (var) = SLIST_NEXT((var), field))
234#define SLIST_FOREACH_FROM(var, head, field) \
235 for ((var) = ((var) ? (var) : SLIST_FIRST((head))); \
237 (var) = SLIST_NEXT((var), field))
239#define SLIST_FOREACH_SAFE(var, head, field, tvar) \
240 for ((var) = SLIST_FIRST((head)); \
241 (var) && ((tvar) = SLIST_NEXT((var), field), 1); \
244#define SLIST_FOREACH_FROM_SAFE(var, head, field, tvar) \
245 for ((var) = ((var) ? (var) : SLIST_FIRST((head))); \
246 (var) && ((tvar) = SLIST_NEXT((var), field), 1); \
249#define SLIST_FOREACH_PREVPTR(var, varp, head, field) \
250 for ((varp) = &SLIST_FIRST((head)); \
251 ((var) = *(varp)) != NULL; \
252 (varp) = &SLIST_NEXT((var), field))
254#define SLIST_INIT(head) do { \
255 SLIST_FIRST((head)) = NULL; \
258#define SLIST_INSERT_AFTER(slistelm, elm, field) do { \
259 SLIST_NEXT((elm), field) = SLIST_NEXT((slistelm), field); \
260 SLIST_NEXT((slistelm), field) = (elm); \
263#define SLIST_INSERT_HEAD(head, elm, field) do { \
264 SLIST_NEXT((elm), field) = SLIST_FIRST((head)); \
265 SLIST_FIRST((head)) = (elm); \
268#define SLIST_NEXT(elm, field) ((elm)->field.sle_next)
270#define SLIST_REMOVE(head, elm, type, field) do { \
271 if (SLIST_FIRST((head)) == (elm)) { \
272 SLIST_REMOVE_HEAD((head), field); \
275 QUEUE_TYPEOF(type) *curelm = SLIST_FIRST(head); \
276 while (SLIST_NEXT(curelm, field) != (elm)) \
277 curelm = SLIST_NEXT(curelm, field); \
278 SLIST_REMOVE_AFTER(curelm, field); \
282#define SLIST_REMOVE_AFTER(elm, field) do { \
283 QMD_SAVELINK(oldnext, SLIST_NEXT(elm, field)->field.sle_next); \
284 SLIST_NEXT(elm, field) = \
285 SLIST_NEXT(SLIST_NEXT(elm, field), field); \
289#define SLIST_REMOVE_HEAD(head, field) do { \
290 QMD_SAVELINK(oldnext, SLIST_FIRST(head)->field.sle_next); \
291 SLIST_FIRST((head)) = SLIST_NEXT(SLIST_FIRST((head)), field); \
295#define SLIST_REMOVE_PREVPTR(prevp, elm, field) do { \
296 QMD_SLIST_CHECK_PREVPTR(prevp, elm); \
297 *(prevp) = SLIST_NEXT(elm, field); \
298 TRASHIT((elm)->field.sle_next); \
301#define SLIST_SWAP(head1, head2, type) do { \
302 QUEUE_TYPEOF(type) *swap_first = SLIST_FIRST(head1); \
303 SLIST_FIRST(head1) = SLIST_FIRST(head2); \
304 SLIST_FIRST(head2) = swap_first; \
307#define SLIST_END(head) NULL
312#define STAILQ_HEAD(name, type) \
314 struct type *stqh_first; \
315 struct type **stqh_last; \
318#define STAILQ_CLASS_HEAD(name, type) \
320 class type *stqh_first; \
321 class type **stqh_last; \
324#define STAILQ_HEAD_INITIALIZER(head) \
325 { NULL, &(head).stqh_first }
327#define STAILQ_ENTRY(type) \
329 struct type *stqe_next; \
332#define STAILQ_CLASS_ENTRY(type) \
334 class type *stqe_next; \
340#if (defined(_KERNEL) && defined(INVARIANTS))
347#define QMD_STAILQ_CHECK_EMPTY(head) do { \
348 if ((head)->stqh_last != &(head)->stqh_first) \
349 panic("Empty stailq %p->stqh_last is %p, not head's " \
350 "first field address", (head), (head)->stqh_last); \
353#define STAILQ_ASSERT_EMPTY(head) do { \
354 if (!STAILQ_EMPTY((head))) \
355 panic("stailq %p is not empty", (head)); \
363#define QMD_STAILQ_CHECK_TAIL(head) do { \
364 if (*(head)->stqh_last != NULL) \
365 panic("Stailq %p last element's next pointer is %p, " \
366 "not NULL", (head), *(head)->stqh_last); \
369#define QMD_STAILQ_CHECK_EMPTY(head)
370#define STAILQ_ASSERT_EMPTY(head)
371#define QMD_STAILQ_CHECK_TAIL(head)
374#define STAILQ_CONCAT(head1, head2) do { \
375 if (!STAILQ_EMPTY((head2))) { \
376 *(head1)->stqh_last = (head2)->stqh_first; \
377 (head1)->stqh_last = (head2)->stqh_last; \
378 STAILQ_INIT((head2)); \
382#define STAILQ_EMPTY(head) ({ \
383 if (STAILQ_FIRST(head) == NULL) \
384 QMD_STAILQ_CHECK_EMPTY(head); \
385 STAILQ_FIRST(head) == NULL; \
388#define STAILQ_FIRST(head) ((head)->stqh_first)
390#define STAILQ_FOREACH(var, head, field) \
391 for((var) = STAILQ_FIRST((head)); \
393 (var) = STAILQ_NEXT((var), field))
395#define STAILQ_FOREACH_FROM(var, head, field) \
396 for ((var) = ((var) ? (var) : STAILQ_FIRST((head))); \
398 (var) = STAILQ_NEXT((var), field))
400#define STAILQ_FOREACH_SAFE(var, head, field, tvar) \
401 for ((var) = STAILQ_FIRST((head)); \
402 (var) && ((tvar) = STAILQ_NEXT((var), field), 1); \
405#define STAILQ_FOREACH_FROM_SAFE(var, head, field, tvar) \
406 for ((var) = ((var) ? (var) : STAILQ_FIRST((head))); \
407 (var) && ((tvar) = STAILQ_NEXT((var), field), 1); \
410#define STAILQ_INIT(head) do { \
411 STAILQ_FIRST((head)) = NULL; \
412 (head)->stqh_last = &STAILQ_FIRST((head)); \
415#define STAILQ_INSERT_AFTER(head, tqelm, elm, field) do { \
416 if ((STAILQ_NEXT((elm), field) = STAILQ_NEXT((tqelm), field)) == NULL)\
417 (head)->stqh_last = &STAILQ_NEXT((elm), field); \
418 STAILQ_NEXT((tqelm), field) = (elm); \
421#define STAILQ_INSERT_HEAD(head, elm, field) do { \
422 if ((STAILQ_NEXT((elm), field) = STAILQ_FIRST((head))) == NULL) \
423 (head)->stqh_last = &STAILQ_NEXT((elm), field); \
424 STAILQ_FIRST((head)) = (elm); \
427#define STAILQ_INSERT_TAIL(head, elm, field) do { \
428 QMD_STAILQ_CHECK_TAIL(head); \
429 STAILQ_NEXT((elm), field) = NULL; \
430 *(head)->stqh_last = (elm); \
431 (head)->stqh_last = &STAILQ_NEXT((elm), field); \
434#define STAILQ_LAST(head, type, field) \
435 (STAILQ_EMPTY((head)) ? NULL : \
436 __containerof((head)->stqh_last, \
437 QUEUE_TYPEOF(type), field.stqe_next))
439#define STAILQ_NEXT(elm, field) ((elm)->field.stqe_next)
441#define STAILQ_REMOVE(head, elm, type, field) do { \
442 QMD_SAVELINK(oldnext, (elm)->field.stqe_next); \
443 if (STAILQ_FIRST((head)) == (elm)) { \
444 STAILQ_REMOVE_HEAD((head), field); \
447 QUEUE_TYPEOF(type) *curelm = STAILQ_FIRST(head); \
448 while (STAILQ_NEXT(curelm, field) != (elm)) \
449 curelm = STAILQ_NEXT(curelm, field); \
450 STAILQ_REMOVE_AFTER(head, curelm, field); \
455#define STAILQ_REMOVE_AFTER(head, elm, field) do { \
456 if ((STAILQ_NEXT(elm, field) = \
457 STAILQ_NEXT(STAILQ_NEXT(elm, field), field)) == NULL) \
458 (head)->stqh_last = &STAILQ_NEXT((elm), field); \
461#define STAILQ_REMOVE_HEAD(head, field) do { \
462 if ((STAILQ_FIRST((head)) = \
463 STAILQ_NEXT(STAILQ_FIRST((head)), field)) == NULL) \
464 (head)->stqh_last = &STAILQ_FIRST((head)); \
467#define STAILQ_SWAP(head1, head2, type) do { \
468 QUEUE_TYPEOF(type) *swap_first = STAILQ_FIRST(head1); \
469 QUEUE_TYPEOF(type) **swap_last = (head1)->stqh_last; \
470 STAILQ_FIRST(head1) = STAILQ_FIRST(head2); \
471 (head1)->stqh_last = (head2)->stqh_last; \
472 STAILQ_FIRST(head2) = swap_first; \
473 (head2)->stqh_last = swap_last; \
474 if (STAILQ_FIRST(head1) == NULL) \
475 (head1)->stqh_last = &STAILQ_FIRST(head1); \
476 if (STAILQ_FIRST(head2) == NULL) \
477 (head2)->stqh_last = &STAILQ_FIRST(head2); \
480#define STAILQ_END(head) NULL
486#define LIST_HEAD(name, type) \
488 struct type *lh_first; \
491#define LIST_CLASS_HEAD(name, type) \
493 class type *lh_first; \
496#define LIST_HEAD_INITIALIZER(head) \
499#define LIST_ENTRY(type) \
501 struct type *le_next; \
502 struct type **le_prev; \
505#define LIST_CLASS_ENTRY(type) \
507 class type *le_next; \
508 class type **le_prev; \
515#if (defined(_KERNEL) && defined(INVARIANTS))
522#define QMD_LIST_CHECK_HEAD(head, field) do { \
523 if (LIST_FIRST((head)) != NULL && \
524 LIST_FIRST((head))->field.le_prev != \
525 &LIST_FIRST((head))) \
526 panic("Bad list head %p first->prev != head", (head)); \
535#define QMD_LIST_CHECK_NEXT(elm, field) do { \
536 if (LIST_NEXT((elm), field) != NULL && \
537 LIST_NEXT((elm), field)->field.le_prev != \
538 &((elm)->field.le_next)) \
539 panic("Bad link elm %p next->prev != elm", (elm)); \
547#define QMD_LIST_CHECK_PREV(elm, field) do { \
548 if (*(elm)->field.le_prev != (elm)) \
549 panic("Bad link elm %p prev->next != elm", (elm)); \
552#define QMD_LIST_CHECK_HEAD(head, field)
553#define QMD_LIST_CHECK_NEXT(elm, field)
554#define QMD_LIST_CHECK_PREV(elm, field)
557#define LIST_CONCAT(head1, head2, type, field) do { \
558 QUEUE_TYPEOF(type) *curelm = LIST_FIRST(head1); \
559 if (curelm == NULL) { \
560 if ((LIST_FIRST(head1) = LIST_FIRST(head2)) != NULL) { \
561 LIST_FIRST(head2)->field.le_prev = \
562 &LIST_FIRST((head1)); \
565 } else if (LIST_FIRST(head2) != NULL) { \
566 while (LIST_NEXT(curelm, field) != NULL) \
567 curelm = LIST_NEXT(curelm, field); \
568 LIST_NEXT(curelm, field) = LIST_FIRST(head2); \
569 LIST_FIRST(head2)->field.le_prev = &LIST_NEXT(curelm, field);\
574#define LIST_EMPTY(head) ((head)->lh_first == NULL)
576#define LIST_FIRST(head) ((head)->lh_first)
578#define LIST_FOREACH(var, head, field) \
579 for ((var) = LIST_FIRST((head)); \
581 (var) = LIST_NEXT((var), field))
583#define LIST_FOREACH_FROM(var, head, field) \
584 for ((var) = ((var) ? (var) : LIST_FIRST((head))); \
586 (var) = LIST_NEXT((var), field))
588#define LIST_FOREACH_SAFE(var, head, field, tvar) \
589 for ((var) = LIST_FIRST((head)); \
590 (var) && ((tvar) = LIST_NEXT((var), field), 1); \
593#define LIST_FOREACH_FROM_SAFE(var, head, field, tvar) \
594 for ((var) = ((var) ? (var) : LIST_FIRST((head))); \
595 (var) && ((tvar) = LIST_NEXT((var), field), 1); \
598#define LIST_INIT(head) do { \
599 LIST_FIRST((head)) = NULL; \
602#define LIST_INSERT_AFTER(listelm, elm, field) do { \
603 QMD_LIST_CHECK_NEXT(listelm, field); \
604 if ((LIST_NEXT((elm), field) = LIST_NEXT((listelm), field)) != NULL)\
605 LIST_NEXT((listelm), field)->field.le_prev = \
606 &LIST_NEXT((elm), field); \
607 LIST_NEXT((listelm), field) = (elm); \
608 (elm)->field.le_prev = &LIST_NEXT((listelm), field); \
611#define LIST_INSERT_BEFORE(listelm, elm, field) do { \
612 QMD_LIST_CHECK_PREV(listelm, field); \
613 (elm)->field.le_prev = (listelm)->field.le_prev; \
614 LIST_NEXT((elm), field) = (listelm); \
615 *(listelm)->field.le_prev = (elm); \
616 (listelm)->field.le_prev = &LIST_NEXT((elm), field); \
619#define LIST_INSERT_HEAD(head, elm, field) do { \
620 QMD_LIST_CHECK_HEAD((head), field); \
621 if ((LIST_NEXT((elm), field) = LIST_FIRST((head))) != NULL) \
622 LIST_FIRST((head))->field.le_prev = &LIST_NEXT((elm), field);\
623 LIST_FIRST((head)) = (elm); \
624 (elm)->field.le_prev = &LIST_FIRST((head)); \
627#define LIST_NEXT(elm, field) ((elm)->field.le_next)
629#define LIST_PREV(elm, head, type, field) \
630 ((elm)->field.le_prev == &LIST_FIRST((head)) ? NULL : \
631 __containerof((elm)->field.le_prev, \
632 QUEUE_TYPEOF(type), field.le_next))
634#define LIST_REMOVE_HEAD(head, field) \
635 LIST_REMOVE(LIST_FIRST(head), field)
637#define LIST_REMOVE(elm, field) do { \
638 QMD_SAVELINK(oldnext, (elm)->field.le_next); \
639 QMD_SAVELINK(oldprev, (elm)->field.le_prev); \
640 QMD_LIST_CHECK_NEXT(elm, field); \
641 QMD_LIST_CHECK_PREV(elm, field); \
642 if (LIST_NEXT((elm), field) != NULL) \
643 LIST_NEXT((elm), field)->field.le_prev = \
644 (elm)->field.le_prev; \
645 *(elm)->field.le_prev = LIST_NEXT((elm), field); \
650#define LIST_REPLACE(elm, elm2, field) do { \
651 QMD_SAVELINK(oldnext, (elm)->field.le_next); \
652 QMD_SAVELINK(oldprev, (elm)->field.le_prev); \
653 QMD_LIST_CHECK_NEXT(elm, field); \
654 QMD_LIST_CHECK_PREV(elm, field); \
655 LIST_NEXT((elm2), field) = LIST_NEXT((elm), field); \
656 if (LIST_NEXT((elm2), field) != NULL) \
657 LIST_NEXT((elm2), field)->field.le_prev = \
658 &(elm2)->field.le_next; \
659 (elm2)->field.le_prev = (elm)->field.le_prev; \
660 *(elm2)->field.le_prev = (elm2); \
665#define LIST_SWAP(head1, head2, type, field) do { \
666 QUEUE_TYPEOF(type) *swap_tmp = LIST_FIRST(head1); \
667 LIST_FIRST((head1)) = LIST_FIRST((head2)); \
668 LIST_FIRST((head2)) = swap_tmp; \
669 if ((swap_tmp = LIST_FIRST((head1))) != NULL) \
670 swap_tmp->field.le_prev = &LIST_FIRST((head1)); \
671 if ((swap_tmp = LIST_FIRST((head2))) != NULL) \
672 swap_tmp->field.le_prev = &LIST_FIRST((head2)); \
675#define LIST_END(head) NULL
680#define TAILQ_HEAD(name, type) \
682 struct type *tqh_first; \
683 struct type **tqh_last; \
687#define TAILQ_CLASS_HEAD(name, type) \
689 class type *tqh_first; \
690 class type **tqh_last; \
694#define TAILQ_HEAD_INITIALIZER(head) \
695 { NULL, &(head).tqh_first, TRACEBUF_INITIALIZER }
697#define TAILQ_ENTRY(type) \
699 struct type *tqe_next; \
700 struct type **tqe_prev; \
704#define TAILQ_CLASS_ENTRY(type) \
706 class type *tqe_next; \
707 class type **tqe_prev; \
714#if (defined(_KERNEL) && defined(INVARIANTS))
721#define QMD_TAILQ_CHECK_HEAD(head, field) do { \
722 if (!TAILQ_EMPTY(head) && \
723 TAILQ_FIRST((head))->field.tqe_prev != \
724 &TAILQ_FIRST((head))) \
725 panic("Bad tailq head %p first->prev != head", (head)); \
733#define QMD_TAILQ_CHECK_TAIL(head, field) do { \
734 if (*(head)->tqh_last != NULL) \
735 panic("Bad tailq NEXT(%p->tqh_last) != NULL", (head)); \
744#define QMD_TAILQ_CHECK_NEXT(elm, field) do { \
745 if (TAILQ_NEXT((elm), field) != NULL && \
746 TAILQ_NEXT((elm), field)->field.tqe_prev != \
747 &((elm)->field.tqe_next)) \
748 panic("Bad link elm %p next->prev != elm", (elm)); \
756#define QMD_TAILQ_CHECK_PREV(elm, field) do { \
757 if (*(elm)->field.tqe_prev != (elm)) \
758 panic("Bad link elm %p prev->next != elm", (elm)); \
761#define QMD_TAILQ_CHECK_HEAD(head, field)
762#define QMD_TAILQ_CHECK_TAIL(head, headname)
763#define QMD_TAILQ_CHECK_NEXT(elm, field)
764#define QMD_TAILQ_CHECK_PREV(elm, field)
767#define TAILQ_CONCAT(head1, head2, field) do { \
768 if (!TAILQ_EMPTY(head2)) { \
769 *(head1)->tqh_last = (head2)->tqh_first; \
770 (head2)->tqh_first->field.tqe_prev = (head1)->tqh_last; \
771 (head1)->tqh_last = (head2)->tqh_last; \
772 TAILQ_INIT((head2)); \
773 QMD_TRACE_HEAD(head1); \
774 QMD_TRACE_HEAD(head2); \
778#define TAILQ_EMPTY(head) ((head)->tqh_first == NULL)
780#define TAILQ_FIRST(head) ((head)->tqh_first)
782#define TAILQ_FOREACH(var, head, field) \
783 for ((var) = TAILQ_FIRST((head)); \
785 (var) = TAILQ_NEXT((var), field))
787#define TAILQ_FOREACH_FROM(var, head, field) \
788 for ((var) = ((var) ? (var) : TAILQ_FIRST((head))); \
790 (var) = TAILQ_NEXT((var), field))
792#define TAILQ_FOREACH_SAFE(var, head, field, tvar) \
793 for ((var) = TAILQ_FIRST((head)); \
794 (var) && ((tvar) = TAILQ_NEXT((var), field), 1); \
797#define TAILQ_FOREACH_FROM_SAFE(var, head, field, tvar) \
798 for ((var) = ((var) ? (var) : TAILQ_FIRST((head))); \
799 (var) && ((tvar) = TAILQ_NEXT((var), field), 1); \
802#define TAILQ_FOREACH_REVERSE(var, head, headname, field) \
803 for ((var) = TAILQ_LAST((head), headname); \
805 (var) = TAILQ_PREV((var), headname, field))
807#define TAILQ_FOREACH_REVERSE_FROM(var, head, headname, field) \
808 for ((var) = ((var) ? (var) : TAILQ_LAST((head), headname)); \
810 (var) = TAILQ_PREV((var), headname, field))
812#define TAILQ_FOREACH_REVERSE_SAFE(var, head, headname, field, tvar) \
813 for ((var) = TAILQ_LAST((head), headname); \
814 (var) && ((tvar) = TAILQ_PREV((var), headname, field), 1); \
817#define TAILQ_FOREACH_REVERSE_FROM_SAFE(var, head, headname, field, tvar)\
818 for ((var) = ((var) ? (var) : TAILQ_LAST((head), headname)); \
819 (var) && ((tvar) = TAILQ_PREV((var), headname, field), 1); \
822#define TAILQ_INIT(head) do { \
823 TAILQ_FIRST((head)) = NULL; \
824 (head)->tqh_last = &TAILQ_FIRST((head)); \
825 QMD_TRACE_HEAD(head); \
828#define TAILQ_INSERT_AFTER(head, listelm, elm, field) do { \
829 QMD_TAILQ_CHECK_NEXT(listelm, field); \
830 if ((TAILQ_NEXT((elm), field) = TAILQ_NEXT((listelm), field)) != NULL)\
831 TAILQ_NEXT((elm), field)->field.tqe_prev = \
832 &TAILQ_NEXT((elm), field); \
834 (head)->tqh_last = &TAILQ_NEXT((elm), field); \
835 QMD_TRACE_HEAD(head); \
837 TAILQ_NEXT((listelm), field) = (elm); \
838 (elm)->field.tqe_prev = &TAILQ_NEXT((listelm), field); \
839 QMD_TRACE_ELEM(&(elm)->field); \
840 QMD_TRACE_ELEM(&(listelm)->field); \
843#define TAILQ_INSERT_BEFORE(listelm, elm, field) do { \
844 QMD_TAILQ_CHECK_PREV(listelm, field); \
845 (elm)->field.tqe_prev = (listelm)->field.tqe_prev; \
846 TAILQ_NEXT((elm), field) = (listelm); \
847 *(listelm)->field.tqe_prev = (elm); \
848 (listelm)->field.tqe_prev = &TAILQ_NEXT((elm), field); \
849 QMD_TRACE_ELEM(&(elm)->field); \
850 QMD_TRACE_ELEM(&(listelm)->field); \
853#define TAILQ_INSERT_HEAD(head, elm, field) do { \
854 QMD_TAILQ_CHECK_HEAD(head, field); \
855 if ((TAILQ_NEXT((elm), field) = TAILQ_FIRST((head))) != NULL) \
856 TAILQ_FIRST((head))->field.tqe_prev = \
857 &TAILQ_NEXT((elm), field); \
859 (head)->tqh_last = &TAILQ_NEXT((elm), field); \
860 TAILQ_FIRST((head)) = (elm); \
861 (elm)->field.tqe_prev = &TAILQ_FIRST((head)); \
862 QMD_TRACE_HEAD(head); \
863 QMD_TRACE_ELEM(&(elm)->field); \
866#define TAILQ_INSERT_TAIL(head, elm, field) do { \
867 QMD_TAILQ_CHECK_TAIL(head, field); \
868 TAILQ_NEXT((elm), field) = NULL; \
869 (elm)->field.tqe_prev = (head)->tqh_last; \
870 *(head)->tqh_last = (elm); \
871 (head)->tqh_last = &TAILQ_NEXT((elm), field); \
872 QMD_TRACE_HEAD(head); \
873 QMD_TRACE_ELEM(&(elm)->field); \
876#define TAILQ_LAST(head, headname) \
877 (*(((struct headname *)((head)->tqh_last))->tqh_last))
886#define TAILQ_LAST_FAST(head, type, field) \
887 (TAILQ_EMPTY(head) ? NULL : __containerof((head)->tqh_last, QUEUE_TYPEOF(type), field.tqe_next))
889#define TAILQ_NEXT(elm, field) ((elm)->field.tqe_next)
891#define TAILQ_PREV(elm, headname, field) \
892 (*(((struct headname *)((elm)->field.tqe_prev))->tqh_last))
894#define TAILQ_PREV_FAST(elm, head, type, field) \
895 ((elm)->field.tqe_prev == &(head)->tqh_first ? NULL : \
896 __containerof((elm)->field.tqe_prev, QUEUE_TYPEOF(type), field.tqe_next))
898#define TAILQ_REMOVE_HEAD(head, field) \
899 TAILQ_REMOVE(head, TAILQ_FIRST(head), field)
901#define TAILQ_REMOVE(head, elm, field) do { \
902 QMD_SAVELINK(oldnext, (elm)->field.tqe_next); \
903 QMD_SAVELINK(oldprev, (elm)->field.tqe_prev); \
904 QMD_TAILQ_CHECK_NEXT(elm, field); \
905 QMD_TAILQ_CHECK_PREV(elm, field); \
906 if ((TAILQ_NEXT((elm), field)) != NULL) \
907 TAILQ_NEXT((elm), field)->field.tqe_prev = \
908 (elm)->field.tqe_prev; \
910 (head)->tqh_last = (elm)->field.tqe_prev; \
911 QMD_TRACE_HEAD(head); \
913 *(elm)->field.tqe_prev = TAILQ_NEXT((elm), field); \
916 QMD_TRACE_ELEM(&(elm)->field); \
919#define TAILQ_REPLACE(head, elm, elm2, field) do { \
920 QMD_SAVELINK(oldnext, (elm)->field.tqe_next); \
921 QMD_SAVELINK(oldprev, (elm)->field.tqe_prev); \
922 QMD_TAILQ_CHECK_NEXT(elm, field); \
923 QMD_TAILQ_CHECK_PREV(elm, field); \
924 TAILQ_NEXT((elm2), field) = TAILQ_NEXT((elm), field); \
925 if (TAILQ_NEXT((elm2), field) != TAILQ_END(head)) \
926 TAILQ_NEXT((elm2), field)->field.tqe_prev = \
927 &(elm2)->field.tqe_next; \
929 (head)->tqh_last = &(elm2)->field.tqe_next; \
930 (elm2)->field.tqe_prev = (elm)->field.tqe_prev; \
931 *(elm2)->field.tqe_prev = (elm2); \
934 QMD_TRACE_ELEM(&(elm)->field); \
937#define TAILQ_SWAP(head1, head2, type, field) do { \
938 QUEUE_TYPEOF(type) *swap_first = (head1)->tqh_first; \
939 QUEUE_TYPEOF(type) **swap_last = (head1)->tqh_last; \
940 (head1)->tqh_first = (head2)->tqh_first; \
941 (head1)->tqh_last = (head2)->tqh_last; \
942 (head2)->tqh_first = swap_first; \
943 (head2)->tqh_last = swap_last; \
944 if ((swap_first = (head1)->tqh_first) != NULL) \
945 swap_first->field.tqe_prev = &(head1)->tqh_first; \
947 (head1)->tqh_last = &(head1)->tqh_first; \
948 if ((swap_first = (head2)->tqh_first) != NULL) \
949 swap_first->field.tqe_prev = &(head2)->tqh_first; \
951 (head2)->tqh_last = &(head2)->tqh_first; \
954#define TAILQ_END(head) NULL