Upgrade to gcc-4.9.2
[akaros.git] / tools / compilers / gcc-glibc / glibc-2.14.1-ros / sysdeps / akaros / sys / 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  *
101  */
102 #ifdef QUEUE_MACRO_DEBUG
103 /* Store the last 2 places the queue element or head was altered */
104 struct qm_trace {
105         char * lastfile;
106         int lastline;
107         char * prevfile;
108         int prevline;
109 };
110
111 #define TRACEBUF        struct qm_trace trace;
112 #define TRASHIT(x)      do {(x) = (void *)-1;} while (0)
113 #define QMD_SAVELINK(name, link)        void **name = (void *)&(link)
114
115 #define QMD_TRACE_HEAD(head) do {                                       \
116         (head)->trace.prevline = (head)->trace.lastline;                \
117         (head)->trace.prevfile = (head)->trace.lastfile;                \
118         (head)->trace.lastline = __LINE__;                              \
119         (head)->trace.lastfile = __FILE__;                              \
120 } while (0)
121
122 #define QMD_TRACE_ELEM(elem) do {                                       \
123         (elem)->trace.prevline = (elem)->trace.lastline;                \
124         (elem)->trace.prevfile = (elem)->trace.lastfile;                \
125         (elem)->trace.lastline = __LINE__;                              \
126         (elem)->trace.lastfile = __FILE__;                              \
127 } while (0)
128
129 #else
130 #define QMD_TRACE_ELEM(elem)
131 #define QMD_TRACE_HEAD(head)
132 #define QMD_SAVELINK(name, link)
133 #define TRACEBUF
134 #define TRASHIT(x)
135 #endif  /* QUEUE_MACRO_DEBUG */
136
137 /*
138  * Singly-linked List declarations.
139  */
140 #define SLIST_HEAD(name, type)                                          \
141 struct name {                                                           \
142         struct type *slh_first; /* first element */                     \
143 }
144
145 #define SLIST_HEAD_INITIALIZER(head)                                    \
146         { NULL }
147
148 #define SLIST_ENTRY(type)                                               \
149 struct {                                                                \
150         struct type *sle_next;  /* next element */                      \
151 }
152
153 /*
154  * Singly-linked List functions.
155  */
156 #define SLIST_EMPTY(head)       ((head)->slh_first == NULL)
157
158 #define SLIST_FIRST(head)       ((head)->slh_first)
159
160 #define SLIST_FOREACH(var, head, field)                                 \
161         for ((var) = SLIST_FIRST((head));                               \
162             (var);                                                      \
163             (var) = SLIST_NEXT((var), field))
164
165 #define SLIST_FOREACH_SAFE(var, head, field, tvar)                      \
166         for ((var) = SLIST_FIRST((head));                               \
167             (var) && ((tvar) = SLIST_NEXT((var), field), 1);            \
168             (var) = (tvar))
169
170 #define SLIST_FOREACH_PREVPTR(var, varp, head, field)                   \
171         for ((varp) = &SLIST_FIRST((head));                             \
172             ((var) = *(varp)) != NULL;                                  \
173             (varp) = &SLIST_NEXT((var), field))
174
175 #define SLIST_INIT(head) do {                                           \
176         SLIST_FIRST((head)) = NULL;                                     \
177 } while (0)
178
179 #define SLIST_INSERT_AFTER(slistelm, elm, field) do {                   \
180         SLIST_NEXT((elm), field) = SLIST_NEXT((slistelm), field);       \
181         SLIST_NEXT((slistelm), field) = (elm);                          \
182 } while (0)
183
184 #define SLIST_INSERT_HEAD(head, elm, field) do {                        \
185         SLIST_NEXT((elm), field) = SLIST_FIRST((head));                 \
186         SLIST_FIRST((head)) = (elm);                                    \
187 } while (0)
188
189 #define SLIST_NEXT(elm, field)  ((elm)->field.sle_next)
190
191 #define SLIST_REMOVE(head, elm, type, field) do {                       \
192         QMD_SAVELINK(oldnext, (elm)->field.sle_next);                   \
193         if (SLIST_FIRST((head)) == (elm)) {                             \
194                 SLIST_REMOVE_HEAD((head), field);                       \
195         }                                                               \
196         else {                                                          \
197                 struct type *curelm = SLIST_FIRST((head));              \
198                 while (SLIST_NEXT(curelm, field) != (elm))              \
199                         curelm = SLIST_NEXT(curelm, field);             \
200                 SLIST_REMOVE_AFTER(curelm, field);                      \
201         }                                                               \
202         TRASHIT(*oldnext);                                              \
203 } while (0)
204
205 #define SLIST_REMOVE_AFTER(elm, field) do {                             \
206         SLIST_NEXT(elm, field) =                                        \
207             SLIST_NEXT(SLIST_NEXT(elm, field), field);                  \
208 } while (0)
209
210 #define SLIST_REMOVE_HEAD(head, field) do {                             \
211         SLIST_FIRST((head)) = SLIST_NEXT(SLIST_FIRST((head)), field);   \
212 } while (0)
213
214 /*
215  * Singly-linked Tail queue declarations.
216  */
217 #define STAILQ_HEAD(name, type)                                         \
218 struct name {                                                           \
219         struct type *stqh_first;/* first element */                     \
220         struct type **stqh_last;/* addr of last next element */         \
221 }
222
223 #define STAILQ_HEAD_INITIALIZER(head)                                   \
224         { NULL, &(head).stqh_first }
225
226 #define STAILQ_ENTRY(type)                                              \
227 struct {                                                                \
228         struct type *stqe_next; /* next element */                      \
229 }
230
231 /*
232  * Singly-linked Tail queue functions.
233  */
234 #define STAILQ_CONCAT(head1, head2) do {                                \
235         if (!STAILQ_EMPTY((head2))) {                                   \
236                 *(head1)->stqh_last = (head2)->stqh_first;              \
237                 (head1)->stqh_last = (head2)->stqh_last;                \
238                 STAILQ_INIT((head2));                                   \
239         }                                                               \
240 } while (0)
241
242 #define STAILQ_EMPTY(head)      ((head)->stqh_first == NULL)
243
244 #define STAILQ_FIRST(head)      ((head)->stqh_first)
245
246 #define STAILQ_FOREACH(var, head, field)                                \
247         for((var) = STAILQ_FIRST((head));                               \
248            (var);                                                       \
249            (var) = STAILQ_NEXT((var), field))
250
251
252 #define STAILQ_FOREACH_SAFE(var, head, field, tvar)                     \
253         for ((var) = STAILQ_FIRST((head));                              \
254             (var) && ((tvar) = STAILQ_NEXT((var), field), 1);           \
255             (var) = (tvar))
256
257 #define STAILQ_INIT(head) do {                                          \
258         STAILQ_FIRST((head)) = NULL;                                    \
259         (head)->stqh_last = &STAILQ_FIRST((head));                      \
260 } while (0)
261
262 #define STAILQ_INSERT_AFTER(head, tqelm, elm, field) do {               \
263         if ((STAILQ_NEXT((elm), field) = STAILQ_NEXT((tqelm), field)) == NULL)\
264                 (head)->stqh_last = &STAILQ_NEXT((elm), field);         \
265         STAILQ_NEXT((tqelm), field) = (elm);                            \
266 } while (0)
267
268 #define STAILQ_INSERT_HEAD(head, elm, field) do {                       \
269         if ((STAILQ_NEXT((elm), field) = STAILQ_FIRST((head))) == NULL) \
270                 (head)->stqh_last = &STAILQ_NEXT((elm), field);         \
271         STAILQ_FIRST((head)) = (elm);                                   \
272 } while (0)
273
274 #define STAILQ_INSERT_TAIL(head, elm, field) do {                       \
275         STAILQ_NEXT((elm), field) = NULL;                               \
276         *(head)->stqh_last = (elm);                                     \
277         (head)->stqh_last = &STAILQ_NEXT((elm), field);                 \
278 } while (0)
279
280 #define STAILQ_LAST(head, type, field)                                  \
281         (STAILQ_EMPTY((head)) ?                                         \
282                 NULL :                                                  \
283                 ((struct type *)(void *)                                \
284                 ((char *)((head)->stqh_last) - __offsetof(struct type, field))))
285
286 #define STAILQ_NEXT(elm, field) ((elm)->field.stqe_next)
287
288 #define STAILQ_REMOVE(head, elm, type, field) do {                      \
289         QMD_SAVELINK(oldnext, (elm)->field.stqe_next);                  \
290         if (STAILQ_FIRST((head)) == (elm)) {                            \
291                 STAILQ_REMOVE_HEAD((head), field);                      \
292         }                                                               \
293         else {                                                          \
294                 struct type *curelm = STAILQ_FIRST((head));             \
295                 while (STAILQ_NEXT(curelm, field) != (elm))             \
296                         curelm = STAILQ_NEXT(curelm, field);            \
297                 STAILQ_REMOVE_AFTER(head, curelm, field);               \
298         }                                                               \
299         TRASHIT(*oldnext);                                              \
300 } while (0)
301
302 #define STAILQ_REMOVE_HEAD(head, field) do {                            \
303         if ((STAILQ_FIRST((head)) =                                     \
304              STAILQ_NEXT(STAILQ_FIRST((head)), field)) == NULL)         \
305                 (head)->stqh_last = &STAILQ_FIRST((head));              \
306 } while (0)
307
308 #define STAILQ_REMOVE_AFTER(head, elm, field) do {                      \
309         if ((STAILQ_NEXT(elm, field) =                                  \
310              STAILQ_NEXT(STAILQ_NEXT(elm, field), field)) == NULL)      \
311                 (head)->stqh_last = &STAILQ_NEXT((elm), field);         \
312 } while (0)
313
314 #define STAILQ_SWAP(head1, head2, type) do {                            \
315         struct type *swap_first = STAILQ_FIRST(head1);                  \
316         struct type **swap_last = (head1)->stqh_last;                   \
317         STAILQ_FIRST(head1) = STAILQ_FIRST(head2);                      \
318         (head1)->stqh_last = (head2)->stqh_last;                        \
319         STAILQ_FIRST(head2) = swap_first;                               \
320         (head2)->stqh_last = swap_last;                                 \
321         if (STAILQ_EMPTY(head1))                                        \
322                 (head1)->stqh_last = &STAILQ_FIRST(head1);              \
323         if (STAILQ_EMPTY(head2))                                        \
324                 (head2)->stqh_last = &STAILQ_FIRST(head2);              \
325 } while (0)
326
327
328 /*
329  * List declarations.
330  */
331 #define LIST_HEAD(name, type)                                           \
332 struct name {                                                           \
333         struct type *lh_first;  /* first element */                     \
334 }
335
336 #define LIST_HEAD_INITIALIZER(head)                                     \
337         { NULL }
338
339 #define LIST_ENTRY(type)                                                \
340 struct {                                                                \
341         struct type *le_next;   /* next element */                      \
342         struct type **le_prev;  /* address of previous next element */  \
343 }
344
345 /*
346  * List functions.
347  */
348
349 #if (defined(_KERNEL) && defined(INVARIANTS))
350 #define QMD_LIST_CHECK_HEAD(head, field) do {                           \
351         if (LIST_FIRST((head)) != NULL &&                               \
352             LIST_FIRST((head))->field.le_prev !=                        \
353              &LIST_FIRST((head)))                                       \
354                 panic("Bad list head %p first->prev != head", (head));  \
355 } while (0)
356
357 #define QMD_LIST_CHECK_NEXT(elm, field) do {                            \
358         if (LIST_NEXT((elm), field) != NULL &&                          \
359             LIST_NEXT((elm), field)->field.le_prev !=                   \
360              &((elm)->field.le_next))                                   \
361                 panic("Bad link elm %p next->prev != elm", (elm));      \
362 } while (0)
363
364 #define QMD_LIST_CHECK_PREV(elm, field) do {                            \
365         if (*(elm)->field.le_prev != (elm))                             \
366                 panic("Bad link elm %p prev->next != elm", (elm));      \
367 } while (0)
368 #else
369 #define QMD_LIST_CHECK_HEAD(head, field)
370 #define QMD_LIST_CHECK_NEXT(elm, field)
371 #define QMD_LIST_CHECK_PREV(elm, field)
372 #endif /* (_KERNEL && INVARIANTS) */
373
374 #define LIST_EMPTY(head)        ((head)->lh_first == NULL)
375
376 #define LIST_FIRST(head)        ((head)->lh_first)
377
378 #define LIST_FOREACH(var, head, field)                                  \
379         for ((var) = LIST_FIRST((head));                                \
380             (var);                                                      \
381             (var) = LIST_NEXT((var), field))
382
383 #define LIST_FOREACH_SAFE(var, head, field, tvar)                       \
384         for ((var) = LIST_FIRST((head));                                \
385             (var) && ((tvar) = LIST_NEXT((var), field), 1);             \
386             (var) = (tvar))
387
388 #define LIST_INIT(head) do {                                            \
389         LIST_FIRST((head)) = NULL;                                      \
390 } while (0)
391
392 #define LIST_INSERT_AFTER(listelm, elm, field) do {                     \
393         QMD_LIST_CHECK_NEXT(listelm, field);                            \
394         if ((LIST_NEXT((elm), field) = LIST_NEXT((listelm), field)) != NULL)\
395                 LIST_NEXT((listelm), field)->field.le_prev =            \
396                     &LIST_NEXT((elm), field);                           \
397         LIST_NEXT((listelm), field) = (elm);                            \
398         (elm)->field.le_prev = &LIST_NEXT((listelm), field);            \
399 } while (0)
400
401 #define LIST_INSERT_BEFORE(listelm, elm, field) do {                    \
402         QMD_LIST_CHECK_PREV(listelm, field);                            \
403         (elm)->field.le_prev = (listelm)->field.le_prev;                \
404         LIST_NEXT((elm), field) = (listelm);                            \
405         *(listelm)->field.le_prev = (elm);                              \
406         (listelm)->field.le_prev = &LIST_NEXT((elm), field);            \
407 } while (0)
408
409 #define LIST_INSERT_HEAD(head, elm, field) do {                         \
410         QMD_LIST_CHECK_HEAD((head), field);                             \
411         if ((LIST_NEXT((elm), field) = LIST_FIRST((head))) != NULL)     \
412                 LIST_FIRST((head))->field.le_prev = &LIST_NEXT((elm), field);\
413         LIST_FIRST((head)) = (elm);                                     \
414         (elm)->field.le_prev = &LIST_FIRST((head));                     \
415 } while (0)
416
417 #define LIST_NEXT(elm, field)   ((elm)->field.le_next)
418
419 #define LIST_REMOVE(elm, field) do {                                    \
420         QMD_SAVELINK(oldnext, (elm)->field.le_next);                    \
421         QMD_SAVELINK(oldprev, (elm)->field.le_prev);                    \
422         QMD_LIST_CHECK_NEXT(elm, field);                                \
423         QMD_LIST_CHECK_PREV(elm, field);                                \
424         if (LIST_NEXT((elm), field) != NULL)                            \
425                 LIST_NEXT((elm), field)->field.le_prev =                \
426                     (elm)->field.le_prev;                               \
427         *(elm)->field.le_prev = LIST_NEXT((elm), field);                \
428         TRASHIT(*oldnext);                                              \
429         TRASHIT(*oldprev);                                              \
430 } while (0)
431
432 #define LIST_SWAP(head1, head2, type, field) do {                       \
433         struct type *swap_tmp = LIST_FIRST((head1));                    \
434         LIST_FIRST((head1)) = LIST_FIRST((head2));                      \
435         LIST_FIRST((head2)) = swap_tmp;                                 \
436         if ((swap_tmp = LIST_FIRST((head1))) != NULL)                   \
437                 swap_tmp->field.le_prev = &LIST_FIRST((head1));         \
438         if ((swap_tmp = LIST_FIRST((head2))) != NULL)                   \
439                 swap_tmp->field.le_prev = &LIST_FIRST((head2));         \
440 } while (0)
441
442 /*
443  * Tail queue declarations.
444  */
445 #define TAILQ_HEAD(name, type)                                          \
446 struct name {                                                           \
447         struct type *tqh_first; /* first element */                     \
448         struct type **tqh_last; /* addr of last next element */         \
449         TRACEBUF                                                        \
450 }
451
452 #define TAILQ_HEAD_INITIALIZER(head)                                    \
453         { NULL, &(head).tqh_first }
454
455 #define TAILQ_ENTRY(type)                                               \
456 struct {                                                                \
457         struct type *tqe_next;  /* next element */                      \
458         struct type **tqe_prev; /* address of previous next element */  \
459         TRACEBUF                                                        \
460 }
461
462 /*
463  * Tail queue functions.
464  */
465 #if (defined(_KERNEL) && defined(INVARIANTS))
466 #define QMD_TAILQ_CHECK_HEAD(head, field) do {                          \
467         if (!TAILQ_EMPTY(head) &&                                       \
468             TAILQ_FIRST((head))->field.tqe_prev !=                      \
469              &TAILQ_FIRST((head)))                                      \
470                 panic("Bad tailq head %p first->prev != head", (head)); \
471 } while (0)
472
473 #define QMD_TAILQ_CHECK_TAIL(head, field) do {                          \
474         if (*(head)->tqh_last != NULL)                                  \
475                 panic("Bad tailq NEXT(%p->tqh_last) != NULL", (head));  \
476 } while (0)
477
478 #define QMD_TAILQ_CHECK_NEXT(elm, field) do {                           \
479         if (TAILQ_NEXT((elm), field) != NULL &&                         \
480             TAILQ_NEXT((elm), field)->field.tqe_prev !=                 \
481              &((elm)->field.tqe_next))                                  \
482                 panic("Bad link elm %p next->prev != elm", (elm));      \
483 } while (0)
484
485 #define QMD_TAILQ_CHECK_PREV(elm, field) do {                           \
486         if (*(elm)->field.tqe_prev != (elm))                            \
487                 panic("Bad link elm %p prev->next != elm", (elm));      \
488 } while (0)
489 #else
490 #define QMD_TAILQ_CHECK_HEAD(head, field)
491 #define QMD_TAILQ_CHECK_TAIL(head, headname)
492 #define QMD_TAILQ_CHECK_NEXT(elm, field)
493 #define QMD_TAILQ_CHECK_PREV(elm, field)
494 #endif /* (_KERNEL && INVARIANTS) */
495
496 #define TAILQ_CONCAT(head1, head2, field) do {                          \
497         if (!TAILQ_EMPTY(head2)) {                                      \
498                 *(head1)->tqh_last = (head2)->tqh_first;                \
499                 (head2)->tqh_first->field.tqe_prev = (head1)->tqh_last; \
500                 (head1)->tqh_last = (head2)->tqh_last;                  \
501                 TAILQ_INIT((head2));                                    \
502                 QMD_TRACE_HEAD(head1);                                  \
503                 QMD_TRACE_HEAD(head2);                                  \
504         }                                                               \
505 } while (0)
506
507 #define TAILQ_EMPTY(head)       ((head)->tqh_first == NULL)
508
509 #define TAILQ_FIRST(head)       ((head)->tqh_first)
510
511 #define TAILQ_FOREACH(var, head, field)                                 \
512         for ((var) = TAILQ_FIRST((head));                               \
513             (var);                                                      \
514             (var) = TAILQ_NEXT((var), field))
515
516 #define TAILQ_FOREACH_SAFE(var, head, field, tvar)                      \
517         for ((var) = TAILQ_FIRST((head));                               \
518             (var) && ((tvar) = TAILQ_NEXT((var), field), 1);            \
519             (var) = (tvar))
520
521 #define TAILQ_FOREACH_REVERSE(var, head, headname, field)               \
522         for ((var) = TAILQ_LAST((head), headname);                      \
523             (var);                                                      \
524             (var) = TAILQ_PREV((var), headname, field))
525
526 #define TAILQ_FOREACH_REVERSE_SAFE(var, head, headname, field, tvar)    \
527         for ((var) = TAILQ_LAST((head), headname);                      \
528             (var) && ((tvar) = TAILQ_PREV((var), headname, field), 1);  \
529             (var) = (tvar))
530
531 #define TAILQ_INIT(head) do {                                           \
532         TAILQ_FIRST((head)) = NULL;                                     \
533         (head)->tqh_last = &TAILQ_FIRST((head));                        \
534         QMD_TRACE_HEAD(head);                                           \
535 } while (0)
536
537 #define TAILQ_INSERT_AFTER(head, listelm, elm, field) do {              \
538         QMD_TAILQ_CHECK_NEXT(listelm, field);                           \
539         if ((TAILQ_NEXT((elm), field) = TAILQ_NEXT((listelm), field)) != NULL)\
540                 TAILQ_NEXT((elm), field)->field.tqe_prev =              \
541                     &TAILQ_NEXT((elm), field);                          \
542         else {                                                          \
543                 (head)->tqh_last = &TAILQ_NEXT((elm), field);           \
544                 QMD_TRACE_HEAD(head);                                   \
545         }                                                               \
546         TAILQ_NEXT((listelm), field) = (elm);                           \
547         (elm)->field.tqe_prev = &TAILQ_NEXT((listelm), field);          \
548         QMD_TRACE_ELEM(&(elm)->field);                                  \
549         QMD_TRACE_ELEM(&listelm->field);                                \
550 } while (0)
551
552 #define TAILQ_INSERT_BEFORE(listelm, elm, field) do {                   \
553         QMD_TAILQ_CHECK_PREV(listelm, field);                           \
554         (elm)->field.tqe_prev = (listelm)->field.tqe_prev;              \
555         TAILQ_NEXT((elm), field) = (listelm);                           \
556         *(listelm)->field.tqe_prev = (elm);                             \
557         (listelm)->field.tqe_prev = &TAILQ_NEXT((elm), field);          \
558         QMD_TRACE_ELEM(&(elm)->field);                                  \
559         QMD_TRACE_ELEM(&listelm->field);                                \
560 } while (0)
561
562 #define TAILQ_INSERT_HEAD(head, elm, field) do {                        \
563         QMD_TAILQ_CHECK_HEAD(head, field);                              \
564         if ((TAILQ_NEXT((elm), field) = TAILQ_FIRST((head))) != NULL)   \
565                 TAILQ_FIRST((head))->field.tqe_prev =                   \
566                     &TAILQ_NEXT((elm), field);                          \
567         else                                                            \
568                 (head)->tqh_last = &TAILQ_NEXT((elm), field);           \
569         TAILQ_FIRST((head)) = (elm);                                    \
570         (elm)->field.tqe_prev = &TAILQ_FIRST((head));                   \
571         QMD_TRACE_HEAD(head);                                           \
572         QMD_TRACE_ELEM(&(elm)->field);                                  \
573 } while (0)
574
575 #define TAILQ_INSERT_TAIL(head, elm, field) do {                        \
576         QMD_TAILQ_CHECK_TAIL(head, field);                              \
577         TAILQ_NEXT((elm), field) = NULL;                                \
578         (elm)->field.tqe_prev = (head)->tqh_last;                       \
579         *(head)->tqh_last = (elm);                                      \
580         (head)->tqh_last = &TAILQ_NEXT((elm), field);                   \
581         QMD_TRACE_HEAD(head);                                           \
582         QMD_TRACE_ELEM(&(elm)->field);                                  \
583 } while (0)
584
585 #define TAILQ_LAST(head, headname)                                      \
586         (*(((struct headname *)((head)->tqh_last))->tqh_last))
587
588 #define TAILQ_NEXT(elm, field) ((elm)->field.tqe_next)
589
590 #define TAILQ_PREV(elm, headname, field)                                \
591         (*(((struct headname *)((elm)->field.tqe_prev))->tqh_last))
592
593 #define TAILQ_REMOVE(head, elm, field) do {                             \
594         QMD_SAVELINK(oldnext, (elm)->field.tqe_next);                   \
595         QMD_SAVELINK(oldprev, (elm)->field.tqe_prev);                   \
596         QMD_TAILQ_CHECK_NEXT(elm, field);                               \
597         QMD_TAILQ_CHECK_PREV(elm, field);                               \
598         if ((TAILQ_NEXT((elm), field)) != NULL)                         \
599                 TAILQ_NEXT((elm), field)->field.tqe_prev =              \
600                     (elm)->field.tqe_prev;                              \
601         else {                                                          \
602                 (head)->tqh_last = (elm)->field.tqe_prev;               \
603                 QMD_TRACE_HEAD(head);                                   \
604         }                                                               \
605         *(elm)->field.tqe_prev = TAILQ_NEXT((elm), field);              \
606         TRASHIT(*oldnext);                                              \
607         TRASHIT(*oldprev);                                              \
608         QMD_TRACE_ELEM(&(elm)->field);                                  \
609 } while (0)
610
611 #define TAILQ_SWAP(head1, head2, type, field) do {                      \
612         struct type *swap_first = (head1)->tqh_first;                   \
613         struct type **swap_last = (head1)->tqh_last;                    \
614         (head1)->tqh_first = (head2)->tqh_first;                        \
615         (head1)->tqh_last = (head2)->tqh_last;                          \
616         (head2)->tqh_first = swap_first;                                \
617         (head2)->tqh_last = swap_last;                                  \
618         if ((swap_first = (head1)->tqh_first) != NULL)                  \
619                 swap_first->field.tqe_prev = &(head1)->tqh_first;       \
620         else                                                            \
621                 (head1)->tqh_last = &(head1)->tqh_first;                \
622         if ((swap_first = (head2)->tqh_first) != NULL)                  \
623                 swap_first->field.tqe_prev = &(head2)->tqh_first;       \
624         else                                                            \
625                 (head2)->tqh_last = &(head2)->tqh_first;                \
626 } while (0)
627
628 #endif /* _SYS_QUEUE_H */