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 #include <sys/cdefs.h> 39 40 /* 41 * This file defines four types of data structures: singly-linked lists, 42 * singly-linked tail queues, lists and tail queues. 43 * 44 * A singly-linked list is headed by a single forward pointer. The elements 45 * are singly linked for minimum space and pointer manipulation overhead at 46 * the expense of O(n) removal for arbitrary elements. New elements can be 47 * added to the list after an existing element or at the head of the list. 48 * Elements being removed from the head of the list should use the explicit 49 * macro for this purpose for optimum efficiency. A singly-linked list may 50 * only be traversed in the forward direction. Singly-linked lists are ideal 51 * for applications with large datasets and few or no removals or for 52 * implementing a LIFO queue. 53 * 54 * A singly-linked tail queue is headed by a pair of pointers, one to the 55 * head of the list and the other to the tail of the list. The elements are 56 * singly linked for minimum space and pointer manipulation overhead at the 57 * expense of O(n) removal for arbitrary elements. New elements can be added 58 * to the list after an existing element, at the head of the list, or at the 59 * end of the list. Elements being removed from the head of the tail queue 60 * should use the explicit macro for this purpose for optimum efficiency. 61 * A singly-linked tail queue may only be traversed in the forward direction. 62 * Singly-linked tail queues are ideal for applications with large datasets 63 * and few or no removals or for implementing a FIFO queue. 64 * 65 * A list is headed by a single forward pointer (or an array of forward 66 * pointers for a hash table header). The elements are doubly linked 67 * so that an arbitrary element can be removed without a need to 68 * traverse the list. New elements can be added to the list before 69 * or after an existing element or at the head of the list. A list 70 * may be traversed in either direction. 71 * 72 * A tail queue is headed by a pair of pointers, one to the head of the 73 * list and the other to the tail of the list. The elements are doubly 74 * linked so that an arbitrary element can be removed without a need to 75 * traverse the list. New elements can be added to the list before or 76 * after an existing element, at the head of the list, or at the end of 77 * the list. A tail queue may be traversed in either direction. 78 * 79 * For details on the use of these macros, see the queue(3) manual page. 80 * 81 * Below is a summary of implemented functions where: 82 * + means the macro is available 83 * - means the macro is not available 84 * s means the macro is available but is slow (runs in O(n) time) 85 * 86 * SLIST LIST STAILQ TAILQ 87 * _HEAD + + + + 88 * _CLASS_HEAD + + + + 89 * _HEAD_INITIALIZER + + + + 90 * _ENTRY + + + + 91 * _CLASS_ENTRY + + + + 92 * _INIT + + + + 93 * _EMPTY + + + + 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 #warn Use QUEUE_MACRO_DEBUG_TRACE and/or QUEUE_MACRO_DEBUG_TRASH 120 #define QUEUE_MACRO_DEBUG_TRACE 121 #define QUEUE_MACRO_DEBUG_TRASH 122 #endif 123 124 #ifdef QUEUE_MACRO_DEBUG_TRACE 125 /* Store the last 2 places the queue element or head was altered */ 126 struct qm_trace { 127 unsigned long lastline; 128 unsigned long prevline; 129 const char *lastfile; 130 const char *prevfile; 131 }; 132 133 #define TRACEBUF struct qm_trace trace; 134 #define TRACEBUF_INITIALIZER { __LINE__, 0, __FILE__, NULL } , 135 136 #define QMD_TRACE_HEAD(head) do { \ 137 (head)->trace.prevline = (head)->trace.lastline; \ 138 (head)->trace.prevfile = (head)->trace.lastfile; \ 139 (head)->trace.lastline = __LINE__; \ 140 (head)->trace.lastfile = __FILE__; \ 141 } while (0) 142 143 #define QMD_TRACE_ELEM(elem) do { \ 144 (elem)->trace.prevline = (elem)->trace.lastline; \ 145 (elem)->trace.prevfile = (elem)->trace.lastfile; \ 146 (elem)->trace.lastline = __LINE__; \ 147 (elem)->trace.lastfile = __FILE__; \ 148 } while (0) 149 150 #else /* !QUEUE_MACRO_DEBUG_TRACE */ 151 #define QMD_TRACE_ELEM(elem) 152 #define QMD_TRACE_HEAD(head) 153 #define TRACEBUF 154 #define TRACEBUF_INITIALIZER 155 #endif /* QUEUE_MACRO_DEBUG_TRACE */ 156 157 #ifdef QUEUE_MACRO_DEBUG_TRASH 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 TRASHIT(x) 162 #define QMD_IS_TRASHED(x) 0 163 #endif /* QUEUE_MACRO_DEBUG_TRASH */ 164 165 #if defined(QUEUE_MACRO_DEBUG_TRACE) || defined(QUEUE_MACRO_DEBUG_TRASH) 166 #define QMD_SAVELINK(name, link) void **name = (void *)&(link) 167 #else /* !QUEUE_MACRO_DEBUG_TRACE && !QUEUE_MACRO_DEBUG_TRASH */ 168 #define QMD_SAVELINK(name, link) 169 #endif /* QUEUE_MACRO_DEBUG_TRACE || QUEUE_MACRO_DEBUG_TRASH */ 170 171 #ifdef __cplusplus 172 /* 173 * In C++ there can be structure lists and class lists: 174 */ 175 #define QUEUE_TYPEOF(type) type 176 #else 177 #define QUEUE_TYPEOF(type) struct type 178 #endif 179 180 /* 181 * Singly-linked List declarations. 182 */ 183 #define SLIST_HEAD(name, type) \ 184 struct name { \ 185 struct type *slh_first; /* first element */ \ 186 } 187 188 #define SLIST_CLASS_HEAD(name, type) \ 189 struct name { \ 190 class type *slh_first; /* first element */ \ 191 } 192 193 #define SLIST_HEAD_INITIALIZER(head) \ 194 { NULL } 195 196 #define SLIST_ENTRY(type) \ 197 struct { \ 198 struct type *sle_next; /* next element */ \ 199 } 200 201 #define SLIST_CLASS_ENTRY(type) \ 202 struct { \ 203 class type *sle_next; /* next element */ \ 204 } 205 206 /* 207 * Singly-linked List functions. 208 */ 209 #if (defined(_KERNEL) && defined(INVARIANTS)) 210 #define QMD_SLIST_CHECK_PREVPTR(prevp, elm) do { \ 211 if (*(prevp) != (elm)) \ 212 panic("Bad prevptr *(%p) == %p != %p", \ 213 (prevp), *(prevp), (elm)); \ 214 } while (0) 215 #else 216 #define QMD_SLIST_CHECK_PREVPTR(prevp, elm) 217 #endif 218 219 #define SLIST_CONCAT(head1, head2, type, field) do { \ 220 QUEUE_TYPEOF(type) *curelm = SLIST_FIRST(head1); \ 221 if (curelm == NULL) { \ 222 if ((SLIST_FIRST(head1) = SLIST_FIRST(head2)) != NULL) \ 223 SLIST_INIT(head2); \ 224 } else if (SLIST_FIRST(head2) != NULL) { \ 225 while (SLIST_NEXT(curelm, field) != NULL) \ 226 curelm = SLIST_NEXT(curelm, field); \ 227 SLIST_NEXT(curelm, field) = SLIST_FIRST(head2); \ 228 SLIST_INIT(head2); \ 229 } \ 230 } while (0) 231 232 #define SLIST_EMPTY(head) ((head)->slh_first == NULL) 233 234 #define SLIST_FIRST(head) ((head)->slh_first) 235 236 #define SLIST_FOREACH(var, head, field) \ 237 for ((var) = SLIST_FIRST((head)); \ 238 (var); \ 239 (var) = SLIST_NEXT((var), field)) 240 241 #define SLIST_FOREACH_FROM(var, head, field) \ 242 for ((var) = ((var) ? (var) : SLIST_FIRST((head))); \ 243 (var); \ 244 (var) = SLIST_NEXT((var), field)) 245 246 #define SLIST_FOREACH_SAFE(var, head, field, tvar) \ 247 for ((var) = SLIST_FIRST((head)); \ 248 (var) && ((tvar) = SLIST_NEXT((var), field), 1); \ 249 (var) = (tvar)) 250 251 #define SLIST_FOREACH_FROM_SAFE(var, head, field, tvar) \ 252 for ((var) = ((var) ? (var) : SLIST_FIRST((head))); \ 253 (var) && ((tvar) = SLIST_NEXT((var), field), 1); \ 254 (var) = (tvar)) 255 256 #define SLIST_FOREACH_PREVPTR(var, varp, head, field) \ 257 for ((varp) = &SLIST_FIRST((head)); \ 258 ((var) = *(varp)) != NULL; \ 259 (varp) = &SLIST_NEXT((var), field)) 260 261 #define SLIST_INIT(head) do { \ 262 SLIST_FIRST((head)) = NULL; \ 263 } while (0) 264 265 #define SLIST_INSERT_AFTER(slistelm, elm, field) do { \ 266 SLIST_NEXT((elm), field) = SLIST_NEXT((slistelm), field); \ 267 SLIST_NEXT((slistelm), field) = (elm); \ 268 } while (0) 269 270 #define SLIST_INSERT_HEAD(head, elm, field) do { \ 271 SLIST_NEXT((elm), field) = SLIST_FIRST((head)); \ 272 SLIST_FIRST((head)) = (elm); \ 273 } while (0) 274 275 #define SLIST_NEXT(elm, field) ((elm)->field.sle_next) 276 277 #define SLIST_REMOVE(head, elm, type, field) do { \ 278 QMD_SAVELINK(oldnext, (elm)->field.sle_next); \ 279 if (SLIST_FIRST((head)) == (elm)) { \ 280 SLIST_REMOVE_HEAD((head), field); \ 281 } \ 282 else { \ 283 QUEUE_TYPEOF(type) *curelm = SLIST_FIRST(head); \ 284 while (SLIST_NEXT(curelm, field) != (elm)) \ 285 curelm = SLIST_NEXT(curelm, field); \ 286 SLIST_REMOVE_AFTER(curelm, field); \ 287 } \ 288 TRASHIT(*oldnext); \ 289 } while (0) 290 291 #define SLIST_REMOVE_AFTER(elm, field) do { \ 292 SLIST_NEXT(elm, field) = \ 293 SLIST_NEXT(SLIST_NEXT(elm, field), field); \ 294 } while (0) 295 296 #define SLIST_REMOVE_HEAD(head, field) do { \ 297 SLIST_FIRST((head)) = SLIST_NEXT(SLIST_FIRST((head)), field); \ 298 } while (0) 299 300 #define SLIST_REMOVE_PREVPTR(prevp, elm, field) do { \ 301 QMD_SLIST_CHECK_PREVPTR(prevp, elm); \ 302 *(prevp) = SLIST_NEXT(elm, field); \ 303 TRASHIT((elm)->field.sle_next); \ 304 } while (0) 305 306 #define SLIST_SWAP(head1, head2, type) do { \ 307 QUEUE_TYPEOF(type) *swap_first = SLIST_FIRST(head1); \ 308 SLIST_FIRST(head1) = SLIST_FIRST(head2); \ 309 SLIST_FIRST(head2) = swap_first; \ 310 } while (0) 311 312 /* 313 * Singly-linked Tail queue declarations. 314 */ 315 #define STAILQ_HEAD(name, type) \ 316 struct name { \ 317 struct type *stqh_first;/* first element */ \ 318 struct type **stqh_last;/* addr of last next element */ \ 319 } 320 321 #define STAILQ_CLASS_HEAD(name, type) \ 322 struct name { \ 323 class type *stqh_first; /* first element */ \ 324 class type **stqh_last; /* addr of last next element */ \ 325 } 326 327 #define STAILQ_HEAD_INITIALIZER(head) \ 328 { NULL, &(head).stqh_first } 329 330 #define STAILQ_ENTRY(type) \ 331 struct { \ 332 struct type *stqe_next; /* next element */ \ 333 } 334 335 #define STAILQ_CLASS_ENTRY(type) \ 336 struct { \ 337 class type *stqe_next; /* next element */ \ 338 } 339 340 /* 341 * Singly-linked Tail queue functions. 342 */ 343 #define STAILQ_CONCAT(head1, head2) do { \ 344 if (!STAILQ_EMPTY((head2))) { \ 345 *(head1)->stqh_last = (head2)->stqh_first; \ 346 (head1)->stqh_last = (head2)->stqh_last; \ 347 STAILQ_INIT((head2)); \ 348 } \ 349 } while (0) 350 351 #define STAILQ_EMPTY(head) ((head)->stqh_first == NULL) 352 353 #define STAILQ_FIRST(head) ((head)->stqh_first) 354 355 #define STAILQ_FOREACH(var, head, field) \ 356 for((var) = STAILQ_FIRST((head)); \ 357 (var); \ 358 (var) = STAILQ_NEXT((var), field)) 359 360 #define STAILQ_FOREACH_FROM(var, head, field) \ 361 for ((var) = ((var) ? (var) : STAILQ_FIRST((head))); \ 362 (var); \ 363 (var) = STAILQ_NEXT((var), field)) 364 365 #define STAILQ_FOREACH_SAFE(var, head, field, tvar) \ 366 for ((var) = STAILQ_FIRST((head)); \ 367 (var) && ((tvar) = STAILQ_NEXT((var), field), 1); \ 368 (var) = (tvar)) 369 370 #define STAILQ_FOREACH_FROM_SAFE(var, head, field, tvar) \ 371 for ((var) = ((var) ? (var) : STAILQ_FIRST((head))); \ 372 (var) && ((tvar) = STAILQ_NEXT((var), field), 1); \ 373 (var) = (tvar)) 374 375 #define STAILQ_INIT(head) do { \ 376 STAILQ_FIRST((head)) = NULL; \ 377 (head)->stqh_last = &STAILQ_FIRST((head)); \ 378 } while (0) 379 380 #define STAILQ_INSERT_AFTER(head, tqelm, elm, field) do { \ 381 if ((STAILQ_NEXT((elm), field) = STAILQ_NEXT((tqelm), field)) == NULL)\ 382 (head)->stqh_last = &STAILQ_NEXT((elm), field); \ 383 STAILQ_NEXT((tqelm), field) = (elm); \ 384 } while (0) 385 386 #define STAILQ_INSERT_HEAD(head, elm, field) do { \ 387 if ((STAILQ_NEXT((elm), field) = STAILQ_FIRST((head))) == NULL) \ 388 (head)->stqh_last = &STAILQ_NEXT((elm), field); \ 389 STAILQ_FIRST((head)) = (elm); \ 390 } while (0) 391 392 #define STAILQ_INSERT_TAIL(head, elm, field) do { \ 393 STAILQ_NEXT((elm), field) = NULL; \ 394 *(head)->stqh_last = (elm); \ 395 (head)->stqh_last = &STAILQ_NEXT((elm), field); \ 396 } while (0) 397 398 #define STAILQ_LAST(head, type, field) \ 399 (STAILQ_EMPTY((head)) ? NULL : \ 400 __containerof((head)->stqh_last, \ 401 QUEUE_TYPEOF(type), field.stqe_next)) 402 403 #define STAILQ_NEXT(elm, field) ((elm)->field.stqe_next) 404 405 #define STAILQ_REMOVE(head, elm, type, field) do { \ 406 QMD_SAVELINK(oldnext, (elm)->field.stqe_next); \ 407 if (STAILQ_FIRST((head)) == (elm)) { \ 408 STAILQ_REMOVE_HEAD((head), field); \ 409 } \ 410 else { \ 411 QUEUE_TYPEOF(type) *curelm = STAILQ_FIRST(head); \ 412 while (STAILQ_NEXT(curelm, field) != (elm)) \ 413 curelm = STAILQ_NEXT(curelm, field); \ 414 STAILQ_REMOVE_AFTER(head, curelm, field); \ 415 } \ 416 TRASHIT(*oldnext); \ 417 } while (0) 418 419 #define STAILQ_REMOVE_AFTER(head, elm, field) do { \ 420 if ((STAILQ_NEXT(elm, field) = \ 421 STAILQ_NEXT(STAILQ_NEXT(elm, field), field)) == NULL) \ 422 (head)->stqh_last = &STAILQ_NEXT((elm), field); \ 423 } while (0) 424 425 #define STAILQ_REMOVE_HEAD(head, field) do { \ 426 if ((STAILQ_FIRST((head)) = \ 427 STAILQ_NEXT(STAILQ_FIRST((head)), field)) == NULL) \ 428 (head)->stqh_last = &STAILQ_FIRST((head)); \ 429 } while (0) 430 431 #define STAILQ_SWAP(head1, head2, type) do { \ 432 QUEUE_TYPEOF(type) *swap_first = STAILQ_FIRST(head1); \ 433 QUEUE_TYPEOF(type) **swap_last = (head1)->stqh_last; \ 434 STAILQ_FIRST(head1) = STAILQ_FIRST(head2); \ 435 (head1)->stqh_last = (head2)->stqh_last; \ 436 STAILQ_FIRST(head2) = swap_first; \ 437 (head2)->stqh_last = swap_last; \ 438 if (STAILQ_EMPTY(head1)) \ 439 (head1)->stqh_last = &STAILQ_FIRST(head1); \ 440 if (STAILQ_EMPTY(head2)) \ 441 (head2)->stqh_last = &STAILQ_FIRST(head2); \ 442 } while (0) 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 /* 620 * Tail queue declarations. 621 */ 622 #define TAILQ_HEAD(name, type) \ 623 struct name { \ 624 struct type *tqh_first; /* first element */ \ 625 struct type **tqh_last; /* addr of last next element */ \ 626 TRACEBUF \ 627 } 628 629 #define TAILQ_CLASS_HEAD(name, type) \ 630 struct name { \ 631 class type *tqh_first; /* first element */ \ 632 class type **tqh_last; /* addr of last next element */ \ 633 TRACEBUF \ 634 } 635 636 #define TAILQ_HEAD_INITIALIZER(head) \ 637 { NULL, &(head).tqh_first, TRACEBUF_INITIALIZER } 638 639 #define TAILQ_ENTRY(type) \ 640 struct { \ 641 struct type *tqe_next; /* next element */ \ 642 struct type **tqe_prev; /* address of previous next element */ \ 643 TRACEBUF \ 644 } 645 646 #define TAILQ_CLASS_ENTRY(type) \ 647 struct { \ 648 class type *tqe_next; /* next element */ \ 649 class type **tqe_prev; /* address of previous next element */ \ 650 TRACEBUF \ 651 } 652 653 /* 654 * Tail queue functions. 655 */ 656 #if (defined(_KERNEL) && defined(INVARIANTS)) 657 /* 658 * QMD_TAILQ_CHECK_HEAD(TAILQ_HEAD *head, TAILQ_ENTRY NAME) 659 * 660 * If the tailq is non-empty, validates that the first element of the tailq 661 * points back at 'head.' 662 */ 663 #define QMD_TAILQ_CHECK_HEAD(head, field) do { \ 664 if (!TAILQ_EMPTY(head) && \ 665 TAILQ_FIRST((head))->field.tqe_prev != \ 666 &TAILQ_FIRST((head))) \ 667 panic("Bad tailq head %p first->prev != head", (head)); \ 668 } while (0) 669 670 /* 671 * QMD_TAILQ_CHECK_TAIL(TAILQ_HEAD *head, TAILQ_ENTRY NAME) 672 * 673 * Validates that the tail of the tailq is a pointer to pointer to NULL. 674 */ 675 #define QMD_TAILQ_CHECK_TAIL(head, field) do { \ 676 if (*(head)->tqh_last != NULL) \ 677 panic("Bad tailq NEXT(%p->tqh_last) != NULL", (head)); \ 678 } while (0) 679 680 /* 681 * QMD_TAILQ_CHECK_NEXT(TYPE *elm, TAILQ_ENTRY NAME) 682 * 683 * If an element follows 'elm' in the tailq, validates that the next element 684 * points back at 'elm.' 685 */ 686 #define QMD_TAILQ_CHECK_NEXT(elm, field) do { \ 687 if (TAILQ_NEXT((elm), field) != NULL && \ 688 TAILQ_NEXT((elm), field)->field.tqe_prev != \ 689 &((elm)->field.tqe_next)) \ 690 panic("Bad link elm %p next->prev != elm", (elm)); \ 691 } while (0) 692 693 /* 694 * QMD_TAILQ_CHECK_PREV(TYPE *elm, TAILQ_ENTRY NAME) 695 * 696 * Validates that the previous element (or head of the tailq) points to 'elm.' 697 */ 698 #define QMD_TAILQ_CHECK_PREV(elm, field) do { \ 699 if (*(elm)->field.tqe_prev != (elm)) \ 700 panic("Bad link elm %p prev->next != elm", (elm)); \ 701 } while (0) 702 #else 703 #define QMD_TAILQ_CHECK_HEAD(head, field) 704 #define QMD_TAILQ_CHECK_TAIL(head, headname) 705 #define QMD_TAILQ_CHECK_NEXT(elm, field) 706 #define QMD_TAILQ_CHECK_PREV(elm, field) 707 #endif /* (_KERNEL && INVARIANTS) */ 708 709 #define TAILQ_CONCAT(head1, head2, field) do { \ 710 if (!TAILQ_EMPTY(head2)) { \ 711 *(head1)->tqh_last = (head2)->tqh_first; \ 712 (head2)->tqh_first->field.tqe_prev = (head1)->tqh_last; \ 713 (head1)->tqh_last = (head2)->tqh_last; \ 714 TAILQ_INIT((head2)); \ 715 QMD_TRACE_HEAD(head1); \ 716 QMD_TRACE_HEAD(head2); \ 717 } \ 718 } while (0) 719 720 #define TAILQ_EMPTY(head) ((head)->tqh_first == NULL) 721 722 #define TAILQ_FIRST(head) ((head)->tqh_first) 723 724 #define TAILQ_FOREACH(var, head, field) \ 725 for ((var) = TAILQ_FIRST((head)); \ 726 (var); \ 727 (var) = TAILQ_NEXT((var), field)) 728 729 #define TAILQ_FOREACH_FROM(var, head, field) \ 730 for ((var) = ((var) ? (var) : TAILQ_FIRST((head))); \ 731 (var); \ 732 (var) = TAILQ_NEXT((var), field)) 733 734 #define TAILQ_FOREACH_SAFE(var, head, field, tvar) \ 735 for ((var) = TAILQ_FIRST((head)); \ 736 (var) && ((tvar) = TAILQ_NEXT((var), field), 1); \ 737 (var) = (tvar)) 738 739 #define TAILQ_FOREACH_FROM_SAFE(var, head, field, tvar) \ 740 for ((var) = ((var) ? (var) : TAILQ_FIRST((head))); \ 741 (var) && ((tvar) = TAILQ_NEXT((var), field), 1); \ 742 (var) = (tvar)) 743 744 #define TAILQ_FOREACH_REVERSE(var, head, headname, field) \ 745 for ((var) = TAILQ_LAST((head), headname); \ 746 (var); \ 747 (var) = TAILQ_PREV((var), headname, field)) 748 749 #define TAILQ_FOREACH_REVERSE_FROM(var, head, headname, field) \ 750 for ((var) = ((var) ? (var) : TAILQ_LAST((head), headname)); \ 751 (var); \ 752 (var) = TAILQ_PREV((var), headname, field)) 753 754 #define TAILQ_FOREACH_REVERSE_SAFE(var, head, headname, field, tvar) \ 755 for ((var) = TAILQ_LAST((head), headname); \ 756 (var) && ((tvar) = TAILQ_PREV((var), headname, field), 1); \ 757 (var) = (tvar)) 758 759 #define TAILQ_FOREACH_REVERSE_FROM_SAFE(var, head, headname, field, tvar) \ 760 for ((var) = ((var) ? (var) : TAILQ_LAST((head), headname)); \ 761 (var) && ((tvar) = TAILQ_PREV((var), headname, field), 1); \ 762 (var) = (tvar)) 763 764 #define TAILQ_INIT(head) do { \ 765 TAILQ_FIRST((head)) = NULL; \ 766 (head)->tqh_last = &TAILQ_FIRST((head)); \ 767 QMD_TRACE_HEAD(head); \ 768 } while (0) 769 770 #define TAILQ_INSERT_AFTER(head, listelm, elm, field) do { \ 771 QMD_TAILQ_CHECK_NEXT(listelm, field); \ 772 if ((TAILQ_NEXT((elm), field) = TAILQ_NEXT((listelm), field)) != NULL)\ 773 TAILQ_NEXT((elm), field)->field.tqe_prev = \ 774 &TAILQ_NEXT((elm), field); \ 775 else { \ 776 (head)->tqh_last = &TAILQ_NEXT((elm), field); \ 777 QMD_TRACE_HEAD(head); \ 778 } \ 779 TAILQ_NEXT((listelm), field) = (elm); \ 780 (elm)->field.tqe_prev = &TAILQ_NEXT((listelm), field); \ 781 QMD_TRACE_ELEM(&(elm)->field); \ 782 QMD_TRACE_ELEM(&(listelm)->field); \ 783 } while (0) 784 785 #define TAILQ_INSERT_BEFORE(listelm, elm, field) do { \ 786 QMD_TAILQ_CHECK_PREV(listelm, field); \ 787 (elm)->field.tqe_prev = (listelm)->field.tqe_prev; \ 788 TAILQ_NEXT((elm), field) = (listelm); \ 789 *(listelm)->field.tqe_prev = (elm); \ 790 (listelm)->field.tqe_prev = &TAILQ_NEXT((elm), field); \ 791 QMD_TRACE_ELEM(&(elm)->field); \ 792 QMD_TRACE_ELEM(&(listelm)->field); \ 793 } while (0) 794 795 #define TAILQ_INSERT_HEAD(head, elm, field) do { \ 796 QMD_TAILQ_CHECK_HEAD(head, field); \ 797 if ((TAILQ_NEXT((elm), field) = TAILQ_FIRST((head))) != NULL) \ 798 TAILQ_FIRST((head))->field.tqe_prev = \ 799 &TAILQ_NEXT((elm), field); \ 800 else \ 801 (head)->tqh_last = &TAILQ_NEXT((elm), field); \ 802 TAILQ_FIRST((head)) = (elm); \ 803 (elm)->field.tqe_prev = &TAILQ_FIRST((head)); \ 804 QMD_TRACE_HEAD(head); \ 805 QMD_TRACE_ELEM(&(elm)->field); \ 806 } while (0) 807 808 #define TAILQ_INSERT_TAIL(head, elm, field) do { \ 809 QMD_TAILQ_CHECK_TAIL(head, field); \ 810 TAILQ_NEXT((elm), field) = NULL; \ 811 (elm)->field.tqe_prev = (head)->tqh_last; \ 812 *(head)->tqh_last = (elm); \ 813 (head)->tqh_last = &TAILQ_NEXT((elm), field); \ 814 QMD_TRACE_HEAD(head); \ 815 QMD_TRACE_ELEM(&(elm)->field); \ 816 } while (0) 817 818 #define TAILQ_LAST(head, headname) \ 819 (*(((struct headname *)((head)->tqh_last))->tqh_last)) 820 821 /* 822 * The FAST function is fast in that it causes no data access other 823 * then the access to the head. The standard LAST function above 824 * will cause a data access of both the element you want and 825 * the previous element. FAST is very useful for instances when 826 * you may want to prefetch the last data element. 827 */ 828 #define TAILQ_LAST_FAST(head, type, field) \ 829 (TAILQ_EMPTY(head) ? NULL : __containerof((head)->tqh_last, QUEUE_TYPEOF(type), field.tqe_next)) 830 831 #define TAILQ_NEXT(elm, field) ((elm)->field.tqe_next) 832 833 #define TAILQ_PREV(elm, headname, field) \ 834 (*(((struct headname *)((elm)->field.tqe_prev))->tqh_last)) 835 836 #define TAILQ_PREV_FAST(elm, head, type, field) \ 837 ((elm)->field.tqe_prev == &(head)->tqh_first ? NULL : \ 838 __containerof((elm)->field.tqe_prev, QUEUE_TYPEOF(type), field.tqe_next)) 839 840 #define TAILQ_REMOVE(head, elm, field) do { \ 841 QMD_SAVELINK(oldnext, (elm)->field.tqe_next); \ 842 QMD_SAVELINK(oldprev, (elm)->field.tqe_prev); \ 843 QMD_TAILQ_CHECK_NEXT(elm, field); \ 844 QMD_TAILQ_CHECK_PREV(elm, field); \ 845 if ((TAILQ_NEXT((elm), field)) != NULL) \ 846 TAILQ_NEXT((elm), field)->field.tqe_prev = \ 847 (elm)->field.tqe_prev; \ 848 else { \ 849 (head)->tqh_last = (elm)->field.tqe_prev; \ 850 QMD_TRACE_HEAD(head); \ 851 } \ 852 *(elm)->field.tqe_prev = TAILQ_NEXT((elm), field); \ 853 TRASHIT(*oldnext); \ 854 TRASHIT(*oldprev); \ 855 QMD_TRACE_ELEM(&(elm)->field); \ 856 } while (0) 857 858 #define TAILQ_SWAP(head1, head2, type, field) do { \ 859 QUEUE_TYPEOF(type) *swap_first = (head1)->tqh_first; \ 860 QUEUE_TYPEOF(type) **swap_last = (head1)->tqh_last; \ 861 (head1)->tqh_first = (head2)->tqh_first; \ 862 (head1)->tqh_last = (head2)->tqh_last; \ 863 (head2)->tqh_first = swap_first; \ 864 (head2)->tqh_last = swap_last; \ 865 if ((swap_first = (head1)->tqh_first) != NULL) \ 866 swap_first->field.tqe_prev = &(head1)->tqh_first; \ 867 else \ 868 (head1)->tqh_last = &(head1)->tqh_first; \ 869 if ((swap_first = (head2)->tqh_first) != NULL) \ 870 swap_first->field.tqe_prev = &(head2)->tqh_first; \ 871 else \ 872 (head2)->tqh_last = &(head2)->tqh_first; \ 873 } while (0) 874 875 #endif /* !_SYS_QUEUE_H_ */ 876