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