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