queue.h revision 99091
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 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * @(#)queue.h 8.5 (Berkeley) 8/20/94 34 * $FreeBSD: head/sys/sys/queue.h 99091 2002-06-29 19:16:10Z julian $ 35 */ 36 37#ifndef _SYS_QUEUE_H_ 38#define _SYS_QUEUE_H_ 39 40#include <machine/ansi.h> /* for __offsetof */ 41 42/* 43 * This file defines four types of data structures: singly-linked lists, 44 * singly-linked tail queues, lists and tail queues. 45 * 46 * A singly-linked list is headed by a single forward pointer. The elements 47 * are singly linked for minimum space and pointer manipulation overhead at 48 * the expense of O(n) removal for arbitrary elements. New elements can be 49 * added to the list after an existing element or at the head of the list. 50 * Elements being removed from the head of the list should use the explicit 51 * macro for this purpose for optimum efficiency. A singly-linked list may 52 * only be traversed in the forward direction. Singly-linked lists are ideal 53 * for applications with large datasets and few or no removals or for 54 * implementing a LIFO queue. 55 * 56 * A singly-linked tail queue is headed by a pair of pointers, one to the 57 * head of the list and the other to the tail of the list. The elements are 58 * singly linked for minimum space and pointer manipulation overhead at the 59 * expense of O(n) removal for arbitrary elements. New elements can be added 60 * to the list after an existing element, at the head of the list, or at the 61 * end of the list. Elements being removed from the head of the tail queue 62 * should use the explicit macro for this purpose for optimum efficiency. 63 * A singly-linked tail queue may only be traversed in the forward direction. 64 * Singly-linked tail queues are ideal for applications with large datasets 65 * and few or no removals or for implementing a FIFO queue. 66 * 67 * A list is headed by a single forward pointer (or an array of forward 68 * pointers for a hash table header). The elements are doubly linked 69 * 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 71 * or after an existing element or at the head of the list. A list 72 * may only be traversed in the forward direction. 73 * 74 * A tail 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 78 * after an existing element, at the head of the list, or at the end of 79 * the list. A tail queue may be traversed in either direction. 80 * 81 * For details on the use of these macros, see the queue(3) manual page. 82 * 83 * 84 * SLIST LIST STAILQ TAILQ 85 * _HEAD + + + + 86 * _HEAD_INITIALIZER + + + + 87 * _ENTRY + + + + 88 * _INIT + + + + 89 * _EMPTY + + + + 90 * _FIRST + + + + 91 * _NEXT + + + + 92 * _PREV - - - + 93 * _LAST - - + + 94 * _FOREACH + + + + 95 * _FOREACH_REVERSE - - - + 96 * _INSERT_HEAD + + + + 97 * _INSERT_BEFORE - + - + 98 * _INSERT_AFTER + + + + 99 * _INSERT_TAIL - - + + 100 * _CONCAT - - + + 101 * _REMOVE_HEAD + - + - 102 * _REMOVE + + + + 103 * 104 */ 105#define QUEUE_MACRO_DEBUG 0 106#if QUEUE_MACRO_DEBUG 107/* Store the last 2 places the queue element or head was altered */ 108struct qm_trace { 109 char * lastfile; 110 int lastline; 111 char * prevfile; 112 int prevline; 113}; 114 115#define TRACEBUF struct qm_trace trace; 116 117#define QMD_TRACE_HEAD(head) do { \ 118 (head)->trace.prevline = (head)->trace.lastline; \ 119 (head)->trace.prevfile = (head)->trace.lastfile; \ 120 (head)->trace.lastline = __LINE__; \ 121 (head)->trace.lastfile = __FILE__; \ 122} while (0) 123 124#define QMD_TRACE_ELEM(elem) do { \ 125 (elem)->trace.prevline = (elem)->trace.lastline; \ 126 (elem)->trace.prevfile = (elem)->trace.lastfile; \ 127 (elem)->trace.lastline = __LINE__; \ 128 (elem)->trace.lastfile = __FILE__; \ 129} while (0) 130 131#else 132#define QMD_TRACE_ELEM(elem) 133#define QMD_TRACE_HEAD(head) 134#define TRACEBUF 135#endif /* QUEUE_MACRO_DEBUG */ 136 137/* 138 * Singly-linked List declarations. 139 */ 140#define SLIST_HEAD(name, type) \ 141struct name { \ 142 struct type *slh_first; /* first element */ \ 143} 144 145#define SLIST_HEAD_INITIALIZER(head) \ 146 { NULL } 147 148#define SLIST_ENTRY(type) \ 149struct { \ 150 struct type *sle_next; /* next element */ \ 151} 152 153/* 154 * Singly-linked List functions. 155 */ 156#define SLIST_EMPTY(head) ((head)->slh_first == NULL) 157 158#define SLIST_FIRST(head) ((head)->slh_first) 159 160#define SLIST_FOREACH(var, head, field) \ 161 for ((var) = SLIST_FIRST((head)); \ 162 (var); \ 163 (var) = SLIST_NEXT((var), field)) 164 165#define SLIST_INIT(head) do { \ 166 SLIST_FIRST((head)) = NULL; \ 167} while (0) 168 169#define SLIST_INSERT_AFTER(slistelm, elm, field) do { \ 170 SLIST_NEXT((elm), field) = SLIST_NEXT((slistelm), field); \ 171 SLIST_NEXT((slistelm), field) = (elm); \ 172} while (0) 173 174#define SLIST_INSERT_HEAD(head, elm, field) do { \ 175 SLIST_NEXT((elm), field) = SLIST_FIRST((head)); \ 176 SLIST_FIRST((head)) = (elm); \ 177} while (0) 178 179#define SLIST_NEXT(elm, field) ((elm)->field.sle_next) 180 181#define SLIST_REMOVE(head, elm, type, field) do { \ 182 if (SLIST_FIRST((head)) == (elm)) { \ 183 SLIST_REMOVE_HEAD((head), field); \ 184 } \ 185 else { \ 186 struct type *curelm = SLIST_FIRST((head)); \ 187 while (SLIST_NEXT(curelm, field) != (elm)) \ 188 curelm = SLIST_NEXT(curelm, field); \ 189 SLIST_NEXT(curelm, field) = \ 190 SLIST_NEXT(SLIST_NEXT(curelm, field), field); \ 191 } \ 192} while (0) 193 194#define SLIST_REMOVE_HEAD(head, field) do { \ 195 SLIST_FIRST((head)) = SLIST_NEXT(SLIST_FIRST((head)), field); \ 196} while (0) 197 198/* 199 * Singly-linked Tail queue declarations. 200 */ 201#define STAILQ_HEAD(name, type) \ 202struct name { \ 203 struct type *stqh_first;/* first element */ \ 204 struct type **stqh_last;/* addr of last next element */ \ 205} 206 207#define STAILQ_HEAD_INITIALIZER(head) \ 208 { NULL, &(head).stqh_first } 209 210#define STAILQ_ENTRY(type) \ 211struct { \ 212 struct type *stqe_next; /* next element */ \ 213} 214 215/* 216 * Singly-linked Tail queue functions. 217 */ 218#define STAILQ_CONCAT(head1, head2) do { \ 219 if (!STAILQ_EMPTY((head2))) { \ 220 *(head1)->stqh_last = (head2)->stqh_first; \ 221 (head1)->stqh_last = (head2)->stqh_last; \ 222 STAILQ_INIT((head2)); \ 223 } \ 224} while (0) 225 226#define STAILQ_EMPTY(head) ((head)->stqh_first == NULL) 227 228#define STAILQ_FIRST(head) ((head)->stqh_first) 229 230#define STAILQ_FOREACH(var, head, field) \ 231 for((var) = STAILQ_FIRST((head)); \ 232 (var); \ 233 (var) = STAILQ_NEXT((var), field)) 234 235#define STAILQ_INIT(head) do { \ 236 STAILQ_FIRST((head)) = NULL; \ 237 (head)->stqh_last = &STAILQ_FIRST((head)); \ 238} while (0) 239 240#define STAILQ_INSERT_AFTER(head, tqelm, elm, field) do { \ 241 if ((STAILQ_NEXT((elm), field) = STAILQ_NEXT((tqelm), field)) == NULL)\ 242 (head)->stqh_last = &STAILQ_NEXT((elm), field); \ 243 STAILQ_NEXT((tqelm), field) = (elm); \ 244} while (0) 245 246#define STAILQ_INSERT_HEAD(head, elm, field) do { \ 247 if ((STAILQ_NEXT((elm), field) = STAILQ_FIRST((head))) == NULL) \ 248 (head)->stqh_last = &STAILQ_NEXT((elm), field); \ 249 STAILQ_FIRST((head)) = (elm); \ 250} while (0) 251 252#define STAILQ_INSERT_TAIL(head, elm, field) do { \ 253 STAILQ_NEXT((elm), field) = NULL; \ 254 *(head)->stqh_last = (elm); \ 255 (head)->stqh_last = &STAILQ_NEXT((elm), field); \ 256} while (0) 257 258#define STAILQ_LAST(head, type, field) \ 259 (STAILQ_EMPTY((head)) ? \ 260 NULL : \ 261 ((struct type *) \ 262 ((char *)((head)->stqh_last) - __offsetof(struct type, field)))) 263 264#define STAILQ_NEXT(elm, field) ((elm)->field.stqe_next) 265 266#define STAILQ_REMOVE(head, elm, type, field) do { \ 267 if (STAILQ_FIRST((head)) == (elm)) { \ 268 STAILQ_REMOVE_HEAD((head), field); \ 269 } \ 270 else { \ 271 struct type *curelm = STAILQ_FIRST((head)); \ 272 while (STAILQ_NEXT(curelm, field) != (elm)) \ 273 curelm = STAILQ_NEXT(curelm, field); \ 274 if ((STAILQ_NEXT(curelm, field) = \ 275 STAILQ_NEXT(STAILQ_NEXT(curelm, field), field)) == NULL)\ 276 (head)->stqh_last = &STAILQ_NEXT((curelm), field);\ 277 } \ 278} while (0) 279 280#define STAILQ_REMOVE_HEAD(head, field) do { \ 281 if ((STAILQ_FIRST((head)) = \ 282 STAILQ_NEXT(STAILQ_FIRST((head)), field)) == NULL) \ 283 (head)->stqh_last = &STAILQ_FIRST((head)); \ 284} while (0) 285 286#define STAILQ_REMOVE_HEAD_UNTIL(head, elm, field) do { \ 287 if ((STAILQ_FIRST((head)) = STAILQ_NEXT((elm), field)) == NULL) \ 288 (head)->stqh_last = &STAILQ_FIRST((head)); \ 289} while (0) 290 291/* 292 * List declarations. 293 */ 294#define LIST_HEAD(name, type) \ 295struct name { \ 296 struct type *lh_first; /* first element */ \ 297} 298 299#define LIST_HEAD_INITIALIZER(head) \ 300 { NULL } 301 302#define LIST_ENTRY(type) \ 303struct { \ 304 struct type *le_next; /* next element */ \ 305 struct type **le_prev; /* address of previous next element */ \ 306} 307 308/* 309 * List functions. 310 */ 311 312#define LIST_EMPTY(head) ((head)->lh_first == NULL) 313 314#define LIST_FIRST(head) ((head)->lh_first) 315 316#define LIST_FOREACH(var, head, field) \ 317 for ((var) = LIST_FIRST((head)); \ 318 (var); \ 319 (var) = LIST_NEXT((var), field)) 320 321#define LIST_INIT(head) do { \ 322 LIST_FIRST((head)) = NULL; \ 323} while (0) 324 325#define LIST_INSERT_AFTER(listelm, elm, field) do { \ 326 if ((LIST_NEXT((elm), field) = LIST_NEXT((listelm), field)) != NULL)\ 327 LIST_NEXT((listelm), field)->field.le_prev = \ 328 &LIST_NEXT((elm), field); \ 329 LIST_NEXT((listelm), field) = (elm); \ 330 (elm)->field.le_prev = &LIST_NEXT((listelm), field); \ 331} while (0) 332 333#define LIST_INSERT_BEFORE(listelm, elm, field) do { \ 334 (elm)->field.le_prev = (listelm)->field.le_prev; \ 335 LIST_NEXT((elm), field) = (listelm); \ 336 *(listelm)->field.le_prev = (elm); \ 337 (listelm)->field.le_prev = &LIST_NEXT((elm), field); \ 338} while (0) 339 340#define LIST_INSERT_HEAD(head, elm, field) do { \ 341 if ((LIST_NEXT((elm), field) = LIST_FIRST((head))) != NULL) \ 342 LIST_FIRST((head))->field.le_prev = &LIST_NEXT((elm), field);\ 343 LIST_FIRST((head)) = (elm); \ 344 (elm)->field.le_prev = &LIST_FIRST((head)); \ 345} while (0) 346 347#define LIST_NEXT(elm, field) ((elm)->field.le_next) 348 349#define LIST_REMOVE(elm, field) do { \ 350 if (LIST_NEXT((elm), field) != NULL) \ 351 LIST_NEXT((elm), field)->field.le_prev = \ 352 (elm)->field.le_prev; \ 353 *(elm)->field.le_prev = LIST_NEXT((elm), field); \ 354} while (0) 355 356/* 357 * Tail queue declarations. 358 */ 359#define TAILQ_HEAD(name, type) \ 360struct name { \ 361 struct type *tqh_first; /* first element */ \ 362 struct type **tqh_last; /* addr of last next element */ \ 363 TRACEBUF \ 364} 365 366#define TAILQ_HEAD_INITIALIZER(head) \ 367 { NULL, &(head).tqh_first } 368 369#define TAILQ_ENTRY(type) \ 370struct { \ 371 struct type *tqe_next; /* next element */ \ 372 struct type **tqe_prev; /* address of previous next element */ \ 373 TRACEBUF \ 374} 375 376/* 377 * Tail queue functions. 378 */ 379#define TAILQ_CONCAT(head1, head2, field) do { \ 380 if (!TAILQ_EMPTY(head2)) { \ 381 *(head1)->tqh_last = (head2)->tqh_first; \ 382 (head2)->tqh_first->field.tqe_prev = (head1)->tqh_last; \ 383 (head1)->tqh_last = (head2)->tqh_last; \ 384 TAILQ_INIT((head2)); \ 385 QMD_TRACE_HEAD(head); \ 386 QMD_TRACE_HEAD(head2); \ 387 } \ 388} while (0) 389 390#define TAILQ_EMPTY(head) ((head)->tqh_first == NULL) 391 392#define TAILQ_FIRST(head) ((head)->tqh_first) 393 394#define TAILQ_FOREACH(var, head, field) \ 395 for ((var) = TAILQ_FIRST((head)); \ 396 (var); \ 397 (var) = TAILQ_NEXT((var), field)) 398 399#define TAILQ_FOREACH_REVERSE(var, head, headname, field) \ 400 for ((var) = TAILQ_LAST((head), headname); \ 401 (var); \ 402 (var) = TAILQ_PREV((var), headname, field)) 403 404#define TAILQ_INIT(head) do { \ 405 TAILQ_FIRST((head)) = NULL; \ 406 (head)->tqh_last = &TAILQ_FIRST((head)); \ 407 QMD_TRACE_HEAD(head); \ 408} while (0) 409 410#define TAILQ_INSERT_AFTER(head, listelm, elm, field) do { \ 411 if ((TAILQ_NEXT((elm), field) = TAILQ_NEXT((listelm), field)) != NULL)\ 412 TAILQ_NEXT((elm), field)->field.tqe_prev = \ 413 &TAILQ_NEXT((elm), field); \ 414 else { \ 415 (head)->tqh_last = &TAILQ_NEXT((elm), field); \ 416 QMD_TRACE_HEAD(head); \ 417 } \ 418 TAILQ_NEXT((listelm), field) = (elm); \ 419 (elm)->field.tqe_prev = &TAILQ_NEXT((listelm), field); \ 420 QMD_TRACE_ELEM(&(elm)->field); \ 421 QMD_TRACE_ELEM(&listelm->field); \ 422} while (0) 423 424#define TAILQ_INSERT_BEFORE(listelm, elm, field) do { \ 425 (elm)->field.tqe_prev = (listelm)->field.tqe_prev; \ 426 TAILQ_NEXT((elm), field) = (listelm); \ 427 *(listelm)->field.tqe_prev = (elm); \ 428 (listelm)->field.tqe_prev = &TAILQ_NEXT((elm), field); \ 429 QMD_TRACE_ELEM(&(elm)->field); \ 430 QMD_TRACE_ELEM(&listelm->field); \ 431} while (0) 432 433#define TAILQ_INSERT_HEAD(head, elm, field) do { \ 434 if ((TAILQ_NEXT((elm), field) = TAILQ_FIRST((head))) != NULL) \ 435 TAILQ_FIRST((head))->field.tqe_prev = \ 436 &TAILQ_NEXT((elm), field); \ 437 else \ 438 (head)->tqh_last = &TAILQ_NEXT((elm), field); \ 439 TAILQ_FIRST((head)) = (elm); \ 440 (elm)->field.tqe_prev = &TAILQ_FIRST((head)); \ 441 QMD_TRACE_HEAD(head); \ 442 QMD_TRACE_ELEM(&(elm)->field); \ 443} while (0) 444 445#define TAILQ_INSERT_TAIL(head, elm, field) do { \ 446 TAILQ_NEXT((elm), field) = NULL; \ 447 (elm)->field.tqe_prev = (head)->tqh_last; \ 448 *(head)->tqh_last = (elm); \ 449 (head)->tqh_last = &TAILQ_NEXT((elm), field); \ 450 QMD_TRACE_HEAD(head); \ 451 QMD_TRACE_ELEM(&(elm)->field); \ 452} while (0) 453 454#define TAILQ_LAST(head, headname) \ 455 (*(((struct headname *)((head)->tqh_last))->tqh_last)) 456 457#define TAILQ_NEXT(elm, field) ((elm)->field.tqe_next) 458 459#define TAILQ_PREV(elm, headname, field) \ 460 (*(((struct headname *)((elm)->field.tqe_prev))->tqh_last)) 461 462#define TAILQ_REMOVE(head, elm, field) do { \ 463 if ((TAILQ_NEXT((elm), field)) != NULL) \ 464 TAILQ_NEXT((elm), field)->field.tqe_prev = \ 465 (elm)->field.tqe_prev; \ 466 else { \ 467 (head)->tqh_last = (elm)->field.tqe_prev; \ 468 QMD_TRACE_HEAD(head); \ 469 } \ 470 *(elm)->field.tqe_prev = TAILQ_NEXT((elm), field); \ 471 (elm)->field.tqe_next = (void *)-1; \ 472 QMD_TRACE_ELEM(&(elm)->field); \ 473} while (0) 474 475 476#ifdef _KERNEL 477 478/* 479 * XXX insque() and remque() are an old way of handling certain queues. 480 * They bogusly assumes that all queue heads look alike. 481 */ 482 483struct quehead { 484 struct quehead *qh_link; 485 struct quehead *qh_rlink; 486}; 487 488#ifdef __GNUC__ 489 490static __inline void 491insque(void *a, void *b) 492{ 493 struct quehead *element = (struct quehead *)a, 494 *head = (struct quehead *)b; 495 496 element->qh_link = head->qh_link; 497 element->qh_rlink = head; 498 head->qh_link = element; 499 element->qh_link->qh_rlink = element; 500} 501 502static __inline void 503remque(void *a) 504{ 505 struct quehead *element = (struct quehead *)a; 506 507 element->qh_link->qh_rlink = element->qh_rlink; 508 element->qh_rlink->qh_link = element->qh_link; 509 element->qh_rlink = 0; 510} 511 512#else /* !__GNUC__ */ 513 514void insque(void *a, void *b); 515void remque(void *a); 516 517#endif /* __GNUC__ */ 518 519#endif /* _KERNEL */ 520 521#endif /* !_SYS_QUEUE_H_ */ 522