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 * _END + + + + 95 * _FIRST + + + + 96 * _NEXT + + + + 97 * _PREV - + - + 98 * _LAST - - + + 99 * _LAST_FAST - - - + 100 * _FOREACH + + + + 101 * _FOREACH_FROM + + + + 102 * _FOREACH_SAFE + + + + 103 * _FOREACH_FROM_SAFE + + + + 104 * _FOREACH_REVERSE - - - + 105 * _FOREACH_REVERSE_FROM - - - + 106 * _FOREACH_REVERSE_SAFE - - - + 107 * _FOREACH_REVERSE_FROM_SAFE - - - + 108 * _INSERT_HEAD + + + + 109 * _INSERT_BEFORE - + - + 110 * _INSERT_AFTER + + + + 111 * _INSERT_TAIL - - + + 112 * _CONCAT s s + + 113 * _REMOVE_AFTER + - + - 114 * _REMOVE_HEAD + - + - 115 * _REMOVE s + s + 116 * _SWAP + + + + 117 * 118 */ 119 #ifdef QUEUE_MACRO_DEBUG 120 /* Store the last 2 places the queue element or head was altered */ 121 struct qm_trace { 122 unsigned long lastline; 123 unsigned long prevline; 124 const char *lastfile; 125 const char *prevfile; 126 }; 127 128 #define TRACEBUF struct qm_trace trace; 129 #define TRACEBUF_INITIALIZER { __LINE__, 0, __FILE__, NULL } , 130 #define TRASHIT(x) do {(x) = (void *)-1;} while (0) 131 #define QMD_SAVELINK(name, link) void **name = (void *)&(link) 132 133 #define QMD_TRACE_HEAD(head) do { \ 134 (head)->trace.prevline = (head)->trace.lastline; \ 135 (head)->trace.prevfile = (head)->trace.lastfile; \ 136 (head)->trace.lastline = __LINE__; \ 137 (head)->trace.lastfile = __FILE__; \ 138 } while (0) 139 140 #define QMD_TRACE_ELEM(elem) do { \ 141 (elem)->trace.prevline = (elem)->trace.lastline; \ 142 (elem)->trace.prevfile = (elem)->trace.lastfile; \ 143 (elem)->trace.lastline = __LINE__; \ 144 (elem)->trace.lastfile = __FILE__; \ 145 } while (0) 146 147 #else /* !QUEUE_MACRO_DEBUG_TRACE */ 148 #define QMD_TRACE_ELEM(elem) 149 #define QMD_TRACE_HEAD(head) 150 #define TRACEBUF 151 #define TRACEBUF_INITIALIZER 152 #endif /* QUEUE_MACRO_DEBUG_TRACE */ 153 154 #ifdef QUEUE_MACRO_DEBUG_TRASH 155 #define TRASHIT(x) do {(x) = (void *)-1;} while (0) 156 #define QMD_IS_TRASHED(x) ((x) == (void *)(intptr_t)-1) 157 #else /* !QUEUE_MACRO_DEBUG_TRASH */ 158 #define TRASHIT(x) 159 #define QMD_IS_TRASHED(x) 0 160 #endif /* QUEUE_MACRO_DEBUG_TRASH */ 161 162 #if defined(QUEUE_MACRO_DEBUG_TRACE) || defined(QUEUE_MACRO_DEBUG_TRASH) 163 #define QMD_SAVELINK(name, link) void **name = (void *)&(link) 164 #else /* !QUEUE_MACRO_DEBUG_TRACE && !QUEUE_MACRO_DEBUG_TRASH */ 165 #define QMD_SAVELINK(name, link) 166 #endif /* QUEUE_MACRO_DEBUG_TRACE || QUEUE_MACRO_DEBUG_TRASH */ 167 168 #ifdef __cplusplus 169 /* 170 * In C++ there can be structure lists and class lists: 171 */ 172 #define QUEUE_TYPEOF(type) type 173 #else 174 #define QUEUE_TYPEOF(type) struct type 175 #endif 176 177 /* 178 * Singly-linked List declarations. 179 */ 180 #define SLIST_HEAD(name, type) \ 181 struct name { \ 182 struct type *slh_first; /* first element */ \ 183 } 184 185 #define SLIST_CLASS_HEAD(name, type) \ 186 struct name { \ 187 class type *slh_first; /* first element */ \ 188 } 189 190 #define SLIST_HEAD_INITIALIZER(head) \ 191 { NULL } 192 193 #define SLIST_ENTRY(type) \ 194 struct { \ 195 struct type *sle_next; /* next element */ \ 196 } 197 198 #define SLIST_CLASS_ENTRY(type) \ 199 struct { \ 200 class type *sle_next; /* next element */ \ 201 } 202 203 /* 204 * Singly-linked List functions. 205 */ 206 #if (defined(_KERNEL) && defined(INVARIANTS)) 207 #define QMD_SLIST_CHECK_PREVPTR(prevp, elm) do { \ 208 if (*(prevp) != (elm)) \ 209 panic("Bad prevptr *(%p) == %p != %p", \ 210 (prevp), *(prevp), (elm)); \ 211 } while (0) 212 #else 213 #define QMD_SLIST_CHECK_PREVPTR(prevp, elm) 214 #endif 215 216 #define SLIST_CONCAT(head1, head2, type, field) do { \ 217 QUEUE_TYPEOF(type) *curelm = SLIST_FIRST(head1); \ 218 if (curelm == NULL) { \ 219 if ((SLIST_FIRST(head1) = SLIST_FIRST(head2)) != NULL) \ 220 SLIST_INIT(head2); \ 221 } else if (SLIST_FIRST(head2) != NULL) { \ 222 while (SLIST_NEXT(curelm, field) != NULL) \ 223 curelm = SLIST_NEXT(curelm, field); \ 224 SLIST_NEXT(curelm, field) = SLIST_FIRST(head2); \ 225 SLIST_INIT(head2); \ 226 } \ 227 } while (0) 228 229 #define SLIST_EMPTY(head) ((head)->slh_first == NULL) 230 231 #define SLIST_FIRST(head) ((head)->slh_first) 232 233 #define SLIST_FOREACH(var, head, field) \ 234 for ((var) = SLIST_FIRST((head)); \ 235 (var); \ 236 (var) = SLIST_NEXT((var), field)) 237 238 #define SLIST_FOREACH_FROM(var, head, field) \ 239 for ((var) = ((var) ? (var) : SLIST_FIRST((head))); \ 240 (var); \ 241 (var) = SLIST_NEXT((var), field)) 242 243 #define SLIST_FOREACH_SAFE(var, head, field, tvar) \ 244 for ((var) = SLIST_FIRST((head)); \ 245 (var) && ((tvar) = SLIST_NEXT((var), field), 1); \ 246 (var) = (tvar)) 247 248 #define SLIST_FOREACH_FROM_SAFE(var, head, field, tvar) \ 249 for ((var) = ((var) ? (var) : SLIST_FIRST((head))); \ 250 (var) && ((tvar) = SLIST_NEXT((var), field), 1); \ 251 (var) = (tvar)) 252 253 #define SLIST_FOREACH_PREVPTR(var, varp, head, field) \ 254 for ((varp) = &SLIST_FIRST((head)); \ 255 ((var) = *(varp)) != NULL; \ 256 (varp) = &SLIST_NEXT((var), field)) 257 258 #define SLIST_INIT(head) do { \ 259 SLIST_FIRST((head)) = NULL; \ 260 } while (0) 261 262 #define SLIST_INSERT_AFTER(slistelm, elm, field) do { \ 263 SLIST_NEXT((elm), field) = SLIST_NEXT((slistelm), field); \ 264 SLIST_NEXT((slistelm), field) = (elm); \ 265 } while (0) 266 267 #define SLIST_INSERT_HEAD(head, elm, field) do { \ 268 SLIST_NEXT((elm), field) = SLIST_FIRST((head)); \ 269 SLIST_FIRST((head)) = (elm); \ 270 } while (0) 271 272 #define SLIST_NEXT(elm, field) ((elm)->field.sle_next) 273 274 #define SLIST_REMOVE(head, elm, type, field) do { \ 275 QMD_SAVELINK(oldnext, (elm)->field.sle_next); \ 276 if (SLIST_FIRST((head)) == (elm)) { \ 277 SLIST_REMOVE_HEAD((head), field); \ 278 } \ 279 else { \ 280 QUEUE_TYPEOF(type) *curelm = SLIST_FIRST(head); \ 281 while (SLIST_NEXT(curelm, field) != (elm)) \ 282 curelm = SLIST_NEXT(curelm, field); \ 283 SLIST_REMOVE_AFTER(curelm, field); \ 284 } \ 285 TRASHIT(*oldnext); \ 286 } while (0) 287 288 #define SLIST_REMOVE_AFTER(elm, field) do { \ 289 SLIST_NEXT(elm, field) = \ 290 SLIST_NEXT(SLIST_NEXT(elm, field), field); \ 291 } while (0) 292 293 #define SLIST_REMOVE_HEAD(head, field) do { \ 294 SLIST_FIRST((head)) = SLIST_NEXT(SLIST_FIRST((head)), field); \ 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(elm, field) do { \ 598 QMD_SAVELINK(oldnext, (elm)->field.le_next); \ 599 QMD_SAVELINK(oldprev, (elm)->field.le_prev); \ 600 QMD_LIST_CHECK_NEXT(elm, field); \ 601 QMD_LIST_CHECK_PREV(elm, field); \ 602 if (LIST_NEXT((elm), field) != NULL) \ 603 LIST_NEXT((elm), field)->field.le_prev = \ 604 (elm)->field.le_prev; \ 605 *(elm)->field.le_prev = LIST_NEXT((elm), field); \ 606 TRASHIT(*oldnext); \ 607 TRASHIT(*oldprev); \ 608 } while (0) 609 610 #define LIST_SWAP(head1, head2, type, field) do { \ 611 QUEUE_TYPEOF(type) *swap_tmp = LIST_FIRST(head1); \ 612 LIST_FIRST((head1)) = LIST_FIRST((head2)); \ 613 LIST_FIRST((head2)) = swap_tmp; \ 614 if ((swap_tmp = LIST_FIRST((head1))) != NULL) \ 615 swap_tmp->field.le_prev = &LIST_FIRST((head1)); \ 616 if ((swap_tmp = LIST_FIRST((head2))) != NULL) \ 617 swap_tmp->field.le_prev = &LIST_FIRST((head2)); \ 618 } while (0) 619 620 #define LIST_END(head) NULL 621 622 /* 623 * Tail queue declarations. 624 */ 625 #define TAILQ_HEAD(name, type) \ 626 struct name { \ 627 struct type *tqh_first; /* first element */ \ 628 struct type **tqh_last; /* addr of last next element */ \ 629 TRACEBUF \ 630 } 631 632 #define TAILQ_CLASS_HEAD(name, type) \ 633 struct name { \ 634 class type *tqh_first; /* first element */ \ 635 class type **tqh_last; /* addr of last next element */ \ 636 TRACEBUF \ 637 } 638 639 #define TAILQ_HEAD_INITIALIZER(head) \ 640 { NULL, &(head).tqh_first, TRACEBUF_INITIALIZER } 641 642 #define TAILQ_ENTRY(type) \ 643 struct { \ 644 struct type *tqe_next; /* next element */ \ 645 struct type **tqe_prev; /* address of previous next element */ \ 646 TRACEBUF \ 647 } 648 649 #define TAILQ_CLASS_ENTRY(type) \ 650 struct { \ 651 class type *tqe_next; /* next element */ \ 652 class type **tqe_prev; /* address of previous next element */ \ 653 TRACEBUF \ 654 } 655 656 /* 657 * Tail queue functions. 658 */ 659 #if (defined(_KERNEL) && defined(INVARIANTS)) 660 /* 661 * QMD_TAILQ_CHECK_HEAD(TAILQ_HEAD *head, TAILQ_ENTRY NAME) 662 * 663 * If the tailq is non-empty, validates that the first element of the tailq 664 * points back at 'head.' 665 */ 666 #define QMD_TAILQ_CHECK_HEAD(head, field) do { \ 667 if (!TAILQ_EMPTY(head) && \ 668 TAILQ_FIRST((head))->field.tqe_prev != \ 669 &TAILQ_FIRST((head))) \ 670 panic("Bad tailq head %p first->prev != head", (head)); \ 671 } while (0) 672 673 /* 674 * QMD_TAILQ_CHECK_TAIL(TAILQ_HEAD *head, TAILQ_ENTRY NAME) 675 * 676 * Validates that the tail of the tailq is a pointer to pointer to NULL. 677 */ 678 #define QMD_TAILQ_CHECK_TAIL(head, field) do { \ 679 if (*(head)->tqh_last != NULL) \ 680 panic("Bad tailq NEXT(%p->tqh_last) != NULL", (head)); \ 681 } while (0) 682 683 /* 684 * QMD_TAILQ_CHECK_NEXT(TYPE *elm, TAILQ_ENTRY NAME) 685 * 686 * If an element follows 'elm' in the tailq, validates that the next element 687 * points back at 'elm.' 688 */ 689 #define QMD_TAILQ_CHECK_NEXT(elm, field) do { \ 690 if (TAILQ_NEXT((elm), field) != NULL && \ 691 TAILQ_NEXT((elm), field)->field.tqe_prev != \ 692 &((elm)->field.tqe_next)) \ 693 panic("Bad link elm %p next->prev != elm", (elm)); \ 694 } while (0) 695 696 /* 697 * QMD_TAILQ_CHECK_PREV(TYPE *elm, TAILQ_ENTRY NAME) 698 * 699 * Validates that the previous element (or head of the tailq) points to 'elm.' 700 */ 701 #define QMD_TAILQ_CHECK_PREV(elm, field) do { \ 702 if (*(elm)->field.tqe_prev != (elm)) \ 703 panic("Bad link elm %p prev->next != elm", (elm)); \ 704 } while (0) 705 #else 706 #define QMD_TAILQ_CHECK_HEAD(head, field) 707 #define QMD_TAILQ_CHECK_TAIL(head, headname) 708 #define QMD_TAILQ_CHECK_NEXT(elm, field) 709 #define QMD_TAILQ_CHECK_PREV(elm, field) 710 #endif /* (_KERNEL && INVARIANTS) */ 711 712 #define TAILQ_CONCAT(head1, head2, field) do { \ 713 if (!TAILQ_EMPTY(head2)) { \ 714 *(head1)->tqh_last = (head2)->tqh_first; \ 715 (head2)->tqh_first->field.tqe_prev = (head1)->tqh_last; \ 716 (head1)->tqh_last = (head2)->tqh_last; \ 717 TAILQ_INIT((head2)); \ 718 QMD_TRACE_HEAD(head1); \ 719 QMD_TRACE_HEAD(head2); \ 720 } \ 721 } while (0) 722 723 #define TAILQ_EMPTY(head) ((head)->tqh_first == NULL) 724 725 #define TAILQ_FIRST(head) ((head)->tqh_first) 726 727 #define TAILQ_FOREACH(var, head, field) \ 728 for ((var) = TAILQ_FIRST((head)); \ 729 (var); \ 730 (var) = TAILQ_NEXT((var), field)) 731 732 #define TAILQ_FOREACH_FROM(var, head, field) \ 733 for ((var) = ((var) ? (var) : TAILQ_FIRST((head))); \ 734 (var); \ 735 (var) = TAILQ_NEXT((var), field)) 736 737 #define TAILQ_FOREACH_SAFE(var, head, field, tvar) \ 738 for ((var) = TAILQ_FIRST((head)); \ 739 (var) && ((tvar) = TAILQ_NEXT((var), field), 1); \ 740 (var) = (tvar)) 741 742 #define TAILQ_FOREACH_FROM_SAFE(var, head, field, tvar) \ 743 for ((var) = ((var) ? (var) : TAILQ_FIRST((head))); \ 744 (var) && ((tvar) = TAILQ_NEXT((var), field), 1); \ 745 (var) = (tvar)) 746 747 #define TAILQ_FOREACH_REVERSE(var, head, headname, field) \ 748 for ((var) = TAILQ_LAST((head), headname); \ 749 (var); \ 750 (var) = TAILQ_PREV((var), headname, field)) 751 752 #define TAILQ_FOREACH_REVERSE_FROM(var, head, headname, field) \ 753 for ((var) = ((var) ? (var) : TAILQ_LAST((head), headname)); \ 754 (var); \ 755 (var) = TAILQ_PREV((var), headname, field)) 756 757 #define TAILQ_FOREACH_REVERSE_SAFE(var, head, headname, field, tvar) \ 758 for ((var) = TAILQ_LAST((head), headname); \ 759 (var) && ((tvar) = TAILQ_PREV((var), headname, field), 1); \ 760 (var) = (tvar)) 761 762 #define TAILQ_FOREACH_REVERSE_FROM_SAFE(var, head, headname, field, tvar) \ 763 for ((var) = ((var) ? (var) : TAILQ_LAST((head), headname)); \ 764 (var) && ((tvar) = TAILQ_PREV((var), headname, field), 1); \ 765 (var) = (tvar)) 766 767 #define TAILQ_INIT(head) do { \ 768 TAILQ_FIRST((head)) = NULL; \ 769 (head)->tqh_last = &TAILQ_FIRST((head)); \ 770 QMD_TRACE_HEAD(head); \ 771 } while (0) 772 773 #define TAILQ_INSERT_AFTER(head, listelm, elm, field) do { \ 774 QMD_TAILQ_CHECK_NEXT(listelm, field); \ 775 if ((TAILQ_NEXT((elm), field) = TAILQ_NEXT((listelm), field)) != NULL)\ 776 TAILQ_NEXT((elm), field)->field.tqe_prev = \ 777 &TAILQ_NEXT((elm), field); \ 778 else { \ 779 (head)->tqh_last = &TAILQ_NEXT((elm), field); \ 780 QMD_TRACE_HEAD(head); \ 781 } \ 782 TAILQ_NEXT((listelm), field) = (elm); \ 783 (elm)->field.tqe_prev = &TAILQ_NEXT((listelm), field); \ 784 QMD_TRACE_ELEM(&(elm)->field); \ 785 QMD_TRACE_ELEM(&(listelm)->field); \ 786 } while (0) 787 788 #define TAILQ_INSERT_BEFORE(listelm, elm, field) do { \ 789 QMD_TAILQ_CHECK_PREV(listelm, field); \ 790 (elm)->field.tqe_prev = (listelm)->field.tqe_prev; \ 791 TAILQ_NEXT((elm), field) = (listelm); \ 792 *(listelm)->field.tqe_prev = (elm); \ 793 (listelm)->field.tqe_prev = &TAILQ_NEXT((elm), field); \ 794 QMD_TRACE_ELEM(&(elm)->field); \ 795 QMD_TRACE_ELEM(&(listelm)->field); \ 796 } while (0) 797 798 #define TAILQ_INSERT_HEAD(head, elm, field) do { \ 799 QMD_TAILQ_CHECK_HEAD(head, field); \ 800 if ((TAILQ_NEXT((elm), field) = TAILQ_FIRST((head))) != NULL) \ 801 TAILQ_FIRST((head))->field.tqe_prev = \ 802 &TAILQ_NEXT((elm), field); \ 803 else \ 804 (head)->tqh_last = &TAILQ_NEXT((elm), field); \ 805 TAILQ_FIRST((head)) = (elm); \ 806 (elm)->field.tqe_prev = &TAILQ_FIRST((head)); \ 807 QMD_TRACE_HEAD(head); \ 808 QMD_TRACE_ELEM(&(elm)->field); \ 809 } while (0) 810 811 #define TAILQ_INSERT_TAIL(head, elm, field) do { \ 812 QMD_TAILQ_CHECK_TAIL(head, field); \ 813 TAILQ_NEXT((elm), field) = NULL; \ 814 (elm)->field.tqe_prev = (head)->tqh_last; \ 815 *(head)->tqh_last = (elm); \ 816 (head)->tqh_last = &TAILQ_NEXT((elm), field); \ 817 QMD_TRACE_HEAD(head); \ 818 QMD_TRACE_ELEM(&(elm)->field); \ 819 } while (0) 820 821 #define TAILQ_LAST(head, headname) \ 822 (*(((struct headname *)((head)->tqh_last))->tqh_last)) 823 824 /* 825 * The FAST function is fast in that it causes no data access other 826 * then the access to the head. The standard LAST function above 827 * will cause a data access of both the element you want and 828 * the previous element. FAST is very useful for instances when 829 * you may want to prefetch the last data element. 830 */ 831 #define TAILQ_LAST_FAST(head, type, field) \ 832 (TAILQ_EMPTY(head) ? NULL : __containerof((head)->tqh_last, QUEUE_TYPEOF(type), field.tqe_next)) 833 834 #define TAILQ_NEXT(elm, field) ((elm)->field.tqe_next) 835 836 #define TAILQ_PREV(elm, headname, field) \ 837 (*(((struct headname *)((elm)->field.tqe_prev))->tqh_last)) 838 839 #define TAILQ_PREV_FAST(elm, head, type, field) \ 840 ((elm)->field.tqe_prev == &(head)->tqh_first ? NULL : \ 841 __containerof((elm)->field.tqe_prev, QUEUE_TYPEOF(type), field.tqe_next)) 842 843 #define TAILQ_REMOVE(head, elm, field) do { \ 844 QMD_SAVELINK(oldnext, (elm)->field.tqe_next); \ 845 QMD_SAVELINK(oldprev, (elm)->field.tqe_prev); \ 846 QMD_TAILQ_CHECK_NEXT(elm, field); \ 847 QMD_TAILQ_CHECK_PREV(elm, field); \ 848 if ((TAILQ_NEXT((elm), field)) != NULL) \ 849 TAILQ_NEXT((elm), field)->field.tqe_prev = \ 850 (elm)->field.tqe_prev; \ 851 else { \ 852 (head)->tqh_last = (elm)->field.tqe_prev; \ 853 QMD_TRACE_HEAD(head); \ 854 } \ 855 *(elm)->field.tqe_prev = TAILQ_NEXT((elm), field); \ 856 TRASHIT(*oldnext); \ 857 TRASHIT(*oldprev); \ 858 QMD_TRACE_ELEM(&(elm)->field); \ 859 } while (0) 860 861 #define TAILQ_SWAP(head1, head2, type, field) do { \ 862 QUEUE_TYPEOF(type) *swap_first = (head1)->tqh_first; \ 863 QUEUE_TYPEOF(type) **swap_last = (head1)->tqh_last; \ 864 (head1)->tqh_first = (head2)->tqh_first; \ 865 (head1)->tqh_last = (head2)->tqh_last; \ 866 (head2)->tqh_first = swap_first; \ 867 (head2)->tqh_last = swap_last; \ 868 if ((swap_first = (head1)->tqh_first) != NULL) \ 869 swap_first->field.tqe_prev = &(head1)->tqh_first; \ 870 else \ 871 (head1)->tqh_last = &(head1)->tqh_first; \ 872 if ((swap_first = (head2)->tqh_first) != NULL) \ 873 swap_first->field.tqe_prev = &(head2)->tqh_first; \ 874 else \ 875 (head2)->tqh_last = &(head2)->tqh_first; \ 876 } while (0) 877 878 #define TAILQ_END(head) NULL 879 880 #endif /* !_SYS_QUEUE_H_ */ 881