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/* 40 * This file defines four types of data structures: singly-linked lists, 41 * singly-linked tail queues, lists and tail queues. 42 * 43 * A singly-linked list is headed by a single forward pointer. The elements 44 * are singly linked for minimum space and pointer manipulation overhead at 45 * the expense of O(n) removal for arbitrary elements. New elements can be 46 * added to the list after an existing element or at the head of the list. 47 * Elements being removed from the head of the list should use the explicit 48 * macro for this purpose for optimum efficiency. A singly-linked list may 49 * only be traversed in the forward direction. Singly-linked lists are ideal 50 * for applications with large datasets and few or no removals or for 51 * implementing a LIFO queue. 52 * 53 * A singly-linked tail queue is headed by a pair of pointers, one to the 54 * head of the list and the other to the tail of the list. The elements are 55 * singly linked for minimum space and pointer manipulation overhead at the 56 * expense of O(n) removal for arbitrary elements. New elements can be added 57 * to the list after an existing element, at the head of the list, or at the 58 * end of the list. Elements being removed from the head of the tail queue 59 * should use the explicit macro for this purpose for optimum efficiency. 60 * A singly-linked tail queue may only be traversed in the forward direction. 61 * Singly-linked tail queues are ideal for applications with large datasets 62 * and few or no removals or for implementing a FIFO queue. 63 * 64 * A list is headed by a single forward pointer (or an array of forward 65 * pointers for a hash table header). The elements are doubly linked 66 * so that an arbitrary element can be removed without a need to 67 * traverse the list. New elements can be added to the list before 68 * or after an existing element or at the head of the list. A list 69 * may be traversed in either direction. 70 * 71 * A tail queue is headed by a pair of pointers, one to the head of the 72 * list and the other to the tail of the list. The elements are doubly 73 * linked so that an arbitrary element can be removed without a need to 74 * traverse the list. New elements can be added to the list before or 75 * after an existing element, at the head of the list, or at the end of 76 * the list. A tail queue may be traversed in either direction. 77 * 78 * For details on the use of these macros, see the queue(3) manual page. 79 * 80 * Below is a summary of implemented functions where: 81 * + means the macro is available 82 * - means the macro is not available 83 * s means the macro is available but is slow (runs in O(n) time) 84 * 85 * SLIST LIST STAILQ TAILQ 86 * _HEAD + + + + 87 * _CLASS_HEAD + + + + 88 * _HEAD_INITIALIZER + + + + 89 * _ENTRY + + + + 90 * _CLASS_ENTRY + + + + 91 * _INIT + + + + 92 * _EMPTY + + + + 93 * _FIRST + + + + 94 * _NEXT + + + + 95 * _PREV - + - + 96 * _LAST - - + + 97 * _FOREACH + + + + 98 * _FOREACH_FROM + + + + 99 * _FOREACH_SAFE + + + + 100 * _FOREACH_FROM_SAFE + + + + 101 * _FOREACH_REVERSE - - - + 102 * _FOREACH_REVERSE_FROM - - - + 103 * _FOREACH_REVERSE_SAFE - - - + 104 * _FOREACH_REVERSE_FROM_SAFE - - - + 105 * _INSERT_HEAD + + + + 106 * _INSERT_BEFORE - + - + 107 * _INSERT_AFTER + + + + 108 * _INSERT_TAIL - - + + 109 * _CONCAT s s + + 110 * _REMOVE_AFTER + - + - 111 * _REMOVE_HEAD + - + - 112 * _REMOVE s + s + 113 * _SWAP + + + + 114 * 115 */ 116#ifdef QUEUE_MACRO_DEBUG 117#warn Use QUEUE_MACRO_DEBUG_TRACE and/or QUEUE_MACRO_DEBUG_TRASH 118#define QUEUE_MACRO_DEBUG_TRACE 119#define QUEUE_MACRO_DEBUG_TRASH 120#endif 121 122#ifdef QUEUE_MACRO_DEBUG_TRACE 123/* Store the last 2 places the queue element or head was altered */ 124struct qm_trace { 125 unsigned long lastline; 126 unsigned long prevline; 127 const char *lastfile; 128 const char *prevfile; 129}; 130 131#define TRACEBUF struct qm_trace trace; 132#define TRACEBUF_INITIALIZER { __LINE__, 0, __FILE__, NULL } , 133 134#define QMD_TRACE_HEAD(head) do { \ 135 (head)->trace.prevline = (head)->trace.lastline; \ 136 (head)->trace.prevfile = (head)->trace.lastfile; \ 137 (head)->trace.lastline = __LINE__; \ 138 (head)->trace.lastfile = __FILE__; \ 139} while (0) 140 141#define QMD_TRACE_ELEM(elem) do { \ 142 (elem)->trace.prevline = (elem)->trace.lastline; \ 143 (elem)->trace.prevfile = (elem)->trace.lastfile; \ 144 (elem)->trace.lastline = __LINE__; \ 145 (elem)->trace.lastfile = __FILE__; \ 146} while (0) 147 148#else /* !QUEUE_MACRO_DEBUG_TRACE */ 149#define QMD_TRACE_ELEM(elem) 150#define QMD_TRACE_HEAD(head) 151#define TRACEBUF 152#define TRACEBUF_INITIALIZER 153#endif /* QUEUE_MACRO_DEBUG_TRACE */ 154 155#ifdef QUEUE_MACRO_DEBUG_TRASH 156#define TRASHIT(x) do {(x) = (void *)-1;} while (0) 157#define QMD_IS_TRASHED(x) ((x) == (void *)(intptr_t)-1) 158#else /* !QUEUE_MACRO_DEBUG_TRASH */ 159#define TRASHIT(x) 160#define QMD_IS_TRASHED(x) 0 161#endif /* QUEUE_MACRO_DEBUG_TRASH */ 162 163#if defined(QUEUE_MACRO_DEBUG_TRACE) || defined(QUEUE_MACRO_DEBUG_TRASH) 164#define QMD_SAVELINK(name, link) void **name = (void *)&(link) 165#else /* !QUEUE_MACRO_DEBUG_TRACE && !QUEUE_MACRO_DEBUG_TRASH */ 166#define QMD_SAVELINK(name, link) 167#endif /* QUEUE_MACRO_DEBUG_TRACE || QUEUE_MACRO_DEBUG_TRASH */ 168 169#ifdef __cplusplus 170/* 171 * In C++ there can be structure lists and class lists: 172 */ 173#define QUEUE_TYPEOF(type) type 174#else 175#define QUEUE_TYPEOF(type) struct type 176#endif 177 178/* 179 * Singly-linked List declarations. 180 */ 181#define SLIST_HEAD(name, type) \ 182struct name { \ 183 struct type *slh_first; /* first element */ \ 184} 185 186#define SLIST_CLASS_HEAD(name, type) \ 187struct name { \ 188 class type *slh_first; /* first element */ \ 189} 190 191#define SLIST_HEAD_INITIALIZER(head) \ 192 { NULL } 193 194#define SLIST_ENTRY(type) \ 195struct { \ 196 struct type *sle_next; /* next element */ \ 197} 198 199#define SLIST_CLASS_ENTRY(type) \ 200struct { \ 201 class type *sle_next; /* next element */ \ 202} 203 204/* 205 * Singly-linked List functions. 206 */ 207#if (defined(_KERNEL) && defined(INVARIANTS)) 208#define QMD_SLIST_CHECK_PREVPTR(prevp, elm) do { \ 209 if (*(prevp) != (elm)) \ 210 panic("Bad prevptr *(%p) == %p != %p", \ 211 (prevp), *(prevp), (elm)); \ 212} while (0) 213#else 214#define QMD_SLIST_CHECK_PREVPTR(prevp, elm) 215#endif 216 217#define SLIST_CONCAT(head1, head2, type, field) do { \ 218 QUEUE_TYPEOF(type) *curelm = SLIST_FIRST(head1); \ 219 if (curelm == NULL) { \ 220 if ((SLIST_FIRST(head1) = SLIST_FIRST(head2)) != NULL) \ 221 SLIST_INIT(head2); \ 222 } else if (SLIST_FIRST(head2) != NULL) { \ 223 while (SLIST_NEXT(curelm, field) != NULL) \ 224 curelm = SLIST_NEXT(curelm, field); \ 225 SLIST_NEXT(curelm, field) = SLIST_FIRST(head2); \ 226 SLIST_INIT(head2); \ 227 } \ 228} while (0) 229 230#define SLIST_EMPTY(head) ((head)->slh_first == NULL) 231 232#define SLIST_FIRST(head) ((head)->slh_first) 233 234#define SLIST_FOREACH(var, head, field) \ 235 for ((var) = SLIST_FIRST((head)); \ 236 (var); \ 237 (var) = SLIST_NEXT((var), field)) 238 239#define SLIST_FOREACH_FROM(var, head, field) \ 240 for ((var) = ((var) ? (var) : SLIST_FIRST((head))); \ 241 (var); \ 242 (var) = SLIST_NEXT((var), field)) 243 244#define SLIST_FOREACH_SAFE(var, head, field, tvar) \ 245 for ((var) = SLIST_FIRST((head)); \ 246 (var) && ((tvar) = SLIST_NEXT((var), field), 1); \ 247 (var) = (tvar)) 248 249#define SLIST_FOREACH_FROM_SAFE(var, head, field, tvar) \ 250 for ((var) = ((var) ? (var) : SLIST_FIRST((head))); \ 251 (var) && ((tvar) = SLIST_NEXT((var), field), 1); \ 252 (var) = (tvar)) 253 254#define SLIST_FOREACH_PREVPTR(var, varp, head, field) \ 255 for ((varp) = &SLIST_FIRST((head)); \ 256 ((var) = *(varp)) != NULL; \ 257 (varp) = &SLIST_NEXT((var), field)) 258 259#define SLIST_INIT(head) do { \ 260 SLIST_FIRST((head)) = NULL; \ 261} while (0) 262 263#define SLIST_INSERT_AFTER(slistelm, elm, field) do { \ 264 SLIST_NEXT((elm), field) = SLIST_NEXT((slistelm), field); \ 265 SLIST_NEXT((slistelm), field) = (elm); \ 266} while (0) 267 268#define SLIST_INSERT_HEAD(head, elm, field) do { \ 269 SLIST_NEXT((elm), field) = SLIST_FIRST((head)); \ 270 SLIST_FIRST((head)) = (elm); \ 271} while (0) 272 273#define SLIST_NEXT(elm, field) ((elm)->field.sle_next) 274 275#define SLIST_REMOVE(head, elm, type, field) do { \ 276 QMD_SAVELINK(oldnext, (elm)->field.sle_next); \ 277 if (SLIST_FIRST((head)) == (elm)) { \ 278 SLIST_REMOVE_HEAD((head), field); \ 279 } \ 280 else { \ 281 QUEUE_TYPEOF(type) *curelm = SLIST_FIRST(head); \ 282 while (SLIST_NEXT(curelm, field) != (elm)) \ 283 curelm = SLIST_NEXT(curelm, field); \ 284 SLIST_REMOVE_AFTER(curelm, field); \ 285 } \ 286 TRASHIT(*oldnext); \ 287} while (0) 288 289#define SLIST_REMOVE_AFTER(elm, field) do { \ 290 SLIST_NEXT(elm, field) = \ 291 SLIST_NEXT(SLIST_NEXT(elm, field), field); \ 292} while (0) 293 294#define SLIST_REMOVE_HEAD(head, field) do { \ 295 SLIST_FIRST((head)) = SLIST_NEXT(SLIST_FIRST((head)), field); \ 296} while (0) 297 298#define SLIST_REMOVE_PREVPTR(prevp, elm, field) do { \ 299 QMD_SLIST_CHECK_PREVPTR(prevp, elm); \ 300 *(prevp) = SLIST_NEXT(elm, field); \ 301 TRASHIT((elm)->field.sle_next); \ 302} while (0) 303 304#define SLIST_SWAP(head1, head2, type) do { \ 305 QUEUE_TYPEOF(type) *swap_first = SLIST_FIRST(head1); \ 306 SLIST_FIRST(head1) = SLIST_FIRST(head2); \ 307 SLIST_FIRST(head2) = swap_first; \ 308} while (0) 309 310/* 311 * Singly-linked Tail queue declarations. 312 */ 313#define STAILQ_HEAD(name, type) \ 314struct name { \ 315 struct type *stqh_first;/* first element */ \ 316 struct type **stqh_last;/* addr of last next element */ \ 317} 318 319#define STAILQ_CLASS_HEAD(name, type) \ 320struct name { \ 321 class type *stqh_first; /* first element */ \ 322 class type **stqh_last; /* addr of last next element */ \ 323} 324 325#define STAILQ_HEAD_INITIALIZER(head) \ 326 { NULL, &(head).stqh_first } 327 328#define STAILQ_ENTRY(type) \ 329struct { \ 330 struct type *stqe_next; /* next element */ \ 331} 332 333#define STAILQ_CLASS_ENTRY(type) \ 334struct { \ 335 class type *stqe_next; /* next element */ \ 336} 337 338/* 339 * Singly-linked Tail queue functions. 340 */ 341#define STAILQ_CONCAT(head1, head2) do { \ 342 if (!STAILQ_EMPTY((head2))) { \ 343 *(head1)->stqh_last = (head2)->stqh_first; \ 344 (head1)->stqh_last = (head2)->stqh_last; \ 345 STAILQ_INIT((head2)); \ 346 } \ 347} while (0) 348 349#define STAILQ_EMPTY(head) ((head)->stqh_first == NULL) 350 351#define STAILQ_FIRST(head) ((head)->stqh_first) 352 353#define STAILQ_FOREACH(var, head, field) \ 354 for((var) = STAILQ_FIRST((head)); \ 355 (var); \ 356 (var) = STAILQ_NEXT((var), field)) 357 358#define STAILQ_FOREACH_FROM(var, head, field) \ 359 for ((var) = ((var) ? (var) : STAILQ_FIRST((head))); \ 360 (var); \ 361 (var) = STAILQ_NEXT((var), field)) 362 363#define STAILQ_FOREACH_SAFE(var, head, field, tvar) \ 364 for ((var) = STAILQ_FIRST((head)); \ 365 (var) && ((tvar) = STAILQ_NEXT((var), field), 1); \ 366 (var) = (tvar)) 367 368#define STAILQ_FOREACH_FROM_SAFE(var, head, field, tvar) \ 369 for ((var) = ((var) ? (var) : STAILQ_FIRST((head))); \ 370 (var) && ((tvar) = STAILQ_NEXT((var), field), 1); \ 371 (var) = (tvar)) 372 373#define STAILQ_INIT(head) do { \ 374 STAILQ_FIRST((head)) = NULL; \ 375 (head)->stqh_last = &STAILQ_FIRST((head)); \ 376} while (0) 377 378#define STAILQ_INSERT_AFTER(head, tqelm, elm, field) do { \ 379 if ((STAILQ_NEXT((elm), field) = STAILQ_NEXT((tqelm), field)) == NULL)\ 380 (head)->stqh_last = &STAILQ_NEXT((elm), field); \ 381 STAILQ_NEXT((tqelm), field) = (elm); \ 382} while (0) 383 384#define STAILQ_INSERT_HEAD(head, elm, field) do { \ 385 if ((STAILQ_NEXT((elm), field) = STAILQ_FIRST((head))) == NULL) \ 386 (head)->stqh_last = &STAILQ_NEXT((elm), field); \ 387 STAILQ_FIRST((head)) = (elm); \ 388} while (0) 389 390#define STAILQ_INSERT_TAIL(head, elm, field) do { \ 391 STAILQ_NEXT((elm), field) = NULL; \ 392 *(head)->stqh_last = (elm); \ 393 (head)->stqh_last = &STAILQ_NEXT((elm), field); \ 394} while (0) 395 396#define STAILQ_LAST(head, type, field) \ 397 (STAILQ_EMPTY((head)) ? NULL : \ 398 __containerof((head)->stqh_last, \ 399 QUEUE_TYPEOF(type), field.stqe_next)) 400 401#define STAILQ_NEXT(elm, field) ((elm)->field.stqe_next) 402 403#define STAILQ_REMOVE(head, elm, type, field) do { \ 404 QMD_SAVELINK(oldnext, (elm)->field.stqe_next); \ 405 if (STAILQ_FIRST((head)) == (elm)) { \ 406 STAILQ_REMOVE_HEAD((head), field); \ 407 } \ 408 else { \ 409 QUEUE_TYPEOF(type) *curelm = STAILQ_FIRST(head); \ 410 while (STAILQ_NEXT(curelm, field) != (elm)) \ 411 curelm = STAILQ_NEXT(curelm, field); \ 412 STAILQ_REMOVE_AFTER(head, curelm, field); \ 413 } \ 414 TRASHIT(*oldnext); \ 415} while (0) 416 417#define STAILQ_REMOVE_AFTER(head, elm, field) do { \ 418 if ((STAILQ_NEXT(elm, field) = \ 419 STAILQ_NEXT(STAILQ_NEXT(elm, field), field)) == NULL) \ 420 (head)->stqh_last = &STAILQ_NEXT((elm), field); \ 421} while (0) 422 423#define STAILQ_REMOVE_HEAD(head, field) do { \ 424 if ((STAILQ_FIRST((head)) = \ 425 STAILQ_NEXT(STAILQ_FIRST((head)), field)) == NULL) \ 426 (head)->stqh_last = &STAILQ_FIRST((head)); \ 427} while (0) 428 429#define STAILQ_SWAP(head1, head2, type) do { \ 430 QUEUE_TYPEOF(type) *swap_first = STAILQ_FIRST(head1); \ 431 QUEUE_TYPEOF(type) **swap_last = (head1)->stqh_last; \ 432 STAILQ_FIRST(head1) = STAILQ_FIRST(head2); \ 433 (head1)->stqh_last = (head2)->stqh_last; \ 434 STAILQ_FIRST(head2) = swap_first; \ 435 (head2)->stqh_last = swap_last; \ 436 if (STAILQ_EMPTY(head1)) \ 437 (head1)->stqh_last = &STAILQ_FIRST(head1); \ 438 if (STAILQ_EMPTY(head2)) \ 439 (head2)->stqh_last = &STAILQ_FIRST(head2); \ 440} while (0) 441 442 443/* 444 * List declarations. 445 */ 446#define LIST_HEAD(name, type) \ 447struct name { \ 448 struct type *lh_first; /* first element */ \ 449} 450 451#define LIST_CLASS_HEAD(name, type) \ 452struct name { \ 453 class type *lh_first; /* first element */ \ 454} 455 456#define LIST_HEAD_INITIALIZER(head) \ 457 { NULL } 458 459#define LIST_ENTRY(type) \ 460struct { \ 461 struct type *le_next; /* next element */ \ 462 struct type **le_prev; /* address of previous next element */ \ 463} 464 465#define LIST_CLASS_ENTRY(type) \ 466struct { \ 467 class type *le_next; /* next element */ \ 468 class type **le_prev; /* address of previous next element */ \ 469} 470 471/* 472 * List functions. 473 */ 474 475#if (defined(_KERNEL) && defined(INVARIANTS)) 476/* 477 * QMD_LIST_CHECK_HEAD(LIST_HEAD *head, LIST_ENTRY NAME) 478 * 479 * If the list is non-empty, validates that the first element of the list 480 * points back at 'head.' 481 */ 482#define QMD_LIST_CHECK_HEAD(head, field) do { \ 483 if (LIST_FIRST((head)) != NULL && \ 484 LIST_FIRST((head))->field.le_prev != \ 485 &LIST_FIRST((head))) \ 486 panic("Bad list head %p first->prev != head", (head)); \ 487} while (0) 488 489/* 490 * QMD_LIST_CHECK_NEXT(TYPE *elm, LIST_ENTRY NAME) 491 * 492 * If an element follows 'elm' in the list, validates that the next element 493 * points back at 'elm.' 494 */ 495#define QMD_LIST_CHECK_NEXT(elm, field) do { \ 496 if (LIST_NEXT((elm), field) != NULL && \ 497 LIST_NEXT((elm), field)->field.le_prev != \ 498 &((elm)->field.le_next)) \ 499 panic("Bad link elm %p next->prev != elm", (elm)); \ 500} while (0) 501 502/* 503 * QMD_LIST_CHECK_PREV(TYPE *elm, LIST_ENTRY NAME) 504 * 505 * Validates that the previous element (or head of the list) points to 'elm.' 506 */ 507#define QMD_LIST_CHECK_PREV(elm, field) do { \ 508 if (*(elm)->field.le_prev != (elm)) \ 509 panic("Bad link elm %p prev->next != elm", (elm)); \ 510} while (0) 511#else 512#define QMD_LIST_CHECK_HEAD(head, field) 513#define QMD_LIST_CHECK_NEXT(elm, field) 514#define QMD_LIST_CHECK_PREV(elm, field) 515#endif /* (_KERNEL && INVARIANTS) */ 516 517#define LIST_CONCAT(head1, head2, type, field) do { \ 518 QUEUE_TYPEOF(type) *curelm = LIST_FIRST(head1); \ 519 if (curelm == NULL) { \ 520 if ((LIST_FIRST(head1) = LIST_FIRST(head2)) != NULL) { \ 521 LIST_FIRST(head2)->field.le_prev = \ 522 &LIST_FIRST((head1)); \ 523 LIST_INIT(head2); \ 524 } \ 525 } else if (LIST_FIRST(head2) != NULL) { \ 526 while (LIST_NEXT(curelm, field) != NULL) \ 527 curelm = LIST_NEXT(curelm, field); \ 528 LIST_NEXT(curelm, field) = LIST_FIRST(head2); \ 529 LIST_FIRST(head2)->field.le_prev = &LIST_NEXT(curelm, field); \ 530 LIST_INIT(head2); \ 531 } \ 532} while (0) 533 534#define LIST_EMPTY(head) ((head)->lh_first == NULL) 535 536#define LIST_FIRST(head) ((head)->lh_first) 537 538#define LIST_FOREACH(var, head, field) \ 539 for ((var) = LIST_FIRST((head)); \ 540 (var); \ 541 (var) = LIST_NEXT((var), field)) 542 543#define LIST_FOREACH_FROM(var, head, field) \ 544 for ((var) = ((var) ? (var) : LIST_FIRST((head))); \ 545 (var); \ 546 (var) = LIST_NEXT((var), field)) 547 548#define LIST_FOREACH_SAFE(var, head, field, tvar) \ 549 for ((var) = LIST_FIRST((head)); \ 550 (var) && ((tvar) = LIST_NEXT((var), field), 1); \ 551 (var) = (tvar)) 552 553#define LIST_FOREACH_FROM_SAFE(var, head, field, tvar) \ 554 for ((var) = ((var) ? (var) : LIST_FIRST((head))); \ 555 (var) && ((tvar) = LIST_NEXT((var), field), 1); \ 556 (var) = (tvar)) 557 558#define LIST_INIT(head) do { \ 559 LIST_FIRST((head)) = NULL; \ 560} while (0) 561 562#define LIST_INSERT_AFTER(listelm, elm, field) do { \ 563 QMD_LIST_CHECK_NEXT(listelm, field); \ 564 if ((LIST_NEXT((elm), field) = LIST_NEXT((listelm), field)) != NULL)\ 565 LIST_NEXT((listelm), field)->field.le_prev = \ 566 &LIST_NEXT((elm), field); \ 567 LIST_NEXT((listelm), field) = (elm); \ 568 (elm)->field.le_prev = &LIST_NEXT((listelm), field); \ 569} while (0) 570 571#define LIST_INSERT_BEFORE(listelm, elm, field) do { \ 572 QMD_LIST_CHECK_PREV(listelm, field); \ 573 (elm)->field.le_prev = (listelm)->field.le_prev; \ 574 LIST_NEXT((elm), field) = (listelm); \ 575 *(listelm)->field.le_prev = (elm); \ 576 (listelm)->field.le_prev = &LIST_NEXT((elm), field); \ 577} while (0) 578 579#define LIST_INSERT_HEAD(head, elm, field) do { \ 580 QMD_LIST_CHECK_HEAD((head), field); \ 581 if ((LIST_NEXT((elm), field) = LIST_FIRST((head))) != NULL) \ 582 LIST_FIRST((head))->field.le_prev = &LIST_NEXT((elm), field);\ 583 LIST_FIRST((head)) = (elm); \ 584 (elm)->field.le_prev = &LIST_FIRST((head)); \ 585} while (0) 586 587#define LIST_NEXT(elm, field) ((elm)->field.le_next) 588 589#define LIST_PREV(elm, head, type, field) \ 590 ((elm)->field.le_prev == &LIST_FIRST((head)) ? NULL : \ 591 __containerof((elm)->field.le_prev, \ 592 QUEUE_TYPEOF(type), field.le_next)) 593 594#define LIST_REMOVE(elm, field) do { \ 595 QMD_SAVELINK(oldnext, (elm)->field.le_next); \ 596 QMD_SAVELINK(oldprev, (elm)->field.le_prev); \ 597 QMD_LIST_CHECK_NEXT(elm, field); \ 598 QMD_LIST_CHECK_PREV(elm, field); \ 599 if (LIST_NEXT((elm), field) != NULL) \ 600 LIST_NEXT((elm), field)->field.le_prev = \ 601 (elm)->field.le_prev; \ 602 *(elm)->field.le_prev = LIST_NEXT((elm), field); \ 603 TRASHIT(*oldnext); \ 604 TRASHIT(*oldprev); \ 605} while (0) 606 607#define LIST_SWAP(head1, head2, type, field) do { \ 608 QUEUE_TYPEOF(type) *swap_tmp = LIST_FIRST(head1); \ 609 LIST_FIRST((head1)) = LIST_FIRST((head2)); \ 610 LIST_FIRST((head2)) = swap_tmp; \ 611 if ((swap_tmp = LIST_FIRST((head1))) != NULL) \ 612 swap_tmp->field.le_prev = &LIST_FIRST((head1)); \ 613 if ((swap_tmp = LIST_FIRST((head2))) != NULL) \ 614 swap_tmp->field.le_prev = &LIST_FIRST((head2)); \ 615} while (0) 616 617/* 618 * Tail queue declarations. 619 */ 620#define TAILQ_HEAD(name, type) \ 621struct name { \ 622 struct type *tqh_first; /* first element */ \ 623 struct type **tqh_last; /* addr of last next element */ \ 624 TRACEBUF \ 625} 626 627#define TAILQ_CLASS_HEAD(name, type) \ 628struct name { \ 629 class type *tqh_first; /* first element */ \ 630 class type **tqh_last; /* addr of last next element */ \ 631 TRACEBUF \ 632} 633 634#define TAILQ_HEAD_INITIALIZER(head) \ 635 { NULL, &(head).tqh_first, TRACEBUF_INITIALIZER } 636 637#define TAILQ_ENTRY(type) \ 638struct { \ 639 struct type *tqe_next; /* next element */ \ 640 struct type **tqe_prev; /* address of previous next element */ \ 641 TRACEBUF \ 642} 643 644#define TAILQ_CLASS_ENTRY(type) \ 645struct { \ 646 class type *tqe_next; /* next element */ \ 647 class type **tqe_prev; /* address of previous next element */ \ 648 TRACEBUF \ 649} 650 651/* 652 * Tail queue functions. 653 */ 654#if (defined(_KERNEL) && defined(INVARIANTS)) 655/* 656 * QMD_TAILQ_CHECK_HEAD(TAILQ_HEAD *head, TAILQ_ENTRY NAME) 657 * 658 * If the tailq is non-empty, validates that the first element of the tailq 659 * points back at 'head.' 660 */ 661#define QMD_TAILQ_CHECK_HEAD(head, field) do { \ 662 if (!TAILQ_EMPTY(head) && \ 663 TAILQ_FIRST((head))->field.tqe_prev != \ 664 &TAILQ_FIRST((head))) \ 665 panic("Bad tailq head %p first->prev != head", (head)); \ 666} while (0) 667 668/* 669 * QMD_TAILQ_CHECK_TAIL(TAILQ_HEAD *head, TAILQ_ENTRY NAME) 670 * 671 * Validates that the tail of the tailq is a pointer to pointer to NULL. 672 */ 673#define QMD_TAILQ_CHECK_TAIL(head, field) do { \ 674 if (*(head)->tqh_last != NULL) \ 675 panic("Bad tailq NEXT(%p->tqh_last) != NULL", (head)); \ 676} while (0) 677 678/* 679 * QMD_TAILQ_CHECK_NEXT(TYPE *elm, TAILQ_ENTRY NAME) 680 * 681 * If an element follows 'elm' in the tailq, validates that the next element 682 * points back at 'elm.' 683 */ 684#define QMD_TAILQ_CHECK_NEXT(elm, field) do { \ 685 if (TAILQ_NEXT((elm), field) != NULL && \ 686 TAILQ_NEXT((elm), field)->field.tqe_prev != \ 687 &((elm)->field.tqe_next)) \ 688 panic("Bad link elm %p next->prev != elm", (elm)); \ 689} while (0) 690 691/* 692 * QMD_TAILQ_CHECK_PREV(TYPE *elm, TAILQ_ENTRY NAME) 693 * 694 * Validates that the previous element (or head of the tailq) points to 'elm.' 695 */ 696#define QMD_TAILQ_CHECK_PREV(elm, field) do { \ 697 if (*(elm)->field.tqe_prev != (elm)) \ 698 panic("Bad link elm %p prev->next != elm", (elm)); \ 699} while (0) 700#else 701#define QMD_TAILQ_CHECK_HEAD(head, field) 702#define QMD_TAILQ_CHECK_TAIL(head, headname) 703#define QMD_TAILQ_CHECK_NEXT(elm, field) 704#define QMD_TAILQ_CHECK_PREV(elm, field) 705#endif /* (_KERNEL && INVARIANTS) */ 706 707#define TAILQ_CONCAT(head1, head2, field) do { \ 708 if (!TAILQ_EMPTY(head2)) { \ 709 *(head1)->tqh_last = (head2)->tqh_first; \ 710 (head2)->tqh_first->field.tqe_prev = (head1)->tqh_last; \ 711 (head1)->tqh_last = (head2)->tqh_last; \ 712 TAILQ_INIT((head2)); \ 713 QMD_TRACE_HEAD(head1); \ 714 QMD_TRACE_HEAD(head2); \ 715 } \ 716} while (0) 717 718#define TAILQ_EMPTY(head) ((head)->tqh_first == NULL) 719 720#define TAILQ_FIRST(head) ((head)->tqh_first) 721 722#define TAILQ_FOREACH(var, head, field) \ 723 for ((var) = TAILQ_FIRST((head)); \ 724 (var); \ 725 (var) = TAILQ_NEXT((var), field)) 726 727#define TAILQ_FOREACH_FROM(var, head, field) \ 728 for ((var) = ((var) ? (var) : TAILQ_FIRST((head))); \ 729 (var); \ 730 (var) = TAILQ_NEXT((var), field)) 731 732#define TAILQ_FOREACH_SAFE(var, head, field, tvar) \ 733 for ((var) = TAILQ_FIRST((head)); \ 734 (var) && ((tvar) = TAILQ_NEXT((var), field), 1); \ 735 (var) = (tvar)) 736 737#define TAILQ_FOREACH_FROM_SAFE(var, head, field, tvar) \ 738 for ((var) = ((var) ? (var) : TAILQ_FIRST((head))); \ 739 (var) && ((tvar) = TAILQ_NEXT((var), field), 1); \ 740 (var) = (tvar)) 741 742#define TAILQ_FOREACH_REVERSE(var, head, headname, field) \ 743 for ((var) = TAILQ_LAST((head), headname); \ 744 (var); \ 745 (var) = TAILQ_PREV((var), headname, field)) 746 747#define TAILQ_FOREACH_REVERSE_FROM(var, head, headname, field) \ 748 for ((var) = ((var) ? (var) : TAILQ_LAST((head), headname)); \ 749 (var); \ 750 (var) = TAILQ_PREV((var), headname, field)) 751 752#define TAILQ_FOREACH_REVERSE_SAFE(var, head, headname, field, tvar) \ 753 for ((var) = TAILQ_LAST((head), headname); \ 754 (var) && ((tvar) = TAILQ_PREV((var), headname, field), 1); \ 755 (var) = (tvar)) 756 757#define TAILQ_FOREACH_REVERSE_FROM_SAFE(var, head, headname, field, tvar) \ 758 for ((var) = ((var) ? (var) : TAILQ_LAST((head), headname)); \ 759 (var) && ((tvar) = TAILQ_PREV((var), headname, field), 1); \ 760 (var) = (tvar)) 761 762#define TAILQ_INIT(head) do { \ 763 TAILQ_FIRST((head)) = NULL; \ 764 (head)->tqh_last = &TAILQ_FIRST((head)); \ 765 QMD_TRACE_HEAD(head); \ 766} while (0) 767 768#define TAILQ_INSERT_AFTER(head, listelm, elm, field) do { \ 769 QMD_TAILQ_CHECK_NEXT(listelm, field); \ 770 if ((TAILQ_NEXT((elm), field) = TAILQ_NEXT((listelm), field)) != NULL)\ 771 TAILQ_NEXT((elm), field)->field.tqe_prev = \ 772 &TAILQ_NEXT((elm), field); \ 773 else { \ 774 (head)->tqh_last = &TAILQ_NEXT((elm), field); \ 775 QMD_TRACE_HEAD(head); \ 776 } \ 777 TAILQ_NEXT((listelm), field) = (elm); \ 778 (elm)->field.tqe_prev = &TAILQ_NEXT((listelm), field); \ 779 QMD_TRACE_ELEM(&(elm)->field); \ 780 QMD_TRACE_ELEM(&(listelm)->field); \ 781} while (0) 782 783#define TAILQ_INSERT_BEFORE(listelm, elm, field) do { \ 784 QMD_TAILQ_CHECK_PREV(listelm, field); \ 785 (elm)->field.tqe_prev = (listelm)->field.tqe_prev; \ 786 TAILQ_NEXT((elm), field) = (listelm); \ 787 *(listelm)->field.tqe_prev = (elm); \ 788 (listelm)->field.tqe_prev = &TAILQ_NEXT((elm), field); \ 789 QMD_TRACE_ELEM(&(elm)->field); \ 790 QMD_TRACE_ELEM(&(listelm)->field); \ 791} while (0) 792 793#define TAILQ_INSERT_HEAD(head, elm, field) do { \ 794 QMD_TAILQ_CHECK_HEAD(head, field); \ 795 if ((TAILQ_NEXT((elm), field) = TAILQ_FIRST((head))) != NULL) \ 796 TAILQ_FIRST((head))->field.tqe_prev = \ 797 &TAILQ_NEXT((elm), field); \ 798 else \ 799 (head)->tqh_last = &TAILQ_NEXT((elm), field); \ 800 TAILQ_FIRST((head)) = (elm); \ 801 (elm)->field.tqe_prev = &TAILQ_FIRST((head)); \ 802 QMD_TRACE_HEAD(head); \ 803 QMD_TRACE_ELEM(&(elm)->field); \ 804} while (0) 805 806#define TAILQ_INSERT_TAIL(head, elm, field) do { \ 807 QMD_TAILQ_CHECK_TAIL(head, field); \ 808 TAILQ_NEXT((elm), field) = NULL; \ 809 (elm)->field.tqe_prev = (head)->tqh_last; \ 810 *(head)->tqh_last = (elm); \ 811 (head)->tqh_last = &TAILQ_NEXT((elm), field); \ 812 QMD_TRACE_HEAD(head); \ 813 QMD_TRACE_ELEM(&(elm)->field); \ 814} while (0) 815 816#define TAILQ_LAST(head, headname) \ 817 (*(((struct headname *)((head)->tqh_last))->tqh_last)) 818 819#define TAILQ_NEXT(elm, field) ((elm)->field.tqe_next) 820 821#define TAILQ_PREV(elm, headname, field) \ 822 (*(((struct headname *)((elm)->field.tqe_prev))->tqh_last)) 823 824#define TAILQ_REMOVE(head, elm, field) do { \ 825 QMD_SAVELINK(oldnext, (elm)->field.tqe_next); \ 826 QMD_SAVELINK(oldprev, (elm)->field.tqe_prev); \ 827 QMD_TAILQ_CHECK_NEXT(elm, field); \ 828 QMD_TAILQ_CHECK_PREV(elm, field); \ 829 if ((TAILQ_NEXT((elm), field)) != NULL) \ 830 TAILQ_NEXT((elm), field)->field.tqe_prev = \ 831 (elm)->field.tqe_prev; \ 832 else { \ 833 (head)->tqh_last = (elm)->field.tqe_prev; \ 834 QMD_TRACE_HEAD(head); \ 835 } \ 836 *(elm)->field.tqe_prev = TAILQ_NEXT((elm), field); \ 837 TRASHIT(*oldnext); \ 838 TRASHIT(*oldprev); \ 839 QMD_TRACE_ELEM(&(elm)->field); \ 840} while (0) 841 842#define TAILQ_SWAP(head1, head2, type, field) do { \ 843 QUEUE_TYPEOF(type) *swap_first = (head1)->tqh_first; \ 844 QUEUE_TYPEOF(type) **swap_last = (head1)->tqh_last; \ 845 (head1)->tqh_first = (head2)->tqh_first; \ 846 (head1)->tqh_last = (head2)->tqh_last; \ 847 (head2)->tqh_first = swap_first; \ 848 (head2)->tqh_last = swap_last; \ 849 if ((swap_first = (head1)->tqh_first) != NULL) \ 850 swap_first->field.tqe_prev = &(head1)->tqh_first; \ 851 else \ 852 (head1)->tqh_last = &(head1)->tqh_first; \ 853 if ((swap_first = (head2)->tqh_first) != NULL) \ 854 swap_first->field.tqe_prev = &(head2)->tqh_first; \ 855 else \ 856 (head2)->tqh_last = &(head2)->tqh_first; \ 857} while (0) 858 859#endif /* !_SYS_QUEUE_H_ */ 860