1 /* $NetBSD: queue.h,v 1.49.6.1 2008/11/20 03:22:38 snj Exp $ */ 2 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 */ 33 34 #ifndef _SYS_QUEUE_H_ 35 #define _SYS_QUEUE_H_ 36 37 /*#include <sys/null.h> */ 38 39 /* 40 * This file defines five types of data structures: singly-linked lists, 41 * lists, simple queues, tail queues, and circular queues. 42 * 43 * A singly-linked list is headed by a single forward pointer. The 44 * elements are singly linked for minimum space and pointer manipulation 45 * overhead at the expense of O(n) removal for arbitrary elements. New 46 * elements can be added to the list after an existing element or at the 47 * head of the list. Elements being removed from the head of the list 48 * should use the explicit macro for this purpose for optimum 49 * efficiency. A singly-linked list may only be traversed in the forward 50 * direction. Singly-linked lists are ideal for applications with large 51 * datasets and few or no removals or for implementing a LIFO queue. 52 * 53 * A list is headed by a single forward pointer (or an array of forward 54 * pointers for a hash table header). The elements are doubly linked 55 * so that an arbitrary element can be removed without a need to 56 * traverse the list. New elements can be added to the list before 57 * or after an existing element or at the head of the list. A list 58 * may only be traversed in the forward direction. 59 * 60 * A simple queue is headed by a pair of pointers, one the head of the 61 * list and the other to the tail of the list. The elements are singly 62 * linked to save space, so elements can only be removed from the 63 * head of the list. New elements can be added to the list after 64 * an existing element, at the head of the list, or at the end of the 65 * list. A simple queue may only be traversed in the forward direction. 66 * 67 * A tail queue is headed by a pair of pointers, one to the head of the 68 * list and the other to the tail of the list. The elements are doubly 69 * linked so that an arbitrary element can be removed without a need to 70 * traverse the list. New elements can be added to the list before or 71 * after an existing element, at the head of the list, or at the end of 72 * the list. A tail queue may be traversed in either direction. 73 * 74 * A circle queue is headed by a pair of pointers, one to the head of the 75 * list and the other to the tail of the list. The elements are doubly 76 * linked so that an arbitrary element can be removed without a need to 77 * traverse the list. New elements can be added to the list before or after 78 * an existing element, at the head of the list, or at the end of the list. 79 * A circle queue may be traversed in either direction, but has a more 80 * complex end of list detection. 81 * 82 * For details on the use of these macros, see the queue(3) manual page. 83 */ 84 85 /* 86 * List definitions. 87 */ 88 #define LIST_HEAD(name, type) \ 89 struct name { \ 90 struct type *lh_first; /* first element */ \ 91 } 92 93 #define LIST_HEAD_INITIALIZER(head) \ 94 { NULL } 95 96 #define LIST_ENTRY(type) \ 97 struct { \ 98 struct type *le_next; /* next element */ \ 99 struct type **le_prev; /* address of previous next element */ \ 100 } 101 102 /* 103 * List functions. 104 */ 105 #if defined(_KERNEL) && defined(QUEUEDEBUG) 106 #define QUEUEDEBUG_LIST_INSERT_HEAD(head, elm, field) \ 107 if ((head)->lh_first && \ 108 (head)->lh_first->field.le_prev != &(head)->lh_first) \ 109 panic("LIST_INSERT_HEAD %p %s:%d", (head), __FILE__, __LINE__); 110 #define QUEUEDEBUG_LIST_OP(elm, field) \ 111 if ((elm)->field.le_next && \ 112 (elm)->field.le_next->field.le_prev != \ 113 &(elm)->field.le_next) \ 114 panic("LIST_* forw %p %s:%d", (elm), __FILE__, __LINE__);\ 115 if (*(elm)->field.le_prev != (elm)) \ 116 panic("LIST_* back %p %s:%d", (elm), __FILE__, __LINE__); 117 #define QUEUEDEBUG_LIST_POSTREMOVE(elm, field) \ 118 (elm)->field.le_next = (void *)1L; \ 119 (elm)->field.le_prev = (void *)1L; 120 #else 121 #define QUEUEDEBUG_LIST_INSERT_HEAD(head, elm, field) 122 #define QUEUEDEBUG_LIST_OP(elm, field) 123 #define QUEUEDEBUG_LIST_POSTREMOVE(elm, field) 124 #endif 125 126 #define LIST_INIT(head) do { \ 127 (head)->lh_first = NULL; \ 128 } while (/* CONSTCOND */0) 129 130 #define LIST_INSERT_AFTER(listelm, elm, field) do { \ 131 QUEUEDEBUG_LIST_OP((listelm), field) \ 132 if (((elm)->field.le_next = (listelm)->field.le_next) != NULL) \ 133 (listelm)->field.le_next->field.le_prev = \ 134 &(elm)->field.le_next; \ 135 (listelm)->field.le_next = (elm); \ 136 (elm)->field.le_prev = &(listelm)->field.le_next; \ 137 } while (/* CONSTCOND */0) 138 139 #define LIST_INSERT_BEFORE(listelm, elm, field) do { \ 140 QUEUEDEBUG_LIST_OP((listelm), field) \ 141 (elm)->field.le_prev = (listelm)->field.le_prev; \ 142 (elm)->field.le_next = (listelm); \ 143 *(listelm)->field.le_prev = (elm); \ 144 (listelm)->field.le_prev = &(elm)->field.le_next; \ 145 } while (/* CONSTCOND */0) 146 147 #define LIST_INSERT_HEAD(head, elm, field) do { \ 148 QUEUEDEBUG_LIST_INSERT_HEAD((head), (elm), field) \ 149 if (((elm)->field.le_next = (head)->lh_first) != NULL) \ 150 (head)->lh_first->field.le_prev = &(elm)->field.le_next;\ 151 (head)->lh_first = (elm); \ 152 (elm)->field.le_prev = &(head)->lh_first; \ 153 } while (/* CONSTCOND */0) 154 155 #define LIST_REMOVE(elm, field) do { \ 156 QUEUEDEBUG_LIST_OP((elm), field) \ 157 if ((elm)->field.le_next != NULL) \ 158 (elm)->field.le_next->field.le_prev = \ 159 (elm)->field.le_prev; \ 160 *(elm)->field.le_prev = (elm)->field.le_next; \ 161 QUEUEDEBUG_LIST_POSTREMOVE((elm), field) \ 162 } while (/* CONSTCOND */0) 163 164 #define LIST_FOREACH(var, head, field) \ 165 for ((var) = ((head)->lh_first); \ 166 (var); \ 167 (var) = ((var)->field.le_next)) 168 169 /* 170 * List access methods. 171 */ 172 #define LIST_EMPTY(head) ((head)->lh_first == NULL) 173 #define LIST_FIRST(head) ((head)->lh_first) 174 #define LIST_NEXT(elm, field) ((elm)->field.le_next) 175 176 /* 177 * Singly-linked List definitions. 178 */ 179 #define SLIST_HEAD(name, type) \ 180 struct name { \ 181 struct type *slh_first; /* first element */ \ 182 } 183 184 #define SLIST_HEAD_INITIALIZER(head) \ 185 { NULL } 186 187 #define SLIST_ENTRY(type) \ 188 struct { \ 189 struct type *sle_next; /* next element */ \ 190 } 191 192 /* 193 * Singly-linked List functions. 194 */ 195 #define SLIST_INIT(head) do { \ 196 (head)->slh_first = NULL; \ 197 } while (/* CONSTCOND */0) 198 199 #define SLIST_INSERT_AFTER(slistelm, elm, field) do { \ 200 (elm)->field.sle_next = (slistelm)->field.sle_next; \ 201 (slistelm)->field.sle_next = (elm); \ 202 } while (/* CONSTCOND */0) 203 204 #define SLIST_INSERT_HEAD(head, elm, field) do { \ 205 (elm)->field.sle_next = (head)->slh_first; \ 206 (head)->slh_first = (elm); \ 207 } while (/* CONSTCOND */0) 208 209 #define SLIST_REMOVE_HEAD(head, field) do { \ 210 (head)->slh_first = (head)->slh_first->field.sle_next; \ 211 } while (/* CONSTCOND */0) 212 213 #define SLIST_REMOVE(head, elm, type, field) do { \ 214 if ((head)->slh_first == (elm)) { \ 215 SLIST_REMOVE_HEAD((head), field); \ 216 } \ 217 else { \ 218 struct type *curelm = (head)->slh_first; \ 219 while(curelm->field.sle_next != (elm)) \ 220 curelm = curelm->field.sle_next; \ 221 curelm->field.sle_next = \ 222 curelm->field.sle_next->field.sle_next; \ 223 } \ 224 } while (/* CONSTCOND */0) 225 226 #define SLIST_REMOVE_AFTER(slistelm, field) do { \ 227 (slistelm)->field.sle_next = \ 228 SLIST_NEXT(SLIST_NEXT((slistelm), field), field); \ 229 } while (/* CONSTCOND */0) 230 231 #define SLIST_FOREACH(var, head, field) \ 232 for((var) = (head)->slh_first; (var); (var) = (var)->field.sle_next) 233 234 /* 235 * Singly-linked List access methods. 236 */ 237 #define SLIST_EMPTY(head) ((head)->slh_first == NULL) 238 #define SLIST_FIRST(head) ((head)->slh_first) 239 #define SLIST_NEXT(elm, field) ((elm)->field.sle_next) 240 241 /* 242 * Singly-linked Tail queue declarations. 243 */ 244 #define STAILQ_HEAD(name, type) \ 245 struct name { \ 246 struct type *stqh_first; /* first element */ \ 247 struct type **stqh_last; /* addr of last next element */ \ 248 } 249 250 #define STAILQ_HEAD_INITIALIZER(head) \ 251 { NULL, &(head).stqh_first } 252 253 #define STAILQ_ENTRY(type) \ 254 struct { \ 255 struct type *stqe_next; /* next element */ \ 256 } 257 258 /* 259 * Singly-linked Tail queue functions. 260 */ 261 #define STAILQ_INIT(head) do { \ 262 (head)->stqh_first = NULL; \ 263 (head)->stqh_last = &(head)->stqh_first; \ 264 } while (/* CONSTCOND */0) 265 266 #define STAILQ_INSERT_HEAD(head, elm, field) do { \ 267 if (((elm)->field.stqe_next = (head)->stqh_first) == NULL) \ 268 (head)->stqh_last = &(elm)->field.stqe_next; \ 269 (head)->stqh_first = (elm); \ 270 } while (/* CONSTCOND */0) 271 272 #define STAILQ_INSERT_TAIL(head, elm, field) do { \ 273 (elm)->field.stqe_next = NULL; \ 274 *(head)->stqh_last = (elm); \ 275 (head)->stqh_last = &(elm)->field.stqe_next; \ 276 } while (/* CONSTCOND */0) 277 278 #define STAILQ_INSERT_AFTER(head, listelm, elm, field) do { \ 279 if (((elm)->field.stqe_next = (listelm)->field.stqe_next) == NULL)\ 280 (head)->stqh_last = &(elm)->field.stqe_next; \ 281 (listelm)->field.stqe_next = (elm); \ 282 } while (/* CONSTCOND */0) 283 284 #define STAILQ_REMOVE_HEAD(head, field) do { \ 285 if (((head)->stqh_first = (head)->stqh_first->field.stqe_next) == NULL) \ 286 (head)->stqh_last = &(head)->stqh_first; \ 287 } while (/* CONSTCOND */0) 288 289 #define STAILQ_REMOVE(head, elm, type, field) do { \ 290 if ((head)->stqh_first == (elm)) { \ 291 STAILQ_REMOVE_HEAD((head), field); \ 292 } else { \ 293 struct type *curelm = (head)->stqh_first; \ 294 while (curelm->field.stqe_next != (elm)) \ 295 curelm = curelm->field.stqe_next; \ 296 if ((curelm->field.stqe_next = \ 297 curelm->field.stqe_next->field.stqe_next) == NULL) \ 298 (head)->stqh_last = &(curelm)->field.stqe_next; \ 299 } \ 300 } while (/* CONSTCOND */0) 301 302 #define STAILQ_FOREACH(var, head, field) \ 303 for ((var) = ((head)->stqh_first); \ 304 (var); \ 305 (var) = ((var)->field.stqe_next)) 306 307 #define STAILQ_CONCAT(head1, head2) do { \ 308 if (!STAILQ_EMPTY((head2))) { \ 309 *(head1)->stqh_last = (head2)->stqh_first; \ 310 (head1)->stqh_last = (head2)->stqh_last; \ 311 STAILQ_INIT((head2)); \ 312 } \ 313 } while (/* CONSTCOND */0) 314 315 /* 316 * Singly-linked Tail queue access methods. 317 */ 318 #define STAILQ_EMPTY(head) ((head)->stqh_first == NULL) 319 #define STAILQ_FIRST(head) ((head)->stqh_first) 320 #define STAILQ_NEXT(elm, field) ((elm)->field.stqe_next) 321 322 /* 323 * Simple queue definitions. 324 */ 325 #define SIMPLEQ_HEAD(name, type) \ 326 struct name { \ 327 struct type *sqh_first; /* first element */ \ 328 struct type **sqh_last; /* addr of last next element */ \ 329 } 330 331 #define SIMPLEQ_HEAD_INITIALIZER(head) \ 332 { NULL, &(head).sqh_first } 333 334 #define SIMPLEQ_ENTRY(type) \ 335 struct { \ 336 struct type *sqe_next; /* next element */ \ 337 } 338 339 /* 340 * Simple queue functions. 341 */ 342 #define SIMPLEQ_INIT(head) do { \ 343 (head)->sqh_first = NULL; \ 344 (head)->sqh_last = &(head)->sqh_first; \ 345 } while (/* CONSTCOND */0) 346 347 #define SIMPLEQ_INSERT_HEAD(head, elm, field) do { \ 348 if (((elm)->field.sqe_next = (head)->sqh_first) == NULL) \ 349 (head)->sqh_last = &(elm)->field.sqe_next; \ 350 (head)->sqh_first = (elm); \ 351 } while (/* CONSTCOND */0) 352 353 #define SIMPLEQ_INSERT_TAIL(head, elm, field) do { \ 354 (elm)->field.sqe_next = NULL; \ 355 *(head)->sqh_last = (elm); \ 356 (head)->sqh_last = &(elm)->field.sqe_next; \ 357 } while (/* CONSTCOND */0) 358 359 #define SIMPLEQ_INSERT_AFTER(head, listelm, elm, field) do { \ 360 if (((elm)->field.sqe_next = (listelm)->field.sqe_next) == NULL)\ 361 (head)->sqh_last = &(elm)->field.sqe_next; \ 362 (listelm)->field.sqe_next = (elm); \ 363 } while (/* CONSTCOND */0) 364 365 #define SIMPLEQ_REMOVE_HEAD(head, field) do { \ 366 if (((head)->sqh_first = (head)->sqh_first->field.sqe_next) == NULL) \ 367 (head)->sqh_last = &(head)->sqh_first; \ 368 } while (/* CONSTCOND */0) 369 370 #define SIMPLEQ_REMOVE(head, elm, type, field) do { \ 371 if ((head)->sqh_first == (elm)) { \ 372 SIMPLEQ_REMOVE_HEAD((head), field); \ 373 } else { \ 374 struct type *curelm = (head)->sqh_first; \ 375 while (curelm->field.sqe_next != (elm)) \ 376 curelm = curelm->field.sqe_next; \ 377 if ((curelm->field.sqe_next = \ 378 curelm->field.sqe_next->field.sqe_next) == NULL) \ 379 (head)->sqh_last = &(curelm)->field.sqe_next; \ 380 } \ 381 } while (/* CONSTCOND */0) 382 383 #define SIMPLEQ_FOREACH(var, head, field) \ 384 for ((var) = ((head)->sqh_first); \ 385 (var); \ 386 (var) = ((var)->field.sqe_next)) 387 388 /* 389 * Simple queue access methods. 390 */ 391 #define SIMPLEQ_EMPTY(head) ((head)->sqh_first == NULL) 392 #define SIMPLEQ_FIRST(head) ((head)->sqh_first) 393 #define SIMPLEQ_NEXT(elm, field) ((elm)->field.sqe_next) 394 395 /* 396 * Tail queue definitions. 397 */ 398 #define _TAILQ_HEAD(name, type, qual) \ 399 struct name { \ 400 qual type *tqh_first; /* first element */ \ 401 qual type *qual *tqh_last; /* addr of last next element */ \ 402 } 403 #define TAILQ_HEAD(name, type) _TAILQ_HEAD(name, struct type,) 404 405 #define TAILQ_HEAD_INITIALIZER(head) \ 406 { NULL, &(head).tqh_first } 407 408 #define _TAILQ_ENTRY(type, qual) \ 409 struct { \ 410 qual type *tqe_next; /* next element */ \ 411 qual type *qual *tqe_prev; /* address of previous next element */\ 412 } 413 #define TAILQ_ENTRY(type) _TAILQ_ENTRY(struct type,) 414 415 /* 416 * Tail queue functions. 417 */ 418 #if defined(_KERNEL) && defined(QUEUEDEBUG) 419 #define QUEUEDEBUG_TAILQ_INSERT_HEAD(head, elm, field) \ 420 if ((head)->tqh_first && \ 421 (head)->tqh_first->field.tqe_prev != &(head)->tqh_first) \ 422 panic("TAILQ_INSERT_HEAD %p %s:%d", (head), __FILE__, __LINE__); 423 #define QUEUEDEBUG_TAILQ_INSERT_TAIL(head, elm, field) \ 424 if (*(head)->tqh_last != NULL) \ 425 panic("TAILQ_INSERT_TAIL %p %s:%d", (head), __FILE__, __LINE__); 426 #define QUEUEDEBUG_TAILQ_OP(elm, field) \ 427 if ((elm)->field.tqe_next && \ 428 (elm)->field.tqe_next->field.tqe_prev != \ 429 &(elm)->field.tqe_next) \ 430 panic("TAILQ_* forw %p %s:%d", (elm), __FILE__, __LINE__);\ 431 if (*(elm)->field.tqe_prev != (elm)) \ 432 panic("TAILQ_* back %p %s:%d", (elm), __FILE__, __LINE__); 433 #define QUEUEDEBUG_TAILQ_PREREMOVE(head, elm, field) \ 434 if ((elm)->field.tqe_next == NULL && \ 435 (head)->tqh_last != &(elm)->field.tqe_next) \ 436 panic("TAILQ_PREREMOVE head %p elm %p %s:%d", \ 437 (head), (elm), __FILE__, __LINE__); 438 #define QUEUEDEBUG_TAILQ_POSTREMOVE(elm, field) \ 439 (elm)->field.tqe_next = (void *)1L; \ 440 (elm)->field.tqe_prev = (void *)1L; 441 #else 442 #define QUEUEDEBUG_TAILQ_INSERT_HEAD(head, elm, field) 443 #define QUEUEDEBUG_TAILQ_INSERT_TAIL(head, elm, field) 444 #define QUEUEDEBUG_TAILQ_OP(elm, field) 445 #define QUEUEDEBUG_TAILQ_PREREMOVE(head, elm, field) 446 #define QUEUEDEBUG_TAILQ_POSTREMOVE(elm, field) 447 #endif 448 449 #define TAILQ_INIT(head) do { \ 450 (head)->tqh_first = NULL; \ 451 (head)->tqh_last = &(head)->tqh_first; \ 452 } while (/* CONSTCOND */0) 453 454 #define TAILQ_INSERT_HEAD(head, elm, field) do { \ 455 QUEUEDEBUG_TAILQ_INSERT_HEAD((head), (elm), field) \ 456 if (((elm)->field.tqe_next = (head)->tqh_first) != NULL) \ 457 (head)->tqh_first->field.tqe_prev = \ 458 &(elm)->field.tqe_next; \ 459 else \ 460 (head)->tqh_last = &(elm)->field.tqe_next; \ 461 (head)->tqh_first = (elm); \ 462 (elm)->field.tqe_prev = &(head)->tqh_first; \ 463 } while (/* CONSTCOND */0) 464 465 #define TAILQ_INSERT_TAIL(head, elm, field) do { \ 466 QUEUEDEBUG_TAILQ_INSERT_TAIL((head), (elm), field) \ 467 (elm)->field.tqe_next = NULL; \ 468 (elm)->field.tqe_prev = (head)->tqh_last; \ 469 *(head)->tqh_last = (elm); \ 470 (head)->tqh_last = &(elm)->field.tqe_next; \ 471 } while (/* CONSTCOND */0) 472 473 #define TAILQ_INSERT_AFTER(head, listelm, elm, field) do { \ 474 QUEUEDEBUG_TAILQ_OP((listelm), field) \ 475 if (((elm)->field.tqe_next = (listelm)->field.tqe_next) != NULL)\ 476 (elm)->field.tqe_next->field.tqe_prev = \ 477 &(elm)->field.tqe_next; \ 478 else \ 479 (head)->tqh_last = &(elm)->field.tqe_next; \ 480 (listelm)->field.tqe_next = (elm); \ 481 (elm)->field.tqe_prev = &(listelm)->field.tqe_next; \ 482 } while (/* CONSTCOND */0) 483 484 #define TAILQ_INSERT_BEFORE(listelm, elm, field) do { \ 485 QUEUEDEBUG_TAILQ_OP((listelm), field) \ 486 (elm)->field.tqe_prev = (listelm)->field.tqe_prev; \ 487 (elm)->field.tqe_next = (listelm); \ 488 *(listelm)->field.tqe_prev = (elm); \ 489 (listelm)->field.tqe_prev = &(elm)->field.tqe_next; \ 490 } while (/* CONSTCOND */0) 491 492 #define TAILQ_REMOVE(head, elm, field) do { \ 493 QUEUEDEBUG_TAILQ_PREREMOVE((head), (elm), field) \ 494 QUEUEDEBUG_TAILQ_OP((elm), field) \ 495 if (((elm)->field.tqe_next) != NULL) \ 496 (elm)->field.tqe_next->field.tqe_prev = \ 497 (elm)->field.tqe_prev; \ 498 else \ 499 (head)->tqh_last = (elm)->field.tqe_prev; \ 500 *(elm)->field.tqe_prev = (elm)->field.tqe_next; \ 501 QUEUEDEBUG_TAILQ_POSTREMOVE((elm), field); \ 502 } while (/* CONSTCOND */0) 503 504 #define TAILQ_FOREACH(var, head, field) \ 505 for ((var) = ((head)->tqh_first); \ 506 (var); \ 507 (var) = ((var)->field.tqe_next)) 508 509 #define TAILQ_FOREACH_SAFE(var, head, field, next) \ 510 for ((var) = ((head)->tqh_first); \ 511 (var) != NULL && ((next) = TAILQ_NEXT(var, field), 1); \ 512 (var) = (next)) 513 514 #define TAILQ_FOREACH_REVERSE(var, head, headname, field) \ 515 for ((var) = (*(((struct headname *)((head)->tqh_last))->tqh_last)); \ 516 (var); \ 517 (var) = (*(((struct headname *)((var)->field.tqe_prev))->tqh_last))) 518 519 #define TAILQ_FOREACH_REVERSE_SAFE(var, head, headname, field, prev) \ 520 for ((var) = TAILQ_LAST((head), headname); \ 521 (var) && ((prev) = TAILQ_PREV((var), headname, field), 1);\ 522 (var) = (prev)) 523 524 #define TAILQ_CONCAT(head1, head2, field) do { \ 525 if (!TAILQ_EMPTY(head2)) { \ 526 *(head1)->tqh_last = (head2)->tqh_first; \ 527 (head2)->tqh_first->field.tqe_prev = (head1)->tqh_last; \ 528 (head1)->tqh_last = (head2)->tqh_last; \ 529 TAILQ_INIT((head2)); \ 530 } \ 531 } while (/* CONSTCOND */0) 532 533 /* 534 * Tail queue access methods. 535 */ 536 #define TAILQ_EMPTY(head) ((head)->tqh_first == NULL) 537 #define TAILQ_FIRST(head) ((head)->tqh_first) 538 #define TAILQ_NEXT(elm, field) ((elm)->field.tqe_next) 539 540 #define TAILQ_LAST(head, headname) \ 541 (*(((struct headname *)((head)->tqh_last))->tqh_last)) 542 #define TAILQ_PREV(elm, headname, field) \ 543 (*(((struct headname *)((elm)->field.tqe_prev))->tqh_last)) 544 545 /* 546 * Circular queue definitions. 547 */ 548 #if defined(_KERNEL) && defined(QUEUEDEBUG) 549 #define QUEUEDEBUG_CIRCLEQ_HEAD(head, field) \ 550 if ((head)->cqh_first != (void *)(head) && \ 551 (head)->cqh_first->field.cqe_prev != (void *)(head)) \ 552 panic("CIRCLEQ head forw %p %s:%d", (head), \ 553 __FILE__, __LINE__); \ 554 if ((head)->cqh_last != (void *)(head) && \ 555 (head)->cqh_last->field.cqe_next != (void *)(head)) \ 556 panic("CIRCLEQ head back %p %s:%d", (head), \ 557 __FILE__, __LINE__); 558 #define QUEUEDEBUG_CIRCLEQ_ELM(head, elm, field) \ 559 if ((elm)->field.cqe_next == (void *)(head)) { \ 560 if ((head)->cqh_last != (elm)) \ 561 panic("CIRCLEQ elm last %p %s:%d", (elm), \ 562 __FILE__, __LINE__); \ 563 } else { \ 564 if ((elm)->field.cqe_next->field.cqe_prev != (elm)) \ 565 panic("CIRCLEQ elm forw %p %s:%d", (elm), \ 566 __FILE__, __LINE__); \ 567 } \ 568 if ((elm)->field.cqe_prev == (void *)(head)) { \ 569 if ((head)->cqh_first != (elm)) \ 570 panic("CIRCLEQ elm first %p %s:%d", (elm), \ 571 __FILE__, __LINE__); \ 572 } else { \ 573 if ((elm)->field.cqe_prev->field.cqe_next != (elm)) \ 574 panic("CIRCLEQ elm prev %p %s:%d", (elm), \ 575 __FILE__, __LINE__); \ 576 } 577 #define QUEUEDEBUG_CIRCLEQ_POSTREMOVE(elm, field) \ 578 (elm)->field.cqe_next = (void *)1L; \ 579 (elm)->field.cqe_prev = (void *)1L; 580 #else 581 #define QUEUEDEBUG_CIRCLEQ_HEAD(head, field) 582 #define QUEUEDEBUG_CIRCLEQ_ELM(head, elm, field) 583 #define QUEUEDEBUG_CIRCLEQ_POSTREMOVE(elm, field) 584 #endif 585 586 #define CIRCLEQ_HEAD(name, type) \ 587 struct name { \ 588 struct type *cqh_first; /* first element */ \ 589 struct type *cqh_last; /* last element */ \ 590 } 591 592 #define CIRCLEQ_HEAD_INITIALIZER(head) \ 593 { (void *)&head, (void *)&head } 594 595 #define CIRCLEQ_ENTRY(type) \ 596 struct { \ 597 struct type *cqe_next; /* next element */ \ 598 struct type *cqe_prev; /* previous element */ \ 599 } 600 601 /* 602 * Circular queue functions. 603 */ 604 #define CIRCLEQ_INIT(head) do { \ 605 (head)->cqh_first = (void *)(head); \ 606 (head)->cqh_last = (void *)(head); \ 607 } while (/* CONSTCOND */0) 608 609 #define CIRCLEQ_INSERT_AFTER(head, listelm, elm, field) do { \ 610 QUEUEDEBUG_CIRCLEQ_HEAD((head), field) \ 611 QUEUEDEBUG_CIRCLEQ_ELM((head), (listelm), field) \ 612 (elm)->field.cqe_next = (listelm)->field.cqe_next; \ 613 (elm)->field.cqe_prev = (listelm); \ 614 if ((listelm)->field.cqe_next == (void *)(head)) \ 615 (head)->cqh_last = (elm); \ 616 else \ 617 (listelm)->field.cqe_next->field.cqe_prev = (elm); \ 618 (listelm)->field.cqe_next = (elm); \ 619 } while (/* CONSTCOND */0) 620 621 #define CIRCLEQ_INSERT_BEFORE(head, listelm, elm, field) do { \ 622 QUEUEDEBUG_CIRCLEQ_HEAD((head), field) \ 623 QUEUEDEBUG_CIRCLEQ_ELM((head), (listelm), field) \ 624 (elm)->field.cqe_next = (listelm); \ 625 (elm)->field.cqe_prev = (listelm)->field.cqe_prev; \ 626 if ((listelm)->field.cqe_prev == (void *)(head)) \ 627 (head)->cqh_first = (elm); \ 628 else \ 629 (listelm)->field.cqe_prev->field.cqe_next = (elm); \ 630 (listelm)->field.cqe_prev = (elm); \ 631 } while (/* CONSTCOND */0) 632 633 #define CIRCLEQ_INSERT_HEAD(head, elm, field) do { \ 634 QUEUEDEBUG_CIRCLEQ_HEAD((head), field) \ 635 (elm)->field.cqe_next = (head)->cqh_first; \ 636 (elm)->field.cqe_prev = (void *)(head); \ 637 if ((head)->cqh_last == (void *)(head)) \ 638 (head)->cqh_last = (elm); \ 639 else \ 640 (head)->cqh_first->field.cqe_prev = (elm); \ 641 (head)->cqh_first = (elm); \ 642 } while (/* CONSTCOND */0) 643 644 #define CIRCLEQ_INSERT_TAIL(head, elm, field) do { \ 645 QUEUEDEBUG_CIRCLEQ_HEAD((head), field) \ 646 (elm)->field.cqe_next = (void *)(head); \ 647 (elm)->field.cqe_prev = (head)->cqh_last; \ 648 if ((head)->cqh_first == (void *)(head)) \ 649 (head)->cqh_first = (elm); \ 650 else \ 651 (head)->cqh_last->field.cqe_next = (elm); \ 652 (head)->cqh_last = (elm); \ 653 } while (/* CONSTCOND */0) 654 655 #define CIRCLEQ_REMOVE(head, elm, field) do { \ 656 QUEUEDEBUG_CIRCLEQ_HEAD((head), field) \ 657 QUEUEDEBUG_CIRCLEQ_ELM((head), (elm), field) \ 658 if ((elm)->field.cqe_next == (void *)(head)) \ 659 (head)->cqh_last = (elm)->field.cqe_prev; \ 660 else \ 661 (elm)->field.cqe_next->field.cqe_prev = \ 662 (elm)->field.cqe_prev; \ 663 if ((elm)->field.cqe_prev == (void *)(head)) \ 664 (head)->cqh_first = (elm)->field.cqe_next; \ 665 else \ 666 (elm)->field.cqe_prev->field.cqe_next = \ 667 (elm)->field.cqe_next; \ 668 QUEUEDEBUG_CIRCLEQ_POSTREMOVE((elm), field) \ 669 } while (/* CONSTCOND */0) 670 671 #define CIRCLEQ_FOREACH(var, head, field) \ 672 for ((var) = ((head)->cqh_first); \ 673 (var) != (const void *)(head); \ 674 (var) = ((var)->field.cqe_next)) 675 676 #define CIRCLEQ_FOREACH_REVERSE(var, head, field) \ 677 for ((var) = ((head)->cqh_last); \ 678 (var) != (const void *)(head); \ 679 (var) = ((var)->field.cqe_prev)) 680 681 /* 682 * Circular queue access methods. 683 */ 684 #define CIRCLEQ_EMPTY(head) ((head)->cqh_first == (void *)(head)) 685 #define CIRCLEQ_FIRST(head) ((head)->cqh_first) 686 #define CIRCLEQ_LAST(head) ((head)->cqh_last) 687 #define CIRCLEQ_NEXT(elm, field) ((elm)->field.cqe_next) 688 #define CIRCLEQ_PREV(elm, field) ((elm)->field.cqe_prev) 689 690 #define CIRCLEQ_LOOP_NEXT(head, elm, field) \ 691 (((elm)->field.cqe_next == (void *)(head)) \ 692 ? ((head)->cqh_first) \ 693 : (elm->field.cqe_next)) 694 #define CIRCLEQ_LOOP_PREV(head, elm, field) \ 695 (((elm)->field.cqe_prev == (void *)(head)) \ 696 ? ((head)->cqh_last) \ 697 : (elm->field.cqe_prev)) 698 699 #endif /* !_SYS_QUEUE_H_ */ 700