queue.h revision 99262
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 99262 2002-07-02 16:37:56Z 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#define TRASHIT(x) do {(x) = (void *)-1} while (0) 117 118#define QMD_TRACE_HEAD(head) do { \ 119 (head)->trace.prevline = (head)->trace.lastline; \ 120 (head)->trace.prevfile = (head)->trace.lastfile; \ 121 (head)->trace.lastline = __LINE__; \ 122 (head)->trace.lastfile = __FILE__; \ 123} while (0) 124 125#define QMD_TRACE_ELEM(elem) do { \ 126 (elem)->trace.prevline = (elem)->trace.lastline; \ 127 (elem)->trace.prevfile = (elem)->trace.lastfile; \ 128 (elem)->trace.lastline = __LINE__; \ 129 (elem)->trace.lastfile = __FILE__; \ 130} while (0) 131 132#else 133#define QMD_TRACE_ELEM(elem) 134#define QMD_TRACE_HEAD(head) 135#define TRACEBUF 136#define TRASHIT(x) 137#endif /* QUEUE_MACRO_DEBUG */ 138 139/* 140 * Singly-linked List declarations. 141 */ 142#define SLIST_HEAD(name, type) \ 143struct name { \ 144 struct type *slh_first; /* first element */ \ 145} 146 147#define SLIST_HEAD_INITIALIZER(head) \ 148 { NULL } 149 150#define SLIST_ENTRY(type) \ 151struct { \ 152 struct type *sle_next; /* next element */ \ 153} 154 155/* 156 * Singly-linked List functions. 157 */ 158#define SLIST_EMPTY(head) ((head)->slh_first == NULL) 159 160#define SLIST_FIRST(head) ((head)->slh_first) 161 162#define SLIST_FOREACH(var, head, field) \ 163 for ((var) = SLIST_FIRST((head)); \ 164 (var); \ 165 (var) = SLIST_NEXT((var), field)) 166 167#define SLIST_INIT(head) do { \ 168 SLIST_FIRST((head)) = NULL; \ 169} while (0) 170 171#define SLIST_INSERT_AFTER(slistelm, elm, field) do { \ 172 SLIST_NEXT((elm), field) = SLIST_NEXT((slistelm), field); \ 173 SLIST_NEXT((slistelm), field) = (elm); \ 174} while (0) 175 176#define SLIST_INSERT_HEAD(head, elm, field) do { \ 177 SLIST_NEXT((elm), field) = SLIST_FIRST((head)); \ 178 SLIST_FIRST((head)) = (elm); \ 179} while (0) 180 181#define SLIST_NEXT(elm, field) ((elm)->field.sle_next) 182 183#define SLIST_REMOVE(head, elm, type, field) do { \ 184 if (SLIST_FIRST((head)) == (elm)) { \ 185 SLIST_REMOVE_HEAD((head), field); \ 186 } \ 187 else { \ 188 struct type *curelm = SLIST_FIRST((head)); \ 189 while (SLIST_NEXT(curelm, field) != (elm)) \ 190 curelm = SLIST_NEXT(curelm, field); \ 191 SLIST_NEXT(curelm, field) = \ 192 SLIST_NEXT(SLIST_NEXT(curelm, field), field); \ 193 } \ 194} while (0) 195 196#define SLIST_REMOVE_HEAD(head, field) do { \ 197 SLIST_FIRST((head)) = SLIST_NEXT(SLIST_FIRST((head)), field); \ 198} while (0) 199 200/* 201 * Singly-linked Tail queue declarations. 202 */ 203#define STAILQ_HEAD(name, type) \ 204struct name { \ 205 struct type *stqh_first;/* first element */ \ 206 struct type **stqh_last;/* addr of last next element */ \ 207} 208 209#define STAILQ_HEAD_INITIALIZER(head) \ 210 { NULL, &(head).stqh_first } 211 212#define STAILQ_ENTRY(type) \ 213struct { \ 214 struct type *stqe_next; /* next element */ \ 215} 216 217/* 218 * Singly-linked Tail queue functions. 219 */ 220#define STAILQ_CONCAT(head1, head2) do { \ 221 if (!STAILQ_EMPTY((head2))) { \ 222 *(head1)->stqh_last = (head2)->stqh_first; \ 223 (head1)->stqh_last = (head2)->stqh_last; \ 224 STAILQ_INIT((head2)); \ 225 } \ 226} while (0) 227 228#define STAILQ_EMPTY(head) ((head)->stqh_first == NULL) 229 230#define STAILQ_FIRST(head) ((head)->stqh_first) 231 232#define STAILQ_FOREACH(var, head, field) \ 233 for((var) = STAILQ_FIRST((head)); \ 234 (var); \ 235 (var) = STAILQ_NEXT((var), field)) 236 237#define STAILQ_INIT(head) do { \ 238 STAILQ_FIRST((head)) = NULL; \ 239 (head)->stqh_last = &STAILQ_FIRST((head)); \ 240} while (0) 241 242#define STAILQ_INSERT_AFTER(head, tqelm, elm, field) do { \ 243 if ((STAILQ_NEXT((elm), field) = STAILQ_NEXT((tqelm), field)) == NULL)\ 244 (head)->stqh_last = &STAILQ_NEXT((elm), field); \ 245 STAILQ_NEXT((tqelm), field) = (elm); \ 246} while (0) 247 248#define STAILQ_INSERT_HEAD(head, elm, field) do { \ 249 if ((STAILQ_NEXT((elm), field) = STAILQ_FIRST((head))) == NULL) \ 250 (head)->stqh_last = &STAILQ_NEXT((elm), field); \ 251 STAILQ_FIRST((head)) = (elm); \ 252} while (0) 253 254#define STAILQ_INSERT_TAIL(head, elm, field) do { \ 255 STAILQ_NEXT((elm), field) = NULL; \ 256 *(head)->stqh_last = (elm); \ 257 (head)->stqh_last = &STAILQ_NEXT((elm), field); \ 258} while (0) 259 260#define STAILQ_LAST(head, type, field) \ 261 (STAILQ_EMPTY((head)) ? \ 262 NULL : \ 263 ((struct type *) \ 264 ((char *)((head)->stqh_last) - __offsetof(struct type, field)))) 265 266#define STAILQ_NEXT(elm, field) ((elm)->field.stqe_next) 267 268#define STAILQ_REMOVE(head, elm, type, field) do { \ 269 if (STAILQ_FIRST((head)) == (elm)) { \ 270 STAILQ_REMOVE_HEAD((head), field); \ 271 } \ 272 else { \ 273 struct type *curelm = STAILQ_FIRST((head)); \ 274 while (STAILQ_NEXT(curelm, field) != (elm)) \ 275 curelm = STAILQ_NEXT(curelm, field); \ 276 if ((STAILQ_NEXT(curelm, field) = \ 277 STAILQ_NEXT(STAILQ_NEXT(curelm, field), field)) == NULL)\ 278 (head)->stqh_last = &STAILQ_NEXT((curelm), field);\ 279 } \ 280} while (0) 281 282#define STAILQ_REMOVE_HEAD(head, field) do { \ 283 if ((STAILQ_FIRST((head)) = \ 284 STAILQ_NEXT(STAILQ_FIRST((head)), field)) == NULL) \ 285 (head)->stqh_last = &STAILQ_FIRST((head)); \ 286} while (0) 287 288#define STAILQ_REMOVE_HEAD_UNTIL(head, elm, field) do { \ 289 if ((STAILQ_FIRST((head)) = STAILQ_NEXT((elm), field)) == NULL) \ 290 (head)->stqh_last = &STAILQ_FIRST((head)); \ 291} while (0) 292 293/* 294 * List declarations. 295 */ 296#define LIST_HEAD(name, type) \ 297struct name { \ 298 struct type *lh_first; /* first element */ \ 299} 300 301#define LIST_HEAD_INITIALIZER(head) \ 302 { NULL } 303 304#define LIST_ENTRY(type) \ 305struct { \ 306 struct type *le_next; /* next element */ \ 307 struct type **le_prev; /* address of previous next element */ \ 308} 309 310/* 311 * List functions. 312 */ 313 314#define LIST_EMPTY(head) ((head)->lh_first == NULL) 315 316#define LIST_FIRST(head) ((head)->lh_first) 317 318#define LIST_FOREACH(var, head, field) \ 319 for ((var) = LIST_FIRST((head)); \ 320 (var); \ 321 (var) = LIST_NEXT((var), field)) 322 323#define LIST_INIT(head) do { \ 324 LIST_FIRST((head)) = NULL; \ 325} while (0) 326 327#define LIST_INSERT_AFTER(listelm, elm, field) do { \ 328 if ((LIST_NEXT((elm), field) = LIST_NEXT((listelm), field)) != NULL)\ 329 LIST_NEXT((listelm), field)->field.le_prev = \ 330 &LIST_NEXT((elm), field); \ 331 LIST_NEXT((listelm), field) = (elm); \ 332 (elm)->field.le_prev = &LIST_NEXT((listelm), field); \ 333} while (0) 334 335#define LIST_INSERT_BEFORE(listelm, elm, field) do { \ 336 (elm)->field.le_prev = (listelm)->field.le_prev; \ 337 LIST_NEXT((elm), field) = (listelm); \ 338 *(listelm)->field.le_prev = (elm); \ 339 (listelm)->field.le_prev = &LIST_NEXT((elm), field); \ 340} while (0) 341 342#define LIST_INSERT_HEAD(head, elm, field) do { \ 343 if ((LIST_NEXT((elm), field) = LIST_FIRST((head))) != NULL) \ 344 LIST_FIRST((head))->field.le_prev = &LIST_NEXT((elm), field);\ 345 LIST_FIRST((head)) = (elm); \ 346 (elm)->field.le_prev = &LIST_FIRST((head)); \ 347} while (0) 348 349#define LIST_NEXT(elm, field) ((elm)->field.le_next) 350 351#define LIST_REMOVE(elm, field) do { \ 352 if (LIST_NEXT((elm), field) != NULL) \ 353 LIST_NEXT((elm), field)->field.le_prev = \ 354 (elm)->field.le_prev; \ 355 *(elm)->field.le_prev = LIST_NEXT((elm), field); \ 356} while (0) 357 358/* 359 * Tail queue declarations. 360 */ 361#define TAILQ_HEAD(name, type) \ 362struct name { \ 363 struct type *tqh_first; /* first element */ \ 364 struct type **tqh_last; /* addr of last next element */ \ 365 TRACEBUF \ 366} 367 368#define TAILQ_HEAD_INITIALIZER(head) \ 369 { NULL, &(head).tqh_first } 370 371#define TAILQ_ENTRY(type) \ 372struct { \ 373 struct type *tqe_next; /* next element */ \ 374 struct type **tqe_prev; /* address of previous next element */ \ 375 TRACEBUF \ 376} 377 378/* 379 * Tail queue functions. 380 */ 381#define TAILQ_CONCAT(head1, head2, field) do { \ 382 if (!TAILQ_EMPTY(head2)) { \ 383 *(head1)->tqh_last = (head2)->tqh_first; \ 384 (head2)->tqh_first->field.tqe_prev = (head1)->tqh_last; \ 385 (head1)->tqh_last = (head2)->tqh_last; \ 386 TAILQ_INIT((head2)); \ 387 QMD_TRACE_HEAD(head); \ 388 QMD_TRACE_HEAD(head2); \ 389 } \ 390} while (0) 391 392#define TAILQ_EMPTY(head) ((head)->tqh_first == NULL) 393 394#define TAILQ_FIRST(head) ((head)->tqh_first) 395 396#define TAILQ_FOREACH(var, head, field) \ 397 for ((var) = TAILQ_FIRST((head)); \ 398 (var); \ 399 (var) = TAILQ_NEXT((var), field)) 400 401#define TAILQ_FOREACH_REVERSE(var, head, headname, field) \ 402 for ((var) = TAILQ_LAST((head), headname); \ 403 (var); \ 404 (var) = TAILQ_PREV((var), headname, field)) 405 406#define TAILQ_INIT(head) do { \ 407 TAILQ_FIRST((head)) = NULL; \ 408 (head)->tqh_last = &TAILQ_FIRST((head)); \ 409 QMD_TRACE_HEAD(head); \ 410} while (0) 411 412#define TAILQ_INSERT_AFTER(head, listelm, elm, field) do { \ 413 if ((TAILQ_NEXT((elm), field) = TAILQ_NEXT((listelm), field)) != NULL)\ 414 TAILQ_NEXT((elm), field)->field.tqe_prev = \ 415 &TAILQ_NEXT((elm), field); \ 416 else { \ 417 (head)->tqh_last = &TAILQ_NEXT((elm), field); \ 418 QMD_TRACE_HEAD(head); \ 419 } \ 420 TAILQ_NEXT((listelm), field) = (elm); \ 421 (elm)->field.tqe_prev = &TAILQ_NEXT((listelm), field); \ 422 QMD_TRACE_ELEM(&(elm)->field); \ 423 QMD_TRACE_ELEM(&listelm->field); \ 424} while (0) 425 426#define TAILQ_INSERT_BEFORE(listelm, elm, field) do { \ 427 (elm)->field.tqe_prev = (listelm)->field.tqe_prev; \ 428 TAILQ_NEXT((elm), field) = (listelm); \ 429 *(listelm)->field.tqe_prev = (elm); \ 430 (listelm)->field.tqe_prev = &TAILQ_NEXT((elm), field); \ 431 QMD_TRACE_ELEM(&(elm)->field); \ 432 QMD_TRACE_ELEM(&listelm->field); \ 433} while (0) 434 435#define TAILQ_INSERT_HEAD(head, elm, field) do { \ 436 if ((TAILQ_NEXT((elm), field) = TAILQ_FIRST((head))) != NULL) \ 437 TAILQ_FIRST((head))->field.tqe_prev = \ 438 &TAILQ_NEXT((elm), field); \ 439 else \ 440 (head)->tqh_last = &TAILQ_NEXT((elm), field); \ 441 TAILQ_FIRST((head)) = (elm); \ 442 (elm)->field.tqe_prev = &TAILQ_FIRST((head)); \ 443 QMD_TRACE_HEAD(head); \ 444 QMD_TRACE_ELEM(&(elm)->field); \ 445} while (0) 446 447#define TAILQ_INSERT_TAIL(head, elm, field) do { \ 448 TAILQ_NEXT((elm), field) = NULL; \ 449 (elm)->field.tqe_prev = (head)->tqh_last; \ 450 *(head)->tqh_last = (elm); \ 451 (head)->tqh_last = &TAILQ_NEXT((elm), field); \ 452 QMD_TRACE_HEAD(head); \ 453 QMD_TRACE_ELEM(&(elm)->field); \ 454} while (0) 455 456#define TAILQ_LAST(head, headname) \ 457 (*(((struct headname *)((head)->tqh_last))->tqh_last)) 458 459#define TAILQ_NEXT(elm, field) ((elm)->field.tqe_next) 460 461#define TAILQ_PREV(elm, headname, field) \ 462 (*(((struct headname *)((elm)->field.tqe_prev))->tqh_last)) 463 464#define TAILQ_REMOVE(head, elm, field) do { \ 465 if ((TAILQ_NEXT((elm), field)) != NULL) \ 466 TAILQ_NEXT((elm), field)->field.tqe_prev = \ 467 (elm)->field.tqe_prev; \ 468 else { \ 469 (head)->tqh_last = (elm)->field.tqe_prev; \ 470 QMD_TRACE_HEAD(head); \ 471 } \ 472 *(elm)->field.tqe_prev = TAILQ_NEXT((elm), field); \ 473 TRASHIT((elm)->field.tqe_next); \ 474 TRASHIT((elm)->field.tqe_prev); \ 475 QMD_TRACE_ELEM(&(elm)->field); \ 476} while (0) 477 478 479#ifdef _KERNEL 480 481/* 482 * XXX insque() and remque() are an old way of handling certain queues. 483 * They bogusly assumes that all queue heads look alike. 484 */ 485 486struct quehead { 487 struct quehead *qh_link; 488 struct quehead *qh_rlink; 489}; 490 491#ifdef __GNUC__ 492 493static __inline void 494insque(void *a, void *b) 495{ 496 struct quehead *element = (struct quehead *)a, 497 *head = (struct quehead *)b; 498 499 element->qh_link = head->qh_link; 500 element->qh_rlink = head; 501 head->qh_link = element; 502 element->qh_link->qh_rlink = element; 503} 504 505static __inline void 506remque(void *a) 507{ 508 struct quehead *element = (struct quehead *)a; 509 510 element->qh_link->qh_rlink = element->qh_rlink; 511 element->qh_rlink->qh_link = element->qh_link; 512 element->qh_rlink = 0; 513} 514 515#else /* !__GNUC__ */ 516 517void insque(void *a, void *b); 518void remque(void *a); 519 520#endif /* __GNUC__ */ 521 522#endif /* _KERNEL */ 523 524#endif /* !_SYS_QUEUE_H_ */ 525