]> git.sommitrealweird.co.uk Git - quagga-debian.git/blob - lib/queue.h
New upstream release and new maintainer
[quagga-debian.git] / lib / queue.h
1 /*-
2  * Copyright (c) 1991, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
16  *
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
27  * SUCH DAMAGE.
28  *
29  *      @(#)queue.h     8.5 (Berkeley) 8/20/94
30  * $FreeBSD$
31  */
32
33 #ifndef _SYS_QUEUE_H_
34 #define _SYS_QUEUE_H_
35
36 /*
37  * This file defines four types of data structures: singly-linked lists,
38  * singly-linked tail queues, lists and tail queues.
39  *
40  * A singly-linked list is headed by a single forward pointer. The elements
41  * are singly linked for minimum space and pointer manipulation overhead at
42  * the expense of O(n) removal for arbitrary elements. New elements can be
43  * added to the list after an existing element or at the head of the list.
44  * Elements being removed from the head of the list should use the explicit
45  * macro for this purpose for optimum efficiency. A singly-linked list may
46  * only be traversed in the forward direction.  Singly-linked lists are ideal
47  * for applications with large datasets and few or no removals or for
48  * implementing a LIFO queue.
49  *
50  * A singly-linked tail queue is headed by a pair of pointers, one to the
51  * head of the list and the other to the tail of the list. The elements are
52  * singly linked for minimum space and pointer manipulation overhead at the
53  * expense of O(n) removal for arbitrary elements. New elements can be added
54  * to the list after an existing element, at the head of the list, or at the
55  * end of the list. Elements being removed from the head of the tail queue
56  * should use the explicit macro for this purpose for optimum efficiency.
57  * A singly-linked tail queue may only be traversed in the forward direction.
58  * Singly-linked tail queues are ideal for applications with large datasets
59  * and few or no removals or for implementing a FIFO queue.
60  *
61  * A list is headed by a single forward pointer (or an array of forward
62  * pointers for a hash table header). The elements are doubly linked
63  * so that an arbitrary element can be removed without a need to
64  * traverse the list. New elements can be added to the list before
65  * or after an existing element or at the head of the list. A list
66  * may only be traversed in the forward direction.
67  *
68  * A tail queue is headed by a pair of pointers, one to the head of the
69  * list and the other to the tail of the list. The elements are doubly
70  * linked so that an arbitrary element can be removed without a need to
71  * traverse the list. New elements can be added to the list before or
72  * after an existing element, at the head of the list, or at the end of
73  * the list. A tail queue may be traversed in either direction.
74  *
75  * For details on the use of these macros, see the queue(3) manual page.
76  *
77  *
78  *                              SLIST   LIST    STAILQ  TAILQ
79  * _HEAD                        +       +       +       +
80  * _HEAD_INITIALIZER            +       +       +       +
81  * _ENTRY                       +       +       +       +
82  * _INIT                        +       +       +       +
83  * _EMPTY                       +       +       +       +
84  * _FIRST                       +       +       +       +
85  * _NEXT                        +       +       +       +
86  * _PREV                        -       -       -       +
87  * _LAST                        -       -       +       +
88  * _FOREACH                     +       +       +       +
89  * _FOREACH_SAFE                +       +       +       +
90  * _FOREACH_REVERSE             -       -       -       +
91  * _FOREACH_REVERSE_SAFE        -       -       -       +
92  * _INSERT_HEAD                 +       +       +       +
93  * _INSERT_BEFORE               -       +       -       +
94  * _INSERT_AFTER                +       +       +       +
95  * _INSERT_TAIL                 -       -       +       +
96  * _CONCAT                      -       -       +       +
97  * _REMOVE_AFTER                +       -       +       -
98  * _REMOVE_HEAD                 +       -       +       -
99  * _REMOVE                      +       +       +       +
100  * _SWAP                        +       +       +       +
101  *
102  */
103 #ifdef QUEUE_MACRO_DEBUG
104 /* Store the last 2 places the queue element or head was altered */
105 struct qm_trace {
106         char * lastfile;
107         int lastline;
108         char * prevfile;
109         int prevline;
110 };
111
112 #define TRACEBUF        struct qm_trace trace;
113 #define TRASHIT(x)      do {(x) = (void *)-1;} while (0)
114 #define QMD_SAVELINK(name, link)        void **name = (void *)&(link)
115
116 #define QMD_TRACE_HEAD(head) do {                                       \
117         (head)->trace.prevline = (head)->trace.lastline;                \
118         (head)->trace.prevfile = (head)->trace.lastfile;                \
119         (head)->trace.lastline = __LINE__;                              \
120         (head)->trace.lastfile = __FILE__;                              \
121 } while (0)
122
123 #define QMD_TRACE_ELEM(elem) do {                                       \
124         (elem)->trace.prevline = (elem)->trace.lastline;                \
125         (elem)->trace.prevfile = (elem)->trace.lastfile;                \
126         (elem)->trace.lastline = __LINE__;                              \
127         (elem)->trace.lastfile = __FILE__;                              \
128 } while (0)
129
130 #else
131 #define QMD_TRACE_ELEM(elem)
132 #define QMD_TRACE_HEAD(head)
133 #define QMD_SAVELINK(name, link)
134 #define TRACEBUF
135 #define TRASHIT(x)
136 #endif  /* QUEUE_MACRO_DEBUG */
137
138 /*
139  * Singly-linked List declarations.
140  */
141 #define SLIST_HEAD(name, type)                                          \
142 struct name {                                                           \
143         struct type *slh_first; /* first element */                     \
144 }
145
146 #define SLIST_HEAD_INITIALIZER(head)                                    \
147         { NULL }
148
149 #define SLIST_ENTRY(type)                                               \
150 struct {                                                                \
151         struct type *sle_next;  /* next element */                      \
152 }
153
154 /*
155  * Singly-linked List functions.
156  */
157 #define SLIST_EMPTY(head)       ((head)->slh_first == NULL)
158
159 #define SLIST_FIRST(head)       ((head)->slh_first)
160
161 #define SLIST_FOREACH(var, head, field)                                 \
162         for ((var) = SLIST_FIRST((head));                               \
163             (var);                                                      \
164             (var) = SLIST_NEXT((var), field))
165
166 #define SLIST_FOREACH_SAFE(var, head, field, tvar)                      \
167         for ((var) = SLIST_FIRST((head));                               \
168             (var) && ((tvar) = SLIST_NEXT((var), field), 1);            \
169             (var) = (tvar))
170
171 #define SLIST_FOREACH_PREVPTR(var, varp, head, field)                   \
172         for ((varp) = &SLIST_FIRST((head));                             \
173             ((var) = *(varp)) != NULL;                                  \
174             (varp) = &SLIST_NEXT((var), field))
175
176 #define SLIST_INIT(head) do {                                           \
177         SLIST_FIRST((head)) = NULL;                                     \
178 } while (0)
179
180 #define SLIST_INSERT_AFTER(slistelm, elm, field) do {                   \
181         SLIST_NEXT((elm), field) = SLIST_NEXT((slistelm), field);       \
182         SLIST_NEXT((slistelm), field) = (elm);                          \
183 } while (0)
184
185 #define SLIST_INSERT_HEAD(head, elm, field) do {                        \
186         SLIST_NEXT((elm), field) = SLIST_FIRST((head));                 \
187         SLIST_FIRST((head)) = (elm);                                    \
188 } while (0)
189
190 #define SLIST_NEXT(elm, field)  ((elm)->field.sle_next)
191
192 #define SLIST_REMOVE(head, elm, type, field) do {                       \
193         QMD_SAVELINK(oldnext, (elm)->field.sle_next);                   \
194         if (SLIST_FIRST((head)) == (elm)) {                             \
195                 SLIST_REMOVE_HEAD((head), field);                       \
196         }                                                               \
197         else {                                                          \
198                 struct type *curelm = SLIST_FIRST((head));              \
199                 while (SLIST_NEXT(curelm, field) != (elm))              \
200                         curelm = SLIST_NEXT(curelm, field);             \
201                 SLIST_REMOVE_AFTER(curelm, field);                      \
202         }                                                               \
203         TRASHIT(*oldnext);                                              \
204 } while (0)
205
206 #define SLIST_REMOVE_AFTER(elm, field) do {                             \
207         SLIST_NEXT(elm, field) =                                        \
208             SLIST_NEXT(SLIST_NEXT(elm, field), field);                  \
209 } while (0)
210
211 #define SLIST_REMOVE_HEAD(head, field) do {                             \
212         SLIST_FIRST((head)) = SLIST_NEXT(SLIST_FIRST((head)), field);   \
213 } while (0)
214
215 #define SLIST_SWAP(head1, head2, type) do {                             \
216         struct type *swap_first = SLIST_FIRST(head1);                   \
217         SLIST_FIRST(head1) = SLIST_FIRST(head2);                        \
218         SLIST_FIRST(head2) = swap_first;                                \
219 } while (0)
220
221 /*
222  * Singly-linked Tail queue declarations.
223  */
224 #define STAILQ_HEAD(name, type)                                         \
225 struct name {                                                           \
226         struct type *stqh_first;/* first element */                     \
227         struct type **stqh_last;/* addr of last next element */         \
228 }
229
230 #define STAILQ_HEAD_INITIALIZER(head)                                   \
231         { NULL, &(head).stqh_first }
232
233 #define STAILQ_ENTRY(type)                                              \
234 struct {                                                                \
235         struct type *stqe_next; /* next element */                      \
236 }
237
238 /*
239  * Singly-linked Tail queue functions.
240  */
241 #define STAILQ_CONCAT(head1, head2) do {                                \
242         if (!STAILQ_EMPTY((head2))) {                                   \
243                 *(head1)->stqh_last = (head2)->stqh_first;              \
244                 (head1)->stqh_last = (head2)->stqh_last;                \
245                 STAILQ_INIT((head2));                                   \
246         }                                                               \
247 } while (0)
248
249 #define STAILQ_EMPTY(head)      ((head)->stqh_first == NULL)
250
251 #define STAILQ_FIRST(head)      ((head)->stqh_first)
252
253 #define STAILQ_FOREACH(var, head, field)                                \
254         for((var) = STAILQ_FIRST((head));                               \
255            (var);                                                       \
256            (var) = STAILQ_NEXT((var), field))
257
258
259 #define STAILQ_FOREACH_SAFE(var, head, field, tvar)                     \
260         for ((var) = STAILQ_FIRST((head));                              \
261             (var) && ((tvar) = STAILQ_NEXT((var), field), 1);           \
262             (var) = (tvar))
263
264 #define STAILQ_INIT(head) do {                                          \
265         STAILQ_FIRST((head)) = NULL;                                    \
266         (head)->stqh_last = &STAILQ_FIRST((head));                      \
267 } while (0)
268
269 #define STAILQ_INSERT_AFTER(head, tqelm, elm, field) do {               \
270         if ((STAILQ_NEXT((elm), field) = STAILQ_NEXT((tqelm), field)) == NULL)\
271                 (head)->stqh_last = &STAILQ_NEXT((elm), field);         \
272         STAILQ_NEXT((tqelm), field) = (elm);                            \
273 } while (0)
274
275 #define STAILQ_INSERT_HEAD(head, elm, field) do {                       \
276         if ((STAILQ_NEXT((elm), field) = STAILQ_FIRST((head))) == NULL) \
277                 (head)->stqh_last = &STAILQ_NEXT((elm), field);         \
278         STAILQ_FIRST((head)) = (elm);                                   \
279 } while (0)
280
281 #define STAILQ_INSERT_TAIL(head, elm, field) do {                       \
282         STAILQ_NEXT((elm), field) = NULL;                               \
283         *(head)->stqh_last = (elm);                                     \
284         (head)->stqh_last = &STAILQ_NEXT((elm), field);                 \
285 } while (0)
286
287 #define STAILQ_LAST(head, type, field)                                  \
288         (STAILQ_EMPTY((head)) ?                                         \
289                 NULL :                                                  \
290                 ((struct type *)(void *)                                \
291                 ((char *)((head)->stqh_last) - __offsetof(struct type, field))))
292
293 #define STAILQ_NEXT(elm, field) ((elm)->field.stqe_next)
294
295 #define STAILQ_REMOVE(head, elm, type, field) do {                      \
296         QMD_SAVELINK(oldnext, (elm)->field.stqe_next);                  \
297         if (STAILQ_FIRST((head)) == (elm)) {                            \
298                 STAILQ_REMOVE_HEAD((head), field);                      \
299         }                                                               \
300         else {                                                          \
301                 struct type *curelm = STAILQ_FIRST((head));             \
302                 while (STAILQ_NEXT(curelm, field) != (elm))             \
303                         curelm = STAILQ_NEXT(curelm, field);            \
304                 STAILQ_REMOVE_AFTER(head, curelm, field);               \
305         }                                                               \
306         TRASHIT(*oldnext);                                              \
307 } while (0)
308
309 #define STAILQ_REMOVE_AFTER(head, elm, field) do {                      \
310         if ((STAILQ_NEXT(elm, field) =                                  \
311              STAILQ_NEXT(STAILQ_NEXT(elm, field), field)) == NULL)      \
312                 (head)->stqh_last = &STAILQ_NEXT((elm), field);         \
313 } while (0)
314
315 #define STAILQ_REMOVE_HEAD(head, field) do {                            \
316         if ((STAILQ_FIRST((head)) =                                     \
317              STAILQ_NEXT(STAILQ_FIRST((head)), field)) == NULL)         \
318                 (head)->stqh_last = &STAILQ_FIRST((head));              \
319 } while (0)
320
321 #define STAILQ_SWAP(head1, head2, type) do {                            \
322         struct type *swap_first = STAILQ_FIRST(head1);                  \
323         struct type **swap_last = (head1)->stqh_last;                   \
324         STAILQ_FIRST(head1) = STAILQ_FIRST(head2);                      \
325         (head1)->stqh_last = (head2)->stqh_last;                        \
326         STAILQ_FIRST(head2) = swap_first;                               \
327         (head2)->stqh_last = swap_last;                                 \
328         if (STAILQ_EMPTY(head1))                                        \
329                 (head1)->stqh_last = &STAILQ_FIRST(head1);              \
330         if (STAILQ_EMPTY(head2))                                        \
331                 (head2)->stqh_last = &STAILQ_FIRST(head2);              \
332 } while (0)
333
334
335 /*
336  * List declarations.
337  */
338 #define LIST_HEAD(name, type)                                           \
339 struct name {                                                           \
340         struct type *lh_first;  /* first element */                     \
341 }
342
343 #define LIST_HEAD_INITIALIZER(head)                                     \
344         { NULL }
345
346 #define LIST_ENTRY(type)                                                \
347 struct {                                                                \
348         struct type *le_next;   /* next element */                      \
349         struct type **le_prev;  /* address of previous next element */  \
350 }
351
352 /*
353  * List functions.
354  */
355
356 #if (defined(_KERNEL) && defined(INVARIANTS))
357 #define QMD_LIST_CHECK_HEAD(head, field) do {                           \
358         if (LIST_FIRST((head)) != NULL &&                               \
359             LIST_FIRST((head))->field.le_prev !=                        \
360              &LIST_FIRST((head)))                                       \
361                 panic("Bad list head %p first->prev != head", (head));  \
362 } while (0)
363
364 #define QMD_LIST_CHECK_NEXT(elm, field) do {                            \
365         if (LIST_NEXT((elm), field) != NULL &&                          \
366             LIST_NEXT((elm), field)->field.le_prev !=                   \
367              &((elm)->field.le_next))                                   \
368                 panic("Bad link elm %p next->prev != elm", (elm));      \
369 } while (0)
370
371 #define QMD_LIST_CHECK_PREV(elm, field) do {                            \
372         if (*(elm)->field.le_prev != (elm))                             \
373                 panic("Bad link elm %p prev->next != elm", (elm));      \
374 } while (0)
375 #else
376 #define QMD_LIST_CHECK_HEAD(head, field)
377 #define QMD_LIST_CHECK_NEXT(elm, field)
378 #define QMD_LIST_CHECK_PREV(elm, field)
379 #endif /* (_KERNEL && INVARIANTS) */
380
381 #define LIST_EMPTY(head)        ((head)->lh_first == NULL)
382
383 #define LIST_FIRST(head)        ((head)->lh_first)
384
385 #define LIST_FOREACH(var, head, field)                                  \
386         for ((var) = LIST_FIRST((head));                                \
387             (var);                                                      \
388             (var) = LIST_NEXT((var), field))
389
390 #define LIST_FOREACH_SAFE(var, head, field, tvar)                       \
391         for ((var) = LIST_FIRST((head));                                \
392             (var) && ((tvar) = LIST_NEXT((var), field), 1);             \
393             (var) = (tvar))
394
395 #define LIST_INIT(head) do {                                            \
396         LIST_FIRST((head)) = NULL;                                      \
397 } while (0)
398
399 #define LIST_INSERT_AFTER(listelm, elm, field) do {                     \
400         QMD_LIST_CHECK_NEXT(listelm, field);                            \
401         if ((LIST_NEXT((elm), field) = LIST_NEXT((listelm), field)) != NULL)\
402                 LIST_NEXT((listelm), field)->field.le_prev =            \
403                     &LIST_NEXT((elm), field);                           \
404         LIST_NEXT((listelm), field) = (elm);                            \
405         (elm)->field.le_prev = &LIST_NEXT((listelm), field);            \
406 } while (0)
407
408 #define LIST_INSERT_BEFORE(listelm, elm, field) do {                    \
409         QMD_LIST_CHECK_PREV(listelm, field);                            \
410         (elm)->field.le_prev = (listelm)->field.le_prev;                \
411         LIST_NEXT((elm), field) = (listelm);                            \
412         *(listelm)->field.le_prev = (elm);                              \
413         (listelm)->field.le_prev = &LIST_NEXT((elm), field);            \
414 } while (0)
415
416 #define LIST_INSERT_HEAD(head, elm, field) do {                         \
417         QMD_LIST_CHECK_HEAD((head), field);                             \
418         if ((LIST_NEXT((elm), field) = LIST_FIRST((head))) != NULL)     \
419                 LIST_FIRST((head))->field.le_prev = &LIST_NEXT((elm), field);\
420         LIST_FIRST((head)) = (elm);                                     \
421         (elm)->field.le_prev = &LIST_FIRST((head));                     \
422 } while (0)
423
424 #define LIST_NEXT(elm, field)   ((elm)->field.le_next)
425
426 #define LIST_REMOVE(elm, field) do {                                    \
427         QMD_SAVELINK(oldnext, (elm)->field.le_next);                    \
428         QMD_SAVELINK(oldprev, (elm)->field.le_prev);                    \
429         QMD_LIST_CHECK_NEXT(elm, field);                                \
430         QMD_LIST_CHECK_PREV(elm, field);                                \
431         if (LIST_NEXT((elm), field) != NULL)                            \
432                 LIST_NEXT((elm), field)->field.le_prev =                \
433                     (elm)->field.le_prev;                               \
434         *(elm)->field.le_prev = LIST_NEXT((elm), field);                \
435         TRASHIT(*oldnext);                                              \
436         TRASHIT(*oldprev);                                              \
437 } while (0)
438
439 #define LIST_SWAP(head1, head2, type, field) do {                       \
440         struct type *swap_tmp = LIST_FIRST((head1));                    \
441         LIST_FIRST((head1)) = LIST_FIRST((head2));                      \
442         LIST_FIRST((head2)) = swap_tmp;                                 \
443         if ((swap_tmp = LIST_FIRST((head1))) != NULL)                   \
444                 swap_tmp->field.le_prev = &LIST_FIRST((head1));         \
445         if ((swap_tmp = LIST_FIRST((head2))) != NULL)                   \
446                 swap_tmp->field.le_prev = &LIST_FIRST((head2));         \
447 } while (0)
448
449 /*
450  * Tail queue declarations.
451  */
452 #define TAILQ_HEAD(name, type)                                          \
453 struct name {                                                           \
454         struct type *tqh_first; /* first element */                     \
455         struct type **tqh_last; /* addr of last next element */         \
456         TRACEBUF                                                        \
457 }
458
459 #define TAILQ_HEAD_INITIALIZER(head)                                    \
460         { NULL, &(head).tqh_first }
461
462 #define TAILQ_ENTRY(type)                                               \
463 struct {                                                                \
464         struct type *tqe_next;  /* next element */                      \
465         struct type **tqe_prev; /* address of previous next element */  \
466         TRACEBUF                                                        \
467 }
468
469 /*
470  * Tail queue functions.
471  */
472 #if (defined(_KERNEL) && defined(INVARIANTS))
473 #define QMD_TAILQ_CHECK_HEAD(head, field) do {                          \
474         if (!TAILQ_EMPTY(head) &&                                       \
475             TAILQ_FIRST((head))->field.tqe_prev !=                      \
476              &TAILQ_FIRST((head)))                                      \
477                 panic("Bad tailq head %p first->prev != head", (head)); \
478 } while (0)
479
480 #define QMD_TAILQ_CHECK_TAIL(head, field) do {                          \
481         if (*(head)->tqh_last != NULL)                                  \
482                 panic("Bad tailq NEXT(%p->tqh_last) != NULL", (head));  \
483 } while (0)
484
485 #define QMD_TAILQ_CHECK_NEXT(elm, field) do {                           \
486         if (TAILQ_NEXT((elm), field) != NULL &&                         \
487             TAILQ_NEXT((elm), field)->field.tqe_prev !=                 \
488              &((elm)->field.tqe_next))                                  \
489                 panic("Bad link elm %p next->prev != elm", (elm));      \
490 } while (0)
491
492 #define QMD_TAILQ_CHECK_PREV(elm, field) do {                           \
493         if (*(elm)->field.tqe_prev != (elm))                            \
494                 panic("Bad link elm %p prev->next != elm", (elm));      \
495 } while (0)
496 #else
497 #define QMD_TAILQ_CHECK_HEAD(head, field)
498 #define QMD_TAILQ_CHECK_TAIL(head, headname)
499 #define QMD_TAILQ_CHECK_NEXT(elm, field)
500 #define QMD_TAILQ_CHECK_PREV(elm, field)
501 #endif /* (_KERNEL && INVARIANTS) */
502
503 #define TAILQ_CONCAT(head1, head2, field) do {                          \
504         if (!TAILQ_EMPTY(head2)) {                                      \
505                 *(head1)->tqh_last = (head2)->tqh_first;                \
506                 (head2)->tqh_first->field.tqe_prev = (head1)->tqh_last; \
507                 (head1)->tqh_last = (head2)->tqh_last;                  \
508                 TAILQ_INIT((head2));                                    \
509                 QMD_TRACE_HEAD(head1);                                  \
510                 QMD_TRACE_HEAD(head2);                                  \
511         }                                                               \
512 } while (0)
513
514 #define TAILQ_EMPTY(head)       ((head)->tqh_first == NULL)
515
516 #define TAILQ_FIRST(head)       ((head)->tqh_first)
517
518 #define TAILQ_FOREACH(var, head, field)                                 \
519         for ((var) = TAILQ_FIRST((head));                               \
520             (var);                                                      \
521             (var) = TAILQ_NEXT((var), field))
522
523 #define TAILQ_FOREACH_SAFE(var, head, field, tvar)                      \
524         for ((var) = TAILQ_FIRST((head));                               \
525             (var) && ((tvar) = TAILQ_NEXT((var), field), 1);            \
526             (var) = (tvar))
527
528 #define TAILQ_FOREACH_REVERSE(var, head, headname, field)               \
529         for ((var) = TAILQ_LAST((head), headname);                      \
530             (var);                                                      \
531             (var) = TAILQ_PREV((var), headname, field))
532
533 #define TAILQ_FOREACH_REVERSE_SAFE(var, head, headname, field, tvar)    \
534         for ((var) = TAILQ_LAST((head), headname);                      \
535             (var) && ((tvar) = TAILQ_PREV((var), headname, field), 1);  \
536             (var) = (tvar))
537
538 #define TAILQ_INIT(head) do {                                           \
539         TAILQ_FIRST((head)) = NULL;                                     \
540         (head)->tqh_last = &TAILQ_FIRST((head));                        \
541         QMD_TRACE_HEAD(head);                                           \
542 } while (0)
543
544 #define TAILQ_INSERT_AFTER(head, listelm, elm, field) do {              \
545         QMD_TAILQ_CHECK_NEXT(listelm, field);                           \
546         if ((TAILQ_NEXT((elm), field) = TAILQ_NEXT((listelm), field)) != NULL)\
547                 TAILQ_NEXT((elm), field)->field.tqe_prev =              \
548                     &TAILQ_NEXT((elm), field);                          \
549         else {                                                          \
550                 (head)->tqh_last = &TAILQ_NEXT((elm), field);           \
551                 QMD_TRACE_HEAD(head);                                   \
552         }                                                               \
553         TAILQ_NEXT((listelm), field) = (elm);                           \
554         (elm)->field.tqe_prev = &TAILQ_NEXT((listelm), field);          \
555         QMD_TRACE_ELEM(&(elm)->field);                                  \
556         QMD_TRACE_ELEM(&listelm->field);                                \
557 } while (0)
558
559 #define TAILQ_INSERT_BEFORE(listelm, elm, field) do {                   \
560         QMD_TAILQ_CHECK_PREV(listelm, field);                           \
561         (elm)->field.tqe_prev = (listelm)->field.tqe_prev;              \
562         TAILQ_NEXT((elm), field) = (listelm);                           \
563         *(listelm)->field.tqe_prev = (elm);                             \
564         (listelm)->field.tqe_prev = &TAILQ_NEXT((elm), field);          \
565         QMD_TRACE_ELEM(&(elm)->field);                                  \
566         QMD_TRACE_ELEM(&listelm->field);                                \
567 } while (0)
568
569 #define TAILQ_INSERT_HEAD(head, elm, field) do {                        \
570         QMD_TAILQ_CHECK_HEAD(head, field);                              \
571         if ((TAILQ_NEXT((elm), field) = TAILQ_FIRST((head))) != NULL)   \
572                 TAILQ_FIRST((head))->field.tqe_prev =                   \
573                     &TAILQ_NEXT((elm), field);                          \
574         else                                                            \
575                 (head)->tqh_last = &TAILQ_NEXT((elm), field);           \
576         TAILQ_FIRST((head)) = (elm);                                    \
577         (elm)->field.tqe_prev = &TAILQ_FIRST((head));                   \
578         QMD_TRACE_HEAD(head);                                           \
579         QMD_TRACE_ELEM(&(elm)->field);                                  \
580 } while (0)
581
582 #define TAILQ_INSERT_TAIL(head, elm, field) do {                        \
583         QMD_TAILQ_CHECK_TAIL(head, field);                              \
584         TAILQ_NEXT((elm), field) = NULL;                                \
585         (elm)->field.tqe_prev = (head)->tqh_last;                       \
586         *(head)->tqh_last = (elm);                                      \
587         (head)->tqh_last = &TAILQ_NEXT((elm), field);                   \
588         QMD_TRACE_HEAD(head);                                           \
589         QMD_TRACE_ELEM(&(elm)->field);                                  \
590 } while (0)
591
592 #define TAILQ_LAST(head, headname)                                      \
593         (*(((struct headname *)((head)->tqh_last))->tqh_last))
594
595 #define TAILQ_NEXT(elm, field) ((elm)->field.tqe_next)
596
597 #define TAILQ_PREV(elm, headname, field)                                \
598         (*(((struct headname *)((elm)->field.tqe_prev))->tqh_last))
599
600 #define TAILQ_REMOVE(head, elm, field) do {                             \
601         QMD_SAVELINK(oldnext, (elm)->field.tqe_next);                   \
602         QMD_SAVELINK(oldprev, (elm)->field.tqe_prev);                   \
603         QMD_TAILQ_CHECK_NEXT(elm, field);                               \
604         QMD_TAILQ_CHECK_PREV(elm, field);                               \
605         if ((TAILQ_NEXT((elm), field)) != NULL)                         \
606                 TAILQ_NEXT((elm), field)->field.tqe_prev =              \
607                     (elm)->field.tqe_prev;                              \
608         else {                                                          \
609                 (head)->tqh_last = (elm)->field.tqe_prev;               \
610                 QMD_TRACE_HEAD(head);                                   \
611         }                                                               \
612         *(elm)->field.tqe_prev = TAILQ_NEXT((elm), field);              \
613         TRASHIT(*oldnext);                                              \
614         TRASHIT(*oldprev);                                              \
615         QMD_TRACE_ELEM(&(elm)->field);                                  \
616 } while (0)
617
618 #define TAILQ_SWAP(head1, head2, type, field) do {                      \
619         struct type *swap_first = (head1)->tqh_first;                   \
620         struct type **swap_last = (head1)->tqh_last;                    \
621         (head1)->tqh_first = (head2)->tqh_first;                        \
622         (head1)->tqh_last = (head2)->tqh_last;                          \
623         (head2)->tqh_first = swap_first;                                \
624         (head2)->tqh_last = swap_last;                                  \
625         if ((swap_first = (head1)->tqh_first) != NULL)                  \
626                 swap_first->field.tqe_prev = &(head1)->tqh_first;       \
627         else                                                            \
628                 (head1)->tqh_last = &(head1)->tqh_first;                \
629         if ((swap_first = (head2)->tqh_first) != NULL)                  \
630                 swap_first->field.tqe_prev = &(head2)->tqh_first;       \
631         else                                                            \
632                 (head2)->tqh_last = &(head2)->tqh_first;                \
633 } while (0)
634
635 #endif /* !_SYS_QUEUE_H_ */