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