sys-queue.h
1/* $OpenBSD: queue.h,v 1.38 2013/07/03 15:05:21 fgsch Exp $ */
2/* $NetBSD: queue.h,v 1.11 1996/05/16 05:17:14 mycroft Exp $ */
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 */
34
35/* OPENBSD ORIGINAL: sys/sys/queue.h */
36
37#ifndef _FAKE_QUEUE_H_
38#define _FAKE_QUEUE_H_
39
40/*
41 * Require for OS/X and other platforms that have old/broken/incomplete
42 * <sys/queue.h>.
43 */
44#undef SLIST_HEAD
45#undef SLIST_HEAD_INITIALIZER
46#undef SLIST_ENTRY
47#undef SLIST_FOREACH_PREVPTR
48#undef SLIST_FOREACH_SAFE
49#undef SLIST_FIRST
50#undef SLIST_END
51#undef SLIST_EMPTY
52#undef SLIST_NEXT
53#undef SLIST_FOREACH
54#undef SLIST_INIT
55#undef SLIST_INSERT_AFTER
56#undef SLIST_INSERT_HEAD
57#undef SLIST_REMOVE_HEAD
58#undef SLIST_REMOVE_AFTER
59#undef SLIST_REMOVE
60#undef SLIST_REMOVE_NEXT
61#undef LIST_HEAD
62#undef LIST_HEAD_INITIALIZER
63#undef LIST_ENTRY
64#undef LIST_FIRST
65#undef LIST_END
66#undef LIST_EMPTY
67#undef LIST_NEXT
68#undef LIST_FOREACH
69#undef LIST_FOREACH_SAFE
70#undef LIST_INIT
71#undef LIST_INSERT_AFTER
72#undef LIST_INSERT_BEFORE
73#undef LIST_INSERT_HEAD
74#undef LIST_REMOVE
75#undef LIST_REPLACE
76#undef SIMPLEQ_HEAD
77#undef SIMPLEQ_HEAD_INITIALIZER
78#undef SIMPLEQ_ENTRY
79#undef SIMPLEQ_FIRST
80#undef SIMPLEQ_END
81#undef SIMPLEQ_EMPTY
82#undef SIMPLEQ_NEXT
83#undef SIMPLEQ_FOREACH
84#undef SIMPLEQ_INIT
85#undef SIMPLEQ_INSERT_HEAD
86#undef SIMPLEQ_INSERT_TAIL
87#undef SIMPLEQ_INSERT_AFTER
88#undef SIMPLEQ_REMOVE_HEAD
89#undef XSIMPLEQ_HEAD
90#undef XSIMPLEQ_ENTRY
91#undef XSIMPLEQ_XOR
92#undef XSIMPLEQ_FIRST
93#undef XSIMPLEQ_END
94#undef XSIMPLEQ_EMPTY
95#undef XSIMPLEQ_NEXT
96#undef XSIMPLEQ_FOREACH
97#undef XSIMPLEQ_FOREACH_SAFE
98#undef XSIMPLEQ_INIT
99#undef XSIMPLEQ_INSERT_HEAD
100#undef XSIMPLEQ_INSERT_TAIL
101#undef XSIMPLEQ_INSERT_AFTER
102#undef XSIMPLEQ_REMOVE_HEAD
103#undef XSIMPLEQ_REMOVE_AFTER
104#undef TAILQ_HEAD
105#undef TAILQ_HEAD_INITIALIZER
106#undef TAILQ_ENTRY
107#undef TAILQ_FIRST
108#undef TAILQ_END
109#undef TAILQ_NEXT
110#undef TAILQ_LAST
111#undef TAILQ_PREV
112#undef TAILQ_EMPTY
113#undef TAILQ_FOREACH
114#undef TAILQ_FOREACH_REVERSE
115#undef TAILQ_FOREACH_SAFE
116#undef TAILQ_FOREACH_REVERSE_SAFE
117#undef TAILQ_INIT
118#undef TAILQ_INSERT_HEAD
119#undef TAILQ_INSERT_TAIL
120#undef TAILQ_INSERT_AFTER
121#undef TAILQ_INSERT_BEFORE
122#undef TAILQ_REMOVE
123#undef TAILQ_REPLACE
124#undef CIRCLEQ_HEAD
125#undef CIRCLEQ_HEAD_INITIALIZER
126#undef CIRCLEQ_ENTRY
127#undef CIRCLEQ_FIRST
128#undef CIRCLEQ_LAST
129#undef CIRCLEQ_END
130#undef CIRCLEQ_NEXT
131#undef CIRCLEQ_PREV
132#undef CIRCLEQ_EMPTY
133#undef CIRCLEQ_FOREACH
134#undef CIRCLEQ_FOREACH_REVERSE
135#undef CIRCLEQ_INIT
136#undef CIRCLEQ_INSERT_AFTER
137#undef CIRCLEQ_INSERT_BEFORE
138#undef CIRCLEQ_INSERT_HEAD
139#undef CIRCLEQ_INSERT_TAIL
140#undef CIRCLEQ_REMOVE
141#undef CIRCLEQ_REPLACE
142
143/*
144 * This file defines five types of data structures: singly-linked lists,
145 * lists, simple queues, tail queues, and circular queues.
146 *
147 *
148 * A singly-linked list is headed by a single forward pointer. The elements
149 * are singly linked for minimum space and pointer manipulation overhead at
150 * the expense of O(n) removal for arbitrary elements. New elements can be
151 * added to the list after an existing element or at the head of the list.
152 * Elements being removed from the head of the list should use the explicit
153 * macro for this purpose for optimum efficiency. A singly-linked list may
154 * only be traversed in the forward direction. Singly-linked lists are ideal
155 * for applications with large datasets and few or no removals or for
156 * implementing a LIFO queue.
157 *
158 * A list is headed by a single forward pointer (or an array of forward
159 * pointers for a hash table header). The elements are doubly linked
160 * so that an arbitrary element can be removed without a need to
161 * traverse the list. New elements can be added to the list before
162 * or after an existing element or at the head of the list. A list
163 * may only be traversed in the forward direction.
164 *
165 * A simple queue is headed by a pair of pointers, one the head of the
166 * list and the other to the tail of the list. The elements are singly
167 * linked to save space, so elements can only be removed from the
168 * head of the list. New elements can be added to the list before or after
169 * an existing element, at the head of the list, or at the end of the
170 * list. A simple queue may only be traversed in the forward direction.
171 *
172 * A tail queue is headed by a pair of pointers, one to the head of the
173 * list and the other to the tail of the list. The elements are doubly
174 * linked so that an arbitrary element can be removed without a need to
175 * traverse the list. New elements can be added to the list before or
176 * after an existing element, at the head of the list, or at the end of
177 * the list. A tail queue may be traversed in either direction.
178 *
179 * A circle queue is headed by a pair of pointers, one to the head of the
180 * list and the other to the tail of the list. The elements are doubly
181 * linked so that an arbitrary element can be removed without a need to
182 * traverse the list. New elements can be added to the list before or after
183 * an existing element, at the head of the list, or at the end of the list.
184 * A circle queue may be traversed in either direction, but has a more
185 * complex end of list detection.
186 *
187 * For details on the use of these macros, see the queue(3) manual page.
188 */
189
190#if defined(QUEUE_MACRO_DEBUG) || (defined(_KERNEL) && defined(DIAGNOSTIC))
191#define _Q_INVALIDATE(a) (a) = ((void *)-1)
192#else
193#define _Q_INVALIDATE(a)
194#endif
195
196/*
197 * Singly-linked List definitions.
198 */
199#define SLIST_HEAD(name, type) \
200struct name { \
201 struct type *slh_first; /* first element */ \
202}
203
204#define SLIST_HEAD_INITIALIZER(head) \
205 { NULL }
206
207#define SLIST_ENTRY(type) \
208struct { \
209 struct type *sle_next; /* next element */ \
210}
211
212/*
213 * Singly-linked List access methods.
214 */
215#define SLIST_FIRST(head) ((head)->slh_first)
216#define SLIST_END(head) NULL
217#define SLIST_EMPTY(head) (SLIST_FIRST(head) == SLIST_END(head))
218#define SLIST_NEXT(elm, field) ((elm)->field.sle_next)
219
220#define SLIST_FOREACH(var, head, field) \
221 for((var) = SLIST_FIRST(head); \
222 (var) != SLIST_END(head); \
223 (var) = SLIST_NEXT(var, field))
224
225#define SLIST_FOREACH_SAFE(var, head, field, tvar) \
226 for ((var) = SLIST_FIRST(head); \
227 (var) && ((tvar) = SLIST_NEXT(var, field), 1); \
228 (var) = (tvar))
229
230/*
231 * Singly-linked List functions.
232 */
233#define SLIST_INIT(head) { \
234 SLIST_FIRST(head) = SLIST_END(head); \
235}
236
237#define SLIST_INSERT_AFTER(slistelm, elm, field) do { \
238 (elm)->field.sle_next = (slistelm)->field.sle_next; \
239 (slistelm)->field.sle_next = (elm); \
240} while (0)
241
242#define SLIST_INSERT_HEAD(head, elm, field) do { \
243 (elm)->field.sle_next = (head)->slh_first; \
244 (head)->slh_first = (elm); \
245} while (0)
246
247#define SLIST_REMOVE_AFTER(elm, field) do { \
248 (elm)->field.sle_next = (elm)->field.sle_next->field.sle_next; \
249} while (0)
250
251#define SLIST_REMOVE_HEAD(head, field) do { \
252 (head)->slh_first = (head)->slh_first->field.sle_next; \
253} while (0)
254
255#define SLIST_REMOVE(head, elm, type, field) do { \
256 if ((head)->slh_first == (elm)) { \
257 SLIST_REMOVE_HEAD((head), field); \
258 } else { \
259 struct type *curelm = (head)->slh_first; \
260 \
261 while (curelm->field.sle_next != (elm)) \
262 curelm = curelm->field.sle_next; \
263 curelm->field.sle_next = \
264 curelm->field.sle_next->field.sle_next; \
265 _Q_INVALIDATE((elm)->field.sle_next); \
266 } \
267} while (0)
268
269/*
270 * List definitions.
271 */
272#define LIST_HEAD(name, type) \
273struct name { \
274 struct type *lh_first; /* first element */ \
275}
276
277#define LIST_HEAD_INITIALIZER(head) \
278 { NULL }
279
280#define LIST_ENTRY(type) \
281struct { \
282 struct type *le_next; /* next element */ \
283 struct type **le_prev; /* address of previous next element */ \
284}
285
286/*
287 * List access methods
288 */
289#define LIST_FIRST(head) ((head)->lh_first)
290#define LIST_END(head) NULL
291#define LIST_EMPTY(head) (LIST_FIRST(head) == LIST_END(head))
292#define LIST_NEXT(elm, field) ((elm)->field.le_next)
293
294#define LIST_FOREACH(var, head, field) \
295 for((var) = LIST_FIRST(head); \
296 (var)!= LIST_END(head); \
297 (var) = LIST_NEXT(var, field))
298
299#define LIST_FOREACH_SAFE(var, head, field, tvar) \
300 for ((var) = LIST_FIRST(head); \
301 (var) && ((tvar) = LIST_NEXT(var, field), 1); \
302 (var) = (tvar))
303
304/*
305 * List functions.
306 */
307#define LIST_INIT(head) do { \
308 LIST_FIRST(head) = LIST_END(head); \
309} while (0)
310
311#define LIST_INSERT_AFTER(listelm, elm, field) do { \
312 if (((elm)->field.le_next = (listelm)->field.le_next) != NULL) \
313 (listelm)->field.le_next->field.le_prev = \
314 &(elm)->field.le_next; \
315 (listelm)->field.le_next = (elm); \
316 (elm)->field.le_prev = &(listelm)->field.le_next; \
317} while (0)
318
319#define LIST_INSERT_BEFORE(listelm, elm, field) do { \
320 (elm)->field.le_prev = (listelm)->field.le_prev; \
321 (elm)->field.le_next = (listelm); \
322 *(listelm)->field.le_prev = (elm); \
323 (listelm)->field.le_prev = &(elm)->field.le_next; \
324} while (0)
325
326#define LIST_INSERT_HEAD(head, elm, field) do { \
327 if (((elm)->field.le_next = (head)->lh_first) != NULL) \
328 (head)->lh_first->field.le_prev = &(elm)->field.le_next;\
329 (head)->lh_first = (elm); \
330 (elm)->field.le_prev = &(head)->lh_first; \
331} while (0)
332
333#define LIST_REMOVE(elm, field) do { \
334 if ((elm)->field.le_next != NULL) \
335 (elm)->field.le_next->field.le_prev = \
336 (elm)->field.le_prev; \
337 *(elm)->field.le_prev = (elm)->field.le_next; \
338 _Q_INVALIDATE((elm)->field.le_prev); \
339 _Q_INVALIDATE((elm)->field.le_next); \
340} while (0)
341
342#define LIST_REPLACE(elm, elm2, field) do { \
343 if (((elm2)->field.le_next = (elm)->field.le_next) != NULL) \
344 (elm2)->field.le_next->field.le_prev = \
345 &(elm2)->field.le_next; \
346 (elm2)->field.le_prev = (elm)->field.le_prev; \
347 *(elm2)->field.le_prev = (elm2); \
348 _Q_INVALIDATE((elm)->field.le_prev); \
349 _Q_INVALIDATE((elm)->field.le_next); \
350} while (0)
351
352/*
353 * Simple queue definitions.
354 */
355#define SIMPLEQ_HEAD(name, type) \
356struct name { \
357 struct type *sqh_first; /* first element */ \
358 struct type **sqh_last; /* addr of last next element */ \
359}
360
361#define SIMPLEQ_HEAD_INITIALIZER(head) \
362 { NULL, &(head).sqh_first }
363
364#define SIMPLEQ_ENTRY(type) \
365struct { \
366 struct type *sqe_next; /* next element */ \
367}
368
369/*
370 * Simple queue access methods.
371 */
372#define SIMPLEQ_FIRST(head) ((head)->sqh_first)
373#define SIMPLEQ_END(head) NULL
374#define SIMPLEQ_EMPTY(head) (SIMPLEQ_FIRST(head) == SIMPLEQ_END(head))
375#define SIMPLEQ_NEXT(elm, field) ((elm)->field.sqe_next)
376
377#define SIMPLEQ_FOREACH(var, head, field) \
378 for((var) = SIMPLEQ_FIRST(head); \
379 (var) != SIMPLEQ_END(head); \
380 (var) = SIMPLEQ_NEXT(var, field))
381
382#define SIMPLEQ_FOREACH_SAFE(var, head, field, tvar) \
383 for ((var) = SIMPLEQ_FIRST(head); \
384 (var) && ((tvar) = SIMPLEQ_NEXT(var, field), 1); \
385 (var) = (tvar))
386
387/*
388 * Simple queue functions.
389 */
390#define SIMPLEQ_INIT(head) do { \
391 (head)->sqh_first = NULL; \
392 (head)->sqh_last = &(head)->sqh_first; \
393} while (0)
394
395#define SIMPLEQ_INSERT_HEAD(head, elm, field) do { \
396 if (((elm)->field.sqe_next = (head)->sqh_first) == NULL) \
397 (head)->sqh_last = &(elm)->field.sqe_next; \
398 (head)->sqh_first = (elm); \
399} while (0)
400
401#define SIMPLEQ_INSERT_TAIL(head, elm, field) do { \
402 (elm)->field.sqe_next = NULL; \
403 *(head)->sqh_last = (elm); \
404 (head)->sqh_last = &(elm)->field.sqe_next; \
405} while (0)
406
407#define SIMPLEQ_INSERT_AFTER(head, listelm, elm, field) do { \
408 if (((elm)->field.sqe_next = (listelm)->field.sqe_next) == NULL)\
409 (head)->sqh_last = &(elm)->field.sqe_next; \
410 (listelm)->field.sqe_next = (elm); \
411} while (0)
412
413#define SIMPLEQ_REMOVE_HEAD(head, field) do { \
414 if (((head)->sqh_first = (head)->sqh_first->field.sqe_next) == NULL) \
415 (head)->sqh_last = &(head)->sqh_first; \
416} while (0)
417
418#define SIMPLEQ_REMOVE_AFTER(head, elm, field) do { \
419 if (((elm)->field.sqe_next = (elm)->field.sqe_next->field.sqe_next) \
420 == NULL) \
421 (head)->sqh_last = &(elm)->field.sqe_next; \
422} while (0)
423
424/*
425 * XOR Simple queue definitions.
426 */
427#define XSIMPLEQ_HEAD(name, type) \
428struct name { \
429 struct type *sqx_first; /* first element */ \
430 struct type **sqx_last; /* addr of last next element */ \
431 unsigned long sqx_cookie; \
432}
433
434#define XSIMPLEQ_ENTRY(type) \
435struct { \
436 struct type *sqx_next; /* next element */ \
437}
438
439/*
440 * XOR Simple queue access methods.
441 */
442#define XSIMPLEQ_XOR(head, ptr) ((__typeof(ptr))((head)->sqx_cookie ^ \
443 (unsigned long)(ptr)))
444#define XSIMPLEQ_FIRST(head) XSIMPLEQ_XOR(head, ((head)->sqx_first))
445#define XSIMPLEQ_END(head) NULL
446#define XSIMPLEQ_EMPTY(head) (XSIMPLEQ_FIRST(head) == XSIMPLEQ_END(head))
447#define XSIMPLEQ_NEXT(head, elm, field) XSIMPLEQ_XOR(head, ((elm)->field.sqx_next))
448
449
450#define XSIMPLEQ_FOREACH(var, head, field) \
451 for ((var) = XSIMPLEQ_FIRST(head); \
452 (var) != XSIMPLEQ_END(head); \
453 (var) = XSIMPLEQ_NEXT(head, var, field))
454
455#define XSIMPLEQ_FOREACH_SAFE(var, head, field, tvar) \
456 for ((var) = XSIMPLEQ_FIRST(head); \
457 (var) && ((tvar) = XSIMPLEQ_NEXT(head, var, field), 1); \
458 (var) = (tvar))
459
460/*
461 * XOR Simple queue functions.
462 */
463#define XSIMPLEQ_INIT(head) do { \
464 arc4random_buf(&(head)->sqx_cookie, sizeof((head)->sqx_cookie)); \
465 (head)->sqx_first = XSIMPLEQ_XOR(head, NULL); \
466 (head)->sqx_last = XSIMPLEQ_XOR(head, &(head)->sqx_first); \
467} while (0)
468
469#define XSIMPLEQ_INSERT_HEAD(head, elm, field) do { \
470 if (((elm)->field.sqx_next = (head)->sqx_first) == \
471 XSIMPLEQ_XOR(head, NULL)) \
472 (head)->sqx_last = XSIMPLEQ_XOR(head, &(elm)->field.sqx_next); \
473 (head)->sqx_first = XSIMPLEQ_XOR(head, (elm)); \
474} while (0)
475
476#define XSIMPLEQ_INSERT_TAIL(head, elm, field) do { \
477 (elm)->field.sqx_next = XSIMPLEQ_XOR(head, NULL); \
478 *(XSIMPLEQ_XOR(head, (head)->sqx_last)) = XSIMPLEQ_XOR(head, (elm)); \
479 (head)->sqx_last = XSIMPLEQ_XOR(head, &(elm)->field.sqx_next); \
480} while (0)
481
482#define XSIMPLEQ_INSERT_AFTER(head, listelm, elm, field) do { \
483 if (((elm)->field.sqx_next = (listelm)->field.sqx_next) == \
484 XSIMPLEQ_XOR(head, NULL)) \
485 (head)->sqx_last = XSIMPLEQ_XOR(head, &(elm)->field.sqx_next); \
486 (listelm)->field.sqx_next = XSIMPLEQ_XOR(head, (elm)); \
487} while (0)
488
489#define XSIMPLEQ_REMOVE_HEAD(head, field) do { \
490 if (((head)->sqx_first = XSIMPLEQ_XOR(head, \
491 (head)->sqx_first)->field.sqx_next) == XSIMPLEQ_XOR(head, NULL)) \
492 (head)->sqx_last = XSIMPLEQ_XOR(head, &(head)->sqx_first); \
493} while (0)
494
495#define XSIMPLEQ_REMOVE_AFTER(head, elm, field) do { \
496 if (((elm)->field.sqx_next = XSIMPLEQ_XOR(head, \
497 (elm)->field.sqx_next)->field.sqx_next) \
498 == XSIMPLEQ_XOR(head, NULL)) \
499 (head)->sqx_last = \
500 XSIMPLEQ_XOR(head, &(elm)->field.sqx_next); \
501} while (0)
502
503
504/*
505 * Tail queue definitions.
506 */
507#define TAILQ_HEAD(name, type) \
508struct name { \
509 struct type *tqh_first; /* first element */ \
510 struct type **tqh_last; /* addr of last next element */ \
511}
512
513#define TAILQ_HEAD_INITIALIZER(head) \
514 { NULL, &(head).tqh_first }
515
516#define TAILQ_ENTRY(type) \
517struct { \
518 struct type *tqe_next; /* next element */ \
519 struct type **tqe_prev; /* address of previous next element */ \
520}
521
522/*
523 * tail queue access methods
524 */
525#define TAILQ_FIRST(head) ((head)->tqh_first)
526#define TAILQ_END(head) NULL
527#define TAILQ_NEXT(elm, field) ((elm)->field.tqe_next)
528#define TAILQ_LAST(head, headname) \
529 (*(((struct headname *)((head)->tqh_last))->tqh_last))
530/* XXX */
531#define TAILQ_PREV(elm, headname, field) \
532 (*(((struct headname *)((elm)->field.tqe_prev))->tqh_last))
533#define TAILQ_EMPTY(head) \
534 (TAILQ_FIRST(head) == TAILQ_END(head))
535
536#define TAILQ_FOREACH(var, head, field) \
537 for((var) = TAILQ_FIRST(head); \
538 (var) != TAILQ_END(head); \
539 (var) = TAILQ_NEXT(var, field))
540
541#define TAILQ_FOREACH_SAFE(var, head, field, tvar) \
542 for ((var) = TAILQ_FIRST(head); \
543 (var) != TAILQ_END(head) && \
544 ((tvar) = TAILQ_NEXT(var, field), 1); \
545 (var) = (tvar))
546
547
548#define TAILQ_FOREACH_REVERSE(var, head, headname, field) \
549 for((var) = TAILQ_LAST(head, headname); \
550 (var) != TAILQ_END(head); \
551 (var) = TAILQ_PREV(var, headname, field))
552
553#define TAILQ_FOREACH_REVERSE_SAFE(var, head, headname, field, tvar) \
554 for ((var) = TAILQ_LAST(head, headname); \
555 (var) != TAILQ_END(head) && \
556 ((tvar) = TAILQ_PREV(var, headname, field), 1); \
557 (var) = (tvar))
558
559/*
560 * Tail queue functions.
561 */
562#define TAILQ_INIT(head) do { \
563 (head)->tqh_first = NULL; \
564 (head)->tqh_last = &(head)->tqh_first; \
565} while (0)
566
567#define TAILQ_INSERT_HEAD(head, elm, field) do { \
568 if (((elm)->field.tqe_next = (head)->tqh_first) != NULL) \
569 (head)->tqh_first->field.tqe_prev = \
570 &(elm)->field.tqe_next; \
571 else \
572 (head)->tqh_last = &(elm)->field.tqe_next; \
573 (head)->tqh_first = (elm); \
574 (elm)->field.tqe_prev = &(head)->tqh_first; \
575} while (0)
576
577#define TAILQ_INSERT_TAIL(head, elm, field) do { \
578 (elm)->field.tqe_next = NULL; \
579 (elm)->field.tqe_prev = (head)->tqh_last; \
580 *(head)->tqh_last = (elm); \
581 (head)->tqh_last = &(elm)->field.tqe_next; \
582} while (0)
583
584#define TAILQ_INSERT_AFTER(head, listelm, elm, field) do { \
585 if (((elm)->field.tqe_next = (listelm)->field.tqe_next) != NULL)\
586 (elm)->field.tqe_next->field.tqe_prev = \
587 &(elm)->field.tqe_next; \
588 else \
589 (head)->tqh_last = &(elm)->field.tqe_next; \
590 (listelm)->field.tqe_next = (elm); \
591 (elm)->field.tqe_prev = &(listelm)->field.tqe_next; \
592} while (0)
593
594#define TAILQ_INSERT_BEFORE(listelm, elm, field) do { \
595 (elm)->field.tqe_prev = (listelm)->field.tqe_prev; \
596 (elm)->field.tqe_next = (listelm); \
597 *(listelm)->field.tqe_prev = (elm); \
598 (listelm)->field.tqe_prev = &(elm)->field.tqe_next; \
599} while (0)
600
601#define TAILQ_REMOVE(head, elm, field) do { \
602 if (((elm)->field.tqe_next) != NULL) \
603 (elm)->field.tqe_next->field.tqe_prev = \
604 (elm)->field.tqe_prev; \
605 else \
606 (head)->tqh_last = (elm)->field.tqe_prev; \
607 *(elm)->field.tqe_prev = (elm)->field.tqe_next; \
608 _Q_INVALIDATE((elm)->field.tqe_prev); \
609 _Q_INVALIDATE((elm)->field.tqe_next); \
610} while (0)
611
612#define TAILQ_REPLACE(head, elm, elm2, field) do { \
613 if (((elm2)->field.tqe_next = (elm)->field.tqe_next) != NULL) \
614 (elm2)->field.tqe_next->field.tqe_prev = \
615 &(elm2)->field.tqe_next; \
616 else \
617 (head)->tqh_last = &(elm2)->field.tqe_next; \
618 (elm2)->field.tqe_prev = (elm)->field.tqe_prev; \
619 *(elm2)->field.tqe_prev = (elm2); \
620 _Q_INVALIDATE((elm)->field.tqe_prev); \
621 _Q_INVALIDATE((elm)->field.tqe_next); \
622} while (0)
623
624/*
625 * Circular queue definitions.
626 */
627#define CIRCLEQ_HEAD(name, type) \
628struct name { \
629 struct type *cqh_first; /* first element */ \
630 struct type *cqh_last; /* last element */ \
631}
632
633#define CIRCLEQ_HEAD_INITIALIZER(head) \
634 { CIRCLEQ_END(&head), CIRCLEQ_END(&head) }
635
636#define CIRCLEQ_ENTRY(type) \
637struct { \
638 struct type *cqe_next; /* next element */ \
639 struct type *cqe_prev; /* previous element */ \
640}
641
642/*
643 * Circular queue access methods
644 */
645#define CIRCLEQ_FIRST(head) ((head)->cqh_first)
646#define CIRCLEQ_LAST(head) ((head)->cqh_last)
647#define CIRCLEQ_END(head) ((void *)(head))
648#define CIRCLEQ_NEXT(elm, field) ((elm)->field.cqe_next)
649#define CIRCLEQ_PREV(elm, field) ((elm)->field.cqe_prev)
650#define CIRCLEQ_EMPTY(head) \
651 (CIRCLEQ_FIRST(head) == CIRCLEQ_END(head))
652
653#define CIRCLEQ_FOREACH(var, head, field) \
654 for((var) = CIRCLEQ_FIRST(head); \
655 (var) != CIRCLEQ_END(head); \
656 (var) = CIRCLEQ_NEXT(var, field))
657
658#define CIRCLEQ_FOREACH_SAFE(var, head, field, tvar) \
659 for ((var) = CIRCLEQ_FIRST(head); \
660 (var) != CIRCLEQ_END(head) && \
661 ((tvar) = CIRCLEQ_NEXT(var, field), 1); \
662 (var) = (tvar))
663
664#define CIRCLEQ_FOREACH_REVERSE(var, head, field) \
665 for((var) = CIRCLEQ_LAST(head); \
666 (var) != CIRCLEQ_END(head); \
667 (var) = CIRCLEQ_PREV(var, field))
668
669#define CIRCLEQ_FOREACH_REVERSE_SAFE(var, head, headname, field, tvar) \
670 for ((var) = CIRCLEQ_LAST(head, headname); \
671 (var) != CIRCLEQ_END(head) && \
672 ((tvar) = CIRCLEQ_PREV(var, headname, field), 1); \
673 (var) = (tvar))
674
675/*
676 * Circular queue functions.
677 */
678#define CIRCLEQ_INIT(head) do { \
679 (head)->cqh_first = CIRCLEQ_END(head); \
680 (head)->cqh_last = CIRCLEQ_END(head); \
681} while (0)
682
683#define CIRCLEQ_INSERT_AFTER(head, listelm, elm, field) do { \
684 (elm)->field.cqe_next = (listelm)->field.cqe_next; \
685 (elm)->field.cqe_prev = (listelm); \
686 if ((listelm)->field.cqe_next == CIRCLEQ_END(head)) \
687 (head)->cqh_last = (elm); \
688 else \
689 (listelm)->field.cqe_next->field.cqe_prev = (elm); \
690 (listelm)->field.cqe_next = (elm); \
691} while (0)
692
693#define CIRCLEQ_INSERT_BEFORE(head, listelm, elm, field) do { \
694 (elm)->field.cqe_next = (listelm); \
695 (elm)->field.cqe_prev = (listelm)->field.cqe_prev; \
696 if ((listelm)->field.cqe_prev == CIRCLEQ_END(head)) \
697 (head)->cqh_first = (elm); \
698 else \
699 (listelm)->field.cqe_prev->field.cqe_next = (elm); \
700 (listelm)->field.cqe_prev = (elm); \
701} while (0)
702
703#define CIRCLEQ_INSERT_HEAD(head, elm, field) do { \
704 (elm)->field.cqe_next = (head)->cqh_first; \
705 (elm)->field.cqe_prev = CIRCLEQ_END(head); \
706 if ((head)->cqh_last == CIRCLEQ_END(head)) \
707 (head)->cqh_last = (elm); \
708 else \
709 (head)->cqh_first->field.cqe_prev = (elm); \
710 (head)->cqh_first = (elm); \
711} while (0)
712
713#define CIRCLEQ_INSERT_TAIL(head, elm, field) do { \
714 (elm)->field.cqe_next = CIRCLEQ_END(head); \
715 (elm)->field.cqe_prev = (head)->cqh_last; \
716 if ((head)->cqh_first == CIRCLEQ_END(head)) \
717 (head)->cqh_first = (elm); \
718 else \
719 (head)->cqh_last->field.cqe_next = (elm); \
720 (head)->cqh_last = (elm); \
721} while (0)
722
723#define CIRCLEQ_REMOVE(head, elm, field) do { \
724 if ((elm)->field.cqe_next == CIRCLEQ_END(head)) \
725 (head)->cqh_last = (elm)->field.cqe_prev; \
726 else \
727 (elm)->field.cqe_next->field.cqe_prev = \
728 (elm)->field.cqe_prev; \
729 if ((elm)->field.cqe_prev == CIRCLEQ_END(head)) \
730 (head)->cqh_first = (elm)->field.cqe_next; \
731 else \
732 (elm)->field.cqe_prev->field.cqe_next = \
733 (elm)->field.cqe_next; \
734 _Q_INVALIDATE((elm)->field.cqe_prev); \
735 _Q_INVALIDATE((elm)->field.cqe_next); \
736} while (0)
737
738#define CIRCLEQ_REPLACE(head, elm, elm2, field) do { \
739 if (((elm2)->field.cqe_next = (elm)->field.cqe_next) == \
740 CIRCLEQ_END(head)) \
741 (head)->cqh_last = (elm2); \
742 else \
743 (elm2)->field.cqe_next->field.cqe_prev = (elm2); \
744 if (((elm2)->field.cqe_prev = (elm)->field.cqe_prev) == \
745 CIRCLEQ_END(head)) \
746 (head)->cqh_first = (elm2); \
747 else \
748 (elm2)->field.cqe_prev->field.cqe_next = (elm2); \
749 _Q_INVALIDATE((elm)->field.cqe_prev); \
750 _Q_INVALIDATE((elm)->field.cqe_next); \
751} while (0)
752
753#endif /* !_FAKE_QUEUE_H_ */