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