2 * <sys/queue.h> implementation for systems that don't have it.
4 * Copyright (c) 1991, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * @(#)queue.h 8.5 (Berkeley) 8/20/94
32 * $FreeBSD: src/sys/sys/queue.h,v 1.32.2.4 2001/03/31 03:33:39 hsu Exp $
39 * This file defines four types of data structures: singly-linked lists,
40 * singly-linked tail queues, lists and tail queues.
42 * A singly-linked list is headed by a single forward pointer. The elements
43 * are singly linked for minimum space and pointer manipulation overhead at
44 * the expense of O(n) removal for arbitrary elements. New elements can be
45 * added to the list after an existing element or at the head of the list.
46 * Elements being removed from the head of the list should use the explicit
47 * macro for this purpose for optimum efficiency. A singly-linked list may
48 * only be traversed in the forward direction. Singly-linked lists are ideal
49 * for applications with large datasets and few or no removals or for
50 * implementing a LIFO queue.
52 * A singly-linked tail queue is headed by a pair of pointers, one to the
53 * head of the list and the other to the tail of the list. The elements are
54 * singly linked for minimum space and pointer manipulation overhead at the
55 * expense of O(n) removal for arbitrary elements. New elements can be added
56 * to the list after an existing element, at the head of the list, or at the
57 * end of the list. Elements being removed from the head of the tail queue
58 * should use the explicit macro for this purpose for optimum efficiency.
59 * A singly-linked tail queue may only be traversed in the forward direction.
60 * Singly-linked tail queues are ideal for applications with large datasets
61 * and few or no removals or for implementing a FIFO queue.
63 * A list is headed by a single forward pointer (or an array of forward
64 * pointers for a hash table header). The elements are doubly linked
65 * so that an arbitrary element can be removed without a need to
66 * traverse the list. New elements can be added to the list before
67 * or after an existing element or at the head of the list. A list
68 * may only be traversed in the forward direction.
70 * A tail queue is headed by a pair of pointers, one to the head of the
71 * list and the other to the tail of the list. The elements are doubly
72 * linked so that an arbitrary element can be removed without a need to
73 * traverse the list. New elements can be added to the list before or
74 * after an existing element, at the head of the list, or at the end of
75 * the list. A tail queue may be traversed in either direction.
77 * For details on the use of these macros, see the queue(3) manual page.
80 * SLIST LIST STAILQ TAILQ
82 * _HEAD_INITIALIZER + + + +
91 * _FOREACH_SAFE + + + +
92 * _FOREACH_REVERSE - - - +
93 * _FOREACH_REVERSE_SAFE - - - +
94 * _INSERT_HEAD + + + +
95 * _INSERT_BEFORE - + - +
96 * _INSERT_AFTER + + + +
97 * _INSERT_TAIL - - + +
99 * _REMOVE_HEAD + - + -
105 * Singly-linked List declarations.
107 #define SLIST_HEAD(name, type) \
109 struct type *slh_first; /* first element */ \
112 #define SLIST_HEAD_INITIALIZER(head) \
115 #define SLIST_ENTRY(type) \
117 struct type *sle_next; /* next element */ \
121 * Singly-linked List functions.
123 #define SLIST_EMPTY(head) ((head)->slh_first == NULL)
125 #define SLIST_FIRST(head) ((head)->slh_first)
127 #define SLIST_FOREACH(var, head, field) \
128 for ((var) = SLIST_FIRST((head)); \
130 (var) = SLIST_NEXT((var), field))
132 #define SLIST_FOREACH_SAFE(var, head, field, tvar) \
133 for ((var) = SLIST_FIRST((head)); \
134 (var) && ((tvar) = SLIST_NEXT((var), field), 1); \
137 #define SLIST_FOREACH_PREVPTR(var, varp, head, field) \
138 for ((varp) = &SLIST_FIRST((head)); \
139 ((var) = *(varp)) != NULL; \
140 (varp) = &SLIST_NEXT((var), field))
142 #define SLIST_INIT(head) do { \
143 SLIST_FIRST((head)) = NULL; \
146 #define SLIST_INSERT_AFTER(slistelm, elm, field) do { \
147 SLIST_NEXT((elm), field) = SLIST_NEXT((slistelm), field); \
148 SLIST_NEXT((slistelm), field) = (elm); \
151 #define SLIST_INSERT_HEAD(head, elm, field) do { \
152 SLIST_NEXT((elm), field) = SLIST_FIRST((head)); \
153 SLIST_FIRST((head)) = (elm); \
156 #define SLIST_NEXT(elm, field) ((elm)->field.sle_next)
158 #define SLIST_REMOVE(head, elm, type, field) do { \
159 if (SLIST_FIRST((head)) == (elm)) { \
160 SLIST_REMOVE_HEAD((head), field); \
163 struct type *curelm = SLIST_FIRST((head)); \
164 while (SLIST_NEXT(curelm, field) != (elm)) \
165 curelm = SLIST_NEXT(curelm, field); \
166 SLIST_NEXT(curelm, field) = \
167 SLIST_NEXT(SLIST_NEXT(curelm, field), field); \
171 #define SLIST_REMOVE_HEAD(head, field) do { \
172 SLIST_FIRST((head)) = SLIST_NEXT(SLIST_FIRST((head)), field); \
176 * Singly-linked Tail queue declarations.
178 #define STAILQ_HEAD(name, type) \
180 struct type *stqh_first;/* first element */ \
181 struct type **stqh_last;/* addr of last next element */ \
184 #define STAILQ_HEAD_INITIALIZER(head) \
185 { NULL, &(head).stqh_first }
187 #define STAILQ_ENTRY(type) \
189 struct type *stqe_next; /* next element */ \
193 * Singly-linked Tail queue functions.
195 #define STAILQ_CONCAT(head1, head2) do { \
196 if (!STAILQ_EMPTY((head2))) { \
197 *(head1)->stqh_last = (head2)->stqh_first; \
198 (head1)->stqh_last = (head2)->stqh_last; \
199 STAILQ_INIT((head2)); \
203 #define STAILQ_EMPTY(head) ((head)->stqh_first == NULL)
205 #define STAILQ_FIRST(head) ((head)->stqh_first)
207 #define STAILQ_FOREACH(var, head, field) \
208 for((var) = STAILQ_FIRST((head)); \
210 (var) = STAILQ_NEXT((var), field))
213 #define STAILQ_FOREACH_SAFE(var, head, field, tvar) \
214 for ((var) = STAILQ_FIRST((head)); \
215 (var) && ((tvar) = STAILQ_NEXT((var), field), 1); \
218 #define STAILQ_INIT(head) do { \
219 STAILQ_FIRST((head)) = NULL; \
220 (head)->stqh_last = &STAILQ_FIRST((head)); \
223 #define STAILQ_INSERT_AFTER(head, tqelm, elm, field) do { \
224 if ((STAILQ_NEXT((elm), field) = STAILQ_NEXT((tqelm), field)) == NULL)\
225 (head)->stqh_last = &STAILQ_NEXT((elm), field); \
226 STAILQ_NEXT((tqelm), field) = (elm); \
229 #define STAILQ_INSERT_HEAD(head, elm, field) do { \
230 if ((STAILQ_NEXT((elm), field) = STAILQ_FIRST((head))) == NULL) \
231 (head)->stqh_last = &STAILQ_NEXT((elm), field); \
232 STAILQ_FIRST((head)) = (elm); \
235 #define STAILQ_INSERT_TAIL(head, elm, field) do { \
236 STAILQ_NEXT((elm), field) = NULL; \
237 *(head)->stqh_last = (elm); \
238 (head)->stqh_last = &STAILQ_NEXT((elm), field); \
241 #define STAILQ_LAST(head, type, field) \
242 (STAILQ_EMPTY((head)) ? \
245 ((char *)((head)->stqh_last) - offsetof(struct type, field))))
247 #define STAILQ_NEXT(elm, field) ((elm)->field.stqe_next)
249 #define STAILQ_REMOVE(head, elm, type, field) do { \
250 if (STAILQ_FIRST((head)) == (elm)) { \
251 STAILQ_REMOVE_HEAD((head), field); \
254 struct type *curelm = STAILQ_FIRST((head)); \
255 while (STAILQ_NEXT(curelm, field) != (elm)) \
256 curelm = STAILQ_NEXT(curelm, field); \
257 if ((STAILQ_NEXT(curelm, field) = \
258 STAILQ_NEXT(STAILQ_NEXT(curelm, field), field)) == NULL)\
259 (head)->stqh_last = &STAILQ_NEXT((curelm), field);\
263 #define STAILQ_REMOVE_HEAD(head, field) do { \
264 if ((STAILQ_FIRST((head)) = \
265 STAILQ_NEXT(STAILQ_FIRST((head)), field)) == NULL) \
266 (head)->stqh_last = &STAILQ_FIRST((head)); \
269 #define STAILQ_REMOVE_HEAD_UNTIL(head, elm, field) do { \
270 if ((STAILQ_FIRST((head)) = STAILQ_NEXT((elm), field)) == NULL) \
271 (head)->stqh_last = &STAILQ_FIRST((head)); \
277 #define LIST_HEAD(name, type) \
279 struct type *lh_first; /* first element */ \
282 #define LIST_HEAD_INITIALIZER(head) \
285 #define LIST_ENTRY(type) \
287 struct type *le_next; /* next element */ \
288 struct type **le_prev; /* address of previous next element */ \
295 #define LIST_EMPTY(head) ((head)->lh_first == NULL)
297 #define LIST_FIRST(head) ((head)->lh_first)
299 #define LIST_FOREACH(var, head, field) \
300 for ((var) = LIST_FIRST((head)); \
302 (var) = LIST_NEXT((var), field))
304 #define LIST_FOREACH_SAFE(var, head, field, tvar) \
305 for ((var) = LIST_FIRST((head)); \
306 (var) && ((tvar) = LIST_NEXT((var), field), 1); \
309 #define LIST_INIT(head) do { \
310 LIST_FIRST((head)) = NULL; \
313 #define LIST_INSERT_AFTER(listelm, elm, field) do { \
314 if ((LIST_NEXT((elm), field) = LIST_NEXT((listelm), field)) != NULL)\
315 LIST_NEXT((listelm), field)->field.le_prev = \
316 &LIST_NEXT((elm), field); \
317 LIST_NEXT((listelm), field) = (elm); \
318 (elm)->field.le_prev = &LIST_NEXT((listelm), field); \
321 #define LIST_INSERT_BEFORE(listelm, elm, field) do { \
322 (elm)->field.le_prev = (listelm)->field.le_prev; \
323 LIST_NEXT((elm), field) = (listelm); \
324 *(listelm)->field.le_prev = (elm); \
325 (listelm)->field.le_prev = &LIST_NEXT((elm), field); \
328 #define LIST_INSERT_HEAD(head, elm, field) do { \
329 if ((LIST_NEXT((elm), field) = LIST_FIRST((head))) != NULL) \
330 LIST_FIRST((head))->field.le_prev = &LIST_NEXT((elm), field);\
331 LIST_FIRST((head)) = (elm); \
332 (elm)->field.le_prev = &LIST_FIRST((head)); \
335 #define LIST_NEXT(elm, field) ((elm)->field.le_next)
337 #define LIST_REMOVE(elm, field) do { \
338 if (LIST_NEXT((elm), field) != NULL) \
339 LIST_NEXT((elm), field)->field.le_prev = \
340 (elm)->field.le_prev; \
341 *(elm)->field.le_prev = LIST_NEXT((elm), field); \
345 * Tail queue declarations.
347 #define TAILQ_HEAD(name, type) \
349 struct type *tqh_first; /* first element */ \
350 struct type **tqh_last; /* addr of last next element */ \
353 #define TAILQ_HEAD_INITIALIZER(head) \
354 { NULL, &(head).tqh_first }
356 #define TAILQ_ENTRY(type) \
358 struct type *tqe_next; /* next element */ \
359 struct type **tqe_prev; /* address of previous next element */ \
363 * Tail queue functions.
365 #define TAILQ_CONCAT(head1, head2, field) do { \
366 if (!TAILQ_EMPTY(head2)) { \
367 *(head1)->tqh_last = (head2)->tqh_first; \
368 (head2)->tqh_first->field.tqe_prev = (head1)->tqh_last; \
369 (head1)->tqh_last = (head2)->tqh_last; \
370 TAILQ_INIT((head2)); \
374 #define TAILQ_EMPTY(head) ((head)->tqh_first == NULL)
376 #define TAILQ_FIRST(head) ((head)->tqh_first)
378 #define TAILQ_FOREACH(var, head, field) \
379 for ((var) = TAILQ_FIRST((head)); \
381 (var) = TAILQ_NEXT((var), field))
383 #define TAILQ_FOREACH_SAFE(var, head, field, tvar) \
384 for ((var) = TAILQ_FIRST((head)); \
385 (var) && ((tvar) = TAILQ_NEXT((var), field), 1); \
388 #define TAILQ_FOREACH_REVERSE(var, head, headname, field) \
389 for ((var) = TAILQ_LAST((head), headname); \
391 (var) = TAILQ_PREV((var), headname, field))
393 #define TAILQ_FOREACH_REVERSE_SAFE(var, head, headname, field, tvar) \
394 for ((var) = TAILQ_LAST((head), headname); \
395 (var) && ((tvar) = TAILQ_PREV((var), headname, field), 1); \
398 #define TAILQ_INIT(head) do { \
399 TAILQ_FIRST((head)) = NULL; \
400 (head)->tqh_last = &TAILQ_FIRST((head)); \
403 #define TAILQ_INSERT_AFTER(head, listelm, elm, field) do { \
404 if ((TAILQ_NEXT((elm), field) = TAILQ_NEXT((listelm), field)) != NULL)\
405 TAILQ_NEXT((elm), field)->field.tqe_prev = \
406 &TAILQ_NEXT((elm), field); \
408 (head)->tqh_last = &TAILQ_NEXT((elm), field); \
410 TAILQ_NEXT((listelm), field) = (elm); \
411 (elm)->field.tqe_prev = &TAILQ_NEXT((listelm), field); \
414 #define TAILQ_INSERT_BEFORE(listelm, elm, field) do { \
415 (elm)->field.tqe_prev = (listelm)->field.tqe_prev; \
416 TAILQ_NEXT((elm), field) = (listelm); \
417 *(listelm)->field.tqe_prev = (elm); \
418 (listelm)->field.tqe_prev = &TAILQ_NEXT((elm), field); \
421 #define TAILQ_INSERT_HEAD(head, elm, field) do { \
422 if ((TAILQ_NEXT((elm), field) = TAILQ_FIRST((head))) != NULL) \
423 TAILQ_FIRST((head))->field.tqe_prev = \
424 &TAILQ_NEXT((elm), field); \
426 (head)->tqh_last = &TAILQ_NEXT((elm), field); \
427 TAILQ_FIRST((head)) = (elm); \
428 (elm)->field.tqe_prev = &TAILQ_FIRST((head)); \
431 #define TAILQ_INSERT_TAIL(head, elm, field) do { \
432 TAILQ_NEXT((elm), field) = NULL; \
433 (elm)->field.tqe_prev = (head)->tqh_last; \
434 *(head)->tqh_last = (elm); \
435 (head)->tqh_last = &TAILQ_NEXT((elm), field); \
438 #define TAILQ_LAST(head, headname) \
439 (*(((struct headname *)((head)->tqh_last))->tqh_last))
441 #define TAILQ_NEXT(elm, field) ((elm)->field.tqe_next)
443 #define TAILQ_PREV(elm, headname, field) \
444 (*(((struct headname *)((elm)->field.tqe_prev))->tqh_last))
446 #define TAILQ_REMOVE(head, elm, field) do { \
447 if ((TAILQ_NEXT((elm), field)) != NULL) \
448 TAILQ_NEXT((elm), field)->field.tqe_prev = \
449 (elm)->field.tqe_prev; \
451 (head)->tqh_last = (elm)->field.tqe_prev; \
453 *(elm)->field.tqe_prev = TAILQ_NEXT((elm), field); \
456 #endif /* !SYS_QUEUE_H */