1 /* $OpenBSD: queue.h,v 1.26 2004/05/04 16:59:32 grange 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
44 * This file defines linked list operations. It is derived from BSD with
45 * only the MACRO names being prefixed with DWC_. This is because a few of
46 * these names conflict with those on Linux. For documentation on use, see the
47 * inline comments in the source code. The original license for this source
48 * code applies and is preserved in the dwc_list.h source file.
52 * This file defines five types of data structures: singly-linked lists,
53 * lists, simple queues, tail queues, and circular queues.
56 * A singly-linked list is headed by a single forward pointer. The elements
57 * are singly linked for minimum space and pointer manipulation overhead at
58 * the expense of O(n) removal for arbitrary elements. New elements can be
59 * added to the list after an existing element or at the head of the list.
60 * Elements being removed from the head of the list should use the explicit
61 * macro for this purpose for optimum efficiency. A singly-linked list may
62 * only be traversed in the forward direction. Singly-linked lists are ideal
63 * for applications with large datasets and few or no removals or for
64 * implementing a LIFO queue.
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.
73 * A simple queue is headed by a pair of pointers, one the head of the
74 * list and the other to the tail of the list. The elements are singly
75 * linked to save space, so elements can only be removed from the
76 * head of the list. New elements can be added to the list before or after
77 * an existing element, at the head of the list, or at the end of the
78 * list. A simple queue may only be traversed in the forward direction.
80 * A tail queue is headed by a pair of pointers, one to the head of the
81 * list and the other to the tail of the list. The elements are doubly
82 * linked so that an arbitrary element can be removed without a need to
83 * traverse the list. New elements can be added to the list before or
84 * after an existing element, at the head of the list, or at the end of
85 * the list. A tail queue may be traversed in either direction.
87 * A circle queue is headed by a pair of pointers, one to the head of the
88 * list and the other to the tail of the list. The elements are doubly
89 * linked so that an arbitrary element can be removed without a need to
90 * traverse the list. New elements can be added to the list before or after
91 * an existing element, at the head of the list, or at the end of the list.
92 * A circle queue may be traversed in either direction, but has a more
93 * complex end of list detection.
95 * For details on the use of these macros, see the queue(3) manual page.
102 typedef struct dwc_list_link {
103 struct dwc_list_link *next;
104 struct dwc_list_link *prev;
107 #define DWC_LIST_INIT(link) do { \
108 (link)->next = (link); \
109 (link)->prev = (link); \
112 #define DWC_LIST_FIRST(link) ((link)->next)
113 #define DWC_LIST_LAST(link) ((link)->prev)
114 #define DWC_LIST_END(link) (link)
115 #define DWC_LIST_NEXT(link) ((link)->next)
116 #define DWC_LIST_PREV(link) ((link)->prev)
117 #define DWC_LIST_EMPTY(link) \
118 (DWC_LIST_FIRST(link) == DWC_LIST_END(link))
119 #define DWC_LIST_ENTRY(link, type, field) \
120 (type *)((uint8_t *)(link) - (size_t)(&((type *)0)->field))
123 #define DWC_LIST_INSERT_HEAD(list, link) do { \
124 (link)->next = (list)->next; \
125 (link)->prev = (list); \
126 (list)->next->prev = (link); \
127 (list)->next = (link); \
130 #define DWC_LIST_INSERT_TAIL(list, link) do { \
131 (link)->next = (list); \
132 (link)->prev = (list)->prev; \
133 (list)->prev->next = (link); \
134 (list)->prev = (link); \
137 #define DWC_LIST_INSERT_HEAD(list, link) do { \
138 dwc_list_link_t *__next__ = (list)->next; \
139 __next__->prev = (link); \
140 (link)->next = __next__; \
141 (link)->prev = (list); \
142 (list)->next = (link); \
145 #define DWC_LIST_INSERT_TAIL(list, link) do { \
146 dwc_list_link_t *__prev__ = (list)->prev; \
147 (list)->prev = (link); \
148 (link)->next = (list); \
149 (link)->prev = __prev__; \
150 __prev__->next = (link); \
155 static inline void __list_add(struct list_head *new,
156 struct list_head *prev,
157 struct list_head *next)
165 static inline void list_add(struct list_head *new,
166 struct list_head *head)
168 __list_add(new, head, head->next);
171 static inline void list_add_tail(struct list_head *new,
172 struct list_head *head)
174 __list_add(new, head->prev, head);
177 static inline void __list_del(struct list_head *prev,
178 struct list_head *next)
184 static inline void list_del(struct list_head *entry)
186 __list_del(entry->prev, entry->next);
187 entry->next = LIST_POISON1;
188 entry->prev = LIST_POISON2;
192 #define DWC_LIST_REMOVE(link) do { \
193 (link)->next->prev = (link)->prev; \
194 (link)->prev->next = (link)->next; \
197 #define DWC_LIST_REMOVE_INIT(link) do { \
198 DWC_LIST_REMOVE(link); \
199 DWC_LIST_INIT(link); \
202 #define DWC_LIST_MOVE_HEAD(list, link) do { \
203 DWC_LIST_REMOVE(link); \
204 DWC_LIST_INSERT_HEAD(list, link); \
207 #define DWC_LIST_MOVE_TAIL(list, link) do { \
208 DWC_LIST_REMOVE(link); \
209 DWC_LIST_INSERT_TAIL(list, link); \
212 #define DWC_LIST_FOREACH(var, list) \
213 for ((var) = DWC_LIST_FIRST(list); \
214 (var) != DWC_LIST_END(list); \
215 (var) = DWC_LIST_NEXT(var))
217 #define DWC_LIST_FOREACH_SAFE(var, var2, list) \
218 for ((var) = DWC_LIST_FIRST(list), (var2) = DWC_LIST_NEXT(var); \
219 (var) != DWC_LIST_END(list); \
220 (var) = (var2), (var2) = DWC_LIST_NEXT(var2))
222 #define DWC_LIST_FOREACH_REVERSE(var, list) \
223 for ((var) = DWC_LIST_LAST(list); \
224 (var) != DWC_LIST_END(list); \
225 (var) = DWC_LIST_PREV(var))
228 * Singly-linked List definitions.
230 #define DWC_SLIST_HEAD(name, type) \
232 struct type *slh_first; /* first element */ \
235 #define DWC_SLIST_HEAD_INITIALIZER(head) \
238 #define DWC_SLIST_ENTRY(type) \
240 struct type *sle_next; /* next element */ \
244 * Singly-linked List access methods.
246 #define DWC_SLIST_FIRST(head) ((head)->slh_first)
247 #define DWC_SLIST_END(head) NULL
248 #define DWC_SLIST_EMPTY(head) (SLIST_FIRST(head) == SLIST_END(head))
249 #define DWC_SLIST_NEXT(elm, field) ((elm)->field.sle_next)
251 #define DWC_SLIST_FOREACH(var, head, field) \
252 for ((var) = SLIST_FIRST(head); \
253 (var) != SLIST_END(head); \
254 (var) = SLIST_NEXT(var, field))
256 #define DWC_SLIST_FOREACH_PREVPTR(var, varp, head, field) \
257 for ((varp) = &SLIST_FIRST((head)); \
258 ((var) = *(varp)) != SLIST_END(head); \
259 (varp) = &SLIST_NEXT((var), field))
262 * Singly-linked List functions.
264 #define DWC_SLIST_INIT(head) { \
265 SLIST_FIRST(head) = SLIST_END(head); \
268 #define DWC_SLIST_INSERT_AFTER(slistelm, elm, field) do { \
269 (elm)->field.sle_next = (slistelm)->field.sle_next; \
270 (slistelm)->field.sle_next = (elm); \
273 #define DWC_SLIST_INSERT_HEAD(head, elm, field) do { \
274 (elm)->field.sle_next = (head)->slh_first; \
275 (head)->slh_first = (elm); \
278 #define DWC_SLIST_REMOVE_NEXT(head, elm, field) do { \
279 (elm)->field.sle_next = (elm)->field.sle_next->field.sle_next; \
282 #define DWC_SLIST_REMOVE_HEAD(head, field) do { \
283 (head)->slh_first = (head)->slh_first->field.sle_next; \
286 #define DWC_SLIST_REMOVE(head, elm, type, field) do { \
287 if ((head)->slh_first == (elm)) { \
288 SLIST_REMOVE_HEAD((head), field); \
291 struct type *curelm = (head)->slh_first; \
292 while (curelm->field.sle_next != (elm)) \
293 curelm = curelm->field.sle_next; \
294 curelm->field.sle_next = \
295 curelm->field.sle_next->field.sle_next; \
300 * Simple queue definitions.
302 #define DWC_SIMPLEQ_HEAD(name, type) \
304 struct type *sqh_first; /* first element */ \
305 struct type **sqh_last; /* addr of last next element */ \
308 #define DWC_SIMPLEQ_HEAD_INITIALIZER(head) \
309 { NULL, &(head).sqh_first }
311 #define DWC_SIMPLEQ_ENTRY(type) \
313 struct type *sqe_next; /* next element */ \
317 * Simple queue access methods.
319 #define DWC_SIMPLEQ_FIRST(head) ((head)->sqh_first)
320 #define DWC_SIMPLEQ_END(head) NULL
321 #define DWC_SIMPLEQ_EMPTY(head) (SIMPLEQ_FIRST(head) == SIMPLEQ_END(head))
322 #define DWC_SIMPLEQ_NEXT(elm, field) ((elm)->field.sqe_next)
324 #define DWC_SIMPLEQ_FOREACH(var, head, field) \
325 for ((var) = SIMPLEQ_FIRST(head); \
326 (var) != SIMPLEQ_END(head); \
327 (var) = SIMPLEQ_NEXT(var, field))
330 * Simple queue functions.
332 #define DWC_SIMPLEQ_INIT(head) do { \
333 (head)->sqh_first = NULL; \
334 (head)->sqh_last = &(head)->sqh_first; \
337 #define DWC_SIMPLEQ_INSERT_HEAD(head, elm, field) do { \
338 (elm)->field.sqe_next = (head)->sqh_first; \
339 if ((elm)->field.sqe_next == NULL) \
340 (head)->sqh_last = &(elm)->field.sqe_next; \
341 (head)->sqh_first = (elm); \
344 #define DWC_SIMPLEQ_INSERT_TAIL(head, elm, field) do { \
345 (elm)->field.sqe_next = NULL; \
346 *(head)->sqh_last = (elm); \
347 (head)->sqh_last = &(elm)->field.sqe_next; \
350 #define DWC_SIMPLEQ_INSERT_AFTER(head, listelm, elm, field) do { \
351 (elm)->field.sqe_next = (listelm)->field.sqe_next; \
352 if ((elm)->field.sqe_next == NULL) \
353 (head)->sqh_last = &(elm)->field.sqe_next; \
354 (listelm)->field.sqe_next = (elm); \
357 #define DWC_SIMPLEQ_REMOVE_HEAD(head, field) do { \
358 (head)->sqh_first = (head)->sqh_first->field.sqe_next; \
359 if ((head)->sqh_first == NULL) \
360 (head)->sqh_last = &(head)->sqh_first; \
364 * Tail queue definitions.
366 #define DWC_TAILQ_HEAD(name, type) \
368 struct type *tqh_first; /* first element */ \
369 struct type **tqh_last; /* addr of last next element */ \
372 #define DWC_TAILQ_HEAD_INITIALIZER(head) \
373 { NULL, &(head).tqh_first }
375 #define DWC_TAILQ_ENTRY(type) \
377 struct type *tqe_next; /* next element */ \
378 struct type **tqe_prev; /* address of previous next element */ \
382 * tail queue access methods
384 #define DWC_TAILQ_FIRST(head) ((head)->tqh_first)
385 #define DWC_TAILQ_END(head) NULL
386 #define DWC_TAILQ_NEXT(elm, field) ((elm)->field.tqe_next)
387 #define DWC_TAILQ_LAST(head, headname) \
388 (*(((struct headname *)((head)->tqh_last))->tqh_last))
390 #define DWC_TAILQ_PREV(elm, headname, field) \
391 (*(((struct headname *)((elm)->field.tqe_prev))->tqh_last))
392 #define DWC_TAILQ_EMPTY(head) \
393 (TAILQ_FIRST(head) == TAILQ_END(head))
395 #define DWC_TAILQ_FOREACH(var, head, field) \
396 for ((var) = TAILQ_FIRST(head); \
397 (var) != TAILQ_END(head); \
398 (var) = TAILQ_NEXT(var, field))
400 #define DWC_TAILQ_FOREACH_REVERSE(var, head, headname, field) \
401 for ((var) = TAILQ_LAST(head, headname); \
402 (var) != TAILQ_END(head); \
403 (var) = TAILQ_PREV(var, headname, field))
406 * Tail queue functions.
408 #define DWC_TAILQ_INIT(head) do { \
409 (head)->tqh_first = NULL; \
410 (head)->tqh_last = &(head)->tqh_first; \
413 #define DWC_TAILQ_INSERT_HEAD(head, elm, field) do { \
414 (elm)->field.tqe_next = (head)->tqh_first; \
415 if ((elm)->field.tqe_next != NULL) \
416 (head)->tqh_first->field.tqe_prev = \
417 &(elm)->field.tqe_next; \
419 (head)->tqh_last = &(elm)->field.tqe_next; \
420 (head)->tqh_first = (elm); \
421 (elm)->field.tqe_prev = &(head)->tqh_first; \
424 #define DWC_TAILQ_INSERT_TAIL(head, elm, field) do { \
425 (elm)->field.tqe_next = NULL; \
426 (elm)->field.tqe_prev = (head)->tqh_last; \
427 *(head)->tqh_last = (elm); \
428 (head)->tqh_last = &(elm)->field.tqe_next; \
431 #define DWC_TAILQ_INSERT_AFTER(head, listelm, elm, field) do { \
432 (elm)->field.tqe_next = (listelm)->field.tqe_next; \
433 if ((elm)->field.tqe_next != NULL) \
434 (elm)->field.tqe_next->field.tqe_prev = \
435 &(elm)->field.tqe_next; \
437 (head)->tqh_last = &(elm)->field.tqe_next; \
438 (listelm)->field.tqe_next = (elm); \
439 (elm)->field.tqe_prev = &(listelm)->field.tqe_next; \
442 #define DWC_TAILQ_INSERT_BEFORE(listelm, elm, field) do { \
443 (elm)->field.tqe_prev = (listelm)->field.tqe_prev; \
444 (elm)->field.tqe_next = (listelm); \
445 *(listelm)->field.tqe_prev = (elm); \
446 (listelm)->field.tqe_prev = &(elm)->field.tqe_next; \
449 #define DWC_TAILQ_REMOVE(head, elm, field) do { \
450 if (((elm)->field.tqe_next) != NULL) \
451 (elm)->field.tqe_next->field.tqe_prev = \
452 (elm)->field.tqe_prev; \
454 (head)->tqh_last = (elm)->field.tqe_prev; \
455 *(elm)->field.tqe_prev = (elm)->field.tqe_next; \
458 #define DWC_TAILQ_REPLACE(head, elm, elm2, field) do { \
459 (elm2)->field.tqe_next = (elm)->field.tqe_next; \
460 if ((elm2)->field.tqe_next != NULL) \
461 (elm2)->field.tqe_next->field.tqe_prev = \
462 &(elm2)->field.tqe_next; \
464 (head)->tqh_last = &(elm2)->field.tqe_next; \
465 (elm2)->field.tqe_prev = (elm)->field.tqe_prev; \
466 *(elm2)->field.tqe_prev = (elm2); \
470 * Circular queue definitions.
472 #define DWC_CIRCLEQ_HEAD(name, type) \
474 struct type *cqh_first; /* first element */ \
475 struct type *cqh_last; /* last element */ \
478 #define DWC_CIRCLEQ_HEAD_INITIALIZER(head) \
479 { DWC_CIRCLEQ_END(&head), DWC_CIRCLEQ_END(&head) }
481 #define DWC_CIRCLEQ_ENTRY(type) \
483 struct type *cqe_next; /* next element */ \
484 struct type *cqe_prev; /* previous element */ \
488 * Circular queue access methods
490 #define DWC_CIRCLEQ_FIRST(head) ((head)->cqh_first)
491 #define DWC_CIRCLEQ_LAST(head) ((head)->cqh_last)
492 #define DWC_CIRCLEQ_END(head) ((void *)(head))
493 #define DWC_CIRCLEQ_NEXT(elm, field) ((elm)->field.cqe_next)
494 #define DWC_CIRCLEQ_PREV(elm, field) ((elm)->field.cqe_prev)
495 #define DWC_CIRCLEQ_EMPTY(head) \
496 (DWC_CIRCLEQ_FIRST(head) == DWC_CIRCLEQ_END(head))
498 #define DWC_CIRCLEQ_EMPTY_ENTRY(elm, field) (((elm)->field.cqe_next == NULL) && ((elm)->field.cqe_prev == NULL))
500 #define DWC_CIRCLEQ_FOREACH(var, head, field) \
501 for ((var) = DWC_CIRCLEQ_FIRST(head); \
502 (var) != DWC_CIRCLEQ_END(head); \
503 (var) = DWC_CIRCLEQ_NEXT(var, field))
505 #define DWC_CIRCLEQ_FOREACH_SAFE(var, var2, head, field) \
506 for ((var) = DWC_CIRCLEQ_FIRST(head), var2 = DWC_CIRCLEQ_NEXT(var, field); \
507 (var) != DWC_CIRCLEQ_END(head); \
508 (var) = var2, var2 = DWC_CIRCLEQ_NEXT(var, field))
510 #define DWC_CIRCLEQ_FOREACH_REVERSE(var, head, field) \
511 for ((var) = DWC_CIRCLEQ_LAST(head); \
512 (var) != DWC_CIRCLEQ_END(head); \
513 (var) = DWC_CIRCLEQ_PREV(var, field))
516 * Circular queue functions.
518 #define DWC_CIRCLEQ_INIT(head) do { \
519 (head)->cqh_first = DWC_CIRCLEQ_END(head); \
520 (head)->cqh_last = DWC_CIRCLEQ_END(head); \
523 #define DWC_CIRCLEQ_INIT_ENTRY(elm, field) do { \
524 (elm)->field.cqe_next = NULL; \
525 (elm)->field.cqe_prev = NULL; \
528 #define DWC_CIRCLEQ_INSERT_AFTER(head, listelm, elm, field) do { \
529 (elm)->field.cqe_next = (listelm)->field.cqe_next; \
530 (elm)->field.cqe_prev = (listelm); \
531 if ((listelm)->field.cqe_next == DWC_CIRCLEQ_END(head)) \
532 (head)->cqh_last = (elm); \
534 (listelm)->field.cqe_next->field.cqe_prev = (elm); \
535 (listelm)->field.cqe_next = (elm); \
538 #define DWC_CIRCLEQ_INSERT_BEFORE(head, listelm, elm, field) do { \
539 (elm)->field.cqe_next = (listelm); \
540 (elm)->field.cqe_prev = (listelm)->field.cqe_prev; \
541 if ((listelm)->field.cqe_prev == DWC_CIRCLEQ_END(head)) \
542 (head)->cqh_first = (elm); \
544 (listelm)->field.cqe_prev->field.cqe_next = (elm); \
545 (listelm)->field.cqe_prev = (elm); \
548 #define DWC_CIRCLEQ_INSERT_HEAD(head, elm, field) do { \
549 (elm)->field.cqe_next = (head)->cqh_first; \
550 (elm)->field.cqe_prev = DWC_CIRCLEQ_END(head); \
551 if ((head)->cqh_last == DWC_CIRCLEQ_END(head)) \
552 (head)->cqh_last = (elm); \
554 (head)->cqh_first->field.cqe_prev = (elm); \
555 (head)->cqh_first = (elm); \
558 #define DWC_CIRCLEQ_INSERT_TAIL(head, elm, field) do { \
559 (elm)->field.cqe_next = DWC_CIRCLEQ_END(head); \
560 (elm)->field.cqe_prev = (head)->cqh_last; \
561 if ((head)->cqh_first == DWC_CIRCLEQ_END(head)) \
562 (head)->cqh_first = (elm); \
564 (head)->cqh_last->field.cqe_next = (elm); \
565 (head)->cqh_last = (elm); \
568 #define DWC_CIRCLEQ_REMOVE(head, elm, field) do { \
569 if ((elm)->field.cqe_next == DWC_CIRCLEQ_END(head)) \
570 (head)->cqh_last = (elm)->field.cqe_prev; \
572 (elm)->field.cqe_next->field.cqe_prev = \
573 (elm)->field.cqe_prev; \
574 if ((elm)->field.cqe_prev == DWC_CIRCLEQ_END(head)) \
575 (head)->cqh_first = (elm)->field.cqe_next; \
577 (elm)->field.cqe_prev->field.cqe_next = \
578 (elm)->field.cqe_next; \
581 #define DWC_CIRCLEQ_REMOVE_INIT(head, elm, field) do { \
582 DWC_CIRCLEQ_REMOVE(head, elm, field); \
583 DWC_CIRCLEQ_INIT_ENTRY(elm, field); \
586 #define DWC_CIRCLEQ_REPLACE(head, elm, elm2, field) do { \
587 (elm2)->field.cqe_next = (elm)->field.cqe_next; \
588 if ((elm2)->field.cqe_next == \
589 DWC_CIRCLEQ_END(head)) \
590 (head).cqh_last = (elm2); \
592 (elm2)->field.cqe_next->field.cqe_prev = (elm2); \
593 (elm2)->field.cqe_prev = (elm)->field.cqe_prev; \
594 if ((elm2)->field.cqe_prev == \
595 DWC_CIRCLEQ_END(head)) \
596 (head).cqh_first = (elm2); \
598 (elm2)->field.cqe_prev->field.cqe_next = (elm2); \
605 #endif /* _DWC_LIST_H_ */