Blob


1 /*
2 * A compatible version of OpenBSD <sys/queue.h>.
3 */
4 /*
5 * Copyright (c) 1991, 1993
6 * The Regents of the University of California. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ''AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 *
32 * @(#)queue.h 8.5 (Berkeley) 8/20/94
33 */
35 /* OPENBSD ORIGINAL: sys/sys/queue.h */
37 /*
38 * Require for OS/X and other platforms that have old/broken/incomplete
39 * <sys/queue.h>.
40 */
42 #undef LIST_EMPTY
43 #undef LIST_END
44 #undef LIST_ENTRY
45 #undef LIST_FIRST
46 #undef LIST_FOREACH
47 #undef LIST_FOREACH_SAFE
48 #undef LIST_HEAD
49 #undef LIST_HEAD_INITIALIZER
50 #undef LIST_INIT
51 #undef LIST_INSERT_AFTER
52 #undef LIST_INSERT_BEFORE
53 #undef LIST_INSERT_HEAD
54 #undef LIST_NEXT
55 #undef LIST_REMOVE
56 #undef LIST_REPLACE
57 #undef SIMPLEQ_CONCAT
58 #undef SIMPLEQ_EMPTY
59 #undef SIMPLEQ_END
60 #undef SIMPLEQ_ENTRY
61 #undef SIMPLEQ_FIRST
62 #undef SIMPLEQ_FOREACH
63 #undef SIMPLEQ_FOREACH_SAFE
64 #undef SIMPLEQ_HEAD
65 #undef SIMPLEQ_HEAD_INITIALIZER
66 #undef SIMPLEQ_INIT
67 #undef SIMPLEQ_INSERT_AFTER
68 #undef SIMPLEQ_INSERT_HEAD
69 #undef SIMPLEQ_INSERT_TAIL
70 #undef SIMPLEQ_NEXT
71 #undef SIMPLEQ_REMOVE_AFTER
72 #undef SIMPLEQ_REMOVE_HEAD
73 #undef SLIST_EMPTY
74 #undef SLIST_END
75 #undef SLIST_ENTRY
76 #undef SLIST_FIRST
77 #undef SLIST_FOREACH
78 #undef SLIST_FOREACH_SAFE
79 #undef SLIST_HEAD
80 #undef SLIST_HEAD_INITIALIZER
81 #undef SLIST_INIT
82 #undef SLIST_INSERT_AFTER
83 #undef SLIST_INSERT_HEAD
84 #undef SLIST_NEXT
85 #undef SLIST_REMOVE
86 #undef SLIST_REMOVE_AFTER
87 #undef SLIST_REMOVE_HEAD
88 #undef TAILQ_CONCAT
89 #undef TAILQ_EMPTY
90 #undef TAILQ_END
91 #undef TAILQ_ENTRY
92 #undef TAILQ_FIRST
93 #undef TAILQ_FOREACH
94 #undef TAILQ_FOREACH_REVERSE
95 #undef TAILQ_FOREACH_REVERSE_SAFE
96 #undef TAILQ_FOREACH_SAFE
97 #undef TAILQ_HEAD
98 #undef TAILQ_HEAD_INITIALIZER
99 #undef TAILQ_INIT
100 #undef TAILQ_INSERT_AFTER
101 #undef TAILQ_INSERT_BEFORE
102 #undef TAILQ_INSERT_HEAD
103 #undef TAILQ_INSERT_TAIL
104 #undef TAILQ_LAST
105 #undef TAILQ_NEXT
106 #undef TAILQ_PREV
107 #undef TAILQ_REMOVE
108 #undef TAILQ_REPLACE
109 #undef XSIMPLEQ_EMPTY
110 #undef XSIMPLEQ_END
111 #undef XSIMPLEQ_ENTRY
112 #undef XSIMPLEQ_FIRST
113 #undef XSIMPLEQ_FOREACH
114 #undef XSIMPLEQ_FOREACH_SAFE
115 #undef XSIMPLEQ_HEAD
116 #undef XSIMPLEQ_INIT
117 #undef XSIMPLEQ_INSERT_AFTER
118 #undef XSIMPLEQ_INSERT_HEAD
119 #undef XSIMPLEQ_INSERT_TAIL
120 #undef XSIMPLEQ_NEXT
121 #undef XSIMPLEQ_REMOVE_AFTER
122 #undef XSIMPLEQ_REMOVE_HEAD
123 #undef XSIMPLEQ_XOR
125 /*
126 * This file defines five types of data structures: singly-linked lists,
127 * lists, simple queues, tail queues and XOR simple queues.
130 * A singly-linked list is headed by a single forward pointer. The elements
131 * are singly linked for minimum space and pointer manipulation overhead at
132 * the expense of O(n) removal for arbitrary elements. New elements can be
133 * added to the list after an existing element or at the head of the list.
134 * Elements being removed from the head of the list should use the explicit
135 * macro for this purpose for optimum efficiency. A singly-linked list may
136 * only be traversed in the forward direction. Singly-linked lists are ideal
137 * for applications with large datasets and few or no removals or for
138 * implementing a LIFO queue.
140 * A list is headed by a single forward pointer (or an array of forward
141 * pointers for a hash table header). The elements are doubly linked
142 * so that an arbitrary element can be removed without a need to
143 * traverse the list. New elements can be added to the list before
144 * or after an existing element or at the head of the list. A list
145 * may only be traversed in the forward direction.
147 * A simple queue is headed by a pair of pointers, one to the head of the
148 * list and the other to the tail of the list. The elements are singly
149 * linked to save space, so elements can only be removed from the
150 * head of the list. New elements can be added to the list before or after
151 * an existing element, at the head of the list, or at the end of the
152 * list. A simple queue may only be traversed in the forward direction.
154 * A tail queue is headed by a pair of pointers, one to the head of the
155 * list and the other to the tail of the list. The elements are doubly
156 * linked so that an arbitrary element can be removed without a need to
157 * traverse the list. New elements can be added to the list before or
158 * after an existing element, at the head of the list, or at the end of
159 * the list. A tail queue may be traversed in either direction.
161 * An XOR simple queue is used in the same way as a regular simple queue.
162 * The difference is that the head structure also includes a "cookie" that
163 * is XOR'd with the queue pointer (first, last or next) to generate the
164 * real pointer value.
166 * For details on the use of these macros, see the queue(3) manual page.
167 */
169 #if defined(QUEUE_MACRO_DEBUG) || (defined(_KERNEL) && defined(DIAGNOSTIC))
170 #define _Q_INVALID ((void *)-1)
171 #define _Q_INVALIDATE(a) (a) = _Q_INVALID
172 #else
173 #define _Q_INVALIDATE(a)
174 #endif
176 /*
177 * Singly-linked List definitions.
178 */
179 #define SLIST_HEAD(name, type) \
180 struct name { \
181 struct type *slh_first; /* first element */ \
184 #define SLIST_HEAD_INITIALIZER(head) \
185 { NULL }
187 #define SLIST_ENTRY(type) \
188 struct { \
189 struct type *sle_next; /* next element */ \
192 /*
193 * Singly-linked List access methods.
194 */
195 #define SLIST_FIRST(head) ((head)->slh_first)
196 #define SLIST_END(head) NULL
197 #define SLIST_EMPTY(head) (SLIST_FIRST(head) == SLIST_END(head))
198 #define SLIST_NEXT(elm, field) ((elm)->field.sle_next)
200 #define SLIST_FOREACH(var, head, field) \
201 for((var) = SLIST_FIRST(head); \
202 (var) != SLIST_END(head); \
203 (var) = SLIST_NEXT(var, field))
205 #define SLIST_FOREACH_SAFE(var, head, field, tvar) \
206 for ((var) = SLIST_FIRST(head); \
207 (var) && ((tvar) = SLIST_NEXT(var, field), 1); \
208 (var) = (tvar))
210 /*
211 * Singly-linked List functions.
212 */
213 #define SLIST_INIT(head) { \
214 SLIST_FIRST(head) = SLIST_END(head); \
217 #define SLIST_INSERT_AFTER(slistelm, elm, field) do { \
218 (elm)->field.sle_next = (slistelm)->field.sle_next; \
219 (slistelm)->field.sle_next = (elm); \
220 } while (0)
222 #define SLIST_INSERT_HEAD(head, elm, field) do { \
223 (elm)->field.sle_next = (head)->slh_first; \
224 (head)->slh_first = (elm); \
225 } while (0)
227 #define SLIST_REMOVE_AFTER(elm, field) do { \
228 (elm)->field.sle_next = (elm)->field.sle_next->field.sle_next; \
229 } while (0)
231 #define SLIST_REMOVE_HEAD(head, field) do { \
232 (head)->slh_first = (head)->slh_first->field.sle_next; \
233 } while (0)
235 #define SLIST_REMOVE(head, elm, type, field) do { \
236 if ((head)->slh_first == (elm)) { \
237 SLIST_REMOVE_HEAD((head), field); \
238 } else { \
239 struct type *curelm = (head)->slh_first; \
241 while (curelm->field.sle_next != (elm)) \
242 curelm = curelm->field.sle_next; \
243 curelm->field.sle_next = \
244 curelm->field.sle_next->field.sle_next; \
245 } \
246 _Q_INVALIDATE((elm)->field.sle_next); \
247 } while (0)
249 /*
250 * List definitions.
251 */
252 #define LIST_HEAD(name, type) \
253 struct name { \
254 struct type *lh_first; /* first element */ \
257 #define LIST_HEAD_INITIALIZER(head) \
258 { NULL }
260 #define LIST_ENTRY(type) \
261 struct { \
262 struct type *le_next; /* next element */ \
263 struct type **le_prev; /* address of previous next element */ \
266 /*
267 * List access methods.
268 */
269 #define LIST_FIRST(head) ((head)->lh_first)
270 #define LIST_END(head) NULL
271 #define LIST_EMPTY(head) (LIST_FIRST(head) == LIST_END(head))
272 #define LIST_NEXT(elm, field) ((elm)->field.le_next)
274 #define LIST_FOREACH(var, head, field) \
275 for((var) = LIST_FIRST(head); \
276 (var)!= LIST_END(head); \
277 (var) = LIST_NEXT(var, field))
279 #define LIST_FOREACH_SAFE(var, head, field, tvar) \
280 for ((var) = LIST_FIRST(head); \
281 (var) && ((tvar) = LIST_NEXT(var, field), 1); \
282 (var) = (tvar))
284 /*
285 * List functions.
286 */
287 #define LIST_INIT(head) do { \
288 LIST_FIRST(head) = LIST_END(head); \
289 } while (0)
291 #define LIST_INSERT_AFTER(listelm, elm, field) do { \
292 if (((elm)->field.le_next = (listelm)->field.le_next) != NULL) \
293 (listelm)->field.le_next->field.le_prev = \
294 &(elm)->field.le_next; \
295 (listelm)->field.le_next = (elm); \
296 (elm)->field.le_prev = &(listelm)->field.le_next; \
297 } while (0)
299 #define LIST_INSERT_BEFORE(listelm, elm, field) do { \
300 (elm)->field.le_prev = (listelm)->field.le_prev; \
301 (elm)->field.le_next = (listelm); \
302 *(listelm)->field.le_prev = (elm); \
303 (listelm)->field.le_prev = &(elm)->field.le_next; \
304 } while (0)
306 #define LIST_INSERT_HEAD(head, elm, field) do { \
307 if (((elm)->field.le_next = (head)->lh_first) != NULL) \
308 (head)->lh_first->field.le_prev = &(elm)->field.le_next;\
309 (head)->lh_first = (elm); \
310 (elm)->field.le_prev = &(head)->lh_first; \
311 } while (0)
313 #define LIST_REMOVE(elm, field) do { \
314 if ((elm)->field.le_next != NULL) \
315 (elm)->field.le_next->field.le_prev = \
316 (elm)->field.le_prev; \
317 *(elm)->field.le_prev = (elm)->field.le_next; \
318 _Q_INVALIDATE((elm)->field.le_prev); \
319 _Q_INVALIDATE((elm)->field.le_next); \
320 } while (0)
322 #define LIST_REPLACE(elm, elm2, field) do { \
323 if (((elm2)->field.le_next = (elm)->field.le_next) != NULL) \
324 (elm2)->field.le_next->field.le_prev = \
325 &(elm2)->field.le_next; \
326 (elm2)->field.le_prev = (elm)->field.le_prev; \
327 *(elm2)->field.le_prev = (elm2); \
328 _Q_INVALIDATE((elm)->field.le_prev); \
329 _Q_INVALIDATE((elm)->field.le_next); \
330 } while (0)
332 /*
333 * Simple queue definitions.
334 */
335 #define SIMPLEQ_HEAD(name, type) \
336 struct name { \
337 struct type *sqh_first; /* first element */ \
338 struct type **sqh_last; /* addr of last next element */ \
341 #define SIMPLEQ_HEAD_INITIALIZER(head) \
342 { NULL, &(head).sqh_first }
344 #define SIMPLEQ_ENTRY(type) \
345 struct { \
346 struct type *sqe_next; /* next element */ \
349 /*
350 * Simple queue access methods.
351 */
352 #define SIMPLEQ_FIRST(head) ((head)->sqh_first)
353 #define SIMPLEQ_END(head) NULL
354 #define SIMPLEQ_EMPTY(head) (SIMPLEQ_FIRST(head) == SIMPLEQ_END(head))
355 #define SIMPLEQ_NEXT(elm, field) ((elm)->field.sqe_next)
357 #define SIMPLEQ_FOREACH(var, head, field) \
358 for((var) = SIMPLEQ_FIRST(head); \
359 (var) != SIMPLEQ_END(head); \
360 (var) = SIMPLEQ_NEXT(var, field))
362 #define SIMPLEQ_FOREACH_SAFE(var, head, field, tvar) \
363 for ((var) = SIMPLEQ_FIRST(head); \
364 (var) && ((tvar) = SIMPLEQ_NEXT(var, field), 1); \
365 (var) = (tvar))
367 /*
368 * Simple queue functions.
369 */
370 #define SIMPLEQ_INIT(head) do { \
371 (head)->sqh_first = NULL; \
372 (head)->sqh_last = &(head)->sqh_first; \
373 } while (0)
375 #define SIMPLEQ_INSERT_HEAD(head, elm, field) do { \
376 if (((elm)->field.sqe_next = (head)->sqh_first) == NULL) \
377 (head)->sqh_last = &(elm)->field.sqe_next; \
378 (head)->sqh_first = (elm); \
379 } while (0)
381 #define SIMPLEQ_INSERT_TAIL(head, elm, field) do { \
382 (elm)->field.sqe_next = NULL; \
383 *(head)->sqh_last = (elm); \
384 (head)->sqh_last = &(elm)->field.sqe_next; \
385 } while (0)
387 #define SIMPLEQ_INSERT_AFTER(head, listelm, elm, field) do { \
388 if (((elm)->field.sqe_next = (listelm)->field.sqe_next) == NULL)\
389 (head)->sqh_last = &(elm)->field.sqe_next; \
390 (listelm)->field.sqe_next = (elm); \
391 } while (0)
393 #define SIMPLEQ_REMOVE_HEAD(head, field) do { \
394 if (((head)->sqh_first = (head)->sqh_first->field.sqe_next) == NULL) \
395 (head)->sqh_last = &(head)->sqh_first; \
396 } while (0)
398 #define SIMPLEQ_REMOVE_AFTER(head, elm, field) do { \
399 if (((elm)->field.sqe_next = (elm)->field.sqe_next->field.sqe_next) \
400 == NULL) \
401 (head)->sqh_last = &(elm)->field.sqe_next; \
402 } while (0)
404 #define SIMPLEQ_CONCAT(head1, head2) do { \
405 if (!SIMPLEQ_EMPTY((head2))) { \
406 *(head1)->sqh_last = (head2)->sqh_first; \
407 (head1)->sqh_last = (head2)->sqh_last; \
408 SIMPLEQ_INIT((head2)); \
409 } \
410 } while (0)
412 /*
413 * XOR Simple queue definitions.
414 */
415 #define XSIMPLEQ_HEAD(name, type) \
416 struct name { \
417 struct type *sqx_first; /* first element */ \
418 struct type **sqx_last; /* addr of last next element */ \
419 unsigned long sqx_cookie; \
422 #define XSIMPLEQ_ENTRY(type) \
423 struct { \
424 struct type *sqx_next; /* next element */ \
427 /*
428 * XOR Simple queue access methods.
429 */
430 #define XSIMPLEQ_XOR(head, ptr) ((__typeof(ptr))((head)->sqx_cookie ^ \
431 (unsigned long)(ptr)))
432 #define XSIMPLEQ_FIRST(head) XSIMPLEQ_XOR(head, ((head)->sqx_first))
433 #define XSIMPLEQ_END(head) NULL
434 #define XSIMPLEQ_EMPTY(head) (XSIMPLEQ_FIRST(head) == XSIMPLEQ_END(head))
435 #define XSIMPLEQ_NEXT(head, elm, field) XSIMPLEQ_XOR(head, ((elm)->field.sqx_next))
438 #define XSIMPLEQ_FOREACH(var, head, field) \
439 for ((var) = XSIMPLEQ_FIRST(head); \
440 (var) != XSIMPLEQ_END(head); \
441 (var) = XSIMPLEQ_NEXT(head, var, field))
443 #define XSIMPLEQ_FOREACH_SAFE(var, head, field, tvar) \
444 for ((var) = XSIMPLEQ_FIRST(head); \
445 (var) && ((tvar) = XSIMPLEQ_NEXT(head, var, field), 1); \
446 (var) = (tvar))
448 /*
449 * XOR Simple queue functions.
450 */
451 #define XSIMPLEQ_INIT(head) do { \
452 arc4random_buf(&(head)->sqx_cookie, sizeof((head)->sqx_cookie)); \
453 (head)->sqx_first = XSIMPLEQ_XOR(head, NULL); \
454 (head)->sqx_last = XSIMPLEQ_XOR(head, &(head)->sqx_first); \
455 } while (0)
457 #define XSIMPLEQ_INSERT_HEAD(head, elm, field) do { \
458 if (((elm)->field.sqx_next = (head)->sqx_first) == \
459 XSIMPLEQ_XOR(head, NULL)) \
460 (head)->sqx_last = XSIMPLEQ_XOR(head, &(elm)->field.sqx_next); \
461 (head)->sqx_first = XSIMPLEQ_XOR(head, (elm)); \
462 } while (0)
464 #define XSIMPLEQ_INSERT_TAIL(head, elm, field) do { \
465 (elm)->field.sqx_next = XSIMPLEQ_XOR(head, NULL); \
466 *(XSIMPLEQ_XOR(head, (head)->sqx_last)) = XSIMPLEQ_XOR(head, (elm)); \
467 (head)->sqx_last = XSIMPLEQ_XOR(head, &(elm)->field.sqx_next); \
468 } while (0)
470 #define XSIMPLEQ_INSERT_AFTER(head, listelm, elm, field) do { \
471 if (((elm)->field.sqx_next = (listelm)->field.sqx_next) == \
472 XSIMPLEQ_XOR(head, NULL)) \
473 (head)->sqx_last = XSIMPLEQ_XOR(head, &(elm)->field.sqx_next); \
474 (listelm)->field.sqx_next = XSIMPLEQ_XOR(head, (elm)); \
475 } while (0)
477 #define XSIMPLEQ_REMOVE_HEAD(head, field) do { \
478 if (((head)->sqx_first = XSIMPLEQ_XOR(head, \
479 (head)->sqx_first)->field.sqx_next) == XSIMPLEQ_XOR(head, NULL)) \
480 (head)->sqx_last = XSIMPLEQ_XOR(head, &(head)->sqx_first); \
481 } while (0)
483 #define XSIMPLEQ_REMOVE_AFTER(head, elm, field) do { \
484 if (((elm)->field.sqx_next = XSIMPLEQ_XOR(head, \
485 (elm)->field.sqx_next)->field.sqx_next) \
486 == XSIMPLEQ_XOR(head, NULL)) \
487 (head)->sqx_last = \
488 XSIMPLEQ_XOR(head, &(elm)->field.sqx_next); \
489 } while (0)
492 /*
493 * Tail queue definitions.
494 */
495 #define TAILQ_HEAD(name, type) \
496 struct name { \
497 struct type *tqh_first; /* first element */ \
498 struct type **tqh_last; /* addr of last next element */ \
501 #define TAILQ_HEAD_INITIALIZER(head) \
502 { NULL, &(head).tqh_first }
504 #define TAILQ_ENTRY(type) \
505 struct { \
506 struct type *tqe_next; /* next element */ \
507 struct type **tqe_prev; /* address of previous next element */ \
510 /*
511 * Tail queue access methods.
512 */
513 #define TAILQ_FIRST(head) ((head)->tqh_first)
514 #define TAILQ_END(head) NULL
515 #define TAILQ_NEXT(elm, field) ((elm)->field.tqe_next)
516 #define TAILQ_LAST(head, headname) \
517 (*(((struct headname *)((head)->tqh_last))->tqh_last))
518 /* XXX */
519 #define TAILQ_PREV(elm, headname, field) \
520 (*(((struct headname *)((elm)->field.tqe_prev))->tqh_last))
521 #define TAILQ_EMPTY(head) \
522 (TAILQ_FIRST(head) == TAILQ_END(head))
524 #define TAILQ_FOREACH(var, head, field) \
525 for((var) = TAILQ_FIRST(head); \
526 (var) != TAILQ_END(head); \
527 (var) = TAILQ_NEXT(var, field))
529 #define TAILQ_FOREACH_SAFE(var, head, field, tvar) \
530 for ((var) = TAILQ_FIRST(head); \
531 (var) != TAILQ_END(head) && \
532 ((tvar) = TAILQ_NEXT(var, field), 1); \
533 (var) = (tvar))
536 #define TAILQ_FOREACH_REVERSE(var, head, headname, field) \
537 for((var) = TAILQ_LAST(head, headname); \
538 (var) != TAILQ_END(head); \
539 (var) = TAILQ_PREV(var, headname, field))
541 #define TAILQ_FOREACH_REVERSE_SAFE(var, head, headname, field, tvar) \
542 for ((var) = TAILQ_LAST(head, headname); \
543 (var) != TAILQ_END(head) && \
544 ((tvar) = TAILQ_PREV(var, headname, field), 1); \
545 (var) = (tvar))
547 /*
548 * Tail queue functions.
549 */
550 #define TAILQ_INIT(head) do { \
551 (head)->tqh_first = NULL; \
552 (head)->tqh_last = &(head)->tqh_first; \
553 } while (0)
555 #define TAILQ_INSERT_HEAD(head, elm, field) do { \
556 if (((elm)->field.tqe_next = (head)->tqh_first) != NULL) \
557 (head)->tqh_first->field.tqe_prev = \
558 &(elm)->field.tqe_next; \
559 else \
560 (head)->tqh_last = &(elm)->field.tqe_next; \
561 (head)->tqh_first = (elm); \
562 (elm)->field.tqe_prev = &(head)->tqh_first; \
563 } while (0)
565 #define TAILQ_INSERT_TAIL(head, elm, field) do { \
566 (elm)->field.tqe_next = NULL; \
567 (elm)->field.tqe_prev = (head)->tqh_last; \
568 *(head)->tqh_last = (elm); \
569 (head)->tqh_last = &(elm)->field.tqe_next; \
570 } while (0)
572 #define TAILQ_INSERT_AFTER(head, listelm, elm, field) do { \
573 if (((elm)->field.tqe_next = (listelm)->field.tqe_next) != NULL)\
574 (elm)->field.tqe_next->field.tqe_prev = \
575 &(elm)->field.tqe_next; \
576 else \
577 (head)->tqh_last = &(elm)->field.tqe_next; \
578 (listelm)->field.tqe_next = (elm); \
579 (elm)->field.tqe_prev = &(listelm)->field.tqe_next; \
580 } while (0)
582 #define TAILQ_INSERT_BEFORE(listelm, elm, field) do { \
583 (elm)->field.tqe_prev = (listelm)->field.tqe_prev; \
584 (elm)->field.tqe_next = (listelm); \
585 *(listelm)->field.tqe_prev = (elm); \
586 (listelm)->field.tqe_prev = &(elm)->field.tqe_next; \
587 } while (0)
589 #define TAILQ_REMOVE(head, elm, field) do { \
590 if (((elm)->field.tqe_next) != NULL) \
591 (elm)->field.tqe_next->field.tqe_prev = \
592 (elm)->field.tqe_prev; \
593 else \
594 (head)->tqh_last = (elm)->field.tqe_prev; \
595 *(elm)->field.tqe_prev = (elm)->field.tqe_next; \
596 _Q_INVALIDATE((elm)->field.tqe_prev); \
597 _Q_INVALIDATE((elm)->field.tqe_next); \
598 } while (0)
600 #define TAILQ_REPLACE(head, elm, elm2, field) do { \
601 if (((elm2)->field.tqe_next = (elm)->field.tqe_next) != NULL) \
602 (elm2)->field.tqe_next->field.tqe_prev = \
603 &(elm2)->field.tqe_next; \
604 else \
605 (head)->tqh_last = &(elm2)->field.tqe_next; \
606 (elm2)->field.tqe_prev = (elm)->field.tqe_prev; \
607 *(elm2)->field.tqe_prev = (elm2); \
608 _Q_INVALIDATE((elm)->field.tqe_prev); \
609 _Q_INVALIDATE((elm)->field.tqe_next); \
610 } while (0)
612 #define TAILQ_CONCAT(head1, head2, field) do { \
613 if (!TAILQ_EMPTY(head2)) { \
614 *(head1)->tqh_last = (head2)->tqh_first; \
615 (head2)->tqh_first->field.tqe_prev = (head1)->tqh_last; \
616 (head1)->tqh_last = (head2)->tqh_last; \
617 TAILQ_INIT((head2)); \
618 } \
619 } while (0)