NeoMutt  2022-04-29-215-gc12b98
Teaching an old dog new tricks
DOXYGEN
queue.h
Go to the documentation of this file.
1/*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright (c) 1991, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 *
31 * @(#)queue.h 8.5 (Berkeley) 8/20/94
32 * $FreeBSD$
33 */
34
35#ifndef _SYS_QUEUE_H_
36#define _SYS_QUEUE_H_
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 be traversed in either 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 * Below is a summary of implemented functions where:
80 * + means the macro is available
81 * - means the macro is not available
82 * s means the macro is available but is slow (runs in O(n) time)
83 *
84 * SLIST LIST STAILQ TAILQ
85 * _HEAD + + + +
86 * _CLASS_HEAD + + + +
87 * _HEAD_INITIALIZER + + + +
88 * _ENTRY + + + +
89 * _CLASS_ENTRY + + + +
90 * _INIT + + + +
91 * _EMPTY + + + +
92 * _END + + + +
93 * _FIRST + + + +
94 * _NEXT + + + +
95 * _PREV - + - +
96 * _LAST - - + +
97 * _LAST_FAST - - - +
98 * _FOREACH + + + +
99 * _FOREACH_FROM + + + +
100 * _FOREACH_SAFE + + + +
101 * _FOREACH_FROM_SAFE + + + +
102 * _FOREACH_REVERSE - - - +
103 * _FOREACH_REVERSE_FROM - - - +
104 * _FOREACH_REVERSE_SAFE - - - +
105 * _FOREACH_REVERSE_FROM_SAFE - - - +
106 * _INSERT_HEAD + + + +
107 * _INSERT_BEFORE - + - +
108 * _INSERT_AFTER + + + +
109 * _INSERT_TAIL - - + +
110 * _CONCAT s s + +
111 * _REMOVE_AFTER + - + -
112 * _REMOVE_HEAD + - + -
113 * _REMOVE s + s +
114 * _SWAP + + + +
115 *
116 */
117#ifdef QUEUE_MACRO_DEBUG
118#warn Use QUEUE_MACRO_DEBUG_TRACE and/or QUEUE_MACRO_DEBUG_TRASH
119#define QUEUE_MACRO_DEBUG_TRACE
120#define QUEUE_MACRO_DEBUG_TRASH
121#endif
122
123#ifdef QUEUE_MACRO_DEBUG_TRACE
124/* Store the last 2 places the queue element or head was altered */
125struct qm_trace {
126 unsigned long lastline;
127 unsigned long prevline;
128 const char *lastfile;
129 const char *prevfile;
130};
131
132#define TRACEBUF struct qm_trace trace;
133#define TRACEBUF_INITIALIZER { __LINE__, 0, __FILE__, NULL } ,
134
135#define QMD_TRACE_HEAD(head) do { \
136 (head)->trace.prevline = (head)->trace.lastline; \
137 (head)->trace.prevfile = (head)->trace.lastfile; \
138 (head)->trace.lastline = __LINE__; \
139 (head)->trace.lastfile = __FILE__; \
140} while (0)
141
142#define QMD_TRACE_ELEM(elem) do { \
143 (elem)->trace.prevline = (elem)->trace.lastline; \
144 (elem)->trace.prevfile = (elem)->trace.lastfile; \
145 (elem)->trace.lastline = __LINE__; \
146 (elem)->trace.lastfile = __FILE__; \
147} while (0)
148
149#else /* !QUEUE_MACRO_DEBUG_TRACE */
150#define QMD_TRACE_ELEM(elem)
151#define QMD_TRACE_HEAD(head)
152#define TRACEBUF
153#define TRACEBUF_INITIALIZER
154#endif /* QUEUE_MACRO_DEBUG_TRACE */
155
156#ifdef QUEUE_MACRO_DEBUG_TRASH
157#define QMD_SAVELINK(name, link) void **name = (void *)&(link)
158#define TRASHIT(x) do {(x) = (void *)-1;} while (0)
159#define QMD_IS_TRASHED(x) ((x) == (void *)(intptr_t)-1)
160#else /* !QUEUE_MACRO_DEBUG_TRASH */
161#define QMD_SAVELINK(name, link)
162#define TRASHIT(x)
163#define QMD_IS_TRASHED(x) 0
164#endif /* QUEUE_MACRO_DEBUG_TRASH */
165
166#ifdef __cplusplus
167/*
168 * In C++ there can be structure lists and class lists:
169 */
170#define QUEUE_TYPEOF(type) type
171#else
172#define QUEUE_TYPEOF(type) struct type
173#endif
174
175/*
176 * Singly-linked List declarations.
177 */
178#define SLIST_HEAD(name, type) \
179struct name { \
180 struct type *slh_first; /* first element */ \
181}
182
183#define SLIST_CLASS_HEAD(name, type) \
184struct name { \
185 class type *slh_first; /* first element */ \
186}
187
188#define SLIST_HEAD_INITIALIZER(head) \
189 { NULL }
190
191#define SLIST_ENTRY(type) \
192struct { \
193 struct type *sle_next; /* next element */ \
194}
195
196#define SLIST_CLASS_ENTRY(type) \
197struct { \
198 class type *sle_next; /* next element */ \
199}
200
201/*
202 * Singly-linked List functions.
203 */
204#if (defined(_KERNEL) && defined(INVARIANTS))
205#define QMD_SLIST_CHECK_PREVPTR(prevp, elm) do { \
206 if (*(prevp) != (elm)) \
207 panic("Bad prevptr *(%p) == %p != %p", \
208 (prevp), *(prevp), (elm)); \
209} while (0)
210#else
211#define QMD_SLIST_CHECK_PREVPTR(prevp, elm)
212#endif
213
214#define SLIST_CONCAT(head1, head2, type, field) do { \
215 QUEUE_TYPEOF(type) *curelm = SLIST_FIRST(head1); \
216 if (curelm == NULL) { \
217 if ((SLIST_FIRST(head1) = SLIST_FIRST(head2)) != NULL) \
218 SLIST_INIT(head2); \
219 } else if (SLIST_FIRST(head2) != NULL) { \
220 while (SLIST_NEXT(curelm, field) != NULL) \
221 curelm = SLIST_NEXT(curelm, field); \
222 SLIST_NEXT(curelm, field) = SLIST_FIRST(head2); \
223 SLIST_INIT(head2); \
224 } \
225} while (0)
226
227#define SLIST_EMPTY(head) ((head)->slh_first == NULL)
228
229#define SLIST_FIRST(head) ((head)->slh_first)
230
231#define SLIST_FOREACH(var, head, field) \
232 for ((var) = SLIST_FIRST((head)); \
233 (var); \
234 (var) = SLIST_NEXT((var), field))
235
236#define SLIST_FOREACH_FROM(var, head, field) \
237 for ((var) = ((var) ? (var) : SLIST_FIRST((head))); \
238 (var); \
239 (var) = SLIST_NEXT((var), field))
240
241#define SLIST_FOREACH_SAFE(var, head, field, tvar) \
242 for ((var) = SLIST_FIRST((head)); \
243 (var) && ((tvar) = SLIST_NEXT((var), field), 1); \
244 (var) = (tvar))
245
246#define SLIST_FOREACH_FROM_SAFE(var, head, field, tvar) \
247 for ((var) = ((var) ? (var) : SLIST_FIRST((head))); \
248 (var) && ((tvar) = SLIST_NEXT((var), field), 1); \
249 (var) = (tvar))
250
251#define SLIST_FOREACH_PREVPTR(var, varp, head, field) \
252 for ((varp) = &SLIST_FIRST((head)); \
253 ((var) = *(varp)) != NULL; \
254 (varp) = &SLIST_NEXT((var), field))
255
256#define SLIST_INIT(head) do { \
257 SLIST_FIRST((head)) = NULL; \
258} while (0)
259
260#define SLIST_INSERT_AFTER(slistelm, elm, field) do { \
261 SLIST_NEXT((elm), field) = SLIST_NEXT((slistelm), field); \
262 SLIST_NEXT((slistelm), field) = (elm); \
263} while (0)
264
265#define SLIST_INSERT_HEAD(head, elm, field) do { \
266 SLIST_NEXT((elm), field) = SLIST_FIRST((head)); \
267 SLIST_FIRST((head)) = (elm); \
268} while (0)
269
270#define SLIST_NEXT(elm, field) ((elm)->field.sle_next)
271
272#define SLIST_REMOVE(head, elm, type, field) do { \
273 QMD_SAVELINK(oldnext, (elm)->field.sle_next); \
274 if (SLIST_FIRST((head)) == (elm)) { \
275 SLIST_REMOVE_HEAD((head), field); \
276 } \
277 else { \
278 QUEUE_TYPEOF(type) *curelm = SLIST_FIRST(head); \
279 while (SLIST_NEXT(curelm, field) != (elm)) \
280 curelm = SLIST_NEXT(curelm, field); \
281 SLIST_REMOVE_AFTER(curelm, field); \
282 } \
283 TRASHIT(*oldnext); \
284} while (0)
285
286#define SLIST_REMOVE_AFTER(elm, field) do { \
287 SLIST_NEXT(elm, field) = \
288 SLIST_NEXT(SLIST_NEXT(elm, field), field); \
289} while (0)
290
291#define SLIST_REMOVE_HEAD(head, field) do { \
292 SLIST_FIRST((head)) = SLIST_NEXT(SLIST_FIRST((head)), field); \
293} while (0)
294
295#define SLIST_REMOVE_PREVPTR(prevp, elm, field) do { \
296 QMD_SLIST_CHECK_PREVPTR(prevp, elm); \
297 *(prevp) = SLIST_NEXT(elm, field); \
298 TRASHIT((elm)->field.sle_next); \
299} while (0)
300
301#define SLIST_SWAP(head1, head2, type) do { \
302 QUEUE_TYPEOF(type) *swap_first = SLIST_FIRST(head1); \
303 SLIST_FIRST(head1) = SLIST_FIRST(head2); \
304 SLIST_FIRST(head2) = swap_first; \
305} while (0)
306
307#define SLIST_END(head) NULL
308
309/*
310 * Singly-linked Tail queue declarations.
311 */
312#define STAILQ_HEAD(name, type) \
313struct name { \
314 struct type *stqh_first;/* first element */ \
315 struct type **stqh_last;/* addr of last next element */ \
316}
317
318#define STAILQ_CLASS_HEAD(name, type) \
319struct name { \
320 class type *stqh_first; /* first element */ \
321 class type **stqh_last; /* addr of last next element */ \
322}
323
324#define STAILQ_HEAD_INITIALIZER(head) \
325 { NULL, &(head).stqh_first }
326
327#define STAILQ_ENTRY(type) \
328struct { \
329 struct type *stqe_next; /* next element */ \
330}
331
332#define STAILQ_CLASS_ENTRY(type) \
333struct { \
334 class type *stqe_next; /* next element */ \
335}
336
337/*
338 * Singly-linked Tail queue functions.
339 */
340#define STAILQ_CONCAT(head1, head2) do { \
341 if (!STAILQ_EMPTY((head2))) { \
342 *(head1)->stqh_last = (head2)->stqh_first; \
343 (head1)->stqh_last = (head2)->stqh_last; \
344 STAILQ_INIT((head2)); \
345 } \
346} while (0)
347
348#define STAILQ_EMPTY(head) ((head)->stqh_first == NULL)
349
350#define STAILQ_FIRST(head) ((head)->stqh_first)
351
352#define STAILQ_FOREACH(var, head, field) \
353 for((var) = STAILQ_FIRST((head)); \
354 (var); \
355 (var) = STAILQ_NEXT((var), field))
356
357#define STAILQ_FOREACH_FROM(var, head, field) \
358 for ((var) = ((var) ? (var) : STAILQ_FIRST((head))); \
359 (var); \
360 (var) = STAILQ_NEXT((var), field))
361
362#define STAILQ_FOREACH_SAFE(var, head, field, tvar) \
363 for ((var) = STAILQ_FIRST((head)); \
364 (var) && ((tvar) = STAILQ_NEXT((var), field), 1); \
365 (var) = (tvar))
366
367#define STAILQ_FOREACH_FROM_SAFE(var, head, field, tvar) \
368 for ((var) = ((var) ? (var) : STAILQ_FIRST((head))); \
369 (var) && ((tvar) = STAILQ_NEXT((var), field), 1); \
370 (var) = (tvar))
371
372#define STAILQ_INIT(head) do { \
373 STAILQ_FIRST((head)) = NULL; \
374 (head)->stqh_last = &STAILQ_FIRST((head)); \
375} while (0)
376
377#define STAILQ_INSERT_AFTER(head, tqelm, elm, field) do { \
378 if ((STAILQ_NEXT((elm), field) = STAILQ_NEXT((tqelm), field)) == NULL)\
379 (head)->stqh_last = &STAILQ_NEXT((elm), field); \
380 STAILQ_NEXT((tqelm), field) = (elm); \
381} while (0)
382
383#define STAILQ_INSERT_HEAD(head, elm, field) do { \
384 if ((STAILQ_NEXT((elm), field) = STAILQ_FIRST((head))) == NULL) \
385 (head)->stqh_last = &STAILQ_NEXT((elm), field); \
386 STAILQ_FIRST((head)) = (elm); \
387} while (0)
388
389#define STAILQ_INSERT_TAIL(head, elm, field) do { \
390 STAILQ_NEXT((elm), field) = NULL; \
391 *(head)->stqh_last = (elm); \
392 (head)->stqh_last = &STAILQ_NEXT((elm), field); \
393} while (0)
394
395#define STAILQ_LAST(head, type, field) \
396 (STAILQ_EMPTY((head)) ? NULL : \
397 __containerof((head)->stqh_last, \
398 QUEUE_TYPEOF(type), field.stqe_next))
399
400#define STAILQ_NEXT(elm, field) ((elm)->field.stqe_next)
401
402#define STAILQ_REMOVE(head, elm, type, field) do { \
403 QMD_SAVELINK(oldnext, (elm)->field.stqe_next); \
404 if (STAILQ_FIRST((head)) == (elm)) { \
405 STAILQ_REMOVE_HEAD((head), field); \
406 } \
407 else { \
408 QUEUE_TYPEOF(type) *curelm = STAILQ_FIRST(head); \
409 while (STAILQ_NEXT(curelm, field) != (elm)) \
410 curelm = STAILQ_NEXT(curelm, field); \
411 STAILQ_REMOVE_AFTER(head, curelm, field); \
412 } \
413 TRASHIT(*oldnext); \
414} while (0)
415
416#define STAILQ_REMOVE_AFTER(head, elm, field) do { \
417 if ((STAILQ_NEXT(elm, field) = \
418 STAILQ_NEXT(STAILQ_NEXT(elm, field), field)) == NULL) \
419 (head)->stqh_last = &STAILQ_NEXT((elm), field); \
420} while (0)
421
422#define STAILQ_REMOVE_HEAD(head, field) do { \
423 if ((STAILQ_FIRST((head)) = \
424 STAILQ_NEXT(STAILQ_FIRST((head)), field)) == NULL) \
425 (head)->stqh_last = &STAILQ_FIRST((head)); \
426} while (0)
427
428#define STAILQ_SWAP(head1, head2, type) do { \
429 QUEUE_TYPEOF(type) *swap_first = STAILQ_FIRST(head1); \
430 QUEUE_TYPEOF(type) **swap_last = (head1)->stqh_last; \
431 STAILQ_FIRST(head1) = STAILQ_FIRST(head2); \
432 (head1)->stqh_last = (head2)->stqh_last; \
433 STAILQ_FIRST(head2) = swap_first; \
434 (head2)->stqh_last = swap_last; \
435 if (STAILQ_EMPTY(head1)) \
436 (head1)->stqh_last = &STAILQ_FIRST(head1); \
437 if (STAILQ_EMPTY(head2)) \
438 (head2)->stqh_last = &STAILQ_FIRST(head2); \
439} while (0)
440
441#define STAILQ_END(head) NULL
442
443
444/*
445 * List declarations.
446 */
447#define LIST_HEAD(name, type) \
448struct name { \
449 struct type *lh_first; /* first element */ \
450}
451
452#define LIST_CLASS_HEAD(name, type) \
453struct name { \
454 class type *lh_first; /* first element */ \
455}
456
457#define LIST_HEAD_INITIALIZER(head) \
458 { NULL }
459
460#define LIST_ENTRY(type) \
461struct { \
462 struct type *le_next; /* next element */ \
463 struct type **le_prev; /* address of previous next element */ \
464}
465
466#define LIST_CLASS_ENTRY(type) \
467struct { \
468 class type *le_next; /* next element */ \
469 class type **le_prev; /* address of previous next element */ \
470}
471
472/*
473 * List functions.
474 */
475
476#if (defined(_KERNEL) && defined(INVARIANTS))
477/*
478 * QMD_LIST_CHECK_HEAD(LIST_HEAD *head, LIST_ENTRY NAME)
479 *
480 * If the list is non-empty, validates that the first element of the list
481 * points back at 'head.'
482 */
483#define QMD_LIST_CHECK_HEAD(head, field) do { \
484 if (LIST_FIRST((head)) != NULL && \
485 LIST_FIRST((head))->field.le_prev != \
486 &LIST_FIRST((head))) \
487 panic("Bad list head %p first->prev != head", (head)); \
488} while (0)
489
490/*
491 * QMD_LIST_CHECK_NEXT(TYPE *elm, LIST_ENTRY NAME)
492 *
493 * If an element follows 'elm' in the list, validates that the next element
494 * points back at 'elm.'
495 */
496#define QMD_LIST_CHECK_NEXT(elm, field) do { \
497 if (LIST_NEXT((elm), field) != NULL && \
498 LIST_NEXT((elm), field)->field.le_prev != \
499 &((elm)->field.le_next)) \
500 panic("Bad link elm %p next->prev != elm", (elm)); \
501} while (0)
502
503/*
504 * QMD_LIST_CHECK_PREV(TYPE *elm, LIST_ENTRY NAME)
505 *
506 * Validates that the previous element (or head of the list) points to 'elm.'
507 */
508#define QMD_LIST_CHECK_PREV(elm, field) do { \
509 if (*(elm)->field.le_prev != (elm)) \
510 panic("Bad link elm %p prev->next != elm", (elm)); \
511} while (0)
512#else
513#define QMD_LIST_CHECK_HEAD(head, field)
514#define QMD_LIST_CHECK_NEXT(elm, field)
515#define QMD_LIST_CHECK_PREV(elm, field)
516#endif /* (_KERNEL && INVARIANTS) */
517
518#define LIST_CONCAT(head1, head2, type, field) do { \
519 QUEUE_TYPEOF(type) *curelm = LIST_FIRST(head1); \
520 if (curelm == NULL) { \
521 if ((LIST_FIRST(head1) = LIST_FIRST(head2)) != NULL) { \
522 LIST_FIRST(head2)->field.le_prev = \
523 &LIST_FIRST((head1)); \
524 LIST_INIT(head2); \
525 } \
526 } else if (LIST_FIRST(head2) != NULL) { \
527 while (LIST_NEXT(curelm, field) != NULL) \
528 curelm = LIST_NEXT(curelm, field); \
529 LIST_NEXT(curelm, field) = LIST_FIRST(head2); \
530 LIST_FIRST(head2)->field.le_prev = &LIST_NEXT(curelm, field); \
531 LIST_INIT(head2); \
532 } \
533} while (0)
534
535#define LIST_EMPTY(head) ((head)->lh_first == NULL)
536
537#define LIST_FIRST(head) ((head)->lh_first)
538
539#define LIST_FOREACH(var, head, field) \
540 for ((var) = LIST_FIRST((head)); \
541 (var); \
542 (var) = LIST_NEXT((var), field))
543
544#define LIST_FOREACH_FROM(var, head, field) \
545 for ((var) = ((var) ? (var) : LIST_FIRST((head))); \
546 (var); \
547 (var) = LIST_NEXT((var), field))
548
549#define LIST_FOREACH_SAFE(var, head, field, tvar) \
550 for ((var) = LIST_FIRST((head)); \
551 (var) && ((tvar) = LIST_NEXT((var), field), 1); \
552 (var) = (tvar))
553
554#define LIST_FOREACH_FROM_SAFE(var, head, field, tvar) \
555 for ((var) = ((var) ? (var) : LIST_FIRST((head))); \
556 (var) && ((tvar) = LIST_NEXT((var), field), 1); \
557 (var) = (tvar))
558
559#define LIST_INIT(head) do { \
560 LIST_FIRST((head)) = NULL; \
561} while (0)
562
563#define LIST_INSERT_AFTER(listelm, elm, field) do { \
564 QMD_LIST_CHECK_NEXT(listelm, field); \
565 if ((LIST_NEXT((elm), field) = LIST_NEXT((listelm), field)) != NULL)\
566 LIST_NEXT((listelm), field)->field.le_prev = \
567 &LIST_NEXT((elm), field); \
568 LIST_NEXT((listelm), field) = (elm); \
569 (elm)->field.le_prev = &LIST_NEXT((listelm), field); \
570} while (0)
571
572#define LIST_INSERT_BEFORE(listelm, elm, field) do { \
573 QMD_LIST_CHECK_PREV(listelm, field); \
574 (elm)->field.le_prev = (listelm)->field.le_prev; \
575 LIST_NEXT((elm), field) = (listelm); \
576 *(listelm)->field.le_prev = (elm); \
577 (listelm)->field.le_prev = &LIST_NEXT((elm), field); \
578} while (0)
579
580#define LIST_INSERT_HEAD(head, elm, field) do { \
581 QMD_LIST_CHECK_HEAD((head), field); \
582 if ((LIST_NEXT((elm), field) = LIST_FIRST((head))) != NULL) \
583 LIST_FIRST((head))->field.le_prev = &LIST_NEXT((elm), field);\
584 LIST_FIRST((head)) = (elm); \
585 (elm)->field.le_prev = &LIST_FIRST((head)); \
586} while (0)
587
588#define LIST_NEXT(elm, field) ((elm)->field.le_next)
589
590#define LIST_PREV(elm, head, type, field) \
591 ((elm)->field.le_prev == &LIST_FIRST((head)) ? NULL : \
592 __containerof((elm)->field.le_prev, \
593 QUEUE_TYPEOF(type), field.le_next))
594
595#define LIST_REMOVE(elm, field) do { \
596 QMD_SAVELINK(oldnext, (elm)->field.le_next); \
597 QMD_SAVELINK(oldprev, (elm)->field.le_prev); \
598 QMD_LIST_CHECK_NEXT(elm, field); \
599 QMD_LIST_CHECK_PREV(elm, field); \
600 if (LIST_NEXT((elm), field) != NULL) \
601 LIST_NEXT((elm), field)->field.le_prev = \
602 (elm)->field.le_prev; \
603 *(elm)->field.le_prev = LIST_NEXT((elm), field); \
604 TRASHIT(*oldnext); \
605 TRASHIT(*oldprev); \
606} while (0)
607
608#define LIST_SWAP(head1, head2, type, field) do { \
609 QUEUE_TYPEOF(type) *swap_tmp = LIST_FIRST(head1); \
610 LIST_FIRST((head1)) = LIST_FIRST((head2)); \
611 LIST_FIRST((head2)) = swap_tmp; \
612 if ((swap_tmp = LIST_FIRST((head1))) != NULL) \
613 swap_tmp->field.le_prev = &LIST_FIRST((head1)); \
614 if ((swap_tmp = LIST_FIRST((head2))) != NULL) \
615 swap_tmp->field.le_prev = &LIST_FIRST((head2)); \
616} while (0)
617
618#define LIST_END(head) NULL
619
620/*
621 * Tail queue declarations.
622 */
623#define TAILQ_HEAD(name, type) \
624struct name { \
625 struct type *tqh_first; /* first element */ \
626 struct type **tqh_last; /* addr of last next element */ \
627 TRACEBUF \
628}
629
630#define TAILQ_CLASS_HEAD(name, type) \
631struct name { \
632 class type *tqh_first; /* first element */ \
633 class type **tqh_last; /* addr of last next element */ \
634 TRACEBUF \
635}
636
637#define TAILQ_HEAD_INITIALIZER(head) \
638 { NULL, &(head).tqh_first, TRACEBUF_INITIALIZER }
639
640#define TAILQ_ENTRY(type) \
641struct { \
642 struct type *tqe_next; /* next element */ \
643 struct type **tqe_prev; /* address of previous next element */ \
644 TRACEBUF \
645}
646
647#define TAILQ_CLASS_ENTRY(type) \
648struct { \
649 class type *tqe_next; /* next element */ \
650 class type **tqe_prev; /* address of previous next element */ \
651 TRACEBUF \
652}
653
654/*
655 * Tail queue functions.
656 */
657#if (defined(_KERNEL) && defined(INVARIANTS))
658/*
659 * QMD_TAILQ_CHECK_HEAD(TAILQ_HEAD *head, TAILQ_ENTRY NAME)
660 *
661 * If the tailq is non-empty, validates that the first element of the tailq
662 * points back at 'head.'
663 */
664#define QMD_TAILQ_CHECK_HEAD(head, field) do { \
665 if (!TAILQ_EMPTY(head) && \
666 TAILQ_FIRST((head))->field.tqe_prev != \
667 &TAILQ_FIRST((head))) \
668 panic("Bad tailq head %p first->prev != head", (head)); \
669} while (0)
670
671/*
672 * QMD_TAILQ_CHECK_TAIL(TAILQ_HEAD *head, TAILQ_ENTRY NAME)
673 *
674 * Validates that the tail of the tailq is a pointer to pointer to NULL.
675 */
676#define QMD_TAILQ_CHECK_TAIL(head, field) do { \
677 if (*(head)->tqh_last != NULL) \
678 panic("Bad tailq NEXT(%p->tqh_last) != NULL", (head)); \
679} while (0)
680
681/*
682 * QMD_TAILQ_CHECK_NEXT(TYPE *elm, TAILQ_ENTRY NAME)
683 *
684 * If an element follows 'elm' in the tailq, validates that the next element
685 * points back at 'elm.'
686 */
687#define QMD_TAILQ_CHECK_NEXT(elm, field) do { \
688 if (TAILQ_NEXT((elm), field) != NULL && \
689 TAILQ_NEXT((elm), field)->field.tqe_prev != \
690 &((elm)->field.tqe_next)) \
691 panic("Bad link elm %p next->prev != elm", (elm)); \
692} while (0)
693
694/*
695 * QMD_TAILQ_CHECK_PREV(TYPE *elm, TAILQ_ENTRY NAME)
696 *
697 * Validates that the previous element (or head of the tailq) points to 'elm.'
698 */
699#define QMD_TAILQ_CHECK_PREV(elm, field) do { \
700 if (*(elm)->field.tqe_prev != (elm)) \
701 panic("Bad link elm %p prev->next != elm", (elm)); \
702} while (0)
703#else
704#define QMD_TAILQ_CHECK_HEAD(head, field)
705#define QMD_TAILQ_CHECK_TAIL(head, headname)
706#define QMD_TAILQ_CHECK_NEXT(elm, field)
707#define QMD_TAILQ_CHECK_PREV(elm, field)
708#endif /* (_KERNEL && INVARIANTS) */
709
710#define TAILQ_CONCAT(head1, head2, field) do { \
711 if (!TAILQ_EMPTY(head2)) { \
712 *(head1)->tqh_last = (head2)->tqh_first; \
713 (head2)->tqh_first->field.tqe_prev = (head1)->tqh_last; \
714 (head1)->tqh_last = (head2)->tqh_last; \
715 TAILQ_INIT((head2)); \
716 QMD_TRACE_HEAD(head1); \
717 QMD_TRACE_HEAD(head2); \
718 } \
719} while (0)
720
721#define TAILQ_EMPTY(head) ((head)->tqh_first == NULL)
722
723#define TAILQ_FIRST(head) ((head)->tqh_first)
724
725#define TAILQ_FOREACH(var, head, field) \
726 for ((var) = TAILQ_FIRST((head)); \
727 (var); \
728 (var) = TAILQ_NEXT((var), field))
729
730#define TAILQ_FOREACH_FROM(var, head, field) \
731 for ((var) = ((var) ? (var) : TAILQ_FIRST((head))); \
732 (var); \
733 (var) = TAILQ_NEXT((var), field))
734
735#define TAILQ_FOREACH_SAFE(var, head, field, tvar) \
736 for ((var) = TAILQ_FIRST((head)); \
737 (var) && ((tvar) = TAILQ_NEXT((var), field), 1); \
738 (var) = (tvar))
739
740#define TAILQ_FOREACH_FROM_SAFE(var, head, field, tvar) \
741 for ((var) = ((var) ? (var) : TAILQ_FIRST((head))); \
742 (var) && ((tvar) = TAILQ_NEXT((var), field), 1); \
743 (var) = (tvar))
744
745#define TAILQ_FOREACH_REVERSE(var, head, headname, field) \
746 for ((var) = TAILQ_LAST((head), headname); \
747 (var); \
748 (var) = TAILQ_PREV((var), headname, field))
749
750#define TAILQ_FOREACH_REVERSE_FROM(var, head, headname, field) \
751 for ((var) = ((var) ? (var) : TAILQ_LAST((head), headname)); \
752 (var); \
753 (var) = TAILQ_PREV((var), headname, field))
754
755#define TAILQ_FOREACH_REVERSE_SAFE(var, head, headname, field, tvar) \
756 for ((var) = TAILQ_LAST((head), headname); \
757 (var) && ((tvar) = TAILQ_PREV((var), headname, field), 1); \
758 (var) = (tvar))
759
760#define TAILQ_FOREACH_REVERSE_FROM_SAFE(var, head, headname, field, tvar) \
761 for ((var) = ((var) ? (var) : TAILQ_LAST((head), headname)); \
762 (var) && ((tvar) = TAILQ_PREV((var), headname, field), 1); \
763 (var) = (tvar))
764
765#define TAILQ_INIT(head) do { \
766 TAILQ_FIRST((head)) = NULL; \
767 (head)->tqh_last = &TAILQ_FIRST((head)); \
768 QMD_TRACE_HEAD(head); \
769} while (0)
770
771#define TAILQ_INSERT_AFTER(head, listelm, elm, field) do { \
772 QMD_TAILQ_CHECK_NEXT(listelm, field); \
773 if ((TAILQ_NEXT((elm), field) = TAILQ_NEXT((listelm), field)) != NULL)\
774 TAILQ_NEXT((elm), field)->field.tqe_prev = \
775 &TAILQ_NEXT((elm), field); \
776 else { \
777 (head)->tqh_last = &TAILQ_NEXT((elm), field); \
778 QMD_TRACE_HEAD(head); \
779 } \
780 TAILQ_NEXT((listelm), field) = (elm); \
781 (elm)->field.tqe_prev = &TAILQ_NEXT((listelm), field); \
782 QMD_TRACE_ELEM(&(elm)->field); \
783 QMD_TRACE_ELEM(&(listelm)->field); \
784} while (0)
785
786#define TAILQ_INSERT_BEFORE(listelm, elm, field) do { \
787 QMD_TAILQ_CHECK_PREV(listelm, field); \
788 (elm)->field.tqe_prev = (listelm)->field.tqe_prev; \
789 TAILQ_NEXT((elm), field) = (listelm); \
790 *(listelm)->field.tqe_prev = (elm); \
791 (listelm)->field.tqe_prev = &TAILQ_NEXT((elm), field); \
792 QMD_TRACE_ELEM(&(elm)->field); \
793 QMD_TRACE_ELEM(&(listelm)->field); \
794} while (0)
795
796#define TAILQ_INSERT_HEAD(head, elm, field) do { \
797 QMD_TAILQ_CHECK_HEAD(head, field); \
798 if ((TAILQ_NEXT((elm), field) = TAILQ_FIRST((head))) != NULL) \
799 TAILQ_FIRST((head))->field.tqe_prev = \
800 &TAILQ_NEXT((elm), field); \
801 else \
802 (head)->tqh_last = &TAILQ_NEXT((elm), field); \
803 TAILQ_FIRST((head)) = (elm); \
804 (elm)->field.tqe_prev = &TAILQ_FIRST((head)); \
805 QMD_TRACE_HEAD(head); \
806 QMD_TRACE_ELEM(&(elm)->field); \
807} while (0)
808
809#define TAILQ_INSERT_TAIL(head, elm, field) do { \
810 QMD_TAILQ_CHECK_TAIL(head, field); \
811 TAILQ_NEXT((elm), field) = NULL; \
812 (elm)->field.tqe_prev = (head)->tqh_last; \
813 *(head)->tqh_last = (elm); \
814 (head)->tqh_last = &TAILQ_NEXT((elm), field); \
815 QMD_TRACE_HEAD(head); \
816 QMD_TRACE_ELEM(&(elm)->field); \
817} while (0)
818
819#define TAILQ_LAST(head, headname) \
820 (*(((struct headname *)((head)->tqh_last))->tqh_last))
821
822/*
823 * The FAST function is fast in that it causes no data access other
824 * then the access to the head. The standard LAST function above
825 * will cause a data access of both the element you want and
826 * the previous element. FAST is very useful for instances when
827 * you may want to prefetch the last data element.
828 */
829#define TAILQ_LAST_FAST(head, type, field) \
830 (TAILQ_EMPTY(head) ? NULL : __containerof((head)->tqh_last, QUEUE_TYPEOF(type), field.tqe_next))
831
832#define TAILQ_NEXT(elm, field) ((elm)->field.tqe_next)
833
834#define TAILQ_PREV(elm, headname, field) \
835 (*(((struct headname *)((elm)->field.tqe_prev))->tqh_last))
836
837#define TAILQ_PREV_FAST(elm, head, type, field) \
838 ((elm)->field.tqe_prev == &(head)->tqh_first ? NULL : \
839 __containerof((elm)->field.tqe_prev, QUEUE_TYPEOF(type), field.tqe_next))
840
841#define TAILQ_REMOVE(head, elm, field) do { \
842 QMD_SAVELINK(oldnext, (elm)->field.tqe_next); \
843 QMD_SAVELINK(oldprev, (elm)->field.tqe_prev); \
844 QMD_TAILQ_CHECK_NEXT(elm, field); \
845 QMD_TAILQ_CHECK_PREV(elm, field); \
846 if ((TAILQ_NEXT((elm), field)) != NULL) \
847 TAILQ_NEXT((elm), field)->field.tqe_prev = \
848 (elm)->field.tqe_prev; \
849 else { \
850 (head)->tqh_last = (elm)->field.tqe_prev; \
851 QMD_TRACE_HEAD(head); \
852 } \
853 *(elm)->field.tqe_prev = TAILQ_NEXT((elm), field); \
854 TRASHIT(*oldnext); \
855 TRASHIT(*oldprev); \
856 QMD_TRACE_ELEM(&(elm)->field); \
857} while (0)
858
859#define TAILQ_SWAP(head1, head2, type, field) do { \
860 QUEUE_TYPEOF(type) *swap_first = (head1)->tqh_first; \
861 QUEUE_TYPEOF(type) **swap_last = (head1)->tqh_last; \
862 (head1)->tqh_first = (head2)->tqh_first; \
863 (head1)->tqh_last = (head2)->tqh_last; \
864 (head2)->tqh_first = swap_first; \
865 (head2)->tqh_last = swap_last; \
866 if ((swap_first = (head1)->tqh_first) != NULL) \
867 swap_first->field.tqe_prev = &(head1)->tqh_first; \
868 else \
869 (head1)->tqh_last = &(head1)->tqh_first; \
870 if ((swap_first = (head2)->tqh_first) != NULL) \
871 swap_first->field.tqe_prev = &(head2)->tqh_first; \
872 else \
873 (head2)->tqh_last = &(head2)->tqh_first; \
874} while (0)
875
876#define TAILQ_END(head) NULL
877
878#endif /* !_SYS_QUEUE_H_ */