1 /* $OpenBSD: queue.h,v 1.16 2000/09/07 19:47:59 art 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. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * @(#)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 circular 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 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 * A circle queue is headed by a pair of pointers, one to the head of the
79 * list and the other to the tail of the list. The elements are doubly
80 * linked so that an arbitrary element can be removed without a need to
81 * traverse the list. New elements can be added to the list before or after
82 * an existing element, at the head of the list, or at the end of the list.
83 * A circle queue may be traversed in either direction, but has a more
84 * complex end of list detection.
86 * For details on the use of these macros, see the queue(3) manual page.
90 * Singly-linked List definitions.
92 #define SLIST_HEAD(name, type) \
94 struct type *slh_first; /* first element */ \
97 #define SLIST_HEAD_INITIALIZER(head) \
100 #define SLIST_ENTRY(type) \
102 struct type *sle_next; /* next element */ \
106 * Singly-linked List access methods.
108 #define SLIST_FIRST(head) ((head)->slh_first)
109 #define SLIST_END(head) NULL
110 #define SLIST_EMPTY(head) (SLIST_FIRST(head) == SLIST_END(head))
111 #define SLIST_NEXT(elm, field) ((elm)->field.sle_next)
113 #define SLIST_FOREACH(var, head, field) \
114 for((var) = SLIST_FIRST(head); \
115 (var) != SLIST_END(head); \
116 (var) = SLIST_NEXT(var, field))
119 * Singly-linked List functions.
121 #define SLIST_INIT(head) { \
122 SLIST_FIRST(head) = SLIST_END(head); \
125 #define SLIST_INSERT_AFTER(slistelm, elm, field) do { \
126 (elm)->field.sle_next = (slistelm)->field.sle_next; \
127 (slistelm)->field.sle_next = (elm); \
130 #define SLIST_INSERT_HEAD(head, elm, field) do { \
131 (elm)->field.sle_next = (head)->slh_first; \
132 (head)->slh_first = (elm); \
135 #define SLIST_REMOVE_HEAD(head, field) do { \
136 (head)->slh_first = (head)->slh_first->field.sle_next; \
142 #define LIST_HEAD(name, type) \
144 struct type *lh_first; /* first element */ \
147 #define LIST_HEAD_INITIALIZER(head) \
150 #define LIST_ENTRY(type) \
152 struct type *le_next; /* next element */ \
153 struct type **le_prev; /* address of previous next element */ \
157 * List access methods
159 #define LIST_FIRST(head) ((head)->lh_first)
160 #define LIST_END(head) NULL
161 #define LIST_EMPTY(head) (LIST_FIRST(head) == LIST_END(head))
162 #define LIST_NEXT(elm, field) ((elm)->field.le_next)
164 #define LIST_FOREACH(var, head, field) \
165 for((var) = LIST_FIRST(head); \
166 (var)!= LIST_END(head); \
167 (var) = LIST_NEXT(var, field))
172 #define LIST_INIT(head) do { \
173 LIST_FIRST(head) = LIST_END(head); \
176 #define LIST_INSERT_AFTER(listelm, elm, field) do { \
177 if (((elm)->field.le_next = (listelm)->field.le_next) != NULL) \
178 (listelm)->field.le_next->field.le_prev = \
179 &(elm)->field.le_next; \
180 (listelm)->field.le_next = (elm); \
181 (elm)->field.le_prev = &(listelm)->field.le_next; \
184 #define LIST_INSERT_BEFORE(listelm, elm, field) do { \
185 (elm)->field.le_prev = (listelm)->field.le_prev; \
186 (elm)->field.le_next = (listelm); \
187 *(listelm)->field.le_prev = (elm); \
188 (listelm)->field.le_prev = &(elm)->field.le_next; \
191 #define LIST_INSERT_HEAD(head, elm, field) do { \
192 if (((elm)->field.le_next = (head)->lh_first) != NULL) \
193 (head)->lh_first->field.le_prev = &(elm)->field.le_next;\
194 (head)->lh_first = (elm); \
195 (elm)->field.le_prev = &(head)->lh_first; \
198 #define LIST_REMOVE(elm, field) do { \
199 if ((elm)->field.le_next != NULL) \
200 (elm)->field.le_next->field.le_prev = \
201 (elm)->field.le_prev; \
202 *(elm)->field.le_prev = (elm)->field.le_next; \
205 #define LIST_REPLACE(elm, elm2, field) do { \
206 if (((elm2)->field.le_next = (elm)->field.le_next) != NULL) \
207 (elm2)->field.le_next->field.le_prev = \
208 &(elm2)->field.le_next; \
209 (elm2)->field.le_prev = (elm)->field.le_prev; \
210 *(elm2)->field.le_prev = (elm2); \
214 * Simple queue definitions.
216 #define SIMPLEQ_HEAD(name, type) \
218 struct type *sqh_first; /* first element */ \
219 struct type **sqh_last; /* addr of last next element */ \
222 #define SIMPLEQ_HEAD_INITIALIZER(head) \
223 { NULL, &(head).sqh_first }
225 #define SIMPLEQ_ENTRY(type) \
227 struct type *sqe_next; /* next element */ \
231 * Simple queue access methods.
233 #define SIMPLEQ_FIRST(head) ((head)->sqh_first)
234 #define SIMPLEQ_END(head) NULL
235 #define SIMPLEQ_EMPTY(head) (SIMPLEQ_FIRST(head) == SIMPLEQ_END(head))
236 #define SIMPLEQ_NEXT(elm, field) ((elm)->field.sqe_next)
238 #define SIMPLEQ_FOREACH(var, head, field) \
239 for((var) = SIMPLEQ_FIRST(head); \
240 (var) != SIMPLEQ_END(head); \
241 (var) = SIMPLEQ_NEXT(var, field))
244 * Simple queue functions.
246 #define SIMPLEQ_INIT(head) do { \
247 (head)->sqh_first = NULL; \
248 (head)->sqh_last = &(head)->sqh_first; \
251 #define SIMPLEQ_INSERT_HEAD(head, elm, field) do { \
252 if (((elm)->field.sqe_next = (head)->sqh_first) == NULL) \
253 (head)->sqh_last = &(elm)->field.sqe_next; \
254 (head)->sqh_first = (elm); \
257 #define SIMPLEQ_INSERT_TAIL(head, elm, field) do { \
258 (elm)->field.sqe_next = NULL; \
259 *(head)->sqh_last = (elm); \
260 (head)->sqh_last = &(elm)->field.sqe_next; \
263 #define SIMPLEQ_INSERT_AFTER(head, listelm, elm, field) do { \
264 if (((elm)->field.sqe_next = (listelm)->field.sqe_next) == NULL)\
265 (head)->sqh_last = &(elm)->field.sqe_next; \
266 (listelm)->field.sqe_next = (elm); \
269 #define SIMPLEQ_REMOVE_HEAD(head, elm, field) do { \
270 if (((head)->sqh_first = (elm)->field.sqe_next) == NULL) \
271 (head)->sqh_last = &(head)->sqh_first; \
275 * Tail queue definitions.
277 #define TAILQ_HEAD(name, type) \
279 struct type *tqh_first; /* first element */ \
280 struct type **tqh_last; /* addr of last next element */ \
283 #define TAILQ_HEAD_INITIALIZER(head) \
284 { NULL, &(head).tqh_first }
286 #define TAILQ_ENTRY(type) \
288 struct type *tqe_next; /* next element */ \
289 struct type **tqe_prev; /* address of previous next element */ \
293 * tail queue access methods
295 #define TAILQ_FIRST(head) ((head)->tqh_first)
296 #define TAILQ_END(head) NULL
297 #define TAILQ_NEXT(elm, field) ((elm)->field.tqe_next)
298 #define TAILQ_LAST(head, headname) \
299 (*(((struct headname *)((head)->tqh_last))->tqh_last))
301 #define TAILQ_PREV(elm, headname, field) \
302 (*(((struct headname *)((elm)->field.tqe_prev))->tqh_last))
303 #define TAILQ_EMPTY(head) \
304 (TAILQ_FIRST(head) == TAILQ_END(head))
306 #define TAILQ_FOREACH(var, head, field) \
307 for((var) = TAILQ_FIRST(head); \
308 (var) != TAILQ_END(head); \
309 (var) = TAILQ_NEXT(var, field))
311 #define TAILQ_FOREACH_REVERSE(var, head, field, headname) \
312 for((var) = TAILQ_LAST(head, headname); \
313 (var) != TAILQ_END(head); \
314 (var) = TAILQ_PREV(var, headname, field))
317 * Tail queue functions.
319 #define TAILQ_INIT(head) do { \
320 (head)->tqh_first = NULL; \
321 (head)->tqh_last = &(head)->tqh_first; \
324 #define TAILQ_INSERT_HEAD(head, elm, field) do { \
325 if (((elm)->field.tqe_next = (head)->tqh_first) != NULL) \
326 (head)->tqh_first->field.tqe_prev = \
327 &(elm)->field.tqe_next; \
329 (head)->tqh_last = &(elm)->field.tqe_next; \
330 (head)->tqh_first = (elm); \
331 (elm)->field.tqe_prev = &(head)->tqh_first; \
334 #define TAILQ_INSERT_TAIL(head, elm, field) do { \
335 (elm)->field.tqe_next = NULL; \
336 (elm)->field.tqe_prev = (head)->tqh_last; \
337 *(head)->tqh_last = (elm); \
338 (head)->tqh_last = &(elm)->field.tqe_next; \
341 #define TAILQ_INSERT_AFTER(head, listelm, elm, field) do { \
342 if (((elm)->field.tqe_next = (listelm)->field.tqe_next) != NULL)\
343 (elm)->field.tqe_next->field.tqe_prev = \
344 &(elm)->field.tqe_next; \
346 (head)->tqh_last = &(elm)->field.tqe_next; \
347 (listelm)->field.tqe_next = (elm); \
348 (elm)->field.tqe_prev = &(listelm)->field.tqe_next; \
351 #define TAILQ_INSERT_BEFORE(listelm, elm, field) do { \
352 (elm)->field.tqe_prev = (listelm)->field.tqe_prev; \
353 (elm)->field.tqe_next = (listelm); \
354 *(listelm)->field.tqe_prev = (elm); \
355 (listelm)->field.tqe_prev = &(elm)->field.tqe_next; \
358 #define TAILQ_REMOVE(head, elm, field) do { \
359 if (((elm)->field.tqe_next) != NULL) \
360 (elm)->field.tqe_next->field.tqe_prev = \
361 (elm)->field.tqe_prev; \
363 (head)->tqh_last = (elm)->field.tqe_prev; \
364 *(elm)->field.tqe_prev = (elm)->field.tqe_next; \
367 #define TAILQ_REPLACE(head, elm, elm2, field) do { \
368 if (((elm2)->field.tqe_next = (elm)->field.tqe_next) != NULL) \
369 (elm2)->field.tqe_next->field.tqe_prev = \
370 &(elm2)->field.tqe_next; \
372 (head)->tqh_last = &(elm2)->field.tqe_next; \
373 (elm2)->field.tqe_prev = (elm)->field.tqe_prev; \
374 *(elm2)->field.tqe_prev = (elm2); \
378 * Circular queue definitions.
380 #define CIRCLEQ_HEAD(name, type) \
382 struct type *cqh_first; /* first element */ \
383 struct type *cqh_last; /* last element */ \
386 #define CIRCLEQ_HEAD_INITIALIZER(head) \
387 { CIRCLEQ_END(&head), CIRCLEQ_END(&head) }
389 #define CIRCLEQ_ENTRY(type) \
391 struct type *cqe_next; /* next element */ \
392 struct type *cqe_prev; /* previous element */ \
396 * Circular queue access methods
398 #define CIRCLEQ_FIRST(head) ((head)->cqh_first)
399 #define CIRCLEQ_LAST(head) ((head)->cqh_last)
400 #define CIRCLEQ_END(head) ((void *)(head))
401 #define CIRCLEQ_NEXT(elm, field) ((elm)->field.cqe_next)
402 #define CIRCLEQ_PREV(elm, field) ((elm)->field.cqe_prev)
403 #define CIRCLEQ_EMPTY(head) \
404 (CIRCLEQ_FIRST(head) == CIRCLEQ_END(head))
406 #define CIRCLEQ_FOREACH(var, head, field) \
407 for((var) = CIRCLEQ_FIRST(head); \
408 (var) != CIRCLEQ_END(head); \
409 (var) = CIRCLEQ_NEXT(var, field))
411 #define CIRCLEQ_FOREACH_REVERSE(var, head, field) \
412 for((var) = CIRCLEQ_LAST(head); \
413 (var) != CIRCLEQ_END(head); \
414 (var) = CIRCLEQ_PREV(var, field))
417 * Circular queue functions.
419 #define CIRCLEQ_INIT(head) do { \
420 (head)->cqh_first = CIRCLEQ_END(head); \
421 (head)->cqh_last = CIRCLEQ_END(head); \
424 #define CIRCLEQ_INSERT_AFTER(head, listelm, elm, field) do { \
425 (elm)->field.cqe_next = (listelm)->field.cqe_next; \
426 (elm)->field.cqe_prev = (listelm); \
427 if ((listelm)->field.cqe_next == CIRCLEQ_END(head)) \
428 (head)->cqh_last = (elm); \
430 (listelm)->field.cqe_next->field.cqe_prev = (elm); \
431 (listelm)->field.cqe_next = (elm); \
434 #define CIRCLEQ_INSERT_BEFORE(head, listelm, elm, field) do { \
435 (elm)->field.cqe_next = (listelm); \
436 (elm)->field.cqe_prev = (listelm)->field.cqe_prev; \
437 if ((listelm)->field.cqe_prev == CIRCLEQ_END(head)) \
438 (head)->cqh_first = (elm); \
440 (listelm)->field.cqe_prev->field.cqe_next = (elm); \
441 (listelm)->field.cqe_prev = (elm); \
444 #define CIRCLEQ_INSERT_HEAD(head, elm, field) do { \
445 (elm)->field.cqe_next = (head)->cqh_first; \
446 (elm)->field.cqe_prev = CIRCLEQ_END(head); \
447 if ((head)->cqh_last == CIRCLEQ_END(head)) \
448 (head)->cqh_last = (elm); \
450 (head)->cqh_first->field.cqe_prev = (elm); \
451 (head)->cqh_first = (elm); \
454 #define CIRCLEQ_INSERT_TAIL(head, elm, field) do { \
455 (elm)->field.cqe_next = CIRCLEQ_END(head); \
456 (elm)->field.cqe_prev = (head)->cqh_last; \
457 if ((head)->cqh_first == CIRCLEQ_END(head)) \
458 (head)->cqh_first = (elm); \
460 (head)->cqh_last->field.cqe_next = (elm); \
461 (head)->cqh_last = (elm); \
464 #define CIRCLEQ_REMOVE(head, elm, field) do { \
465 if ((elm)->field.cqe_next == CIRCLEQ_END(head)) \
466 (head)->cqh_last = (elm)->field.cqe_prev; \
468 (elm)->field.cqe_next->field.cqe_prev = \
469 (elm)->field.cqe_prev; \
470 if ((elm)->field.cqe_prev == CIRCLEQ_END(head)) \
471 (head)->cqh_first = (elm)->field.cqe_next; \
473 (elm)->field.cqe_prev->field.cqe_next = \
474 (elm)->field.cqe_next; \
477 #define CIRCLEQ_REPLACE(head, elm, elm2, field) do { \
478 if (((elm2)->field.cqe_next = (elm)->field.cqe_next) == \
480 (head).cqh_last = (elm2); \
482 (elm2)->field.cqe_next->field.cqe_prev = (elm2); \
483 if (((elm2)->field.cqe_prev = (elm)->field.cqe_prev) == \
485 (head).cqh_first = (elm2); \
487 (elm2)->field.cqe_prev->field.cqe_next = (elm2); \
490 #endif /* !_SYS_QUEUE_H_ */