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, struct list_head *head)
167 __list_add(new, head, head->next);
170 static inline void list_add_tail(struct list_head *new, struct list_head *head)
172 __list_add(new, head->prev, head);
175 static inline void __list_del(struct list_head * prev, struct list_head * next)
181 static inline void list_del(struct list_head *entry)
183 __list_del(entry->prev, entry->next);
184 entry->next = LIST_POISON1;
185 entry->prev = LIST_POISON2;
189 #define DWC_LIST_REMOVE(link) do { \
190 (link)->next->prev = (link)->prev; \
191 (link)->prev->next = (link)->next; \
194 #define DWC_LIST_REMOVE_INIT(link) do { \
195 DWC_LIST_REMOVE(link); \
196 DWC_LIST_INIT(link); \
199 #define DWC_LIST_MOVE_HEAD(list, link) do { \
200 DWC_LIST_REMOVE(link); \
201 DWC_LIST_INSERT_HEAD(list, link); \
204 #define DWC_LIST_MOVE_TAIL(list, link) do { \
205 DWC_LIST_REMOVE(link); \
206 DWC_LIST_INSERT_TAIL(list, link); \
209 #define DWC_LIST_FOREACH(var, list) \
210 for((var) = DWC_LIST_FIRST(list); \
211 (var) != DWC_LIST_END(list); \
212 (var) = DWC_LIST_NEXT(var))
214 #define DWC_LIST_FOREACH_SAFE(var, var2, list) \
215 for((var) = DWC_LIST_FIRST(list), (var2) = DWC_LIST_NEXT(var); \
216 (var) != DWC_LIST_END(list); \
217 (var) = (var2), (var2) = DWC_LIST_NEXT(var2))
219 #define DWC_LIST_FOREACH_REVERSE(var, list) \
220 for((var) = DWC_LIST_LAST(list); \
221 (var) != DWC_LIST_END(list); \
222 (var) = DWC_LIST_PREV(var))
225 * Singly-linked List definitions.
227 #define DWC_SLIST_HEAD(name, type) \
229 struct type *slh_first; /* first element */ \
232 #define DWC_SLIST_HEAD_INITIALIZER(head) \
235 #define DWC_SLIST_ENTRY(type) \
237 struct type *sle_next; /* next element */ \
241 * Singly-linked List access methods.
243 #define DWC_SLIST_FIRST(head) ((head)->slh_first)
244 #define DWC_SLIST_END(head) NULL
245 #define DWC_SLIST_EMPTY(head) (SLIST_FIRST(head) == SLIST_END(head))
246 #define DWC_SLIST_NEXT(elm, field) ((elm)->field.sle_next)
248 #define DWC_SLIST_FOREACH(var, head, field) \
249 for((var) = SLIST_FIRST(head); \
250 (var) != SLIST_END(head); \
251 (var) = SLIST_NEXT(var, field))
253 #define DWC_SLIST_FOREACH_PREVPTR(var, varp, head, field) \
254 for((varp) = &SLIST_FIRST((head)); \
255 ((var) = *(varp)) != SLIST_END(head); \
256 (varp) = &SLIST_NEXT((var), field))
259 * Singly-linked List functions.
261 #define DWC_SLIST_INIT(head) { \
262 SLIST_FIRST(head) = SLIST_END(head); \
265 #define DWC_SLIST_INSERT_AFTER(slistelm, elm, field) do { \
266 (elm)->field.sle_next = (slistelm)->field.sle_next; \
267 (slistelm)->field.sle_next = (elm); \
270 #define DWC_SLIST_INSERT_HEAD(head, elm, field) do { \
271 (elm)->field.sle_next = (head)->slh_first; \
272 (head)->slh_first = (elm); \
275 #define DWC_SLIST_REMOVE_NEXT(head, elm, field) do { \
276 (elm)->field.sle_next = (elm)->field.sle_next->field.sle_next; \
279 #define DWC_SLIST_REMOVE_HEAD(head, field) do { \
280 (head)->slh_first = (head)->slh_first->field.sle_next; \
283 #define DWC_SLIST_REMOVE(head, elm, type, field) do { \
284 if ((head)->slh_first == (elm)) { \
285 SLIST_REMOVE_HEAD((head), field); \
288 struct type *curelm = (head)->slh_first; \
289 while( curelm->field.sle_next != (elm) ) \
290 curelm = curelm->field.sle_next; \
291 curelm->field.sle_next = \
292 curelm->field.sle_next->field.sle_next; \
297 * Simple queue definitions.
299 #define DWC_SIMPLEQ_HEAD(name, type) \
301 struct type *sqh_first; /* first element */ \
302 struct type **sqh_last; /* addr of last next element */ \
305 #define DWC_SIMPLEQ_HEAD_INITIALIZER(head) \
306 { NULL, &(head).sqh_first }
308 #define DWC_SIMPLEQ_ENTRY(type) \
310 struct type *sqe_next; /* next element */ \
314 * Simple queue access methods.
316 #define DWC_SIMPLEQ_FIRST(head) ((head)->sqh_first)
317 #define DWC_SIMPLEQ_END(head) NULL
318 #define DWC_SIMPLEQ_EMPTY(head) (SIMPLEQ_FIRST(head) == SIMPLEQ_END(head))
319 #define DWC_SIMPLEQ_NEXT(elm, field) ((elm)->field.sqe_next)
321 #define DWC_SIMPLEQ_FOREACH(var, head, field) \
322 for((var) = SIMPLEQ_FIRST(head); \
323 (var) != SIMPLEQ_END(head); \
324 (var) = SIMPLEQ_NEXT(var, field))
327 * Simple queue functions.
329 #define DWC_SIMPLEQ_INIT(head) do { \
330 (head)->sqh_first = NULL; \
331 (head)->sqh_last = &(head)->sqh_first; \
334 #define DWC_SIMPLEQ_INSERT_HEAD(head, elm, field) do { \
335 if (((elm)->field.sqe_next = (head)->sqh_first) == NULL) \
336 (head)->sqh_last = &(elm)->field.sqe_next; \
337 (head)->sqh_first = (elm); \
340 #define DWC_SIMPLEQ_INSERT_TAIL(head, elm, field) do { \
341 (elm)->field.sqe_next = NULL; \
342 *(head)->sqh_last = (elm); \
343 (head)->sqh_last = &(elm)->field.sqe_next; \
346 #define DWC_SIMPLEQ_INSERT_AFTER(head, listelm, elm, field) do { \
347 if (((elm)->field.sqe_next = (listelm)->field.sqe_next) == NULL)\
348 (head)->sqh_last = &(elm)->field.sqe_next; \
349 (listelm)->field.sqe_next = (elm); \
352 #define DWC_SIMPLEQ_REMOVE_HEAD(head, field) do { \
353 if (((head)->sqh_first = (head)->sqh_first->field.sqe_next) == NULL) \
354 (head)->sqh_last = &(head)->sqh_first; \
358 * Tail queue definitions.
360 #define DWC_TAILQ_HEAD(name, type) \
362 struct type *tqh_first; /* first element */ \
363 struct type **tqh_last; /* addr of last next element */ \
366 #define DWC_TAILQ_HEAD_INITIALIZER(head) \
367 { NULL, &(head).tqh_first }
369 #define DWC_TAILQ_ENTRY(type) \
371 struct type *tqe_next; /* next element */ \
372 struct type **tqe_prev; /* address of previous next element */ \
376 * tail queue access methods
378 #define DWC_TAILQ_FIRST(head) ((head)->tqh_first)
379 #define DWC_TAILQ_END(head) NULL
380 #define DWC_TAILQ_NEXT(elm, field) ((elm)->field.tqe_next)
381 #define DWC_TAILQ_LAST(head, headname) \
382 (*(((struct headname *)((head)->tqh_last))->tqh_last))
384 #define DWC_TAILQ_PREV(elm, headname, field) \
385 (*(((struct headname *)((elm)->field.tqe_prev))->tqh_last))
386 #define DWC_TAILQ_EMPTY(head) \
387 (TAILQ_FIRST(head) == TAILQ_END(head))
389 #define DWC_TAILQ_FOREACH(var, head, field) \
390 for((var) = TAILQ_FIRST(head); \
391 (var) != TAILQ_END(head); \
392 (var) = TAILQ_NEXT(var, field))
394 #define DWC_TAILQ_FOREACH_REVERSE(var, head, headname, field) \
395 for((var) = TAILQ_LAST(head, headname); \
396 (var) != TAILQ_END(head); \
397 (var) = TAILQ_PREV(var, headname, field))
400 * Tail queue functions.
402 #define DWC_TAILQ_INIT(head) do { \
403 (head)->tqh_first = NULL; \
404 (head)->tqh_last = &(head)->tqh_first; \
407 #define DWC_TAILQ_INSERT_HEAD(head, elm, field) do { \
408 if (((elm)->field.tqe_next = (head)->tqh_first) != NULL) \
409 (head)->tqh_first->field.tqe_prev = \
410 &(elm)->field.tqe_next; \
412 (head)->tqh_last = &(elm)->field.tqe_next; \
413 (head)->tqh_first = (elm); \
414 (elm)->field.tqe_prev = &(head)->tqh_first; \
417 #define DWC_TAILQ_INSERT_TAIL(head, elm, field) do { \
418 (elm)->field.tqe_next = NULL; \
419 (elm)->field.tqe_prev = (head)->tqh_last; \
420 *(head)->tqh_last = (elm); \
421 (head)->tqh_last = &(elm)->field.tqe_next; \
424 #define DWC_TAILQ_INSERT_AFTER(head, listelm, elm, field) do { \
425 if (((elm)->field.tqe_next = (listelm)->field.tqe_next) != NULL)\
426 (elm)->field.tqe_next->field.tqe_prev = \
427 &(elm)->field.tqe_next; \
429 (head)->tqh_last = &(elm)->field.tqe_next; \
430 (listelm)->field.tqe_next = (elm); \
431 (elm)->field.tqe_prev = &(listelm)->field.tqe_next; \
434 #define DWC_TAILQ_INSERT_BEFORE(listelm, elm, field) do { \
435 (elm)->field.tqe_prev = (listelm)->field.tqe_prev; \
436 (elm)->field.tqe_next = (listelm); \
437 *(listelm)->field.tqe_prev = (elm); \
438 (listelm)->field.tqe_prev = &(elm)->field.tqe_next; \
441 #define DWC_TAILQ_REMOVE(head, elm, field) do { \
442 if (((elm)->field.tqe_next) != NULL) \
443 (elm)->field.tqe_next->field.tqe_prev = \
444 (elm)->field.tqe_prev; \
446 (head)->tqh_last = (elm)->field.tqe_prev; \
447 *(elm)->field.tqe_prev = (elm)->field.tqe_next; \
450 #define DWC_TAILQ_REPLACE(head, elm, elm2, field) do { \
451 if (((elm2)->field.tqe_next = (elm)->field.tqe_next) != NULL) \
452 (elm2)->field.tqe_next->field.tqe_prev = \
453 &(elm2)->field.tqe_next; \
455 (head)->tqh_last = &(elm2)->field.tqe_next; \
456 (elm2)->field.tqe_prev = (elm)->field.tqe_prev; \
457 *(elm2)->field.tqe_prev = (elm2); \
461 * Circular queue definitions.
463 #define DWC_CIRCLEQ_HEAD(name, type) \
465 struct type *cqh_first; /* first element */ \
466 struct type *cqh_last; /* last element */ \
469 #define DWC_CIRCLEQ_HEAD_INITIALIZER(head) \
470 { DWC_CIRCLEQ_END(&head), DWC_CIRCLEQ_END(&head) }
472 #define DWC_CIRCLEQ_ENTRY(type) \
474 struct type *cqe_next; /* next element */ \
475 struct type *cqe_prev; /* previous element */ \
479 * Circular queue access methods
481 #define DWC_CIRCLEQ_FIRST(head) ((head)->cqh_first)
482 #define DWC_CIRCLEQ_LAST(head) ((head)->cqh_last)
483 #define DWC_CIRCLEQ_END(head) ((void *)(head))
484 #define DWC_CIRCLEQ_NEXT(elm, field) ((elm)->field.cqe_next)
485 #define DWC_CIRCLEQ_PREV(elm, field) ((elm)->field.cqe_prev)
486 #define DWC_CIRCLEQ_EMPTY(head) \
487 (DWC_CIRCLEQ_FIRST(head) == DWC_CIRCLEQ_END(head))
489 #define DWC_CIRCLEQ_EMPTY_ENTRY(elm, field) (((elm)->field.cqe_next == NULL) && ((elm)->field.cqe_prev == NULL))
491 #define DWC_CIRCLEQ_FOREACH(var, head, field) \
492 for((var) = DWC_CIRCLEQ_FIRST(head); \
493 (var) != DWC_CIRCLEQ_END(head); \
494 (var) = DWC_CIRCLEQ_NEXT(var, field))
496 #define DWC_CIRCLEQ_FOREACH_SAFE(var, var2, head, field) \
497 for((var) = DWC_CIRCLEQ_FIRST(head), var2 = DWC_CIRCLEQ_NEXT(var, field); \
498 (var) != DWC_CIRCLEQ_END(head); \
499 (var) = var2, var2 = DWC_CIRCLEQ_NEXT(var, field))
501 #define DWC_CIRCLEQ_FOREACH_REVERSE(var, head, field) \
502 for((var) = DWC_CIRCLEQ_LAST(head); \
503 (var) != DWC_CIRCLEQ_END(head); \
504 (var) = DWC_CIRCLEQ_PREV(var, field))
507 * Circular queue functions.
509 #define DWC_CIRCLEQ_INIT(head) do { \
510 (head)->cqh_first = DWC_CIRCLEQ_END(head); \
511 (head)->cqh_last = DWC_CIRCLEQ_END(head); \
514 #define DWC_CIRCLEQ_INIT_ENTRY(elm, field) do { \
515 (elm)->field.cqe_next = NULL; \
516 (elm)->field.cqe_prev = NULL; \
519 #define DWC_CIRCLEQ_INSERT_AFTER(head, listelm, elm, field) do { \
520 (elm)->field.cqe_next = (listelm)->field.cqe_next; \
521 (elm)->field.cqe_prev = (listelm); \
522 if ((listelm)->field.cqe_next == DWC_CIRCLEQ_END(head)) \
523 (head)->cqh_last = (elm); \
525 (listelm)->field.cqe_next->field.cqe_prev = (elm); \
526 (listelm)->field.cqe_next = (elm); \
529 #define DWC_CIRCLEQ_INSERT_BEFORE(head, listelm, elm, field) do { \
530 (elm)->field.cqe_next = (listelm); \
531 (elm)->field.cqe_prev = (listelm)->field.cqe_prev; \
532 if ((listelm)->field.cqe_prev == DWC_CIRCLEQ_END(head)) \
533 (head)->cqh_first = (elm); \
535 (listelm)->field.cqe_prev->field.cqe_next = (elm); \
536 (listelm)->field.cqe_prev = (elm); \
539 #define DWC_CIRCLEQ_INSERT_HEAD(head, elm, field) do { \
540 (elm)->field.cqe_next = (head)->cqh_first; \
541 (elm)->field.cqe_prev = DWC_CIRCLEQ_END(head); \
542 if ((head)->cqh_last == DWC_CIRCLEQ_END(head)) \
543 (head)->cqh_last = (elm); \
545 (head)->cqh_first->field.cqe_prev = (elm); \
546 (head)->cqh_first = (elm); \
549 #define DWC_CIRCLEQ_INSERT_TAIL(head, elm, field) do { \
550 (elm)->field.cqe_next = DWC_CIRCLEQ_END(head); \
551 (elm)->field.cqe_prev = (head)->cqh_last; \
552 if ((head)->cqh_first == DWC_CIRCLEQ_END(head)) \
553 (head)->cqh_first = (elm); \
555 (head)->cqh_last->field.cqe_next = (elm); \
556 (head)->cqh_last = (elm); \
559 #define DWC_CIRCLEQ_REMOVE(head, elm, field) do { \
560 if ((elm)->field.cqe_next == DWC_CIRCLEQ_END(head)) \
561 (head)->cqh_last = (elm)->field.cqe_prev; \
563 (elm)->field.cqe_next->field.cqe_prev = \
564 (elm)->field.cqe_prev; \
565 if ((elm)->field.cqe_prev == DWC_CIRCLEQ_END(head)) \
566 (head)->cqh_first = (elm)->field.cqe_next; \
568 (elm)->field.cqe_prev->field.cqe_next = \
569 (elm)->field.cqe_next; \
572 #define DWC_CIRCLEQ_REMOVE_INIT(head, elm, field) do { \
573 DWC_CIRCLEQ_REMOVE(head, elm, field); \
574 DWC_CIRCLEQ_INIT_ENTRY(elm, field); \
577 #define DWC_CIRCLEQ_REPLACE(head, elm, elm2, field) do { \
578 if (((elm2)->field.cqe_next = (elm)->field.cqe_next) == \
579 DWC_CIRCLEQ_END(head)) \
580 (head).cqh_last = (elm2); \
582 (elm2)->field.cqe_next->field.cqe_prev = (elm2); \
583 if (((elm2)->field.cqe_prev = (elm)->field.cqe_prev) == \
584 DWC_CIRCLEQ_END(head)) \
585 (head).cqh_first = (elm2); \
587 (elm2)->field.cqe_prev->field.cqe_next = (elm2); \
594 #endif /* _DWC_LIST_H_ */