2 * Copyright (c) 1991, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#)queue.h 8.5 (Berkeley) 8/20/94
39 #define __offsetof(type, field) ((size_t)(&((type *)0)->field))
42 * Singly-linked Tail queue declarations.
45 #define STAILQ_HEAD(name, type) \
47 struct type *stqh_first;/* first element */ \
48 struct type **stqh_last;/* addr of last next element */ \
51 #undef STAILQ_HEAD_INITIALIZER
52 #define STAILQ_HEAD_INITIALIZER(head) \
53 { NULL, &(head).stqh_first }
56 #define STAILQ_ENTRY(type) \
58 struct type *stqe_next; /* next element */ \
62 #define STAILQ_EMPTY(head) ((head)->stqh_first == NULL)
65 #define STAILQ_FIRST(head) ((head)->stqh_first)
68 #define STAILQ_FOREACH(var, head, field) \
69 for((var) = STAILQ_FIRST((head)); \
71 (var) = STAILQ_NEXT((var), field))
73 #undef STAILQ_FOREACH_SAFE
74 #define STAILQ_FOREACH_SAFE(var, head, field, tvar) \
75 for ((var) = STAILQ_FIRST((head)); \
76 (var) && ((tvar) = STAILQ_NEXT((var), field), 1); \
80 #define STAILQ_INIT(head) do { \
81 STAILQ_FIRST((head)) = NULL; \
82 (head)->stqh_last = &STAILQ_FIRST((head)); \
85 #undef STAILQ_INSERT_AFTER
86 #define STAILQ_INSERT_AFTER(head, tqelm, elm, field) do { \
87 if ((STAILQ_NEXT((elm), field) = STAILQ_NEXT((tqelm), field)) == NULL)\
88 (head)->stqh_last = &STAILQ_NEXT((elm), field); \
89 STAILQ_NEXT((tqelm), field) = (elm); \
92 #undef STAILQ_INSERT_HEAD
93 #define STAILQ_INSERT_HEAD(head, elm, field) do { \
94 if ((STAILQ_NEXT((elm), field) = STAILQ_FIRST((head))) == NULL) \
95 (head)->stqh_last = &STAILQ_NEXT((elm), field); \
96 STAILQ_FIRST((head)) = (elm); \
99 #undef STAILQ_INSERT_TAIL
100 #define STAILQ_INSERT_TAIL(head, elm, field) do { \
101 STAILQ_NEXT((elm), field) = NULL; \
102 *(head)->stqh_last = (elm); \
103 (head)->stqh_last = &STAILQ_NEXT((elm), field); \
107 #define STAILQ_LAST(head, type, field) \
108 (STAILQ_EMPTY((head)) ? \
110 ((struct type *)(void *) \
111 ((char *)((head)->stqh_last) - __offsetof(struct type, field))))
114 #define STAILQ_NEXT(elm, field) ((elm)->field.stqe_next)
117 #define STAILQ_REMOVE(head, elm, type, field) do { \
118 if (STAILQ_FIRST((head)) == (elm)) { \
119 STAILQ_REMOVE_HEAD((head), field); \
122 struct type *curelm = STAILQ_FIRST((head)); \
123 while (STAILQ_NEXT(curelm, field) != (elm)) \
124 curelm = STAILQ_NEXT(curelm, field); \
125 if ((STAILQ_NEXT(curelm, field) = \
126 STAILQ_NEXT(STAILQ_NEXT(curelm, field), field)) == NULL)\
127 (head)->stqh_last = &STAILQ_NEXT((curelm), field);\
131 #undef STAILQ_REMOVE_HEAD
132 #define STAILQ_REMOVE_HEAD(head, field) do { \
133 if ((STAILQ_FIRST((head)) = \
134 STAILQ_NEXT(STAILQ_FIRST((head)), field)) == NULL) \
135 (head)->stqh_last = &STAILQ_FIRST((head)); \
138 #undef STAILQ_REMOVE_HEAD_UNTIL
139 #define STAILQ_REMOVE_HEAD_UNTIL(head, elm, field) do { \
140 if ((STAILQ_FIRST((head)) = STAILQ_NEXT((elm), field)) == NULL) \
141 (head)->stqh_last = &STAILQ_FIRST((head)); \
148 #define LIST_HEAD(name, type) \
150 struct type *lh_first; /* first element */ \
153 #undef LIST_HEAD_INITIALIZER
154 #define LIST_HEAD_INITIALIZER(head) \
158 #define LIST_ENTRY(type) \
160 struct type *le_next; /* next element */ \
161 struct type **le_prev; /* address of previous next element */ \
169 #define LIST_EMPTY(head) ((head)->lh_first == NULL)
172 #define LIST_FIRST(head) ((head)->lh_first)
175 #define LIST_FOREACH(var, head, field) \
176 for ((var) = LIST_FIRST((head)); \
178 (var) = LIST_NEXT((var), field))
180 #undef LIST_FOREACH_SAFE
181 #define LIST_FOREACH_SAFE(var, head, field, tvar) \
182 for ((var) = LIST_FIRST((head)); \
183 (var) && ((tvar) = LIST_NEXT((var), field), 1); \
187 #define LIST_INIT(head) do { \
188 LIST_FIRST((head)) = NULL; \
191 #undef LIST_INSERT_AFTER
192 #define LIST_INSERT_AFTER(listelm, elm, field) do { \
193 if ((LIST_NEXT((elm), field) = LIST_NEXT((listelm), field)) != NULL)\
194 LIST_NEXT((listelm), field)->field.le_prev = \
195 &LIST_NEXT((elm), field); \
196 LIST_NEXT((listelm), field) = (elm); \
197 (elm)->field.le_prev = &LIST_NEXT((listelm), field); \
200 #undef LIST_INSERT_BEFORE
201 #define LIST_INSERT_BEFORE(listelm, elm, field) do { \
202 (elm)->field.le_prev = (listelm)->field.le_prev; \
203 LIST_NEXT((elm), field) = (listelm); \
204 *(listelm)->field.le_prev = (elm); \
205 (listelm)->field.le_prev = &LIST_NEXT((elm), field); \
208 #undef LIST_INSERT_HEAD
209 #define LIST_INSERT_HEAD(head, elm, field) do { \
210 if ((LIST_NEXT((elm), field) = LIST_FIRST((head))) != NULL) \
211 LIST_FIRST((head))->field.le_prev = &LIST_NEXT((elm), field);\
212 LIST_FIRST((head)) = (elm); \
213 (elm)->field.le_prev = &LIST_FIRST((head)); \
217 #define LIST_NEXT(elm, field) ((elm)->field.le_next)
220 #define LIST_REMOVE(elm, field) do { \
221 if (LIST_NEXT((elm), field) != NULL) \
222 LIST_NEXT((elm), field)->field.le_prev = \
223 (elm)->field.le_prev; \
224 *(elm)->field.le_prev = LIST_NEXT((elm), field); \
227 #endif /* !_QUEUE_H_ */