1 /* Amanda $Id: queue.h,v 1.2 1999/05/24 16:10:35 kashmir Exp $ */
2 /* from: $NetBSD: queue.h,v 1.16 1998/07/10 23:28:31 nathanw 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 four types of data structures: lists, simple queues,
44 * tail queues, and circular queues.
46 * A list is headed by a single forward pointer (or an array of forward
47 * pointers for a hash table header). The elements are doubly linked
48 * so that an arbitrary element can be removed without a need to
49 * traverse the list. New elements can be added to the list before
50 * or after an existing element or at the head of the list. A list
51 * may only be traversed in the forward direction.
53 * A simple queue is headed by a pair of pointers, one the head of the
54 * list and the other to the tail of the list. The elements are singly
55 * linked to save space, so only elements can only be removed from the
56 * head of the list. New elements can be added to the list before or after
57 * an existing element, at the head of the list, or at the end of the
58 * list. A simple queue may only be traversed in the forward direction.
60 * A tail queue is headed by a pair of pointers, one to the head of the
61 * list and the other to the tail of the list. The elements are doubly
62 * linked so that an arbitrary element can be removed without a need to
63 * traverse the list. New elements can be added to the list before or
64 * after an existing element, at the head of the list, or at the end of
65 * the list. A tail queue may only be traversed in the forward direction.
67 * A circle queue is headed by a pair of pointers, one to the head of the
68 * list and the other to the tail of the list. The elements are doubly
69 * linked so that an arbitrary element can be removed without a need to
70 * traverse the list. New elements can be added to the list before or after
71 * an existing element, at the head of the list, or at the end of the list.
72 * A circle queue may be traversed in either direction, but has a more
73 * complex end of list detection.
75 * For details on the use of these macros, see the QUEUE-API document
76 * in the docs directory.
78 * Note that the #undef's in this file exist so we override any
79 * macros declared in <sys/queue.h>.
86 #define LIST_HEAD(name, type) \
88 struct type *lh_first; /* first element */ \
91 #undef LIST_HEAD_INITIALIZER
92 #define LIST_HEAD_INITIALIZER(head) \
96 #define LIST_ENTRY(type) \
98 struct type *le_next; /* next element */ \
99 struct type **le_prev; /* address of previous next element */ \
106 #define LIST_INIT(head) do { \
107 (head)->lh_first = NULL; \
110 #undef LIST_INSERT_AFTER
111 #define LIST_INSERT_AFTER(listelm, elm, field) do { \
112 if (((elm)->field.le_next = (listelm)->field.le_next) != NULL) \
113 (listelm)->field.le_next->field.le_prev = \
114 &(elm)->field.le_next; \
115 (listelm)->field.le_next = (elm); \
116 (elm)->field.le_prev = &(listelm)->field.le_next; \
119 #undef LIST_INSERT_BEFORE
120 #define LIST_INSERT_BEFORE(listelm, elm, field) do { \
121 (elm)->field.le_prev = (listelm)->field.le_prev; \
122 (elm)->field.le_next = (listelm); \
123 *(listelm)->field.le_prev = (elm); \
124 (listelm)->field.le_prev = &(elm)->field.le_next; \
127 #undef LIST_INSERT_HEAD
128 #define LIST_INSERT_HEAD(head, elm, field) do { \
129 if (((elm)->field.le_next = (head)->lh_first) != NULL) \
130 (head)->lh_first->field.le_prev = &(elm)->field.le_next;\
131 (head)->lh_first = (elm); \
132 (elm)->field.le_prev = &(head)->lh_first; \
136 #define LIST_REMOVE(elm, field) do { \
137 if ((elm)->field.le_next != NULL) \
138 (elm)->field.le_next->field.le_prev = \
139 (elm)->field.le_prev; \
140 *(elm)->field.le_prev = (elm)->field.le_next; \
144 * List access methods.
147 #define LIST_FIRST(head) ((head)->lh_first)
150 #define LIST_NEXT(elm, field) ((elm)->field.le_next)
153 * Simple queue definitions.
156 #define SIMPLEQ_HEAD(name, type) \
158 struct type *sqh_first; /* first element */ \
159 struct type **sqh_last; /* addr of last next element */ \
162 #undef SIMPLEQ_HEAD_INITIALIZER
163 #define SIMPLEQ_HEAD_INITIALIZER(head) \
164 { NULL, &(head).sqh_first }
167 #define SIMPLEQ_ENTRY(type) \
169 struct type *sqe_next; /* next element */ \
173 * Simple queue functions.
176 #define SIMPLEQ_INIT(head) do { \
177 (head)->sqh_first = NULL; \
178 (head)->sqh_last = &(head)->sqh_first; \
181 #undef SIMPLEQ_INSERT_HEAD
182 #define SIMPLEQ_INSERT_HEAD(head, elm, field) do { \
183 if (((elm)->field.sqe_next = (head)->sqh_first) == NULL) \
184 (head)->sqh_last = &(elm)->field.sqe_next; \
185 (head)->sqh_first = (elm); \
188 #undef SIMPLEQ_INSERT_TAIL
189 #define SIMPLEQ_INSERT_TAIL(head, elm, field) do { \
190 (elm)->field.sqe_next = NULL; \
191 *(head)->sqh_last = (elm); \
192 (head)->sqh_last = &(elm)->field.sqe_next; \
195 #undef SIMPLEQ_INSERT_AFTER
196 #define SIMPLEQ_INSERT_AFTER(head, listelm, elm, field) do { \
197 if (((elm)->field.sqe_next = (listelm)->field.sqe_next) == NULL)\
198 (head)->sqh_last = &(elm)->field.sqe_next; \
199 (listelm)->field.sqe_next = (elm); \
202 #undef SIMPLEQ_REMOVE_HEAD
203 #define SIMPLEQ_REMOVE_HEAD(head, elm, field) do { \
204 if (((head)->sqh_first = (elm)->field.sqe_next) == NULL) \
205 (head)->sqh_last = &(head)->sqh_first; \
209 * Simple queue access methods.
212 #define SIMPLEQ_FIRST(head) ((head)->sqh_first)
215 #define SIMPLEQ_NEXT(elm, field) ((elm)->field.sqe_next)
218 * Tail queue definitions.
221 #define TAILQ_HEAD(name, type) \
223 struct type *tqh_first; /* first element */ \
224 struct type **tqh_last; /* addr of last next element */ \
227 #undef TAILQ_HEAD_INITIALIZER
228 #define TAILQ_HEAD_INITIALIZER(head) \
229 { NULL, &(head).tqh_first }
232 #define TAILQ_ENTRY(type) \
234 struct type *tqe_next; /* next element */ \
235 struct type **tqe_prev; /* address of previous next element */ \
239 * Tail queue functions.
242 #define TAILQ_INIT(head) do { \
243 (head)->tqh_first = NULL; \
244 (head)->tqh_last = &(head)->tqh_first; \
247 #undef TAILQ_INSERT_HEAD
248 #define TAILQ_INSERT_HEAD(head, elm, field) do { \
249 if (((elm)->field.tqe_next = (head)->tqh_first) != NULL) \
250 (head)->tqh_first->field.tqe_prev = \
251 &(elm)->field.tqe_next; \
253 (head)->tqh_last = &(elm)->field.tqe_next; \
254 (head)->tqh_first = (elm); \
255 (elm)->field.tqe_prev = &(head)->tqh_first; \
258 #undef TAILQ_INSERT_TAIL
259 #define TAILQ_INSERT_TAIL(head, elm, field) do { \
260 (elm)->field.tqe_next = NULL; \
261 (elm)->field.tqe_prev = (head)->tqh_last; \
262 *(head)->tqh_last = (elm); \
263 (head)->tqh_last = &(elm)->field.tqe_next; \
266 #undef TAILQ_INSERT_AFTER
267 #define TAILQ_INSERT_AFTER(head, listelm, elm, field) do { \
268 if (((elm)->field.tqe_next = (listelm)->field.tqe_next) != NULL)\
269 (elm)->field.tqe_next->field.tqe_prev = \
270 &(elm)->field.tqe_next; \
272 (head)->tqh_last = &(elm)->field.tqe_next; \
273 (listelm)->field.tqe_next = (elm); \
274 (elm)->field.tqe_prev = &(listelm)->field.tqe_next; \
277 #undef TAILQ_INSERT_BEFORE
278 #define TAILQ_INSERT_BEFORE(listelm, elm, field) do { \
279 (elm)->field.tqe_prev = (listelm)->field.tqe_prev; \
280 (elm)->field.tqe_next = (listelm); \
281 *(listelm)->field.tqe_prev = (elm); \
282 (listelm)->field.tqe_prev = &(elm)->field.tqe_next; \
286 #define TAILQ_REMOVE(head, elm, field) do { \
287 if (((elm)->field.tqe_next) != NULL) \
288 (elm)->field.tqe_next->field.tqe_prev = \
289 (elm)->field.tqe_prev; \
291 (head)->tqh_last = (elm)->field.tqe_prev; \
292 *(elm)->field.tqe_prev = (elm)->field.tqe_next; \
296 * Tail queue access methods.
299 #define TAILQ_FIRST(head) ((head)->tqh_first)
302 #define TAILQ_NEXT(elm, field) ((elm)->field.tqe_next)
305 * Circular queue definitions.
308 #define CIRCLEQ_HEAD(name, type) \
310 struct type *cqh_first; /* first element */ \
311 struct type *cqh_last; /* last element */ \
314 #undef CIRCLEQ_HEAD_INITIALIZER
315 #define CIRCLEQ_HEAD_INITIALIZER(head) \
316 { (void *)&head, (void *)&head }
319 #define CIRCLEQ_ENTRY(type) \
321 struct type *cqe_next; /* next element */ \
322 struct type *cqe_prev; /* previous element */ \
326 * Circular queue functions.
329 #define CIRCLEQ_INIT(head) do { \
330 (head)->cqh_first = (void *)(head); \
331 (head)->cqh_last = (void *)(head); \
334 #undef CIRCLEQ_INSERT_AFTER
335 #define CIRCLEQ_INSERT_AFTER(head, listelm, elm, field) do { \
336 (elm)->field.cqe_next = (listelm)->field.cqe_next; \
337 (elm)->field.cqe_prev = (listelm); \
338 if ((listelm)->field.cqe_next == (void *)(head)) \
339 (head)->cqh_last = (elm); \
341 (listelm)->field.cqe_next->field.cqe_prev = (elm); \
342 (listelm)->field.cqe_next = (elm); \
345 #undef CIRCLEQ_INSERT_BEFORE
346 #define CIRCLEQ_INSERT_BEFORE(head, listelm, elm, field) do { \
347 (elm)->field.cqe_next = (listelm); \
348 (elm)->field.cqe_prev = (listelm)->field.cqe_prev; \
349 if ((listelm)->field.cqe_prev == (void *)(head)) \
350 (head)->cqh_first = (elm); \
352 (listelm)->field.cqe_prev->field.cqe_next = (elm); \
353 (listelm)->field.cqe_prev = (elm); \
356 #undef CIRCLEQ_INSERT_HEAD
357 #define CIRCLEQ_INSERT_HEAD(head, elm, field) do { \
358 (elm)->field.cqe_next = (head)->cqh_first; \
359 (elm)->field.cqe_prev = (void *)(head); \
360 if ((head)->cqh_last == (void *)(head)) \
361 (head)->cqh_last = (elm); \
363 (head)->cqh_first->field.cqe_prev = (elm); \
364 (head)->cqh_first = (elm); \
367 #undef CIRCLEQ_INSERT_TAIL
368 #define CIRCLEQ_INSERT_TAIL(head, elm, field) do { \
369 (elm)->field.cqe_next = (void *)(head); \
370 (elm)->field.cqe_prev = (head)->cqh_last; \
371 if ((head)->cqh_first == (void *)(head)) \
372 (head)->cqh_first = (elm); \
374 (head)->cqh_last->field.cqe_next = (elm); \
375 (head)->cqh_last = (elm); \
378 #undef CIRCLEQ_REMOVE
379 #define CIRCLEQ_REMOVE(head, elm, field) do { \
380 if ((elm)->field.cqe_next == (void *)(head)) \
381 (head)->cqh_last = (elm)->field.cqe_prev; \
383 (elm)->field.cqe_next->field.cqe_prev = \
384 (elm)->field.cqe_prev; \
385 if ((elm)->field.cqe_prev == (void *)(head)) \
386 (head)->cqh_first = (elm)->field.cqe_next; \
388 (elm)->field.cqe_prev->field.cqe_next = \
389 (elm)->field.cqe_next; \
393 * Circular queue access methods.
396 #define CIRCLEQ_FIRST(head) ((head)->cqh_first)
399 #define CIRCLEQ_LAST(head) ((head)->cqh_last)
402 #define CIRCLEQ_NEXT(elm, field) ((elm)->field.cqe_next)
405 #define CIRCLEQ_PREV(elm, field) ((elm)->field.cqe_prev)
406 #endif /* !QUEUE_H */