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 */ 33 34 #ifndef _SYS_QUEUE_H_ 35 #define _SYS_QUEUE_H_ 36 37 #include <sys/cdefs.h> 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 * _END + + + + 94 * _FIRST + + + + 95 * _NEXT + + + + 96 * _PREV - + - + 97 * _LAST - - + + 98 * _LAST_FAST - - - + 99 * _FOREACH + + + + 100 * _FOREACH_FROM + + + + 101 * _FOREACH_SAFE + + + + 102 * _FOREACH_FROM_SAFE + + + + 103 * _FOREACH_REVERSE - - - + 104 * _FOREACH_REVERSE_FROM - - - + 105 * _FOREACH_REVERSE_SAFE - - - + 106 * _FOREACH_REVERSE_FROM_SAFE - - - + 107 * _INSERT_HEAD + + + + 108 * _INSERT_BEFORE - + - + 109 * _INSERT_AFTER + + + + 110 * _INSERT_TAIL - - + + 111 * _CONCAT s s + + 112 * _REMOVE_AFTER + - + - 113 * _REMOVE_HEAD + - + - 114 * _REMOVE s + s + 115 * _SWAP + + + + 116 * 117 */ 118 #ifdef QUEUE_MACRO_DEBUG 119 /* Store the last 2 places the queue element or head was altered */ 120 struct qm_trace { 121 unsigned long lastline; 122 unsigned long prevline; 123 const char *lastfile; 124 const char *prevfile; 125 }; 126 127 #define TRACEBUF struct qm_trace trace; 128 #define TRACEBUF_INITIALIZER { __LINE__, 0, __FILE__, NULL } , 129 #define TRASHIT(x) do {(x) = (void *)-1;} while (0) 130 #define QMD_SAVELINK(name, link) void **name = (void *)&(link) 131 132 #define QMD_TRACE_HEAD(head) do { \ 133 (head)->trace.prevline = (head)->trace.lastline; \ 134 (head)->trace.prevfile = (head)->trace.lastfile; \ 135 (head)->trace.lastline = __LINE__; \ 136 (head)->trace.lastfile = __FILE__; \ 137 } while (0) 138 139 #define QMD_TRACE_ELEM(elem) do { \ 140 (elem)->trace.prevline = (elem)->trace.lastline; \ 141 (elem)->trace.prevfile = (elem)->trace.lastfile; \ 142 (elem)->trace.lastline = __LINE__; \ 143 (elem)->trace.lastfile = __FILE__; \ 144 } while (0) 145 146 #else /* !QUEUE_MACRO_DEBUG_TRACE */ 147 #define QMD_TRACE_ELEM(elem) 148 #define QMD_TRACE_HEAD(head) 149 #define TRACEBUF 150 #define TRACEBUF_INITIALIZER 151 #endif /* QUEUE_MACRO_DEBUG_TRACE */ 152 153 #ifdef QUEUE_MACRO_DEBUG_TRASH 154 #define TRASHIT(x) do {(x) = (void *)-1;} while (0) 155 #define QMD_IS_TRASHED(x) ((x) == (void *)(intptr_t)-1) 156 #else /* !QUEUE_MACRO_DEBUG_TRASH */ 157 #define TRASHIT(x) 158 #define QMD_IS_TRASHED(x) 0 159 #endif /* QUEUE_MACRO_DEBUG_TRASH */ 160 161 #if defined(QUEUE_MACRO_DEBUG_TRACE) || defined(QUEUE_MACRO_DEBUG_TRASH) 162 #define QMD_SAVELINK(name, link) void **name = (void *)&(link) 163 #else /* !QUEUE_MACRO_DEBUG_TRACE && !QUEUE_MACRO_DEBUG_TRASH */ 164 #define QMD_SAVELINK(name, link) 165 #endif /* QUEUE_MACRO_DEBUG_TRACE || QUEUE_MACRO_DEBUG_TRASH */ 166 167 #ifdef __cplusplus 168 /* 169 * In C++ there can be structure lists and class lists: 170 */ 171 #define QUEUE_TYPEOF(type) type 172 #else 173 #define QUEUE_TYPEOF(type) struct type 174 #endif 175 176 /* 177 * Singly-linked List declarations. 178 */ 179 #define SLIST_HEAD(name, type) \ 180 struct name { \ 181 struct type *slh_first; /* first element */ \ 182 } 183 184 #define SLIST_CLASS_HEAD(name, type) \ 185 struct name { \ 186 class type *slh_first; /* first element */ \ 187 } 188 189 #define SLIST_HEAD_INITIALIZER(head) \ 190 { NULL } 191 192 #define SLIST_ENTRY(type) \ 193 struct { \ 194 struct type *sle_next; /* next element */ \ 195 } 196 197 #define SLIST_CLASS_ENTRY(type) \ 198 struct { \ 199 class type *sle_next; /* next element */ \ 200 } 201 202 /* 203 * Singly-linked List functions. 204 */ 205 #if (defined(_KERNEL) && defined(INVARIANTS)) 206 #define QMD_SLIST_CHECK_PREVPTR(prevp, elm) do { \ 207 if (*(prevp) != (elm)) \ 208 panic("Bad prevptr *(%p) == %p != %p", \ 209 (prevp), *(prevp), (elm)); \ 210 } while (0) 211 #else 212 #define QMD_SLIST_CHECK_PREVPTR(prevp, elm) 213 #endif 214 215 #define SLIST_CONCAT(head1, head2, type, field) do { \ 216 QUEUE_TYPEOF(type) *curelm = SLIST_FIRST(head1); \ 217 if (curelm == NULL) { \ 218 if ((SLIST_FIRST(head1) = SLIST_FIRST(head2)) != NULL) \ 219 SLIST_INIT(head2); \ 220 } else if (SLIST_FIRST(head2) != NULL) { \ 221 while (SLIST_NEXT(curelm, field) != NULL) \ 222 curelm = SLIST_NEXT(curelm, field); \ 223 SLIST_NEXT(curelm, field) = SLIST_FIRST(head2); \ 224 SLIST_INIT(head2); \ 225 } \ 226 } while (0) 227 228 #define SLIST_EMPTY(head) ((head)->slh_first == NULL) 229 230 #define SLIST_FIRST(head) ((head)->slh_first) 231 232 #define SLIST_FOREACH(var, head, field) \ 233 for ((var) = SLIST_FIRST((head)); \ 234 (var); \ 235 (var) = SLIST_NEXT((var), field)) 236 237 #define SLIST_FOREACH_FROM(var, head, field) \ 238 for ((var) = ((var) ? (var) : SLIST_FIRST((head))); \ 239 (var); \ 240 (var) = SLIST_NEXT((var), field)) 241 242 #define SLIST_FOREACH_SAFE(var, head, field, tvar) \ 243 for ((var) = SLIST_FIRST((head)); \ 244 (var) && ((tvar) = SLIST_NEXT((var), field), 1); \ 245 (var) = (tvar)) 246 247 #define SLIST_FOREACH_FROM_SAFE(var, head, field, tvar) \ 248 for ((var) = ((var) ? (var) : SLIST_FIRST((head))); \ 249 (var) && ((tvar) = SLIST_NEXT((var), field), 1); \ 250 (var) = (tvar)) 251 252 #define SLIST_FOREACH_PREVPTR(var, varp, head, field) \ 253 for ((varp) = &SLIST_FIRST((head)); \ 254 ((var) = *(varp)) != NULL; \ 255 (varp) = &SLIST_NEXT((var), field)) 256 257 #define SLIST_INIT(head) do { \ 258 SLIST_FIRST((head)) = NULL; \ 259 } while (0) 260 261 #define SLIST_INSERT_AFTER(slistelm, elm, field) do { \ 262 SLIST_NEXT((elm), field) = SLIST_NEXT((slistelm), field); \ 263 SLIST_NEXT((slistelm), field) = (elm); \ 264 } while (0) 265 266 #define SLIST_INSERT_HEAD(head, elm, field) do { \ 267 SLIST_NEXT((elm), field) = SLIST_FIRST((head)); \ 268 SLIST_FIRST((head)) = (elm); \ 269 } while (0) 270 271 #define SLIST_NEXT(elm, field) ((elm)->field.sle_next) 272 273 #define SLIST_REMOVE(head, elm, type, field) do { \ 274 QMD_SAVELINK(oldnext, (elm)->field.sle_next); \ 275 if (SLIST_FIRST((head)) == (elm)) { \ 276 SLIST_REMOVE_HEAD((head), field); \ 277 } \ 278 else { \ 279 QUEUE_TYPEOF(type) *curelm = SLIST_FIRST(head); \ 280 while (SLIST_NEXT(curelm, field) != (elm)) \ 281 curelm = SLIST_NEXT(curelm, field); \ 282 SLIST_REMOVE_AFTER(curelm, field); \ 283 } \ 284 TRASHIT(*oldnext); \ 285 } while (0) 286 287 #define SLIST_REMOVE_AFTER(elm, field) do { \ 288 SLIST_NEXT(elm, field) = \ 289 SLIST_NEXT(SLIST_NEXT(elm, field), field); \ 290 } while (0) 291 292 #define SLIST_REMOVE_HEAD(head, field) do { \ 293 SLIST_FIRST((head)) = SLIST_NEXT(SLIST_FIRST((head)), field); \ 294 } while (0) 295 296 #define SLIST_REMOVE_PREVPTR(prevp, elm, field) do { \ 297 QMD_SLIST_CHECK_PREVPTR(prevp, elm); \ 298 *(prevp) = SLIST_NEXT(elm, field); \ 299 TRASHIT((elm)->field.sle_next); \ 300 } while (0) 301 302 #define SLIST_SWAP(head1, head2, type) do { \ 303 QUEUE_TYPEOF(type) *swap_first = SLIST_FIRST(head1); \ 304 SLIST_FIRST(head1) = SLIST_FIRST(head2); \ 305 SLIST_FIRST(head2) = swap_first; \ 306 } while (0) 307 308 #define SLIST_END(head) NULL 309 310 /* 311 * Singly-linked Tail queue declarations. 312 */ 313 #define STAILQ_HEAD(name, type) \ 314 struct 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) \ 320 struct 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) \ 329 struct { \ 330 struct type *stqe_next; /* next element */ \ 331 } 332 333 #define STAILQ_CLASS_ENTRY(type) \ 334 struct { \ 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 #define STAILQ_END(head) NULL 443 444 445 /* 446 * List declarations. 447 */ 448 #define LIST_HEAD(name, type) \ 449 struct name { \ 450 struct type *lh_first; /* first element */ \ 451 } 452 453 #define LIST_CLASS_HEAD(name, type) \ 454 struct name { \ 455 class type *lh_first; /* first element */ \ 456 } 457 458 #define LIST_HEAD_INITIALIZER(head) \ 459 { NULL } 460 461 #define LIST_ENTRY(type) \ 462 struct { \ 463 struct type *le_next; /* next element */ \ 464 struct type **le_prev; /* address of previous next element */ \ 465 } 466 467 #define LIST_CLASS_ENTRY(type) \ 468 struct { \ 469 class type *le_next; /* next element */ \ 470 class type **le_prev; /* address of previous next element */ \ 471 } 472 473 /* 474 * List functions. 475 */ 476 477 #if (defined(_KERNEL) && defined(INVARIANTS)) 478 /* 479 * QMD_LIST_CHECK_HEAD(LIST_HEAD *head, LIST_ENTRY NAME) 480 * 481 * If the list is non-empty, validates that the first element of the list 482 * points back at 'head.' 483 */ 484 #define QMD_LIST_CHECK_HEAD(head, field) do { \ 485 if (LIST_FIRST((head)) != NULL && \ 486 LIST_FIRST((head))->field.le_prev != \ 487 &LIST_FIRST((head))) \ 488 panic("Bad list head %p first->prev != head", (head)); \ 489 } while (0) 490 491 /* 492 * QMD_LIST_CHECK_NEXT(TYPE *elm, LIST_ENTRY NAME) 493 * 494 * If an element follows 'elm' in the list, validates that the next element 495 * points back at 'elm.' 496 */ 497 #define QMD_LIST_CHECK_NEXT(elm, field) do { \ 498 if (LIST_NEXT((elm), field) != NULL && \ 499 LIST_NEXT((elm), field)->field.le_prev != \ 500 &((elm)->field.le_next)) \ 501 panic("Bad link elm %p next->prev != elm", (elm)); \ 502 } while (0) 503 504 /* 505 * QMD_LIST_CHECK_PREV(TYPE *elm, LIST_ENTRY NAME) 506 * 507 * Validates that the previous element (or head of the list) points to 'elm.' 508 */ 509 #define QMD_LIST_CHECK_PREV(elm, field) do { \ 510 if (*(elm)->field.le_prev != (elm)) \ 511 panic("Bad link elm %p prev->next != elm", (elm)); \ 512 } while (0) 513 #else 514 #define QMD_LIST_CHECK_HEAD(head, field) 515 #define QMD_LIST_CHECK_NEXT(elm, field) 516 #define QMD_LIST_CHECK_PREV(elm, field) 517 #endif /* (_KERNEL && INVARIANTS) */ 518 519 #define LIST_CONCAT(head1, head2, type, field) do { \ 520 QUEUE_TYPEOF(type) *curelm = LIST_FIRST(head1); \ 521 if (curelm == NULL) { \ 522 if ((LIST_FIRST(head1) = LIST_FIRST(head2)) != NULL) { \ 523 LIST_FIRST(head2)->field.le_prev = \ 524 &LIST_FIRST((head1)); \ 525 LIST_INIT(head2); \ 526 } \ 527 } else if (LIST_FIRST(head2) != NULL) { \ 528 while (LIST_NEXT(curelm, field) != NULL) \ 529 curelm = LIST_NEXT(curelm, field); \ 530 LIST_NEXT(curelm, field) = LIST_FIRST(head2); \ 531 LIST_FIRST(head2)->field.le_prev = &LIST_NEXT(curelm, field); \ 532 LIST_INIT(head2); \ 533 } \ 534 } while (0) 535 536 #define LIST_EMPTY(head) ((head)->lh_first == NULL) 537 538 #define LIST_FIRST(head) ((head)->lh_first) 539 540 #define LIST_FOREACH(var, head, field) \ 541 for ((var) = LIST_FIRST((head)); \ 542 (var); \ 543 (var) = LIST_NEXT((var), field)) 544 545 #define LIST_FOREACH_FROM(var, head, field) \ 546 for ((var) = ((var) ? (var) : LIST_FIRST((head))); \ 547 (var); \ 548 (var) = LIST_NEXT((var), field)) 549 550 #define LIST_FOREACH_SAFE(var, head, field, tvar) \ 551 for ((var) = LIST_FIRST((head)); \ 552 (var) && ((tvar) = LIST_NEXT((var), field), 1); \ 553 (var) = (tvar)) 554 555 #define LIST_FOREACH_FROM_SAFE(var, head, field, tvar) \ 556 for ((var) = ((var) ? (var) : LIST_FIRST((head))); \ 557 (var) && ((tvar) = LIST_NEXT((var), field), 1); \ 558 (var) = (tvar)) 559 560 #define LIST_INIT(head) do { \ 561 LIST_FIRST((head)) = NULL; \ 562 } while (0) 563 564 #define LIST_INSERT_AFTER(listelm, elm, field) do { \ 565 QMD_LIST_CHECK_NEXT(listelm, field); \ 566 if ((LIST_NEXT((elm), field) = LIST_NEXT((listelm), field)) != NULL)\ 567 LIST_NEXT((listelm), field)->field.le_prev = \ 568 &LIST_NEXT((elm), field); \ 569 LIST_NEXT((listelm), field) = (elm); \ 570 (elm)->field.le_prev = &LIST_NEXT((listelm), field); \ 571 } while (0) 572 573 #define LIST_INSERT_BEFORE(listelm, elm, field) do { \ 574 QMD_LIST_CHECK_PREV(listelm, field); \ 575 (elm)->field.le_prev = (listelm)->field.le_prev; \ 576 LIST_NEXT((elm), field) = (listelm); \ 577 *(listelm)->field.le_prev = (elm); \ 578 (listelm)->field.le_prev = &LIST_NEXT((elm), field); \ 579 } while (0) 580 581 #define LIST_INSERT_HEAD(head, elm, field) do { \ 582 QMD_LIST_CHECK_HEAD((head), field); \ 583 if ((LIST_NEXT((elm), field) = LIST_FIRST((head))) != NULL) \ 584 LIST_FIRST((head))->field.le_prev = &LIST_NEXT((elm), field);\ 585 LIST_FIRST((head)) = (elm); \ 586 (elm)->field.le_prev = &LIST_FIRST((head)); \ 587 } while (0) 588 589 #define LIST_NEXT(elm, field) ((elm)->field.le_next) 590 591 #define LIST_PREV(elm, head, type, field) \ 592 ((elm)->field.le_prev == &LIST_FIRST((head)) ? NULL : \ 593 __containerof((elm)->field.le_prev, \ 594 QUEUE_TYPEOF(type), field.le_next)) 595 596 #define LIST_REMOVE(elm, field) do { \ 597 QMD_SAVELINK(oldnext, (elm)->field.le_next); \ 598 QMD_SAVELINK(oldprev, (elm)->field.le_prev); \ 599 QMD_LIST_CHECK_NEXT(elm, field); \ 600 QMD_LIST_CHECK_PREV(elm, field); \ 601 if (LIST_NEXT((elm), field) != NULL) \ 602 LIST_NEXT((elm), field)->field.le_prev = \ 603 (elm)->field.le_prev; \ 604 *(elm)->field.le_prev = LIST_NEXT((elm), field); \ 605 TRASHIT(*oldnext); \ 606 TRASHIT(*oldprev); \ 607 } while (0) 608 609 #define LIST_SWAP(head1, head2, type, field) do { \ 610 QUEUE_TYPEOF(type) *swap_tmp = LIST_FIRST(head1); \ 611 LIST_FIRST((head1)) = LIST_FIRST((head2)); \ 612 LIST_FIRST((head2)) = swap_tmp; \ 613 if ((swap_tmp = LIST_FIRST((head1))) != NULL) \ 614 swap_tmp->field.le_prev = &LIST_FIRST((head1)); \ 615 if ((swap_tmp = LIST_FIRST((head2))) != NULL) \ 616 swap_tmp->field.le_prev = &LIST_FIRST((head2)); \ 617 } while (0) 618 619 #define LIST_END(head) NULL 620 621 /* 622 * Tail queue declarations. 623 */ 624 #define TAILQ_HEAD(name, type) \ 625 struct name { \ 626 struct type *tqh_first; /* first element */ \ 627 struct type **tqh_last; /* addr of last next element */ \ 628 TRACEBUF \ 629 } 630 631 #define TAILQ_CLASS_HEAD(name, type) \ 632 struct name { \ 633 class type *tqh_first; /* first element */ \ 634 class type **tqh_last; /* addr of last next element */ \ 635 TRACEBUF \ 636 } 637 638 #define TAILQ_HEAD_INITIALIZER(head) \ 639 { NULL, &(head).tqh_first, TRACEBUF_INITIALIZER } 640 641 #define TAILQ_ENTRY(type) \ 642 struct { \ 643 struct type *tqe_next; /* next element */ \ 644 struct type **tqe_prev; /* address of previous next element */ \ 645 TRACEBUF \ 646 } 647 648 #define TAILQ_CLASS_ENTRY(type) \ 649 struct { \ 650 class type *tqe_next; /* next element */ \ 651 class type **tqe_prev; /* address of previous next element */ \ 652 TRACEBUF \ 653 } 654 655 /* 656 * Tail queue functions. 657 */ 658 #if (defined(_KERNEL) && defined(INVARIANTS)) 659 /* 660 * QMD_TAILQ_CHECK_HEAD(TAILQ_HEAD *head, TAILQ_ENTRY NAME) 661 * 662 * If the tailq is non-empty, validates that the first element of the tailq 663 * points back at 'head.' 664 */ 665 #define QMD_TAILQ_CHECK_HEAD(head, field) do { \ 666 if (!TAILQ_EMPTY(head) && \ 667 TAILQ_FIRST((head))->field.tqe_prev != \ 668 &TAILQ_FIRST((head))) \ 669 panic("Bad tailq head %p first->prev != head", (head)); \ 670 } while (0) 671 672 /* 673 * QMD_TAILQ_CHECK_TAIL(TAILQ_HEAD *head, TAILQ_ENTRY NAME) 674 * 675 * Validates that the tail of the tailq is a pointer to pointer to NULL. 676 */ 677 #define QMD_TAILQ_CHECK_TAIL(head, field) do { \ 678 if (*(head)->tqh_last != NULL) \ 679 panic("Bad tailq NEXT(%p->tqh_last) != NULL", (head)); \ 680 } while (0) 681 682 /* 683 * QMD_TAILQ_CHECK_NEXT(TYPE *elm, TAILQ_ENTRY NAME) 684 * 685 * If an element follows 'elm' in the tailq, validates that the next element 686 * points back at 'elm.' 687 */ 688 #define QMD_TAILQ_CHECK_NEXT(elm, field) do { \ 689 if (TAILQ_NEXT((elm), field) != NULL && \ 690 TAILQ_NEXT((elm), field)->field.tqe_prev != \ 691 &((elm)->field.tqe_next)) \ 692 panic("Bad link elm %p next->prev != elm", (elm)); \ 693 } while (0) 694 695 /* 696 * QMD_TAILQ_CHECK_PREV(TYPE *elm, TAILQ_ENTRY NAME) 697 * 698 * Validates that the previous element (or head of the tailq) points to 'elm.' 699 */ 700 #define QMD_TAILQ_CHECK_PREV(elm, field) do { \ 701 if (*(elm)->field.tqe_prev != (elm)) \ 702 panic("Bad link elm %p prev->next != elm", (elm)); \ 703 } while (0) 704 #else 705 #define QMD_TAILQ_CHECK_HEAD(head, field) 706 #define QMD_TAILQ_CHECK_TAIL(head, headname) 707 #define QMD_TAILQ_CHECK_NEXT(elm, field) 708 #define QMD_TAILQ_CHECK_PREV(elm, field) 709 #endif /* (_KERNEL && INVARIANTS) */ 710 711 #define TAILQ_CONCAT(head1, head2, field) do { \ 712 if (!TAILQ_EMPTY(head2)) { \ 713 *(head1)->tqh_last = (head2)->tqh_first; \ 714 (head2)->tqh_first->field.tqe_prev = (head1)->tqh_last; \ 715 (head1)->tqh_last = (head2)->tqh_last; \ 716 TAILQ_INIT((head2)); \ 717 QMD_TRACE_HEAD(head1); \ 718 QMD_TRACE_HEAD(head2); \ 719 } \ 720 } while (0) 721 722 #define TAILQ_EMPTY(head) ((head)->tqh_first == NULL) 723 724 #define TAILQ_FIRST(head) ((head)->tqh_first) 725 726 #define TAILQ_FOREACH(var, head, field) \ 727 for ((var) = TAILQ_FIRST((head)); \ 728 (var); \ 729 (var) = TAILQ_NEXT((var), field)) 730 731 #define TAILQ_FOREACH_FROM(var, head, field) \ 732 for ((var) = ((var) ? (var) : TAILQ_FIRST((head))); \ 733 (var); \ 734 (var) = TAILQ_NEXT((var), field)) 735 736 #define TAILQ_FOREACH_SAFE(var, head, field, tvar) \ 737 for ((var) = TAILQ_FIRST((head)); \ 738 (var) && ((tvar) = TAILQ_NEXT((var), field), 1); \ 739 (var) = (tvar)) 740 741 #define TAILQ_FOREACH_FROM_SAFE(var, head, field, tvar) \ 742 for ((var) = ((var) ? (var) : TAILQ_FIRST((head))); \ 743 (var) && ((tvar) = TAILQ_NEXT((var), field), 1); \ 744 (var) = (tvar)) 745 746 #define TAILQ_FOREACH_REVERSE(var, head, headname, field) \ 747 for ((var) = TAILQ_LAST((head), headname); \ 748 (var); \ 749 (var) = TAILQ_PREV((var), headname, field)) 750 751 #define TAILQ_FOREACH_REVERSE_FROM(var, head, headname, field) \ 752 for ((var) = ((var) ? (var) : TAILQ_LAST((head), headname)); \ 753 (var); \ 754 (var) = TAILQ_PREV((var), headname, field)) 755 756 #define TAILQ_FOREACH_REVERSE_SAFE(var, head, headname, field, tvar) \ 757 for ((var) = TAILQ_LAST((head), headname); \ 758 (var) && ((tvar) = TAILQ_PREV((var), headname, field), 1); \ 759 (var) = (tvar)) 760 761 #define TAILQ_FOREACH_REVERSE_FROM_SAFE(var, head, headname, field, tvar) \ 762 for ((var) = ((var) ? (var) : TAILQ_LAST((head), headname)); \ 763 (var) && ((tvar) = TAILQ_PREV((var), headname, field), 1); \ 764 (var) = (tvar)) 765 766 #define TAILQ_INIT(head) do { \ 767 TAILQ_FIRST((head)) = NULL; \ 768 (head)->tqh_last = &TAILQ_FIRST((head)); \ 769 QMD_TRACE_HEAD(head); \ 770 } while (0) 771 772 #define TAILQ_INSERT_AFTER(head, listelm, elm, field) do { \ 773 QMD_TAILQ_CHECK_NEXT(listelm, field); \ 774 if ((TAILQ_NEXT((elm), field) = TAILQ_NEXT((listelm), field)) != NULL)\ 775 TAILQ_NEXT((elm), field)->field.tqe_prev = \ 776 &TAILQ_NEXT((elm), field); \ 777 else { \ 778 (head)->tqh_last = &TAILQ_NEXT((elm), field); \ 779 QMD_TRACE_HEAD(head); \ 780 } \ 781 TAILQ_NEXT((listelm), field) = (elm); \ 782 (elm)->field.tqe_prev = &TAILQ_NEXT((listelm), field); \ 783 QMD_TRACE_ELEM(&(elm)->field); \ 784 QMD_TRACE_ELEM(&(listelm)->field); \ 785 } while (0) 786 787 #define TAILQ_INSERT_BEFORE(listelm, elm, field) do { \ 788 QMD_TAILQ_CHECK_PREV(listelm, field); \ 789 (elm)->field.tqe_prev = (listelm)->field.tqe_prev; \ 790 TAILQ_NEXT((elm), field) = (listelm); \ 791 *(listelm)->field.tqe_prev = (elm); \ 792 (listelm)->field.tqe_prev = &TAILQ_NEXT((elm), field); \ 793 QMD_TRACE_ELEM(&(elm)->field); \ 794 QMD_TRACE_ELEM(&(listelm)->field); \ 795 } while (0) 796 797 #define TAILQ_INSERT_HEAD(head, elm, field) do { \ 798 QMD_TAILQ_CHECK_HEAD(head, field); \ 799 if ((TAILQ_NEXT((elm), field) = TAILQ_FIRST((head))) != NULL) \ 800 TAILQ_FIRST((head))->field.tqe_prev = \ 801 &TAILQ_NEXT((elm), field); \ 802 else \ 803 (head)->tqh_last = &TAILQ_NEXT((elm), field); \ 804 TAILQ_FIRST((head)) = (elm); \ 805 (elm)->field.tqe_prev = &TAILQ_FIRST((head)); \ 806 QMD_TRACE_HEAD(head); \ 807 QMD_TRACE_ELEM(&(elm)->field); \ 808 } while (0) 809 810 #define TAILQ_INSERT_TAIL(head, elm, field) do { \ 811 QMD_TAILQ_CHECK_TAIL(head, field); \ 812 TAILQ_NEXT((elm), field) = NULL; \ 813 (elm)->field.tqe_prev = (head)->tqh_last; \ 814 *(head)->tqh_last = (elm); \ 815 (head)->tqh_last = &TAILQ_NEXT((elm), field); \ 816 QMD_TRACE_HEAD(head); \ 817 QMD_TRACE_ELEM(&(elm)->field); \ 818 } while (0) 819 820 #define TAILQ_LAST(head, headname) \ 821 (*(((struct headname *)((head)->tqh_last))->tqh_last)) 822 823 /* 824 * The FAST function is fast in that it causes no data access other 825 * then the access to the head. The standard LAST function above 826 * will cause a data access of both the element you want and 827 * the previous element. FAST is very useful for instances when 828 * you may want to prefetch the last data element. 829 */ 830 #define TAILQ_LAST_FAST(head, type, field) \ 831 (TAILQ_EMPTY(head) ? NULL : __containerof((head)->tqh_last, QUEUE_TYPEOF(type), field.tqe_next)) 832 833 #define TAILQ_NEXT(elm, field) ((elm)->field.tqe_next) 834 835 #define TAILQ_PREV(elm, headname, field) \ 836 (*(((struct headname *)((elm)->field.tqe_prev))->tqh_last)) 837 838 #define TAILQ_PREV_FAST(elm, head, type, field) \ 839 ((elm)->field.tqe_prev == &(head)->tqh_first ? NULL : \ 840 __containerof((elm)->field.tqe_prev, QUEUE_TYPEOF(type), field.tqe_next)) 841 842 #define TAILQ_REMOVE(head, elm, field) do { \ 843 QMD_SAVELINK(oldnext, (elm)->field.tqe_next); \ 844 QMD_SAVELINK(oldprev, (elm)->field.tqe_prev); \ 845 QMD_TAILQ_CHECK_NEXT(elm, field); \ 846 QMD_TAILQ_CHECK_PREV(elm, field); \ 847 if ((TAILQ_NEXT((elm), field)) != NULL) \ 848 TAILQ_NEXT((elm), field)->field.tqe_prev = \ 849 (elm)->field.tqe_prev; \ 850 else { \ 851 (head)->tqh_last = (elm)->field.tqe_prev; \ 852 QMD_TRACE_HEAD(head); \ 853 } \ 854 *(elm)->field.tqe_prev = TAILQ_NEXT((elm), field); \ 855 TRASHIT(*oldnext); \ 856 TRASHIT(*oldprev); \ 857 QMD_TRACE_ELEM(&(elm)->field); \ 858 } while (0) 859 860 #define TAILQ_SWAP(head1, head2, type, field) do { \ 861 QUEUE_TYPEOF(type) *swap_first = (head1)->tqh_first; \ 862 QUEUE_TYPEOF(type) **swap_last = (head1)->tqh_last; \ 863 (head1)->tqh_first = (head2)->tqh_first; \ 864 (head1)->tqh_last = (head2)->tqh_last; \ 865 (head2)->tqh_first = swap_first; \ 866 (head2)->tqh_last = swap_last; \ 867 if ((swap_first = (head1)->tqh_first) != NULL) \ 868 swap_first->field.tqe_prev = &(head1)->tqh_first; \ 869 else \ 870 (head1)->tqh_last = &(head1)->tqh_first; \ 871 if ((swap_first = (head2)->tqh_first) != NULL) \ 872 swap_first->field.tqe_prev = &(head2)->tqh_first; \ 873 else \ 874 (head2)->tqh_last = &(head2)->tqh_first; \ 875 } while (0) 876 877 #define TAILQ_END(head) NULL 878 879 #endif /* !_SYS_QUEUE_H_ */ 880