]>
git.proxmox.com Git - mirror_frr.git/blob - lib/openbsd-queue.h
1 /* $OpenBSD: queue.h,v 1.43 2015/12/28 19:38:40 millert Exp $ */
2 /* $NetBSD: queue.h,v 1.11 1996/05/16 05:17:14 mycroft Exp $ */
5 * Copyright (c) 1991, 1993
6 * The Regents of the University of California. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * @(#)queue.h 8.5 (Berkeley) 8/20/94
43 * This file defines five types of data structures: singly-linked lists,
44 * lists, simple queues, tail queues and XOR simple queues.
47 * A singly-linked list is headed by a single forward pointer. The elements
48 * are singly linked for minimum space and pointer manipulation overhead at
49 * the expense of O(n) removal for arbitrary elements. New elements can be
50 * added to the list after an existing element or at the head of the list.
51 * Elements being removed from the head of the list should use the explicit
52 * macro for this purpose for optimum efficiency. A singly-linked list may
53 * only be traversed in the forward direction. Singly-linked lists are ideal
54 * for applications with large datasets and few or no removals or for
55 * implementing a LIFO queue.
57 * A list is headed by a single forward pointer (or an array of forward
58 * pointers for a hash table header). The elements are doubly linked
59 * so that an arbitrary element can be removed without a need to
60 * traverse the list. New elements can be added to the list before
61 * or after an existing element or at the head of the list. A list
62 * may only be traversed in the forward direction.
64 * A simple queue is headed by a pair of pointers, one to the head of the
65 * list and the other to the tail of the list. The elements are singly
66 * linked to save space, so elements can only be removed from the
67 * head of the list. New elements can be added to the list before or after
68 * an existing element, at the head of the list, or at the end of the
69 * list. A simple queue may only be traversed in the forward direction.
71 * A tail queue is headed by a pair of pointers, one to the head of the
72 * list and the other to the tail of the list. The elements are doubly
73 * linked so that an arbitrary element can be removed without a need to
74 * traverse the list. New elements can be added to the list before or
75 * after an existing element, at the head of the list, or at the end of
76 * the list. A tail queue may be traversed in either direction.
78 * An XOR simple queue is used in the same way as a regular simple queue.
79 * The difference is that the head structure also includes a "cookie" that
80 * is XOR'd with the queue pointer (first, last or next) to generate the
83 * For details on the use of these macros, see the queue(3) manual page.
86 #if defined(QUEUE_MACRO_DEBUG) || (defined(_KERNEL) && defined(DIAGNOSTIC))
87 #define _Q_INVALIDATE(a) (a) = ((void *)-1)
89 #define _Q_INVALIDATE(a)
93 * Singly-linked List definitions.
95 #define SLIST_HEAD(name, type) \
97 struct type *slh_first; /* first element */ \
100 #define SLIST_HEAD_INITIALIZER(head) \
105 #define SLIST_ENTRY(type) \
107 struct type *sle_next; /* next element */ \
111 * Singly-linked List access methods.
113 #define SLIST_FIRST(head) ((head)->slh_first)
114 #define SLIST_END(head) NULL
115 #define SLIST_EMPTY(head) (SLIST_FIRST(head) == SLIST_END(head))
116 #define SLIST_NEXT(elm, field) ((elm)->field.sle_next)
118 #define SLIST_FOREACH(var, head, field) \
119 for ((var) = SLIST_FIRST(head); (var) != SLIST_END(head); \
120 (var) = SLIST_NEXT(var, field))
122 #define SLIST_FOREACH_SAFE(var, head, field, tvar) \
123 for ((var) = SLIST_FIRST(head); \
124 (var) && ((tvar) = SLIST_NEXT(var, field), 1); (var) = (tvar))
127 * Singly-linked List functions.
129 #define SLIST_INIT(head) \
131 SLIST_FIRST(head) = SLIST_END(head); \
134 #define SLIST_INSERT_AFTER(slistelm, elm, field) \
136 (elm)->field.sle_next = (slistelm)->field.sle_next; \
137 (slistelm)->field.sle_next = (elm); \
140 #define SLIST_INSERT_HEAD(head, elm, field) \
142 (elm)->field.sle_next = (head)->slh_first; \
143 (head)->slh_first = (elm); \
146 #define SLIST_REMOVE_AFTER(elm, field) \
148 (elm)->field.sle_next = (elm)->field.sle_next->field.sle_next; \
151 #define SLIST_REMOVE_HEAD(head, field) \
153 (head)->slh_first = (head)->slh_first->field.sle_next; \
156 #define SLIST_REMOVE(head, elm, type, field) \
158 if ((head)->slh_first == (elm)) { \
159 SLIST_REMOVE_HEAD((head), field); \
161 struct type *curelm = (head)->slh_first; \
163 while (curelm->field.sle_next != (elm)) \
164 curelm = curelm->field.sle_next; \
165 curelm->field.sle_next = \
166 curelm->field.sle_next->field.sle_next; \
168 _Q_INVALIDATE((elm)->field.sle_next); \
174 #define LIST_HEAD(name, type) \
176 struct type *lh_first; /* first element */ \
179 #define LIST_HEAD_INITIALIZER(head) \
184 #define LIST_ENTRY(type) \
186 struct type *le_next; /* next element */ \
187 struct type **le_prev; /* address of previous next element */ \
191 * List access methods.
193 #define LIST_FIRST(head) ((head)->lh_first)
194 #define LIST_END(head) NULL
195 #define LIST_EMPTY(head) (LIST_FIRST(head) == LIST_END(head))
196 #define LIST_NEXT(elm, field) ((elm)->field.le_next)
198 #define LIST_FOREACH(var, head, field) \
199 for ((var) = LIST_FIRST(head); (var) != LIST_END(head); \
200 (var) = LIST_NEXT(var, field))
202 #define LIST_FOREACH_SAFE(var, head, field, tvar) \
203 for ((var) = LIST_FIRST(head); \
204 (var) && ((tvar) = LIST_NEXT(var, field), 1); (var) = (tvar))
209 #define LIST_INIT(head) \
211 LIST_FIRST(head) = LIST_END(head); \
214 #define LIST_INSERT_AFTER(listelm, elm, field) \
216 if (((elm)->field.le_next = (listelm)->field.le_next) != NULL) \
217 (listelm)->field.le_next->field.le_prev = \
218 &(elm)->field.le_next; \
219 (listelm)->field.le_next = (elm); \
220 (elm)->field.le_prev = &(listelm)->field.le_next; \
223 #define LIST_INSERT_BEFORE(listelm, elm, field) \
225 (elm)->field.le_prev = (listelm)->field.le_prev; \
226 (elm)->field.le_next = (listelm); \
227 *(listelm)->field.le_prev = (elm); \
228 (listelm)->field.le_prev = &(elm)->field.le_next; \
231 #define LIST_INSERT_HEAD(head, elm, field) \
233 if (((elm)->field.le_next = (head)->lh_first) != NULL) \
234 (head)->lh_first->field.le_prev = \
235 &(elm)->field.le_next; \
236 (head)->lh_first = (elm); \
237 (elm)->field.le_prev = &(head)->lh_first; \
240 #define LIST_REMOVE(elm, field) \
242 if ((elm)->field.le_next != NULL) \
243 (elm)->field.le_next->field.le_prev = \
244 (elm)->field.le_prev; \
245 *(elm)->field.le_prev = (elm)->field.le_next; \
246 _Q_INVALIDATE((elm)->field.le_prev); \
247 _Q_INVALIDATE((elm)->field.le_next); \
250 #define LIST_REPLACE(elm, elm2, field) \
252 if (((elm2)->field.le_next = (elm)->field.le_next) != NULL) \
253 (elm2)->field.le_next->field.le_prev = \
254 &(elm2)->field.le_next; \
255 (elm2)->field.le_prev = (elm)->field.le_prev; \
256 *(elm2)->field.le_prev = (elm2); \
257 _Q_INVALIDATE((elm)->field.le_prev); \
258 _Q_INVALIDATE((elm)->field.le_next); \
262 * Simple queue definitions.
264 #define SIMPLEQ_HEAD(name, type) \
266 struct type *sqh_first; /* first element */ \
267 struct type **sqh_last; /* addr of last next element */ \
270 #define SIMPLEQ_HEAD_INITIALIZER(head) \
272 NULL, &(head).sqh_first \
275 #define SIMPLEQ_ENTRY(type) \
277 struct type *sqe_next; /* next element */ \
281 * Simple queue access methods.
283 #define SIMPLEQ_FIRST(head) ((head)->sqh_first)
284 #define SIMPLEQ_END(head) NULL
285 #define SIMPLEQ_EMPTY(head) (SIMPLEQ_FIRST(head) == SIMPLEQ_END(head))
286 #define SIMPLEQ_NEXT(elm, field) ((elm)->field.sqe_next)
288 #define SIMPLEQ_FOREACH(var, head, field) \
289 for ((var) = SIMPLEQ_FIRST(head); (var) != SIMPLEQ_END(head); \
290 (var) = SIMPLEQ_NEXT(var, field))
292 #define SIMPLEQ_FOREACH_SAFE(var, head, field, tvar) \
293 for ((var) = SIMPLEQ_FIRST(head); \
294 (var) && ((tvar) = SIMPLEQ_NEXT(var, field), 1); (var) = (tvar))
297 * Simple queue functions.
299 #define SIMPLEQ_INIT(head) \
301 (head)->sqh_first = NULL; \
302 (head)->sqh_last = &(head)->sqh_first; \
305 #define SIMPLEQ_INSERT_HEAD(head, elm, field) \
307 if (((elm)->field.sqe_next = (head)->sqh_first) == NULL) \
308 (head)->sqh_last = &(elm)->field.sqe_next; \
309 (head)->sqh_first = (elm); \
312 #define SIMPLEQ_INSERT_TAIL(head, elm, field) \
314 (elm)->field.sqe_next = NULL; \
315 *(head)->sqh_last = (elm); \
316 (head)->sqh_last = &(elm)->field.sqe_next; \
319 #define SIMPLEQ_INSERT_AFTER(head, listelm, elm, field) \
321 if (((elm)->field.sqe_next = (listelm)->field.sqe_next) \
323 (head)->sqh_last = &(elm)->field.sqe_next; \
324 (listelm)->field.sqe_next = (elm); \
327 #define SIMPLEQ_REMOVE_HEAD(head, field) \
329 if (((head)->sqh_first = (head)->sqh_first->field.sqe_next) \
331 (head)->sqh_last = &(head)->sqh_first; \
334 #define SIMPLEQ_REMOVE_AFTER(head, elm, field) \
336 if (((elm)->field.sqe_next = \
337 (elm)->field.sqe_next->field.sqe_next) \
339 (head)->sqh_last = &(elm)->field.sqe_next; \
342 #define SIMPLEQ_CONCAT(head1, head2) \
344 if (!SIMPLEQ_EMPTY((head2))) { \
345 *(head1)->sqh_last = (head2)->sqh_first; \
346 (head1)->sqh_last = (head2)->sqh_last; \
347 SIMPLEQ_INIT((head2)); \
352 * XOR Simple queue definitions.
354 #define XSIMPLEQ_HEAD(name, type) \
356 struct type *sqx_first; /* first element */ \
357 struct type **sqx_last; /* addr of last next element */ \
358 unsigned long sqx_cookie; \
361 #define XSIMPLEQ_ENTRY(type) \
363 struct type *sqx_next; /* next element */ \
367 * XOR Simple queue access methods.
369 #define XSIMPLEQ_XOR(head, ptr) \
370 ((__typeof(ptr))((head)->sqx_cookie ^ (unsigned long)(ptr)))
371 #define XSIMPLEQ_FIRST(head) XSIMPLEQ_XOR(head, ((head)->sqx_first))
372 #define XSIMPLEQ_END(head) NULL
373 #define XSIMPLEQ_EMPTY(head) (XSIMPLEQ_FIRST(head) == XSIMPLEQ_END(head))
374 #define XSIMPLEQ_NEXT(head, elm, field) XSIMPLEQ_XOR(head, ((elm)->field.sqx_next))
376 #define XSIMPLEQ_FOREACH(var, head, field) \
377 for ((var) = XSIMPLEQ_FIRST(head); (var) != XSIMPLEQ_END(head); \
378 (var) = XSIMPLEQ_NEXT(head, var, field))
380 #define XSIMPLEQ_FOREACH_SAFE(var, head, field, tvar) \
381 for ((var) = XSIMPLEQ_FIRST(head); \
382 (var) && ((tvar) = XSIMPLEQ_NEXT(head, var, field), 1); \
386 * XOR Simple queue functions.
388 #define XSIMPLEQ_INIT(head) \
390 arc4random_buf(&(head)->sqx_cookie, \
391 sizeof((head)->sqx_cookie)); \
392 (head)->sqx_first = XSIMPLEQ_XOR(head, NULL); \
393 (head)->sqx_last = XSIMPLEQ_XOR(head, &(head)->sqx_first); \
396 #define XSIMPLEQ_INSERT_HEAD(head, elm, field) \
398 if (((elm)->field.sqx_next = (head)->sqx_first) \
399 == XSIMPLEQ_XOR(head, NULL)) \
401 XSIMPLEQ_XOR(head, &(elm)->field.sqx_next); \
402 (head)->sqx_first = XSIMPLEQ_XOR(head, (elm)); \
405 #define XSIMPLEQ_INSERT_TAIL(head, elm, field) \
407 (elm)->field.sqx_next = XSIMPLEQ_XOR(head, NULL); \
408 *(XSIMPLEQ_XOR(head, (head)->sqx_last)) = \
409 XSIMPLEQ_XOR(head, (elm)); \
410 (head)->sqx_last = XSIMPLEQ_XOR(head, &(elm)->field.sqx_next); \
413 #define XSIMPLEQ_INSERT_AFTER(head, listelm, elm, field) \
415 if (((elm)->field.sqx_next = (listelm)->field.sqx_next) \
416 == XSIMPLEQ_XOR(head, NULL)) \
418 XSIMPLEQ_XOR(head, &(elm)->field.sqx_next); \
419 (listelm)->field.sqx_next = XSIMPLEQ_XOR(head, (elm)); \
422 #define XSIMPLEQ_REMOVE_HEAD(head, field) \
424 if (((head)->sqx_first = XSIMPLEQ_XOR(head, (head)->sqx_first) \
426 == XSIMPLEQ_XOR(head, NULL)) \
428 XSIMPLEQ_XOR(head, &(head)->sqx_first); \
431 #define XSIMPLEQ_REMOVE_AFTER(head, elm, field) \
433 if (((elm)->field.sqx_next = \
434 XSIMPLEQ_XOR(head, (elm)->field.sqx_next) \
436 == XSIMPLEQ_XOR(head, NULL)) \
438 XSIMPLEQ_XOR(head, &(elm)->field.sqx_next); \
443 * Tail queue definitions.
445 #define TAILQ_HEAD(name, type) \
447 struct type *tqh_first; /* first element */ \
448 struct type **tqh_last; /* addr of last next element */ \
451 #define TAILQ_HEAD_INITIALIZER(head) \
453 NULL, &(head).tqh_first \
456 #define TAILQ_ENTRY(type) \
458 struct type *tqe_next; /* next element */ \
459 struct type **tqe_prev; /* address of previous next element */ \
463 * Tail queue access methods.
465 #define TAILQ_FIRST(head) ((head)->tqh_first)
466 #define TAILQ_END(head) NULL
467 #define TAILQ_NEXT(elm, field) ((elm)->field.tqe_next)
468 #define TAILQ_LAST(head, headname) \
469 (*(((struct headname *)((head)->tqh_last))->tqh_last))
471 #define TAILQ_PREV(elm, headname, field) \
472 (*(((struct headname *)((elm)->field.tqe_prev))->tqh_last))
473 #define TAILQ_EMPTY(head) (TAILQ_FIRST(head) == TAILQ_END(head))
475 #define TAILQ_FOREACH(var, head, field) \
476 for ((var) = TAILQ_FIRST(head); (var) != TAILQ_END(head); \
477 (var) = TAILQ_NEXT(var, field))
479 #define TAILQ_FOREACH_SAFE(var, head, field, tvar) \
480 for ((var) = TAILQ_FIRST(head); \
481 (var) != TAILQ_END(head) && ((tvar) = TAILQ_NEXT(var, field), 1); \
485 #define TAILQ_FOREACH_REVERSE(var, head, headname, field) \
486 for ((var) = TAILQ_LAST(head, headname); (var) != TAILQ_END(head); \
487 (var) = TAILQ_PREV(var, headname, field))
489 #define TAILQ_FOREACH_REVERSE_SAFE(var, head, headname, field, tvar) \
490 for ((var) = TAILQ_LAST(head, headname); \
491 (var) != TAILQ_END(head) \
492 && ((tvar) = TAILQ_PREV(var, headname, field), 1); \
496 * Tail queue functions.
498 #define TAILQ_INIT(head) \
500 (head)->tqh_first = NULL; \
501 (head)->tqh_last = &(head)->tqh_first; \
504 #define TAILQ_INSERT_HEAD(head, elm, field) \
506 if (((elm)->field.tqe_next = (head)->tqh_first) != NULL) \
507 (head)->tqh_first->field.tqe_prev = \
508 &(elm)->field.tqe_next; \
510 (head)->tqh_last = &(elm)->field.tqe_next; \
511 (head)->tqh_first = (elm); \
512 (elm)->field.tqe_prev = &(head)->tqh_first; \
515 #define TAILQ_INSERT_TAIL(head, elm, field) \
517 (elm)->field.tqe_next = NULL; \
518 (elm)->field.tqe_prev = (head)->tqh_last; \
519 *(head)->tqh_last = (elm); \
520 (head)->tqh_last = &(elm)->field.tqe_next; \
523 #define TAILQ_INSERT_AFTER(head, listelm, elm, field) \
525 if (((elm)->field.tqe_next = (listelm)->field.tqe_next) \
527 (elm)->field.tqe_next->field.tqe_prev = \
528 &(elm)->field.tqe_next; \
530 (head)->tqh_last = &(elm)->field.tqe_next; \
531 (listelm)->field.tqe_next = (elm); \
532 (elm)->field.tqe_prev = &(listelm)->field.tqe_next; \
535 #define TAILQ_INSERT_BEFORE(listelm, elm, field) \
537 (elm)->field.tqe_prev = (listelm)->field.tqe_prev; \
538 (elm)->field.tqe_next = (listelm); \
539 *(listelm)->field.tqe_prev = (elm); \
540 (listelm)->field.tqe_prev = &(elm)->field.tqe_next; \
543 #define TAILQ_REMOVE(head, elm, field) \
545 if (((elm)->field.tqe_next) != NULL) \
546 (elm)->field.tqe_next->field.tqe_prev = \
547 (elm)->field.tqe_prev; \
549 (head)->tqh_last = (elm)->field.tqe_prev; \
550 *(elm)->field.tqe_prev = (elm)->field.tqe_next; \
551 _Q_INVALIDATE((elm)->field.tqe_prev); \
552 _Q_INVALIDATE((elm)->field.tqe_next); \
555 #define TAILQ_REPLACE(head, elm, elm2, field) \
557 if (((elm2)->field.tqe_next = (elm)->field.tqe_next) != NULL) \
558 (elm2)->field.tqe_next->field.tqe_prev = \
559 &(elm2)->field.tqe_next; \
561 (head)->tqh_last = &(elm2)->field.tqe_next; \
562 (elm2)->field.tqe_prev = (elm)->field.tqe_prev; \
563 *(elm2)->field.tqe_prev = (elm2); \
564 _Q_INVALIDATE((elm)->field.tqe_prev); \
565 _Q_INVALIDATE((elm)->field.tqe_next); \
568 #define TAILQ_CONCAT(head1, head2, field) \
570 if (!TAILQ_EMPTY(head2)) { \
571 *(head1)->tqh_last = (head2)->tqh_first; \
572 (head2)->tqh_first->field.tqe_prev = \
574 (head1)->tqh_last = (head2)->tqh_last; \
575 TAILQ_INIT((head2)); \
583 #endif /* !_SYS_QUEUE_H_ */