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 */ 34 35 /* 36 * Slightly edited by tobez@plab.ku.dk, 26-Oct-1999 37 * The edits were: 38 * - removed #ifdef KERNEL part; 39 * - _SYS_QUEUE_H_ changed to _BSD_QUEUE_H_ everywhere. 40 * 41 * tobez@tobez.org, 23-Jun-2000: 42 * - added large ifndef SLIST_FOREACH, in case one of the standard 43 * system headers #include <sys/queue.h>. 44 * 45 * The original version was taken from: 46 * 47 * @(#)queue.h 8.5 (Berkeley) 8/20/94 48 * $FreeBSD: src/sys/sys/queue.h,v 1.30 1999/10/05 20:35:32 n_hibma Exp $ 49 */ 50 51 #ifndef SLIST_FOREACH 52 53 #ifndef _BSD_QUEUE_H_ 54 #define _BSD_QUEUE_H_ 55 56 /* 57 * This file defines five types of data structures: singly-linked lists, 58 * slingly-linked tail queues, lists, tail queues, and circular queues. 59 * 60 * A singly-linked list is headed by a single forward pointer. The elements 61 * are singly linked for minimum space and pointer manipulation overhead at 62 * the expense of O(n) removal for arbitrary elements. New elements can be 63 * added to the list after an existing element or at the head of the list. 64 * Elements being removed from the head of the list should use the explicit 65 * macro for this purpose for optimum efficiency. A singly-linked list may 66 * only be traversed in the forward direction. Singly-linked lists are ideal 67 * for applications with large datasets and few or no removals or for 68 * implementing a LIFO queue. 69 * 70 * A singly-linked tail queue is headed by a pair of pointers, one to the 71 * head of the list and the other to the tail of the list. The elements are 72 * singly linked for minimum space and pointer manipulation overhead at the 73 * expense of O(n) removal for arbitrary elements. New elements can be added 74 * to the list after an existing element, at the head of the list, or at the 75 * end of the list. Elements being removed from the head of the tail queue 76 * should use the explicit macro for this purpose for optimum efficiency. 77 * A singly-linked tail queue may only be traversed in the forward direction. 78 * Singly-linked tail queues are ideal for applications with large datasets 79 * and few or no removals or for implementing a FIFO queue. 80 * 81 * A list is headed by a single forward pointer (or an array of forward 82 * pointers for a hash table header). The elements are doubly linked 83 * so that an arbitrary element can be removed without a need to 84 * traverse the list. New elements can be added to the list before 85 * or after an existing element or at the head of the list. A list 86 * may only be traversed in the forward direction. 87 * 88 * A tail queue is headed by a pair of pointers, one to the head of the 89 * list and the other to the tail of the list. The elements are doubly 90 * linked so that an arbitrary element can be removed without a need to 91 * traverse the list. New elements can be added to the list before or 92 * after an existing element, at the head of the list, or at the end of 93 * the list. A tail queue may only be traversed in the forward direction. 94 * 95 * A circle queue is headed by a pair of pointers, one to the head of the 96 * list and the other to the tail of the list. The elements are doubly 97 * linked so that an arbitrary element can be removed without a need to 98 * traverse the list. New elements can be added to the list before or after 99 * an existing element, at the head of the list, or at the end of the list. 100 * A circle queue may be traversed in either direction, but has a more 101 * complex end of list detection. 102 * 103 * For details on the use of these macros, see the queue(3) manual page. 104 * 105 * 106 * SLIST LIST STAILQ TAILQ CIRCLEQ 107 * _HEAD + + + + + 108 * _ENTRY + + + + + 109 * _INIT + + + + + 110 * _EMPTY + + + + + 111 * _FIRST + + + + + 112 * _NEXT + + + + + 113 * _PREV - - - + + 114 * _LAST - - + + + 115 * _FOREACH + + + + + 116 * _INSERT_HEAD + + + + + 117 * _INSERT_BEFORE - + - + + 118 * _INSERT_AFTER + + + + + 119 * _INSERT_TAIL - - + + + 120 * _REMOVE_HEAD + - + - - 121 * _REMOVE + + + + + 122 * 123 */ 124 125 /* 126 * Singly-linked List definitions. 127 */ 128 #define SLIST_HEAD(name, type) \ 129 struct name { \ 130 struct type *slh_first; /* first element */ \ 131 } 132 133 #define SLIST_HEAD_INITIALIZER(head) \ 134 { NULL } 135 136 #define SLIST_ENTRY(type) \ 137 struct { \ 138 struct type *sle_next; /* next element */ \ 139 } 140 141 /* 142 * Singly-linked List functions. 143 */ 144 #define SLIST_EMPTY(head) ((head)->slh_first == NULL) 145 146 #define SLIST_FIRST(head) ((head)->slh_first) 147 148 #define SLIST_FOREACH(var, head, field) \ 149 for((var) = (head)->slh_first; (var); (var) = (var)->field.sle_next) 150 151 #define SLIST_INIT(head) { \ 152 (head)->slh_first = NULL; \ 153 } 154 155 #define SLIST_INSERT_AFTER(slistelm, elm, field) do { \ 156 (elm)->field.sle_next = (slistelm)->field.sle_next; \ 157 (slistelm)->field.sle_next = (elm); \ 158 } while (0) 159 160 #define SLIST_INSERT_HEAD(head, elm, field) do { \ 161 (elm)->field.sle_next = (head)->slh_first; \ 162 (head)->slh_first = (elm); \ 163 } while (0) 164 165 #define SLIST_NEXT(elm, field) ((elm)->field.sle_next) 166 167 #define SLIST_REMOVE_HEAD(head, field) do { \ 168 (head)->slh_first = (head)->slh_first->field.sle_next; \ 169 } while (0) 170 171 #define SLIST_REMOVE(head, elm, type, field) do { \ 172 if ((head)->slh_first == (elm)) { \ 173 SLIST_REMOVE_HEAD((head), field); \ 174 } \ 175 else { \ 176 struct type *curelm = (head)->slh_first; \ 177 while( curelm->field.sle_next != (elm) ) \ 178 curelm = curelm->field.sle_next; \ 179 curelm->field.sle_next = \ 180 curelm->field.sle_next->field.sle_next; \ 181 } \ 182 } while (0) 183 184 /* 185 * Singly-linked Tail queue definitions. 186 */ 187 #define STAILQ_HEAD(name, type) \ 188 struct name { \ 189 struct type *stqh_first;/* first element */ \ 190 struct type **stqh_last;/* addr of last next element */ \ 191 } 192 193 #define STAILQ_HEAD_INITIALIZER(head) \ 194 { NULL, &(head).stqh_first } 195 196 #define STAILQ_ENTRY(type) \ 197 struct { \ 198 struct type *stqe_next; /* next element */ \ 199 } 200 201 /* 202 * Singly-linked Tail queue functions. 203 */ 204 #define STAILQ_EMPTY(head) ((head)->stqh_first == NULL) 205 206 #define STAILQ_INIT(head) do { \ 207 (head)->stqh_first = NULL; \ 208 (head)->stqh_last = &(head)->stqh_first; \ 209 } while (0) 210 211 #define STAILQ_FIRST(head) ((head)->stqh_first) 212 #define STAILQ_LAST(head) (*(head)->stqh_last) 213 214 #define STAILQ_FOREACH(var, head, field) \ 215 for((var) = (head)->stqh_first; (var); (var) = (var)->field.stqe_next) 216 217 #define STAILQ_INSERT_HEAD(head, elm, field) do { \ 218 if (((elm)->field.stqe_next = (head)->stqh_first) == NULL) \ 219 (head)->stqh_last = &(elm)->field.stqe_next; \ 220 (head)->stqh_first = (elm); \ 221 } while (0) 222 223 #define STAILQ_INSERT_TAIL(head, elm, field) do { \ 224 (elm)->field.stqe_next = NULL; \ 225 *(head)->stqh_last = (elm); \ 226 (head)->stqh_last = &(elm)->field.stqe_next; \ 227 } while (0) 228 229 #define STAILQ_INSERT_AFTER(head, tqelm, elm, field) do { \ 230 if (((elm)->field.stqe_next = (tqelm)->field.stqe_next) == NULL)\ 231 (head)->stqh_last = &(elm)->field.stqe_next; \ 232 (tqelm)->field.stqe_next = (elm); \ 233 } while (0) 234 235 #define STAILQ_NEXT(elm, field) ((elm)->field.stqe_next) 236 237 #define STAILQ_REMOVE_HEAD(head, field) do { \ 238 if (((head)->stqh_first = \ 239 (head)->stqh_first->field.stqe_next) == NULL) \ 240 (head)->stqh_last = &(head)->stqh_first; \ 241 } while (0) 242 243 244 #define STAILQ_REMOVE(head, elm, type, field) do { \ 245 if ((head)->stqh_first == (elm)) { \ 246 STAILQ_REMOVE_HEAD(head, field); \ 247 } \ 248 else { \ 249 struct type *curelm = (head)->stqh_first; \ 250 while( curelm->field.stqe_next != (elm) ) \ 251 curelm = curelm->field.stqe_next; \ 252 if((curelm->field.stqe_next = \ 253 curelm->field.stqe_next->field.stqe_next) == NULL) \ 254 (head)->stqh_last = &(curelm)->field.stqe_next; \ 255 } \ 256 } while (0) 257 258 /* 259 * List definitions. 260 */ 261 #define LIST_HEAD(name, type) \ 262 struct name { \ 263 struct type *lh_first; /* first element */ \ 264 } 265 266 #define LIST_HEAD_INITIALIZER(head) \ 267 { NULL } 268 269 #define LIST_ENTRY(type) \ 270 struct { \ 271 struct type *le_next; /* next element */ \ 272 struct type **le_prev; /* address of previous next element */ \ 273 } 274 275 /* 276 * List functions. 277 */ 278 279 #define LIST_EMPTY(head) ((head)->lh_first == NULL) 280 281 #define LIST_FIRST(head) ((head)->lh_first) 282 283 #define LIST_FOREACH(var, head, field) \ 284 for((var) = (head)->lh_first; (var); (var) = (var)->field.le_next) 285 286 #define LIST_INIT(head) do { \ 287 (head)->lh_first = NULL; \ 288 } while (0) 289 290 #define LIST_INSERT_AFTER(listelm, elm, field) do { \ 291 if (((elm)->field.le_next = (listelm)->field.le_next) != NULL) \ 292 (listelm)->field.le_next->field.le_prev = \ 293 &(elm)->field.le_next; \ 294 (listelm)->field.le_next = (elm); \ 295 (elm)->field.le_prev = &(listelm)->field.le_next; \ 296 } while (0) 297 298 #define LIST_INSERT_BEFORE(listelm, elm, field) do { \ 299 (elm)->field.le_prev = (listelm)->field.le_prev; \ 300 (elm)->field.le_next = (listelm); \ 301 *(listelm)->field.le_prev = (elm); \ 302 (listelm)->field.le_prev = &(elm)->field.le_next; \ 303 } while (0) 304 305 #define LIST_INSERT_HEAD(head, elm, field) do { \ 306 if (((elm)->field.le_next = (head)->lh_first) != NULL) \ 307 (head)->lh_first->field.le_prev = &(elm)->field.le_next;\ 308 (head)->lh_first = (elm); \ 309 (elm)->field.le_prev = &(head)->lh_first; \ 310 } while (0) 311 312 #define LIST_NEXT(elm, field) ((elm)->field.le_next) 313 314 #define LIST_REMOVE(elm, field) do { \ 315 if ((elm)->field.le_next != NULL) \ 316 (elm)->field.le_next->field.le_prev = \ 317 (elm)->field.le_prev; \ 318 *(elm)->field.le_prev = (elm)->field.le_next; \ 319 } while (0) 320 321 /* 322 * Tail queue definitions. 323 */ 324 #define TAILQ_HEAD(name, type) \ 325 struct name { \ 326 struct type *tqh_first; /* first element */ \ 327 struct type **tqh_last; /* addr of last next element */ \ 328 } 329 330 #define TAILQ_HEAD_INITIALIZER(head) \ 331 { NULL, &(head).tqh_first } 332 333 #define TAILQ_ENTRY(type) \ 334 struct { \ 335 struct type *tqe_next; /* next element */ \ 336 struct type **tqe_prev; /* address of previous next element */ \ 337 } 338 339 /* 340 * Tail queue functions. 341 */ 342 #define TAILQ_EMPTY(head) ((head)->tqh_first == NULL) 343 344 #define TAILQ_FOREACH(var, head, field) \ 345 for (var = TAILQ_FIRST(head); var; var = TAILQ_NEXT(var, field)) 346 347 #define TAILQ_FIRST(head) ((head)->tqh_first) 348 349 #define TAILQ_LAST(head, headname) \ 350 (*(((struct headname *)((head)->tqh_last))->tqh_last)) 351 352 #define TAILQ_NEXT(elm, field) ((elm)->field.tqe_next) 353 354 #define TAILQ_PREV(elm, headname, field) \ 355 (*(((struct headname *)((elm)->field.tqe_prev))->tqh_last)) 356 357 #define TAILQ_INIT(head) do { \ 358 (head)->tqh_first = NULL; \ 359 (head)->tqh_last = &(head)->tqh_first; \ 360 } while (0) 361 362 #define TAILQ_INSERT_HEAD(head, elm, field) do { \ 363 if (((elm)->field.tqe_next = (head)->tqh_first) != NULL) \ 364 (head)->tqh_first->field.tqe_prev = \ 365 &(elm)->field.tqe_next; \ 366 else \ 367 (head)->tqh_last = &(elm)->field.tqe_next; \ 368 (head)->tqh_first = (elm); \ 369 (elm)->field.tqe_prev = &(head)->tqh_first; \ 370 } while (0) 371 372 #define TAILQ_INSERT_TAIL(head, elm, field) do { \ 373 (elm)->field.tqe_next = NULL; \ 374 (elm)->field.tqe_prev = (head)->tqh_last; \ 375 *(head)->tqh_last = (elm); \ 376 (head)->tqh_last = &(elm)->field.tqe_next; \ 377 } while (0) 378 379 #define TAILQ_INSERT_AFTER(head, listelm, elm, field) do { \ 380 if (((elm)->field.tqe_next = (listelm)->field.tqe_next) != NULL)\ 381 (elm)->field.tqe_next->field.tqe_prev = \ 382 &(elm)->field.tqe_next; \ 383 else \ 384 (head)->tqh_last = &(elm)->field.tqe_next; \ 385 (listelm)->field.tqe_next = (elm); \ 386 (elm)->field.tqe_prev = &(listelm)->field.tqe_next; \ 387 } while (0) 388 389 #define TAILQ_INSERT_BEFORE(listelm, elm, field) do { \ 390 (elm)->field.tqe_prev = (listelm)->field.tqe_prev; \ 391 (elm)->field.tqe_next = (listelm); \ 392 *(listelm)->field.tqe_prev = (elm); \ 393 (listelm)->field.tqe_prev = &(elm)->field.tqe_next; \ 394 } while (0) 395 396 #define TAILQ_REMOVE(head, elm, field) do { \ 397 if (((elm)->field.tqe_next) != NULL) \ 398 (elm)->field.tqe_next->field.tqe_prev = \ 399 (elm)->field.tqe_prev; \ 400 else \ 401 (head)->tqh_last = (elm)->field.tqe_prev; \ 402 *(elm)->field.tqe_prev = (elm)->field.tqe_next; \ 403 } while (0) 404 405 /* 406 * Circular queue definitions. 407 */ 408 #define CIRCLEQ_HEAD(name, type) \ 409 struct name { \ 410 struct type *cqh_first; /* first element */ \ 411 struct type *cqh_last; /* last element */ \ 412 } 413 414 #define CIRCLEQ_ENTRY(type) \ 415 struct { \ 416 struct type *cqe_next; /* next element */ \ 417 struct type *cqe_prev; /* previous element */ \ 418 } 419 420 /* 421 * Circular queue functions. 422 */ 423 #define CIRCLEQ_EMPTY(head) ((head)->cqh_first == (void *)(head)) 424 425 #define CIRCLEQ_FIRST(head) ((head)->cqh_first) 426 427 #define CIRCLEQ_FOREACH(var, head, field) \ 428 for((var) = (head)->cqh_first; \ 429 (var) != (void *)(head); \ 430 (var) = (var)->field.cqe_next) 431 432 #define CIRCLEQ_INIT(head) do { \ 433 (head)->cqh_first = (void *)(head); \ 434 (head)->cqh_last = (void *)(head); \ 435 } while (0) 436 437 #define CIRCLEQ_INSERT_AFTER(head, listelm, elm, field) do { \ 438 (elm)->field.cqe_next = (listelm)->field.cqe_next; \ 439 (elm)->field.cqe_prev = (listelm); \ 440 if ((listelm)->field.cqe_next == (void *)(head)) \ 441 (head)->cqh_last = (elm); \ 442 else \ 443 (listelm)->field.cqe_next->field.cqe_prev = (elm); \ 444 (listelm)->field.cqe_next = (elm); \ 445 } while (0) 446 447 #define CIRCLEQ_INSERT_BEFORE(head, listelm, elm, field) do { \ 448 (elm)->field.cqe_next = (listelm); \ 449 (elm)->field.cqe_prev = (listelm)->field.cqe_prev; \ 450 if ((listelm)->field.cqe_prev == (void *)(head)) \ 451 (head)->cqh_first = (elm); \ 452 else \ 453 (listelm)->field.cqe_prev->field.cqe_next = (elm); \ 454 (listelm)->field.cqe_prev = (elm); \ 455 } while (0) 456 457 #define CIRCLEQ_INSERT_HEAD(head, elm, field) do { \ 458 (elm)->field.cqe_next = (head)->cqh_first; \ 459 (elm)->field.cqe_prev = (void *)(head); \ 460 if ((head)->cqh_last == (void *)(head)) \ 461 (head)->cqh_last = (elm); \ 462 else \ 463 (head)->cqh_first->field.cqe_prev = (elm); \ 464 (head)->cqh_first = (elm); \ 465 } while (0) 466 467 #define CIRCLEQ_INSERT_TAIL(head, elm, field) do { \ 468 (elm)->field.cqe_next = (void *)(head); \ 469 (elm)->field.cqe_prev = (head)->cqh_last; \ 470 if ((head)->cqh_first == (void *)(head)) \ 471 (head)->cqh_first = (elm); \ 472 else \ 473 (head)->cqh_last->field.cqe_next = (elm); \ 474 (head)->cqh_last = (elm); \ 475 } while (0) 476 477 #define CIRCLEQ_LAST(head) ((head)->cqh_last) 478 479 #define CIRCLEQ_NEXT(elm,field) ((elm)->field.cqe_next) 480 481 #define CIRCLEQ_PREV(elm,field) ((elm)->field.cqe_prev) 482 483 #define CIRCLEQ_REMOVE(head, elm, field) do { \ 484 if ((elm)->field.cqe_next == (void *)(head)) \ 485 (head)->cqh_last = (elm)->field.cqe_prev; \ 486 else \ 487 (elm)->field.cqe_next->field.cqe_prev = \ 488 (elm)->field.cqe_prev; \ 489 if ((elm)->field.cqe_prev == (void *)(head)) \ 490 (head)->cqh_first = (elm)->field.cqe_next; \ 491 else \ 492 (elm)->field.cqe_prev->field.cqe_next = \ 493 (elm)->field.cqe_next; \ 494 } while (0) 495 496 #endif /* !_BSD_QUEUE_H_ */ 497 498 #endif /* !SLIST_FOREACH */ 499