1 /*- 2 * Copyright (c) 1991, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 4. Neither the name of the University nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 * @(#)queue.h 8.5 (Berkeley) 8/20/94 30 * $FreeBSD$ 31 */ 32 33 #ifndef _SYS_QUEUE_H_ 34 #define _SYS_QUEUE_H_ 35 36 #include <sys/cdefs.h> 37 38 /* 39 * This file defines four types of data structures: singly-linked lists, 40 * singly-linked tail queues, lists and tail queues. 41 * 42 * A singly-linked list is headed by a single forward pointer. The elements 43 * are singly linked for minimum space and pointer manipulation overhead at 44 * the expense of O(n) removal for arbitrary elements. New elements can be 45 * added to the list after an existing element or at the head of the list. 46 * Elements being removed from the head of the list should use the explicit 47 * macro for this purpose for optimum efficiency. A singly-linked list may 48 * only be traversed in the forward direction. Singly-linked lists are ideal 49 * for applications with large datasets and few or no removals or for 50 * implementing a LIFO queue. 51 * 52 * A singly-linked tail queue is headed by a pair of pointers, one to the 53 * head of the list and the other to the tail of the list. The elements are 54 * singly linked for minimum space and pointer manipulation overhead at the 55 * expense of O(n) removal for arbitrary elements. New elements can be added 56 * to the list after an existing element, at the head of the list, or at the 57 * end of the list. Elements being removed from the head of the tail queue 58 * should use the explicit macro for this purpose for optimum efficiency. 59 * A singly-linked tail queue may only be traversed in the forward direction. 60 * Singly-linked tail queues are ideal for applications with large datasets 61 * and few or no removals or for implementing a FIFO queue. 62 * 63 * A list is headed by a single forward pointer (or an array of forward 64 * pointers for a hash table header). The elements are doubly linked 65 * so that an arbitrary element can be removed without a need to 66 * traverse the list. New elements can be added to the list before 67 * or after an existing element or at the head of the list. A list 68 * may be traversed in either direction. 69 * 70 * A tail queue is headed by a pair of pointers, one to the head of the 71 * list and the other to the tail of the list. The elements are doubly 72 * linked so that an arbitrary element can be removed without a need to 73 * traverse the list. New elements can be added to the list before or 74 * after an existing element, at the head of the list, or at the end of 75 * the list. A tail queue may be traversed in either direction. 76 * 77 * For details on the use of these macros, see the queue(3) manual page. 78 * 79 * 80 * SLIST LIST STAILQ TAILQ 81 * _HEAD + + + + 82 * _CLASS_HEAD + + + + 83 * _HEAD_INITIALIZER + + + + 84 * _ENTRY + + + + 85 * _CLASS_ENTRY + + + + 86 * _INIT + + + + 87 * _EMPTY + + + + 88 * _FIRST + + + + 89 * _NEXT + + + + 90 * _PREV - + - + 91 * _LAST - - + + 92 * _FOREACH + + + + 93 * _FOREACH_FROM + + + + 94 * _FOREACH_SAFE + + + + 95 * _FOREACH_FROM_SAFE + + + + 96 * _FOREACH_REVERSE - - - + 97 * _FOREACH_REVERSE_FROM - - - + 98 * _FOREACH_REVERSE_SAFE - - - + 99 * _FOREACH_REVERSE_FROM_SAFE - - - + 100 * _INSERT_HEAD + + + + 101 * _INSERT_BEFORE - + - + 102 * _INSERT_AFTER + + + + 103 * _INSERT_TAIL - - + + 104 * _CONCAT - - + + 105 * _REMOVE_AFTER + - + - 106 * _REMOVE_HEAD + - + - 107 * _REMOVE + + + + 108 * _SWAP + + + + 109 * 110 */ 111 #ifdef QUEUE_MACRO_DEBUG 112 /* Store the last 2 places the queue element or head was altered */ 113 struct qm_trace { 114 unsigned long lastline; 115 unsigned long prevline; 116 const char *lastfile; 117 const char *prevfile; 118 }; 119 120 #define TRACEBUF struct qm_trace trace; 121 #define TRACEBUF_INITIALIZER { __LINE__, 0, __FILE__, NULL } , 122 #define TRASHIT(x) do {(x) = (void *)-1;} while (0) 123 #define QMD_SAVELINK(name, link) void **name = (void *)&(link) 124 125 #define QMD_TRACE_HEAD(head) do { \ 126 (head)->trace.prevline = (head)->trace.lastline; \ 127 (head)->trace.prevfile = (head)->trace.lastfile; \ 128 (head)->trace.lastline = __LINE__; \ 129 (head)->trace.lastfile = __FILE__; \ 130 } while (0) 131 132 #define QMD_TRACE_ELEM(elem) do { \ 133 (elem)->trace.prevline = (elem)->trace.lastline; \ 134 (elem)->trace.prevfile = (elem)->trace.lastfile; \ 135 (elem)->trace.lastline = __LINE__; \ 136 (elem)->trace.lastfile = __FILE__; \ 137 } while (0) 138 139 #else 140 #define QMD_TRACE_ELEM(elem) 141 #define QMD_TRACE_HEAD(head) 142 #define QMD_SAVELINK(name, link) 143 #define TRACEBUF 144 #define TRACEBUF_INITIALIZER 145 #define TRASHIT(x) 146 #endif /* QUEUE_MACRO_DEBUG */ 147 148 #ifdef __cplusplus 149 /* 150 * In C++ there can be structure lists and class lists: 151 */ 152 #define QUEUE_TYPEOF(type) type 153 #else 154 #define QUEUE_TYPEOF(type) struct type 155 #endif 156 157 /* 158 * Singly-linked List declarations. 159 */ 160 #define SLIST_HEAD(name, type) \ 161 struct name { \ 162 struct type *slh_first; /* first element */ \ 163 } 164 165 #define SLIST_CLASS_HEAD(name, type) \ 166 struct name { \ 167 class type *slh_first; /* first element */ \ 168 } 169 170 #define SLIST_HEAD_INITIALIZER(head) \ 171 { NULL } 172 173 #define SLIST_ENTRY(type) \ 174 struct { \ 175 struct type *sle_next; /* next element */ \ 176 } 177 178 #define SLIST_CLASS_ENTRY(type) \ 179 struct { \ 180 class type *sle_next; /* next element */ \ 181 } 182 183 /* 184 * Singly-linked List functions. 185 */ 186 #define SLIST_EMPTY(head) ((head)->slh_first == NULL) 187 188 #define SLIST_FIRST(head) ((head)->slh_first) 189 190 #define SLIST_FOREACH(var, head, field) \ 191 for ((var) = SLIST_FIRST((head)); \ 192 (var); \ 193 (var) = SLIST_NEXT((var), field)) 194 195 #define SLIST_FOREACH_FROM(var, head, field) \ 196 for ((var) = ((var) ? (var) : SLIST_FIRST((head))); \ 197 (var); \ 198 (var) = SLIST_NEXT((var), field)) 199 200 #define SLIST_FOREACH_SAFE(var, head, field, tvar) \ 201 for ((var) = SLIST_FIRST((head)); \ 202 (var) && ((tvar) = SLIST_NEXT((var), field), 1); \ 203 (var) = (tvar)) 204 205 #define SLIST_FOREACH_FROM_SAFE(var, head, field, tvar) \ 206 for ((var) = ((var) ? (var) : SLIST_FIRST((head))); \ 207 (var) && ((tvar) = SLIST_NEXT((var), field), 1); \ 208 (var) = (tvar)) 209 210 #define SLIST_FOREACH_PREVPTR(var, varp, head, field) \ 211 for ((varp) = &SLIST_FIRST((head)); \ 212 ((var) = *(varp)) != NULL; \ 213 (varp) = &SLIST_NEXT((var), field)) 214 215 #define SLIST_INIT(head) do { \ 216 SLIST_FIRST((head)) = NULL; \ 217 } while (0) 218 219 #define SLIST_INSERT_AFTER(slistelm, elm, field) do { \ 220 SLIST_NEXT((elm), field) = SLIST_NEXT((slistelm), field); \ 221 SLIST_NEXT((slistelm), field) = (elm); \ 222 } while (0) 223 224 #define SLIST_INSERT_HEAD(head, elm, field) do { \ 225 SLIST_NEXT((elm), field) = SLIST_FIRST((head)); \ 226 SLIST_FIRST((head)) = (elm); \ 227 } while (0) 228 229 #define SLIST_NEXT(elm, field) ((elm)->field.sle_next) 230 231 #define SLIST_REMOVE(head, elm, type, field) do { \ 232 QMD_SAVELINK(oldnext, (elm)->field.sle_next); \ 233 if (SLIST_FIRST((head)) == (elm)) { \ 234 SLIST_REMOVE_HEAD((head), field); \ 235 } \ 236 else { \ 237 QUEUE_TYPEOF(type) *curelm = SLIST_FIRST(head); \ 238 while (SLIST_NEXT(curelm, field) != (elm)) \ 239 curelm = SLIST_NEXT(curelm, field); \ 240 SLIST_REMOVE_AFTER(curelm, field); \ 241 } \ 242 TRASHIT(*oldnext); \ 243 } while (0) 244 245 #define SLIST_REMOVE_AFTER(elm, field) do { \ 246 SLIST_NEXT(elm, field) = \ 247 SLIST_NEXT(SLIST_NEXT(elm, field), field); \ 248 } while (0) 249 250 #define SLIST_REMOVE_HEAD(head, field) do { \ 251 SLIST_FIRST((head)) = SLIST_NEXT(SLIST_FIRST((head)), field); \ 252 } while (0) 253 254 #define SLIST_SWAP(head1, head2, type) do { \ 255 QUEUE_TYPEOF(type) *swap_first = SLIST_FIRST(head1); \ 256 SLIST_FIRST(head1) = SLIST_FIRST(head2); \ 257 SLIST_FIRST(head2) = swap_first; \ 258 } while (0) 259 260 /* 261 * Singly-linked Tail queue declarations. 262 */ 263 #define STAILQ_HEAD(name, type) \ 264 struct name { \ 265 struct type *stqh_first;/* first element */ \ 266 struct type **stqh_last;/* addr of last next element */ \ 267 } 268 269 #define STAILQ_CLASS_HEAD(name, type) \ 270 struct name { \ 271 class type *stqh_first; /* first element */ \ 272 class type **stqh_last; /* addr of last next element */ \ 273 } 274 275 #define STAILQ_HEAD_INITIALIZER(head) \ 276 { NULL, &(head).stqh_first } 277 278 #define STAILQ_ENTRY(type) \ 279 struct { \ 280 struct type *stqe_next; /* next element */ \ 281 } 282 283 #define STAILQ_CLASS_ENTRY(type) \ 284 struct { \ 285 class type *stqe_next; /* next element */ \ 286 } 287 288 /* 289 * Singly-linked Tail queue functions. 290 */ 291 #define STAILQ_CONCAT(head1, head2) do { \ 292 if (!STAILQ_EMPTY((head2))) { \ 293 *(head1)->stqh_last = (head2)->stqh_first; \ 294 (head1)->stqh_last = (head2)->stqh_last; \ 295 STAILQ_INIT((head2)); \ 296 } \ 297 } while (0) 298 299 #define STAILQ_EMPTY(head) ((head)->stqh_first == NULL) 300 301 #define STAILQ_FIRST(head) ((head)->stqh_first) 302 303 #define STAILQ_FOREACH(var, head, field) \ 304 for((var) = STAILQ_FIRST((head)); \ 305 (var); \ 306 (var) = STAILQ_NEXT((var), field)) 307 308 #define STAILQ_FOREACH_FROM(var, head, field) \ 309 for ((var) = ((var) ? (var) : STAILQ_FIRST((head))); \ 310 (var); \ 311 (var) = STAILQ_NEXT((var), field)) 312 313 #define STAILQ_FOREACH_SAFE(var, head, field, tvar) \ 314 for ((var) = STAILQ_FIRST((head)); \ 315 (var) && ((tvar) = STAILQ_NEXT((var), field), 1); \ 316 (var) = (tvar)) 317 318 #define STAILQ_FOREACH_FROM_SAFE(var, head, field, tvar) \ 319 for ((var) = ((var) ? (var) : STAILQ_FIRST((head))); \ 320 (var) && ((tvar) = STAILQ_NEXT((var), field), 1); \ 321 (var) = (tvar)) 322 323 #define STAILQ_INIT(head) do { \ 324 STAILQ_FIRST((head)) = NULL; \ 325 (head)->stqh_last = &STAILQ_FIRST((head)); \ 326 } while (0) 327 328 #define STAILQ_INSERT_AFTER(head, tqelm, elm, field) do { \ 329 if ((STAILQ_NEXT((elm), field) = STAILQ_NEXT((tqelm), field)) == NULL)\ 330 (head)->stqh_last = &STAILQ_NEXT((elm), field); \ 331 STAILQ_NEXT((tqelm), field) = (elm); \ 332 } while (0) 333 334 #define STAILQ_INSERT_HEAD(head, elm, field) do { \ 335 if ((STAILQ_NEXT((elm), field) = STAILQ_FIRST((head))) == NULL) \ 336 (head)->stqh_last = &STAILQ_NEXT((elm), field); \ 337 STAILQ_FIRST((head)) = (elm); \ 338 } while (0) 339 340 #define STAILQ_INSERT_TAIL(head, elm, field) do { \ 341 STAILQ_NEXT((elm), field) = NULL; \ 342 *(head)->stqh_last = (elm); \ 343 (head)->stqh_last = &STAILQ_NEXT((elm), field); \ 344 } while (0) 345 346 #define STAILQ_LAST(head, type, field) \ 347 (STAILQ_EMPTY((head)) ? NULL : \ 348 __containerof((head)->stqh_last, \ 349 QUEUE_TYPEOF(type), field.stqe_next)) 350 351 #define STAILQ_NEXT(elm, field) ((elm)->field.stqe_next) 352 353 #define STAILQ_REMOVE(head, elm, type, field) do { \ 354 QMD_SAVELINK(oldnext, (elm)->field.stqe_next); \ 355 if (STAILQ_FIRST((head)) == (elm)) { \ 356 STAILQ_REMOVE_HEAD((head), field); \ 357 } \ 358 else { \ 359 QUEUE_TYPEOF(type) *curelm = STAILQ_FIRST(head); \ 360 while (STAILQ_NEXT(curelm, field) != (elm)) \ 361 curelm = STAILQ_NEXT(curelm, field); \ 362 STAILQ_REMOVE_AFTER(head, curelm, field); \ 363 } \ 364 TRASHIT(*oldnext); \ 365 } while (0) 366 367 #define STAILQ_REMOVE_AFTER(head, elm, field) do { \ 368 if ((STAILQ_NEXT(elm, field) = \ 369 STAILQ_NEXT(STAILQ_NEXT(elm, field), field)) == NULL) \ 370 (head)->stqh_last = &STAILQ_NEXT((elm), field); \ 371 } while (0) 372 373 #define STAILQ_REMOVE_HEAD(head, field) do { \ 374 if ((STAILQ_FIRST((head)) = \ 375 STAILQ_NEXT(STAILQ_FIRST((head)), field)) == NULL) \ 376 (head)->stqh_last = &STAILQ_FIRST((head)); \ 377 } while (0) 378 379 #define STAILQ_SWAP(head1, head2, type) do { \ 380 QUEUE_TYPEOF(type) *swap_first = STAILQ_FIRST(head1); \ 381 QUEUE_TYPEOF(type) **swap_last = (head1)->stqh_last; \ 382 STAILQ_FIRST(head1) = STAILQ_FIRST(head2); \ 383 (head1)->stqh_last = (head2)->stqh_last; \ 384 STAILQ_FIRST(head2) = swap_first; \ 385 (head2)->stqh_last = swap_last; \ 386 if (STAILQ_EMPTY(head1)) \ 387 (head1)->stqh_last = &STAILQ_FIRST(head1); \ 388 if (STAILQ_EMPTY(head2)) \ 389 (head2)->stqh_last = &STAILQ_FIRST(head2); \ 390 } while (0) 391 392 393 /* 394 * List declarations. 395 */ 396 #define LIST_HEAD(name, type) \ 397 struct name { \ 398 struct type *lh_first; /* first element */ \ 399 } 400 401 #define LIST_CLASS_HEAD(name, type) \ 402 struct name { \ 403 class type *lh_first; /* first element */ \ 404 } 405 406 #define LIST_HEAD_INITIALIZER(head) \ 407 { NULL } 408 409 #define LIST_ENTRY(type) \ 410 struct { \ 411 struct type *le_next; /* next element */ \ 412 struct type **le_prev; /* address of previous next element */ \ 413 } 414 415 #define LIST_CLASS_ENTRY(type) \ 416 struct { \ 417 class type *le_next; /* next element */ \ 418 class type **le_prev; /* address of previous next element */ \ 419 } 420 421 /* 422 * List functions. 423 */ 424 425 #if (defined(_KERNEL) && defined(INVARIANTS)) 426 #define QMD_LIST_CHECK_HEAD(head, field) do { \ 427 if (LIST_FIRST((head)) != NULL && \ 428 LIST_FIRST((head))->field.le_prev != \ 429 &LIST_FIRST((head))) \ 430 panic("Bad list head %p first->prev != head", (head)); \ 431 } while (0) 432 433 #define QMD_LIST_CHECK_NEXT(elm, field) do { \ 434 if (LIST_NEXT((elm), field) != NULL && \ 435 LIST_NEXT((elm), field)->field.le_prev != \ 436 &((elm)->field.le_next)) \ 437 panic("Bad link elm %p next->prev != elm", (elm)); \ 438 } while (0) 439 440 #define QMD_LIST_CHECK_PREV(elm, field) do { \ 441 if (*(elm)->field.le_prev != (elm)) \ 442 panic("Bad link elm %p prev->next != elm", (elm)); \ 443 } while (0) 444 #else 445 #define QMD_LIST_CHECK_HEAD(head, field) 446 #define QMD_LIST_CHECK_NEXT(elm, field) 447 #define QMD_LIST_CHECK_PREV(elm, field) 448 #endif /* (_KERNEL && INVARIANTS) */ 449 450 #define LIST_EMPTY(head) ((head)->lh_first == NULL) 451 452 #define LIST_FIRST(head) ((head)->lh_first) 453 454 #define LIST_FOREACH(var, head, field) \ 455 for ((var) = LIST_FIRST((head)); \ 456 (var); \ 457 (var) = LIST_NEXT((var), field)) 458 459 #define LIST_FOREACH_FROM(var, head, field) \ 460 for ((var) = ((var) ? (var) : LIST_FIRST((head))); \ 461 (var); \ 462 (var) = LIST_NEXT((var), field)) 463 464 #define LIST_FOREACH_SAFE(var, head, field, tvar) \ 465 for ((var) = LIST_FIRST((head)); \ 466 (var) && ((tvar) = LIST_NEXT((var), field), 1); \ 467 (var) = (tvar)) 468 469 #define LIST_FOREACH_FROM_SAFE(var, head, field, tvar) \ 470 for ((var) = ((var) ? (var) : LIST_FIRST((head))); \ 471 (var) && ((tvar) = LIST_NEXT((var), field), 1); \ 472 (var) = (tvar)) 473 474 #define LIST_INIT(head) do { \ 475 LIST_FIRST((head)) = NULL; \ 476 } while (0) 477 478 #define LIST_INSERT_AFTER(listelm, elm, field) do { \ 479 QMD_LIST_CHECK_NEXT(listelm, field); \ 480 if ((LIST_NEXT((elm), field) = LIST_NEXT((listelm), field)) != NULL)\ 481 LIST_NEXT((listelm), field)->field.le_prev = \ 482 &LIST_NEXT((elm), field); \ 483 LIST_NEXT((listelm), field) = (elm); \ 484 (elm)->field.le_prev = &LIST_NEXT((listelm), field); \ 485 } while (0) 486 487 #define LIST_INSERT_BEFORE(listelm, elm, field) do { \ 488 QMD_LIST_CHECK_PREV(listelm, field); \ 489 (elm)->field.le_prev = (listelm)->field.le_prev; \ 490 LIST_NEXT((elm), field) = (listelm); \ 491 *(listelm)->field.le_prev = (elm); \ 492 (listelm)->field.le_prev = &LIST_NEXT((elm), field); \ 493 } while (0) 494 495 #define LIST_INSERT_HEAD(head, elm, field) do { \ 496 QMD_LIST_CHECK_HEAD((head), field); \ 497 if ((LIST_NEXT((elm), field) = LIST_FIRST((head))) != NULL) \ 498 LIST_FIRST((head))->field.le_prev = &LIST_NEXT((elm), field);\ 499 LIST_FIRST((head)) = (elm); \ 500 (elm)->field.le_prev = &LIST_FIRST((head)); \ 501 } while (0) 502 503 #define LIST_NEXT(elm, field) ((elm)->field.le_next) 504 505 #define LIST_PREV(elm, head, type, field) \ 506 ((elm)->field.le_prev == &LIST_FIRST((head)) ? NULL : \ 507 __containerof((elm)->field.le_prev, \ 508 QUEUE_TYPEOF(type), field.le_next)) 509 510 #define LIST_REMOVE(elm, field) do { \ 511 QMD_SAVELINK(oldnext, (elm)->field.le_next); \ 512 QMD_SAVELINK(oldprev, (elm)->field.le_prev); \ 513 QMD_LIST_CHECK_NEXT(elm, field); \ 514 QMD_LIST_CHECK_PREV(elm, field); \ 515 if (LIST_NEXT((elm), field) != NULL) \ 516 LIST_NEXT((elm), field)->field.le_prev = \ 517 (elm)->field.le_prev; \ 518 *(elm)->field.le_prev = LIST_NEXT((elm), field); \ 519 TRASHIT(*oldnext); \ 520 TRASHIT(*oldprev); \ 521 } while (0) 522 523 #define LIST_SWAP(head1, head2, type, field) do { \ 524 QUEUE_TYPEOF(type) *swap_tmp = LIST_FIRST(head1); \ 525 LIST_FIRST((head1)) = LIST_FIRST((head2)); \ 526 LIST_FIRST((head2)) = swap_tmp; \ 527 if ((swap_tmp = LIST_FIRST((head1))) != NULL) \ 528 swap_tmp->field.le_prev = &LIST_FIRST((head1)); \ 529 if ((swap_tmp = LIST_FIRST((head2))) != NULL) \ 530 swap_tmp->field.le_prev = &LIST_FIRST((head2)); \ 531 } while (0) 532 533 /* 534 * Tail queue declarations. 535 */ 536 #define TAILQ_HEAD(name, type) \ 537 struct name { \ 538 struct type *tqh_first; /* first element */ \ 539 struct type **tqh_last; /* addr of last next element */ \ 540 TRACEBUF \ 541 } 542 543 #define TAILQ_CLASS_HEAD(name, type) \ 544 struct name { \ 545 class type *tqh_first; /* first element */ \ 546 class type **tqh_last; /* addr of last next element */ \ 547 TRACEBUF \ 548 } 549 550 #define TAILQ_HEAD_INITIALIZER(head) \ 551 { NULL, &(head).tqh_first, TRACEBUF_INITIALIZER } 552 553 #define TAILQ_ENTRY(type) \ 554 struct { \ 555 struct type *tqe_next; /* next element */ \ 556 struct type **tqe_prev; /* address of previous next element */ \ 557 TRACEBUF \ 558 } 559 560 #define TAILQ_CLASS_ENTRY(type) \ 561 struct { \ 562 class type *tqe_next; /* next element */ \ 563 class type **tqe_prev; /* address of previous next element */ \ 564 TRACEBUF \ 565 } 566 567 /* 568 * Tail queue functions. 569 */ 570 #if (defined(_KERNEL) && defined(INVARIANTS)) 571 #define QMD_TAILQ_CHECK_HEAD(head, field) do { \ 572 if (!TAILQ_EMPTY(head) && \ 573 TAILQ_FIRST((head))->field.tqe_prev != \ 574 &TAILQ_FIRST((head))) \ 575 panic("Bad tailq head %p first->prev != head", (head)); \ 576 } while (0) 577 578 #define QMD_TAILQ_CHECK_TAIL(head, field) do { \ 579 if (*(head)->tqh_last != NULL) \ 580 panic("Bad tailq NEXT(%p->tqh_last) != NULL", (head)); \ 581 } while (0) 582 583 #define QMD_TAILQ_CHECK_NEXT(elm, field) do { \ 584 if (TAILQ_NEXT((elm), field) != NULL && \ 585 TAILQ_NEXT((elm), field)->field.tqe_prev != \ 586 &((elm)->field.tqe_next)) \ 587 panic("Bad link elm %p next->prev != elm", (elm)); \ 588 } while (0) 589 590 #define QMD_TAILQ_CHECK_PREV(elm, field) do { \ 591 if (*(elm)->field.tqe_prev != (elm)) \ 592 panic("Bad link elm %p prev->next != elm", (elm)); \ 593 } while (0) 594 #else 595 #define QMD_TAILQ_CHECK_HEAD(head, field) 596 #define QMD_TAILQ_CHECK_TAIL(head, headname) 597 #define QMD_TAILQ_CHECK_NEXT(elm, field) 598 #define QMD_TAILQ_CHECK_PREV(elm, field) 599 #endif /* (_KERNEL && INVARIANTS) */ 600 601 #define TAILQ_CONCAT(head1, head2, field) do { \ 602 if (!TAILQ_EMPTY(head2)) { \ 603 *(head1)->tqh_last = (head2)->tqh_first; \ 604 (head2)->tqh_first->field.tqe_prev = (head1)->tqh_last; \ 605 (head1)->tqh_last = (head2)->tqh_last; \ 606 TAILQ_INIT((head2)); \ 607 QMD_TRACE_HEAD(head1); \ 608 QMD_TRACE_HEAD(head2); \ 609 } \ 610 } while (0) 611 612 #define TAILQ_EMPTY(head) ((head)->tqh_first == NULL) 613 614 #define TAILQ_FIRST(head) ((head)->tqh_first) 615 616 #define TAILQ_FOREACH(var, head, field) \ 617 for ((var) = TAILQ_FIRST((head)); \ 618 (var); \ 619 (var) = TAILQ_NEXT((var), field)) 620 621 #define TAILQ_FOREACH_FROM(var, head, field) \ 622 for ((var) = ((var) ? (var) : TAILQ_FIRST((head))); \ 623 (var); \ 624 (var) = TAILQ_NEXT((var), field)) 625 626 #define TAILQ_FOREACH_SAFE(var, head, field, tvar) \ 627 for ((var) = TAILQ_FIRST((head)); \ 628 (var) && ((tvar) = TAILQ_NEXT((var), field), 1); \ 629 (var) = (tvar)) 630 631 #define TAILQ_FOREACH_FROM_SAFE(var, head, field, tvar) \ 632 for ((var) = ((var) ? (var) : TAILQ_FIRST((head))); \ 633 (var) && ((tvar) = TAILQ_NEXT((var), field), 1); \ 634 (var) = (tvar)) 635 636 #define TAILQ_FOREACH_REVERSE(var, head, headname, field) \ 637 for ((var) = TAILQ_LAST((head), headname); \ 638 (var); \ 639 (var) = TAILQ_PREV((var), headname, field)) 640 641 #define TAILQ_FOREACH_REVERSE_FROM(var, head, headname, field) \ 642 for ((var) = ((var) ? (var) : TAILQ_LAST((head), headname)); \ 643 (var); \ 644 (var) = TAILQ_PREV((var), headname, field)) 645 646 #define TAILQ_FOREACH_REVERSE_SAFE(var, head, headname, field, tvar) \ 647 for ((var) = TAILQ_LAST((head), headname); \ 648 (var) && ((tvar) = TAILQ_PREV((var), headname, field), 1); \ 649 (var) = (tvar)) 650 651 #define TAILQ_FOREACH_REVERSE_FROM_SAFE(var, head, headname, field, tvar) \ 652 for ((var) = ((var) ? (var) : TAILQ_LAST((head), headname)); \ 653 (var) && ((tvar) = TAILQ_PREV((var), headname, field), 1); \ 654 (var) = (tvar)) 655 656 #define TAILQ_INIT(head) do { \ 657 TAILQ_FIRST((head)) = NULL; \ 658 (head)->tqh_last = &TAILQ_FIRST((head)); \ 659 QMD_TRACE_HEAD(head); \ 660 } while (0) 661 662 #define TAILQ_INSERT_AFTER(head, listelm, elm, field) do { \ 663 QMD_TAILQ_CHECK_NEXT(listelm, field); \ 664 if ((TAILQ_NEXT((elm), field) = TAILQ_NEXT((listelm), field)) != NULL)\ 665 TAILQ_NEXT((elm), field)->field.tqe_prev = \ 666 &TAILQ_NEXT((elm), field); \ 667 else { \ 668 (head)->tqh_last = &TAILQ_NEXT((elm), field); \ 669 QMD_TRACE_HEAD(head); \ 670 } \ 671 TAILQ_NEXT((listelm), field) = (elm); \ 672 (elm)->field.tqe_prev = &TAILQ_NEXT((listelm), field); \ 673 QMD_TRACE_ELEM(&(elm)->field); \ 674 QMD_TRACE_ELEM(&(listelm)->field); \ 675 } while (0) 676 677 #define TAILQ_INSERT_BEFORE(listelm, elm, field) do { \ 678 QMD_TAILQ_CHECK_PREV(listelm, field); \ 679 (elm)->field.tqe_prev = (listelm)->field.tqe_prev; \ 680 TAILQ_NEXT((elm), field) = (listelm); \ 681 *(listelm)->field.tqe_prev = (elm); \ 682 (listelm)->field.tqe_prev = &TAILQ_NEXT((elm), field); \ 683 QMD_TRACE_ELEM(&(elm)->field); \ 684 QMD_TRACE_ELEM(&(listelm)->field); \ 685 } while (0) 686 687 #define TAILQ_INSERT_HEAD(head, elm, field) do { \ 688 QMD_TAILQ_CHECK_HEAD(head, field); \ 689 if ((TAILQ_NEXT((elm), field) = TAILQ_FIRST((head))) != NULL) \ 690 TAILQ_FIRST((head))->field.tqe_prev = \ 691 &TAILQ_NEXT((elm), field); \ 692 else \ 693 (head)->tqh_last = &TAILQ_NEXT((elm), field); \ 694 TAILQ_FIRST((head)) = (elm); \ 695 (elm)->field.tqe_prev = &TAILQ_FIRST((head)); \ 696 QMD_TRACE_HEAD(head); \ 697 QMD_TRACE_ELEM(&(elm)->field); \ 698 } while (0) 699 700 #define TAILQ_INSERT_TAIL(head, elm, field) do { \ 701 QMD_TAILQ_CHECK_TAIL(head, field); \ 702 TAILQ_NEXT((elm), field) = NULL; \ 703 (elm)->field.tqe_prev = (head)->tqh_last; \ 704 *(head)->tqh_last = (elm); \ 705 (head)->tqh_last = &TAILQ_NEXT((elm), field); \ 706 QMD_TRACE_HEAD(head); \ 707 QMD_TRACE_ELEM(&(elm)->field); \ 708 } while (0) 709 710 #define TAILQ_LAST(head, headname) \ 711 (*(((struct headname *)((head)->tqh_last))->tqh_last)) 712 713 #define TAILQ_NEXT(elm, field) ((elm)->field.tqe_next) 714 715 #define TAILQ_PREV(elm, headname, field) \ 716 (*(((struct headname *)((elm)->field.tqe_prev))->tqh_last)) 717 718 #define TAILQ_REMOVE(head, elm, field) do { \ 719 QMD_SAVELINK(oldnext, (elm)->field.tqe_next); \ 720 QMD_SAVELINK(oldprev, (elm)->field.tqe_prev); \ 721 QMD_TAILQ_CHECK_NEXT(elm, field); \ 722 QMD_TAILQ_CHECK_PREV(elm, field); \ 723 if ((TAILQ_NEXT((elm), field)) != NULL) \ 724 TAILQ_NEXT((elm), field)->field.tqe_prev = \ 725 (elm)->field.tqe_prev; \ 726 else { \ 727 (head)->tqh_last = (elm)->field.tqe_prev; \ 728 QMD_TRACE_HEAD(head); \ 729 } \ 730 *(elm)->field.tqe_prev = TAILQ_NEXT((elm), field); \ 731 TRASHIT(*oldnext); \ 732 TRASHIT(*oldprev); \ 733 QMD_TRACE_ELEM(&(elm)->field); \ 734 } while (0) 735 736 #define TAILQ_SWAP(head1, head2, type, field) do { \ 737 QUEUE_TYPEOF(type) *swap_first = (head1)->tqh_first; \ 738 QUEUE_TYPEOF(type) **swap_last = (head1)->tqh_last; \ 739 (head1)->tqh_first = (head2)->tqh_first; \ 740 (head1)->tqh_last = (head2)->tqh_last; \ 741 (head2)->tqh_first = swap_first; \ 742 (head2)->tqh_last = swap_last; \ 743 if ((swap_first = (head1)->tqh_first) != NULL) \ 744 swap_first->field.tqe_prev = &(head1)->tqh_first; \ 745 else \ 746 (head1)->tqh_last = &(head1)->tqh_first; \ 747 if ((swap_first = (head2)->tqh_first) != NULL) \ 748 swap_first->field.tqe_prev = &(head2)->tqh_first; \ 749 else \ 750 (head2)->tqh_last = &(head2)->tqh_first; \ 751 } while (0) 752 753 #endif /* !_SYS_QUEUE_H_ */ 754