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 32 #ifndef _SYS_QUEUE_H_ 33 #define _SYS_QUEUE_H_ 34 35 #include <sys/cdefs.h> 36 37 /* 38 * This file defines four types of data structures: singly-linked lists, 39 * singly-linked tail queues, lists and tail queues. 40 * 41 * A singly-linked list is headed by a single forward pointer. The elements 42 * are singly linked for minimum space and pointer manipulation overhead at 43 * the expense of O(n) removal for arbitrary elements. New elements can be 44 * added to the list after an existing element or at the head of the list. 45 * Elements being removed from the head of the list should use the explicit 46 * macro for this purpose for optimum efficiency. A singly-linked list may 47 * only be traversed in the forward direction. Singly-linked lists are ideal 48 * for applications with large datasets and few or no removals or for 49 * implementing a LIFO queue. 50 * 51 * A singly-linked tail queue is headed by a pair of pointers, one to the 52 * head of the list and the other to the tail of the list. The elements are 53 * singly linked for minimum space and pointer manipulation overhead at the 54 * expense of O(n) removal for arbitrary elements. New elements can be added 55 * to the list after an existing element, at the head of the list, or at the 56 * end of the list. Elements being removed from the head of the tail queue 57 * should use the explicit macro for this purpose for optimum efficiency. 58 * A singly-linked tail queue may only be traversed in the forward direction. 59 * Singly-linked tail queues are ideal for applications with large datasets 60 * and few or no removals or for implementing a FIFO queue. 61 * 62 * A list is headed by a single forward pointer (or an array of forward 63 * pointers for a hash table header). The elements are doubly linked 64 * so that an arbitrary element can be removed without a need to 65 * traverse the list. New elements can be added to the list before 66 * or after an existing element or at the head of the list. A list 67 * may be traversed in either direction. 68 * 69 * A tail queue is headed by a pair of pointers, one to the head of the 70 * list and the other to the tail of the list. The elements are doubly 71 * linked so that an arbitrary element can be removed without a need to 72 * traverse the list. New elements can be added to the list before or 73 * after an existing element, at the head of the list, or at the end of 74 * the list. A tail queue may be traversed in either direction. 75 * 76 * For details on the use of these macros, see the queue(3) manual page. 77 * 78 * Below is a summary of implemented functions where: 79 * + means the macro is available 80 * - means the macro is not available 81 * s means the macro is available but is slow (runs in O(n) time) 82 * 83 * SLIST LIST STAILQ TAILQ 84 * _HEAD + + + + 85 * _CLASS_HEAD + + + + 86 * _HEAD_INITIALIZER + + + + 87 * _ENTRY + + + + 88 * _CLASS_ENTRY + + + + 89 * _INIT + + + + 90 * _EMPTY + + + + 91 * _END + + + + 92 * _FIRST + + + + 93 * _NEXT + + + + 94 * _PREV - + - + 95 * _LAST - - + + 96 * _LAST_FAST - - - + 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 * _REPLACE - + - + 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 */ 125 struct 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) \ 179 struct name { \ 180 struct type *slh_first; /* first element */ \ 181 } 182 183 #define SLIST_CLASS_HEAD(name, type) \ 184 struct name { \ 185 class type *slh_first; /* first element */ \ 186 } 187 188 #define SLIST_HEAD_INITIALIZER(head) \ 189 { NULL } 190 191 #define SLIST_ENTRY(type) \ 192 struct { \ 193 struct type *sle_next; /* next element */ \ 194 } 195 196 #define SLIST_CLASS_ENTRY(type) \ 197 struct { \ 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 if (SLIST_FIRST((head)) == (elm)) { \ 274 SLIST_REMOVE_HEAD((head), field); \ 275 } \ 276 else { \ 277 QUEUE_TYPEOF(type) *curelm = SLIST_FIRST(head); \ 278 while (SLIST_NEXT(curelm, field) != (elm)) \ 279 curelm = SLIST_NEXT(curelm, field); \ 280 SLIST_REMOVE_AFTER(curelm, field); \ 281 } \ 282 } while (0) 283 284 #define SLIST_REMOVE_AFTER(elm, field) do { \ 285 QMD_SAVELINK(oldnext, SLIST_NEXT(elm, field)->field.sle_next); \ 286 SLIST_NEXT(elm, field) = \ 287 SLIST_NEXT(SLIST_NEXT(elm, field), field); \ 288 TRASHIT(*oldnext); \ 289 } while (0) 290 291 #define SLIST_REMOVE_HEAD(head, field) do { \ 292 QMD_SAVELINK(oldnext, SLIST_FIRST(head)->field.sle_next); \ 293 SLIST_FIRST((head)) = SLIST_NEXT(SLIST_FIRST((head)), field); \ 294 TRASHIT(*oldnext); \ 295 } while (0) 296 297 #define SLIST_REMOVE_PREVPTR(prevp, elm, field) do { \ 298 QMD_SLIST_CHECK_PREVPTR(prevp, elm); \ 299 *(prevp) = SLIST_NEXT(elm, field); \ 300 TRASHIT((elm)->field.sle_next); \ 301 } while (0) 302 303 #define SLIST_SWAP(head1, head2, type) do { \ 304 QUEUE_TYPEOF(type) *swap_first = SLIST_FIRST(head1); \ 305 SLIST_FIRST(head1) = SLIST_FIRST(head2); \ 306 SLIST_FIRST(head2) = swap_first; \ 307 } while (0) 308 309 #define SLIST_END(head) NULL 310 311 /* 312 * Singly-linked Tail queue declarations. 313 */ 314 #define STAILQ_HEAD(name, type) \ 315 struct name { \ 316 struct type *stqh_first;/* first element */ \ 317 struct type **stqh_last;/* addr of last next element */ \ 318 } 319 320 #define STAILQ_CLASS_HEAD(name, type) \ 321 struct name { \ 322 class type *stqh_first; /* first element */ \ 323 class type **stqh_last; /* addr of last next element */ \ 324 } 325 326 #define STAILQ_HEAD_INITIALIZER(head) \ 327 { NULL, &(head).stqh_first } 328 329 #define STAILQ_ENTRY(type) \ 330 struct { \ 331 struct type *stqe_next; /* next element */ \ 332 } 333 334 #define STAILQ_CLASS_ENTRY(type) \ 335 struct { \ 336 class type *stqe_next; /* next element */ \ 337 } 338 339 /* 340 * Singly-linked Tail queue functions. 341 */ 342 #define STAILQ_CONCAT(head1, head2) do { \ 343 if (!STAILQ_EMPTY((head2))) { \ 344 *(head1)->stqh_last = (head2)->stqh_first; \ 345 (head1)->stqh_last = (head2)->stqh_last; \ 346 STAILQ_INIT((head2)); \ 347 } \ 348 } while (0) 349 350 #define STAILQ_EMPTY(head) ((head)->stqh_first == NULL) 351 352 #define STAILQ_FIRST(head) ((head)->stqh_first) 353 354 #define STAILQ_FOREACH(var, head, field) \ 355 for((var) = STAILQ_FIRST((head)); \ 356 (var); \ 357 (var) = STAILQ_NEXT((var), field)) 358 359 #define STAILQ_FOREACH_FROM(var, head, field) \ 360 for ((var) = ((var) ? (var) : STAILQ_FIRST((head))); \ 361 (var); \ 362 (var) = STAILQ_NEXT((var), field)) 363 364 #define STAILQ_FOREACH_SAFE(var, head, field, tvar) \ 365 for ((var) = STAILQ_FIRST((head)); \ 366 (var) && ((tvar) = STAILQ_NEXT((var), field), 1); \ 367 (var) = (tvar)) 368 369 #define STAILQ_FOREACH_FROM_SAFE(var, head, field, tvar) \ 370 for ((var) = ((var) ? (var) : STAILQ_FIRST((head))); \ 371 (var) && ((tvar) = STAILQ_NEXT((var), field), 1); \ 372 (var) = (tvar)) 373 374 #define STAILQ_INIT(head) do { \ 375 STAILQ_FIRST((head)) = NULL; \ 376 (head)->stqh_last = &STAILQ_FIRST((head)); \ 377 } while (0) 378 379 #define STAILQ_INSERT_AFTER(head, tqelm, elm, field) do { \ 380 if ((STAILQ_NEXT((elm), field) = STAILQ_NEXT((tqelm), field)) == NULL)\ 381 (head)->stqh_last = &STAILQ_NEXT((elm), field); \ 382 STAILQ_NEXT((tqelm), field) = (elm); \ 383 } while (0) 384 385 #define STAILQ_INSERT_HEAD(head, elm, field) do { \ 386 if ((STAILQ_NEXT((elm), field) = STAILQ_FIRST((head))) == NULL) \ 387 (head)->stqh_last = &STAILQ_NEXT((elm), field); \ 388 STAILQ_FIRST((head)) = (elm); \ 389 } while (0) 390 391 #define STAILQ_INSERT_TAIL(head, elm, field) do { \ 392 STAILQ_NEXT((elm), field) = NULL; \ 393 *(head)->stqh_last = (elm); \ 394 (head)->stqh_last = &STAILQ_NEXT((elm), field); \ 395 } while (0) 396 397 #define STAILQ_LAST(head, type, field) \ 398 (STAILQ_EMPTY((head)) ? NULL : \ 399 __containerof((head)->stqh_last, \ 400 QUEUE_TYPEOF(type), field.stqe_next)) 401 402 #define STAILQ_NEXT(elm, field) ((elm)->field.stqe_next) 403 404 #define STAILQ_REMOVE(head, elm, type, field) do { \ 405 QMD_SAVELINK(oldnext, (elm)->field.stqe_next); \ 406 if (STAILQ_FIRST((head)) == (elm)) { \ 407 STAILQ_REMOVE_HEAD((head), field); \ 408 } \ 409 else { \ 410 QUEUE_TYPEOF(type) *curelm = STAILQ_FIRST(head); \ 411 while (STAILQ_NEXT(curelm, field) != (elm)) \ 412 curelm = STAILQ_NEXT(curelm, field); \ 413 STAILQ_REMOVE_AFTER(head, curelm, field); \ 414 } \ 415 TRASHIT(*oldnext); \ 416 } while (0) 417 418 #define STAILQ_REMOVE_AFTER(head, elm, field) do { \ 419 if ((STAILQ_NEXT(elm, field) = \ 420 STAILQ_NEXT(STAILQ_NEXT(elm, field), field)) == NULL) \ 421 (head)->stqh_last = &STAILQ_NEXT((elm), field); \ 422 } while (0) 423 424 #define STAILQ_REMOVE_HEAD(head, field) do { \ 425 if ((STAILQ_FIRST((head)) = \ 426 STAILQ_NEXT(STAILQ_FIRST((head)), field)) == NULL) \ 427 (head)->stqh_last = &STAILQ_FIRST((head)); \ 428 } while (0) 429 430 #define STAILQ_SWAP(head1, head2, type) do { \ 431 QUEUE_TYPEOF(type) *swap_first = STAILQ_FIRST(head1); \ 432 QUEUE_TYPEOF(type) **swap_last = (head1)->stqh_last; \ 433 STAILQ_FIRST(head1) = STAILQ_FIRST(head2); \ 434 (head1)->stqh_last = (head2)->stqh_last; \ 435 STAILQ_FIRST(head2) = swap_first; \ 436 (head2)->stqh_last = swap_last; \ 437 if (STAILQ_EMPTY(head1)) \ 438 (head1)->stqh_last = &STAILQ_FIRST(head1); \ 439 if (STAILQ_EMPTY(head2)) \ 440 (head2)->stqh_last = &STAILQ_FIRST(head2); \ 441 } while (0) 442 443 #define STAILQ_END(head) NULL 444 445 446 /* 447 * List declarations. 448 */ 449 #define LIST_HEAD(name, type) \ 450 struct name { \ 451 struct type *lh_first; /* first element */ \ 452 } 453 454 #define LIST_CLASS_HEAD(name, type) \ 455 struct name { \ 456 class type *lh_first; /* first element */ \ 457 } 458 459 #define LIST_HEAD_INITIALIZER(head) \ 460 { NULL } 461 462 #define LIST_ENTRY(type) \ 463 struct { \ 464 struct type *le_next; /* next element */ \ 465 struct type **le_prev; /* address of previous next element */ \ 466 } 467 468 #define LIST_CLASS_ENTRY(type) \ 469 struct { \ 470 class type *le_next; /* next element */ \ 471 class type **le_prev; /* address of previous next element */ \ 472 } 473 474 /* 475 * List functions. 476 */ 477 478 #if (defined(_KERNEL) && defined(INVARIANTS)) 479 /* 480 * QMD_LIST_CHECK_HEAD(LIST_HEAD *head, LIST_ENTRY NAME) 481 * 482 * If the list is non-empty, validates that the first element of the list 483 * points back at 'head.' 484 */ 485 #define QMD_LIST_CHECK_HEAD(head, field) do { \ 486 if (LIST_FIRST((head)) != NULL && \ 487 LIST_FIRST((head))->field.le_prev != \ 488 &LIST_FIRST((head))) \ 489 panic("Bad list head %p first->prev != head", (head)); \ 490 } while (0) 491 492 /* 493 * QMD_LIST_CHECK_NEXT(TYPE *elm, LIST_ENTRY NAME) 494 * 495 * If an element follows 'elm' in the list, validates that the next element 496 * points back at 'elm.' 497 */ 498 #define QMD_LIST_CHECK_NEXT(elm, field) do { \ 499 if (LIST_NEXT((elm), field) != NULL && \ 500 LIST_NEXT((elm), field)->field.le_prev != \ 501 &((elm)->field.le_next)) \ 502 panic("Bad link elm %p next->prev != elm", (elm)); \ 503 } while (0) 504 505 /* 506 * QMD_LIST_CHECK_PREV(TYPE *elm, LIST_ENTRY NAME) 507 * 508 * Validates that the previous element (or head of the list) points to 'elm.' 509 */ 510 #define QMD_LIST_CHECK_PREV(elm, field) do { \ 511 if (*(elm)->field.le_prev != (elm)) \ 512 panic("Bad link elm %p prev->next != elm", (elm)); \ 513 } while (0) 514 #else 515 #define QMD_LIST_CHECK_HEAD(head, field) 516 #define QMD_LIST_CHECK_NEXT(elm, field) 517 #define QMD_LIST_CHECK_PREV(elm, field) 518 #endif /* (_KERNEL && INVARIANTS) */ 519 520 #define LIST_CONCAT(head1, head2, type, field) do { \ 521 QUEUE_TYPEOF(type) *curelm = LIST_FIRST(head1); \ 522 if (curelm == NULL) { \ 523 if ((LIST_FIRST(head1) = LIST_FIRST(head2)) != NULL) { \ 524 LIST_FIRST(head2)->field.le_prev = \ 525 &LIST_FIRST((head1)); \ 526 LIST_INIT(head2); \ 527 } \ 528 } else if (LIST_FIRST(head2) != NULL) { \ 529 while (LIST_NEXT(curelm, field) != NULL) \ 530 curelm = LIST_NEXT(curelm, field); \ 531 LIST_NEXT(curelm, field) = LIST_FIRST(head2); \ 532 LIST_FIRST(head2)->field.le_prev = &LIST_NEXT(curelm, field);\ 533 LIST_INIT(head2); \ 534 } \ 535 } while (0) 536 537 #define LIST_EMPTY(head) ((head)->lh_first == NULL) 538 539 #define LIST_FIRST(head) ((head)->lh_first) 540 541 #define LIST_FOREACH(var, head, field) \ 542 for ((var) = LIST_FIRST((head)); \ 543 (var); \ 544 (var) = LIST_NEXT((var), field)) 545 546 #define LIST_FOREACH_FROM(var, head, field) \ 547 for ((var) = ((var) ? (var) : LIST_FIRST((head))); \ 548 (var); \ 549 (var) = LIST_NEXT((var), field)) 550 551 #define LIST_FOREACH_SAFE(var, head, field, tvar) \ 552 for ((var) = LIST_FIRST((head)); \ 553 (var) && ((tvar) = LIST_NEXT((var), field), 1); \ 554 (var) = (tvar)) 555 556 #define LIST_FOREACH_FROM_SAFE(var, head, field, tvar) \ 557 for ((var) = ((var) ? (var) : LIST_FIRST((head))); \ 558 (var) && ((tvar) = LIST_NEXT((var), field), 1); \ 559 (var) = (tvar)) 560 561 #define LIST_INIT(head) do { \ 562 LIST_FIRST((head)) = NULL; \ 563 } while (0) 564 565 #define LIST_INSERT_AFTER(listelm, elm, field) do { \ 566 QMD_LIST_CHECK_NEXT(listelm, field); \ 567 if ((LIST_NEXT((elm), field) = LIST_NEXT((listelm), field)) != NULL)\ 568 LIST_NEXT((listelm), field)->field.le_prev = \ 569 &LIST_NEXT((elm), field); \ 570 LIST_NEXT((listelm), field) = (elm); \ 571 (elm)->field.le_prev = &LIST_NEXT((listelm), field); \ 572 } while (0) 573 574 #define LIST_INSERT_BEFORE(listelm, elm, field) do { \ 575 QMD_LIST_CHECK_PREV(listelm, field); \ 576 (elm)->field.le_prev = (listelm)->field.le_prev; \ 577 LIST_NEXT((elm), field) = (listelm); \ 578 *(listelm)->field.le_prev = (elm); \ 579 (listelm)->field.le_prev = &LIST_NEXT((elm), field); \ 580 } while (0) 581 582 #define LIST_INSERT_HEAD(head, elm, field) do { \ 583 QMD_LIST_CHECK_HEAD((head), field); \ 584 if ((LIST_NEXT((elm), field) = LIST_FIRST((head))) != NULL) \ 585 LIST_FIRST((head))->field.le_prev = &LIST_NEXT((elm), field);\ 586 LIST_FIRST((head)) = (elm); \ 587 (elm)->field.le_prev = &LIST_FIRST((head)); \ 588 } while (0) 589 590 #define LIST_NEXT(elm, field) ((elm)->field.le_next) 591 592 #define LIST_PREV(elm, head, type, field) \ 593 ((elm)->field.le_prev == &LIST_FIRST((head)) ? NULL : \ 594 __containerof((elm)->field.le_prev, \ 595 QUEUE_TYPEOF(type), field.le_next)) 596 597 #define LIST_REMOVE_HEAD(head, field) \ 598 LIST_REMOVE(LIST_FIRST(head), field) 599 600 #define LIST_REMOVE(elm, field) do { \ 601 QMD_SAVELINK(oldnext, (elm)->field.le_next); \ 602 QMD_SAVELINK(oldprev, (elm)->field.le_prev); \ 603 QMD_LIST_CHECK_NEXT(elm, field); \ 604 QMD_LIST_CHECK_PREV(elm, field); \ 605 if (LIST_NEXT((elm), field) != NULL) \ 606 LIST_NEXT((elm), field)->field.le_prev = \ 607 (elm)->field.le_prev; \ 608 *(elm)->field.le_prev = LIST_NEXT((elm), field); \ 609 TRASHIT(*oldnext); \ 610 TRASHIT(*oldprev); \ 611 } while (0) 612 613 #define LIST_REPLACE(elm, elm2, field) do { \ 614 QMD_SAVELINK(oldnext, (elm)->field.le_next); \ 615 QMD_SAVELINK(oldprev, (elm)->field.le_prev); \ 616 QMD_LIST_CHECK_NEXT(elm, field); \ 617 QMD_LIST_CHECK_PREV(elm, field); \ 618 LIST_NEXT((elm2), field) = LIST_NEXT((elm), field); \ 619 if (LIST_NEXT((elm2), field) != NULL) \ 620 LIST_NEXT((elm2), field)->field.le_prev = \ 621 &(elm2)->field.le_next; \ 622 (elm2)->field.le_prev = (elm)->field.le_prev; \ 623 *(elm2)->field.le_prev = (elm2); \ 624 TRASHIT(*oldnext); \ 625 TRASHIT(*oldprev); \ 626 } while (0) 627 628 #define LIST_SWAP(head1, head2, type, field) do { \ 629 QUEUE_TYPEOF(type) *swap_tmp = LIST_FIRST(head1); \ 630 LIST_FIRST((head1)) = LIST_FIRST((head2)); \ 631 LIST_FIRST((head2)) = swap_tmp; \ 632 if ((swap_tmp = LIST_FIRST((head1))) != NULL) \ 633 swap_tmp->field.le_prev = &LIST_FIRST((head1)); \ 634 if ((swap_tmp = LIST_FIRST((head2))) != NULL) \ 635 swap_tmp->field.le_prev = &LIST_FIRST((head2)); \ 636 } while (0) 637 638 #define LIST_END(head) NULL 639 640 /* 641 * Tail queue declarations. 642 */ 643 #define TAILQ_HEAD(name, type) \ 644 struct name { \ 645 struct type *tqh_first; /* first element */ \ 646 struct type **tqh_last; /* addr of last next element */ \ 647 TRACEBUF \ 648 } 649 650 #define TAILQ_CLASS_HEAD(name, type) \ 651 struct name { \ 652 class type *tqh_first; /* first element */ \ 653 class type **tqh_last; /* addr of last next element */ \ 654 TRACEBUF \ 655 } 656 657 #define TAILQ_HEAD_INITIALIZER(head) \ 658 { NULL, &(head).tqh_first, TRACEBUF_INITIALIZER } 659 660 #define TAILQ_ENTRY(type) \ 661 struct { \ 662 struct type *tqe_next; /* next element */ \ 663 struct type **tqe_prev; /* address of previous next element */ \ 664 TRACEBUF \ 665 } 666 667 #define TAILQ_CLASS_ENTRY(type) \ 668 struct { \ 669 class type *tqe_next; /* next element */ \ 670 class type **tqe_prev; /* address of previous next element */ \ 671 TRACEBUF \ 672 } 673 674 /* 675 * Tail queue functions. 676 */ 677 #if (defined(_KERNEL) && defined(INVARIANTS)) 678 /* 679 * QMD_TAILQ_CHECK_HEAD(TAILQ_HEAD *head, TAILQ_ENTRY NAME) 680 * 681 * If the tailq is non-empty, validates that the first element of the tailq 682 * points back at 'head.' 683 */ 684 #define QMD_TAILQ_CHECK_HEAD(head, field) do { \ 685 if (!TAILQ_EMPTY(head) && \ 686 TAILQ_FIRST((head))->field.tqe_prev != \ 687 &TAILQ_FIRST((head))) \ 688 panic("Bad tailq head %p first->prev != head", (head)); \ 689 } while (0) 690 691 /* 692 * QMD_TAILQ_CHECK_TAIL(TAILQ_HEAD *head, TAILQ_ENTRY NAME) 693 * 694 * Validates that the tail of the tailq is a pointer to pointer to NULL. 695 */ 696 #define QMD_TAILQ_CHECK_TAIL(head, field) do { \ 697 if (*(head)->tqh_last != NULL) \ 698 panic("Bad tailq NEXT(%p->tqh_last) != NULL", (head)); \ 699 } while (0) 700 701 /* 702 * QMD_TAILQ_CHECK_NEXT(TYPE *elm, TAILQ_ENTRY NAME) 703 * 704 * If an element follows 'elm' in the tailq, validates that the next element 705 * points back at 'elm.' 706 */ 707 #define QMD_TAILQ_CHECK_NEXT(elm, field) do { \ 708 if (TAILQ_NEXT((elm), field) != NULL && \ 709 TAILQ_NEXT((elm), field)->field.tqe_prev != \ 710 &((elm)->field.tqe_next)) \ 711 panic("Bad link elm %p next->prev != elm", (elm)); \ 712 } while (0) 713 714 /* 715 * QMD_TAILQ_CHECK_PREV(TYPE *elm, TAILQ_ENTRY NAME) 716 * 717 * Validates that the previous element (or head of the tailq) points to 'elm.' 718 */ 719 #define QMD_TAILQ_CHECK_PREV(elm, field) do { \ 720 if (*(elm)->field.tqe_prev != (elm)) \ 721 panic("Bad link elm %p prev->next != elm", (elm)); \ 722 } while (0) 723 #else 724 #define QMD_TAILQ_CHECK_HEAD(head, field) 725 #define QMD_TAILQ_CHECK_TAIL(head, headname) 726 #define QMD_TAILQ_CHECK_NEXT(elm, field) 727 #define QMD_TAILQ_CHECK_PREV(elm, field) 728 #endif /* (_KERNEL && INVARIANTS) */ 729 730 #define TAILQ_CONCAT(head1, head2, field) do { \ 731 if (!TAILQ_EMPTY(head2)) { \ 732 *(head1)->tqh_last = (head2)->tqh_first; \ 733 (head2)->tqh_first->field.tqe_prev = (head1)->tqh_last; \ 734 (head1)->tqh_last = (head2)->tqh_last; \ 735 TAILQ_INIT((head2)); \ 736 QMD_TRACE_HEAD(head1); \ 737 QMD_TRACE_HEAD(head2); \ 738 } \ 739 } while (0) 740 741 #define TAILQ_EMPTY(head) ((head)->tqh_first == NULL) 742 743 #define TAILQ_FIRST(head) ((head)->tqh_first) 744 745 #define TAILQ_FOREACH(var, head, field) \ 746 for ((var) = TAILQ_FIRST((head)); \ 747 (var); \ 748 (var) = TAILQ_NEXT((var), field)) 749 750 #define TAILQ_FOREACH_FROM(var, head, field) \ 751 for ((var) = ((var) ? (var) : TAILQ_FIRST((head))); \ 752 (var); \ 753 (var) = TAILQ_NEXT((var), field)) 754 755 #define TAILQ_FOREACH_SAFE(var, head, field, tvar) \ 756 for ((var) = TAILQ_FIRST((head)); \ 757 (var) && ((tvar) = TAILQ_NEXT((var), field), 1); \ 758 (var) = (tvar)) 759 760 #define TAILQ_FOREACH_FROM_SAFE(var, head, field, tvar) \ 761 for ((var) = ((var) ? (var) : TAILQ_FIRST((head))); \ 762 (var) && ((tvar) = TAILQ_NEXT((var), field), 1); \ 763 (var) = (tvar)) 764 765 #define TAILQ_FOREACH_REVERSE(var, head, headname, field) \ 766 for ((var) = TAILQ_LAST((head), headname); \ 767 (var); \ 768 (var) = TAILQ_PREV((var), headname, field)) 769 770 #define TAILQ_FOREACH_REVERSE_FROM(var, head, headname, field) \ 771 for ((var) = ((var) ? (var) : TAILQ_LAST((head), headname)); \ 772 (var); \ 773 (var) = TAILQ_PREV((var), headname, field)) 774 775 #define TAILQ_FOREACH_REVERSE_SAFE(var, head, headname, field, tvar) \ 776 for ((var) = TAILQ_LAST((head), headname); \ 777 (var) && ((tvar) = TAILQ_PREV((var), headname, field), 1); \ 778 (var) = (tvar)) 779 780 #define TAILQ_FOREACH_REVERSE_FROM_SAFE(var, head, headname, field, tvar)\ 781 for ((var) = ((var) ? (var) : TAILQ_LAST((head), headname)); \ 782 (var) && ((tvar) = TAILQ_PREV((var), headname, field), 1); \ 783 (var) = (tvar)) 784 785 #define TAILQ_INIT(head) do { \ 786 TAILQ_FIRST((head)) = NULL; \ 787 (head)->tqh_last = &TAILQ_FIRST((head)); \ 788 QMD_TRACE_HEAD(head); \ 789 } while (0) 790 791 #define TAILQ_INSERT_AFTER(head, listelm, elm, field) do { \ 792 QMD_TAILQ_CHECK_NEXT(listelm, field); \ 793 if ((TAILQ_NEXT((elm), field) = TAILQ_NEXT((listelm), field)) != NULL)\ 794 TAILQ_NEXT((elm), field)->field.tqe_prev = \ 795 &TAILQ_NEXT((elm), field); \ 796 else { \ 797 (head)->tqh_last = &TAILQ_NEXT((elm), field); \ 798 QMD_TRACE_HEAD(head); \ 799 } \ 800 TAILQ_NEXT((listelm), field) = (elm); \ 801 (elm)->field.tqe_prev = &TAILQ_NEXT((listelm), field); \ 802 QMD_TRACE_ELEM(&(elm)->field); \ 803 QMD_TRACE_ELEM(&(listelm)->field); \ 804 } while (0) 805 806 #define TAILQ_INSERT_BEFORE(listelm, elm, field) do { \ 807 QMD_TAILQ_CHECK_PREV(listelm, field); \ 808 (elm)->field.tqe_prev = (listelm)->field.tqe_prev; \ 809 TAILQ_NEXT((elm), field) = (listelm); \ 810 *(listelm)->field.tqe_prev = (elm); \ 811 (listelm)->field.tqe_prev = &TAILQ_NEXT((elm), field); \ 812 QMD_TRACE_ELEM(&(elm)->field); \ 813 QMD_TRACE_ELEM(&(listelm)->field); \ 814 } while (0) 815 816 #define TAILQ_INSERT_HEAD(head, elm, field) do { \ 817 QMD_TAILQ_CHECK_HEAD(head, field); \ 818 if ((TAILQ_NEXT((elm), field) = TAILQ_FIRST((head))) != NULL) \ 819 TAILQ_FIRST((head))->field.tqe_prev = \ 820 &TAILQ_NEXT((elm), field); \ 821 else \ 822 (head)->tqh_last = &TAILQ_NEXT((elm), field); \ 823 TAILQ_FIRST((head)) = (elm); \ 824 (elm)->field.tqe_prev = &TAILQ_FIRST((head)); \ 825 QMD_TRACE_HEAD(head); \ 826 QMD_TRACE_ELEM(&(elm)->field); \ 827 } while (0) 828 829 #define TAILQ_INSERT_TAIL(head, elm, field) do { \ 830 QMD_TAILQ_CHECK_TAIL(head, field); \ 831 TAILQ_NEXT((elm), field) = NULL; \ 832 (elm)->field.tqe_prev = (head)->tqh_last; \ 833 *(head)->tqh_last = (elm); \ 834 (head)->tqh_last = &TAILQ_NEXT((elm), field); \ 835 QMD_TRACE_HEAD(head); \ 836 QMD_TRACE_ELEM(&(elm)->field); \ 837 } while (0) 838 839 #define TAILQ_LAST(head, headname) \ 840 (*(((struct headname *)((head)->tqh_last))->tqh_last)) 841 842 /* 843 * The FAST function is fast in that it causes no data access other 844 * then the access to the head. The standard LAST function above 845 * will cause a data access of both the element you want and 846 * the previous element. FAST is very useful for instances when 847 * you may want to prefetch the last data element. 848 */ 849 #define TAILQ_LAST_FAST(head, type, field) \ 850 (TAILQ_EMPTY(head) ? NULL : __containerof((head)->tqh_last, QUEUE_TYPEOF(type), field.tqe_next)) 851 852 #define TAILQ_NEXT(elm, field) ((elm)->field.tqe_next) 853 854 #define TAILQ_PREV(elm, headname, field) \ 855 (*(((struct headname *)((elm)->field.tqe_prev))->tqh_last)) 856 857 #define TAILQ_PREV_FAST(elm, head, type, field) \ 858 ((elm)->field.tqe_prev == &(head)->tqh_first ? NULL : \ 859 __containerof((elm)->field.tqe_prev, QUEUE_TYPEOF(type), field.tqe_next)) 860 861 #define TAILQ_REMOVE_HEAD(head, field) \ 862 TAILQ_REMOVE(head, TAILQ_FIRST(head), field) 863 864 #define TAILQ_REMOVE(head, elm, field) do { \ 865 QMD_SAVELINK(oldnext, (elm)->field.tqe_next); \ 866 QMD_SAVELINK(oldprev, (elm)->field.tqe_prev); \ 867 QMD_TAILQ_CHECK_NEXT(elm, field); \ 868 QMD_TAILQ_CHECK_PREV(elm, field); \ 869 if ((TAILQ_NEXT((elm), field)) != NULL) \ 870 TAILQ_NEXT((elm), field)->field.tqe_prev = \ 871 (elm)->field.tqe_prev; \ 872 else { \ 873 (head)->tqh_last = (elm)->field.tqe_prev; \ 874 QMD_TRACE_HEAD(head); \ 875 } \ 876 *(elm)->field.tqe_prev = TAILQ_NEXT((elm), field); \ 877 TRASHIT(*oldnext); \ 878 TRASHIT(*oldprev); \ 879 QMD_TRACE_ELEM(&(elm)->field); \ 880 } while (0) 881 882 #define TAILQ_REPLACE(head, elm, elm2, field) do { \ 883 QMD_SAVELINK(oldnext, (elm)->field.tqe_next); \ 884 QMD_SAVELINK(oldprev, (elm)->field.tqe_prev); \ 885 QMD_TAILQ_CHECK_NEXT(elm, field); \ 886 QMD_TAILQ_CHECK_PREV(elm, field); \ 887 TAILQ_NEXT((elm2), field) = TAILQ_NEXT((elm), field); \ 888 if (TAILQ_NEXT((elm2), field) != TAILQ_END(head)) \ 889 TAILQ_NEXT((elm2), field)->field.tqe_prev = \ 890 &(elm2)->field.tqe_next; \ 891 else \ 892 (head)->tqh_last = &(elm2)->field.tqe_next; \ 893 (elm2)->field.tqe_prev = (elm)->field.tqe_prev; \ 894 *(elm2)->field.tqe_prev = (elm2); \ 895 TRASHIT(*oldnext); \ 896 TRASHIT(*oldprev); \ 897 QMD_TRACE_ELEM(&(elm)->field); \ 898 } while (0) 899 900 #define TAILQ_SWAP(head1, head2, type, field) do { \ 901 QUEUE_TYPEOF(type) *swap_first = (head1)->tqh_first; \ 902 QUEUE_TYPEOF(type) **swap_last = (head1)->tqh_last; \ 903 (head1)->tqh_first = (head2)->tqh_first; \ 904 (head1)->tqh_last = (head2)->tqh_last; \ 905 (head2)->tqh_first = swap_first; \ 906 (head2)->tqh_last = swap_last; \ 907 if ((swap_first = (head1)->tqh_first) != NULL) \ 908 swap_first->field.tqe_prev = &(head1)->tqh_first; \ 909 else \ 910 (head1)->tqh_last = &(head1)->tqh_first; \ 911 if ((swap_first = (head2)->tqh_first) != NULL) \ 912 swap_first->field.tqe_prev = &(head2)->tqh_first; \ 913 else \ 914 (head2)->tqh_last = &(head2)->tqh_first; \ 915 } while (0) 916 917 #define TAILQ_END(head) NULL 918 919 #endif /* !_SYS_QUEUE_H_ */ 920