Blame


1 80189b3a 2022-08-13 op /*
2 80189b3a 2022-08-13 op * A compatible version of OpenBSD <sys/queue.h>.
3 80189b3a 2022-08-13 op */
4 80189b3a 2022-08-13 op /*
5 80189b3a 2022-08-13 op * Copyright (c) 1991, 1993
6 80189b3a 2022-08-13 op * The Regents of the University of California. All rights reserved.
7 80189b3a 2022-08-13 op *
8 80189b3a 2022-08-13 op * Redistribution and use in source and binary forms, with or without
9 80189b3a 2022-08-13 op * modification, are permitted provided that the following conditions
10 80189b3a 2022-08-13 op * are met:
11 80189b3a 2022-08-13 op * 1. Redistributions of source code must retain the above copyright
12 80189b3a 2022-08-13 op * notice, this list of conditions and the following disclaimer.
13 80189b3a 2022-08-13 op * 2. Redistributions in binary form must reproduce the above copyright
14 80189b3a 2022-08-13 op * notice, this list of conditions and the following disclaimer in the
15 80189b3a 2022-08-13 op * documentation and/or other materials provided with the distribution.
16 80189b3a 2022-08-13 op * 3. Neither the name of the University nor the names of its contributors
17 80189b3a 2022-08-13 op * may be used to endorse or promote products derived from this software
18 80189b3a 2022-08-13 op * without specific prior written permission.
19 80189b3a 2022-08-13 op *
20 80189b3a 2022-08-13 op * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ''AS IS'' AND
21 80189b3a 2022-08-13 op * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 80189b3a 2022-08-13 op * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 80189b3a 2022-08-13 op * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 80189b3a 2022-08-13 op * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 80189b3a 2022-08-13 op * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 80189b3a 2022-08-13 op * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 80189b3a 2022-08-13 op * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 80189b3a 2022-08-13 op * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 80189b3a 2022-08-13 op * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 80189b3a 2022-08-13 op * SUCH DAMAGE.
31 80189b3a 2022-08-13 op *
32 80189b3a 2022-08-13 op * @(#)queue.h 8.5 (Berkeley) 8/20/94
33 80189b3a 2022-08-13 op */
34 80189b3a 2022-08-13 op
35 80189b3a 2022-08-13 op /* OPENBSD ORIGINAL: sys/sys/queue.h */
36 80189b3a 2022-08-13 op
37 80189b3a 2022-08-13 op /*
38 80189b3a 2022-08-13 op * Require for OS/X and other platforms that have old/broken/incomplete
39 80189b3a 2022-08-13 op * <sys/queue.h>.
40 80189b3a 2022-08-13 op */
41 80189b3a 2022-08-13 op
42 80189b3a 2022-08-13 op #undef LIST_EMPTY
43 80189b3a 2022-08-13 op #undef LIST_END
44 80189b3a 2022-08-13 op #undef LIST_ENTRY
45 80189b3a 2022-08-13 op #undef LIST_FIRST
46 80189b3a 2022-08-13 op #undef LIST_FOREACH
47 80189b3a 2022-08-13 op #undef LIST_FOREACH_SAFE
48 80189b3a 2022-08-13 op #undef LIST_HEAD
49 80189b3a 2022-08-13 op #undef LIST_HEAD_INITIALIZER
50 80189b3a 2022-08-13 op #undef LIST_INIT
51 80189b3a 2022-08-13 op #undef LIST_INSERT_AFTER
52 80189b3a 2022-08-13 op #undef LIST_INSERT_BEFORE
53 80189b3a 2022-08-13 op #undef LIST_INSERT_HEAD
54 80189b3a 2022-08-13 op #undef LIST_NEXT
55 80189b3a 2022-08-13 op #undef LIST_REMOVE
56 80189b3a 2022-08-13 op #undef LIST_REPLACE
57 80189b3a 2022-08-13 op #undef SIMPLEQ_CONCAT
58 80189b3a 2022-08-13 op #undef SIMPLEQ_EMPTY
59 80189b3a 2022-08-13 op #undef SIMPLEQ_END
60 80189b3a 2022-08-13 op #undef SIMPLEQ_ENTRY
61 80189b3a 2022-08-13 op #undef SIMPLEQ_FIRST
62 80189b3a 2022-08-13 op #undef SIMPLEQ_FOREACH
63 80189b3a 2022-08-13 op #undef SIMPLEQ_FOREACH_SAFE
64 80189b3a 2022-08-13 op #undef SIMPLEQ_HEAD
65 80189b3a 2022-08-13 op #undef SIMPLEQ_HEAD_INITIALIZER
66 80189b3a 2022-08-13 op #undef SIMPLEQ_INIT
67 80189b3a 2022-08-13 op #undef SIMPLEQ_INSERT_AFTER
68 80189b3a 2022-08-13 op #undef SIMPLEQ_INSERT_HEAD
69 80189b3a 2022-08-13 op #undef SIMPLEQ_INSERT_TAIL
70 80189b3a 2022-08-13 op #undef SIMPLEQ_NEXT
71 80189b3a 2022-08-13 op #undef SIMPLEQ_REMOVE_AFTER
72 80189b3a 2022-08-13 op #undef SIMPLEQ_REMOVE_HEAD
73 80189b3a 2022-08-13 op #undef SLIST_EMPTY
74 80189b3a 2022-08-13 op #undef SLIST_END
75 80189b3a 2022-08-13 op #undef SLIST_ENTRY
76 80189b3a 2022-08-13 op #undef SLIST_FIRST
77 80189b3a 2022-08-13 op #undef SLIST_FOREACH
78 80189b3a 2022-08-13 op #undef SLIST_FOREACH_SAFE
79 80189b3a 2022-08-13 op #undef SLIST_HEAD
80 80189b3a 2022-08-13 op #undef SLIST_HEAD_INITIALIZER
81 80189b3a 2022-08-13 op #undef SLIST_INIT
82 80189b3a 2022-08-13 op #undef SLIST_INSERT_AFTER
83 80189b3a 2022-08-13 op #undef SLIST_INSERT_HEAD
84 80189b3a 2022-08-13 op #undef SLIST_NEXT
85 80189b3a 2022-08-13 op #undef SLIST_REMOVE
86 80189b3a 2022-08-13 op #undef SLIST_REMOVE_AFTER
87 80189b3a 2022-08-13 op #undef SLIST_REMOVE_HEAD
88 80189b3a 2022-08-13 op #undef TAILQ_CONCAT
89 80189b3a 2022-08-13 op #undef TAILQ_EMPTY
90 80189b3a 2022-08-13 op #undef TAILQ_END
91 80189b3a 2022-08-13 op #undef TAILQ_ENTRY
92 80189b3a 2022-08-13 op #undef TAILQ_FIRST
93 80189b3a 2022-08-13 op #undef TAILQ_FOREACH
94 80189b3a 2022-08-13 op #undef TAILQ_FOREACH_REVERSE
95 80189b3a 2022-08-13 op #undef TAILQ_FOREACH_REVERSE_SAFE
96 80189b3a 2022-08-13 op #undef TAILQ_FOREACH_SAFE
97 80189b3a 2022-08-13 op #undef TAILQ_HEAD
98 80189b3a 2022-08-13 op #undef TAILQ_HEAD_INITIALIZER
99 80189b3a 2022-08-13 op #undef TAILQ_INIT
100 80189b3a 2022-08-13 op #undef TAILQ_INSERT_AFTER
101 80189b3a 2022-08-13 op #undef TAILQ_INSERT_BEFORE
102 80189b3a 2022-08-13 op #undef TAILQ_INSERT_HEAD
103 80189b3a 2022-08-13 op #undef TAILQ_INSERT_TAIL
104 80189b3a 2022-08-13 op #undef TAILQ_LAST
105 80189b3a 2022-08-13 op #undef TAILQ_NEXT
106 80189b3a 2022-08-13 op #undef TAILQ_PREV
107 80189b3a 2022-08-13 op #undef TAILQ_REMOVE
108 80189b3a 2022-08-13 op #undef TAILQ_REPLACE
109 80189b3a 2022-08-13 op #undef XSIMPLEQ_EMPTY
110 80189b3a 2022-08-13 op #undef XSIMPLEQ_END
111 80189b3a 2022-08-13 op #undef XSIMPLEQ_ENTRY
112 80189b3a 2022-08-13 op #undef XSIMPLEQ_FIRST
113 80189b3a 2022-08-13 op #undef XSIMPLEQ_FOREACH
114 80189b3a 2022-08-13 op #undef XSIMPLEQ_FOREACH_SAFE
115 80189b3a 2022-08-13 op #undef XSIMPLEQ_HEAD
116 80189b3a 2022-08-13 op #undef XSIMPLEQ_INIT
117 80189b3a 2022-08-13 op #undef XSIMPLEQ_INSERT_AFTER
118 80189b3a 2022-08-13 op #undef XSIMPLEQ_INSERT_HEAD
119 80189b3a 2022-08-13 op #undef XSIMPLEQ_INSERT_TAIL
120 80189b3a 2022-08-13 op #undef XSIMPLEQ_NEXT
121 80189b3a 2022-08-13 op #undef XSIMPLEQ_REMOVE_AFTER
122 80189b3a 2022-08-13 op #undef XSIMPLEQ_REMOVE_HEAD
123 80189b3a 2022-08-13 op #undef XSIMPLEQ_XOR
124 80189b3a 2022-08-13 op
125 80189b3a 2022-08-13 op /*
126 80189b3a 2022-08-13 op * This file defines five types of data structures: singly-linked lists,
127 80189b3a 2022-08-13 op * lists, simple queues, tail queues and XOR simple queues.
128 80189b3a 2022-08-13 op *
129 80189b3a 2022-08-13 op *
130 80189b3a 2022-08-13 op * A singly-linked list is headed by a single forward pointer. The elements
131 80189b3a 2022-08-13 op * are singly linked for minimum space and pointer manipulation overhead at
132 80189b3a 2022-08-13 op * the expense of O(n) removal for arbitrary elements. New elements can be
133 80189b3a 2022-08-13 op * added to the list after an existing element or at the head of the list.
134 80189b3a 2022-08-13 op * Elements being removed from the head of the list should use the explicit
135 80189b3a 2022-08-13 op * macro for this purpose for optimum efficiency. A singly-linked list may
136 80189b3a 2022-08-13 op * only be traversed in the forward direction. Singly-linked lists are ideal
137 80189b3a 2022-08-13 op * for applications with large datasets and few or no removals or for
138 80189b3a 2022-08-13 op * implementing a LIFO queue.
139 80189b3a 2022-08-13 op *
140 80189b3a 2022-08-13 op * A list is headed by a single forward pointer (or an array of forward
141 80189b3a 2022-08-13 op * pointers for a hash table header). The elements are doubly linked
142 80189b3a 2022-08-13 op * so that an arbitrary element can be removed without a need to
143 80189b3a 2022-08-13 op * traverse the list. New elements can be added to the list before
144 80189b3a 2022-08-13 op * or after an existing element or at the head of the list. A list
145 80189b3a 2022-08-13 op * may only be traversed in the forward direction.
146 80189b3a 2022-08-13 op *
147 80189b3a 2022-08-13 op * A simple queue is headed by a pair of pointers, one to the head of the
148 80189b3a 2022-08-13 op * list and the other to the tail of the list. The elements are singly
149 80189b3a 2022-08-13 op * linked to save space, so elements can only be removed from the
150 80189b3a 2022-08-13 op * head of the list. New elements can be added to the list before or after
151 80189b3a 2022-08-13 op * an existing element, at the head of the list, or at the end of the
152 80189b3a 2022-08-13 op * list. A simple queue may only be traversed in the forward direction.
153 80189b3a 2022-08-13 op *
154 80189b3a 2022-08-13 op * A tail queue is headed by a pair of pointers, one to the head of the
155 80189b3a 2022-08-13 op * list and the other to the tail of the list. The elements are doubly
156 80189b3a 2022-08-13 op * linked so that an arbitrary element can be removed without a need to
157 80189b3a 2022-08-13 op * traverse the list. New elements can be added to the list before or
158 80189b3a 2022-08-13 op * after an existing element, at the head of the list, or at the end of
159 80189b3a 2022-08-13 op * the list. A tail queue may be traversed in either direction.
160 80189b3a 2022-08-13 op *
161 80189b3a 2022-08-13 op * An XOR simple queue is used in the same way as a regular simple queue.
162 80189b3a 2022-08-13 op * The difference is that the head structure also includes a "cookie" that
163 80189b3a 2022-08-13 op * is XOR'd with the queue pointer (first, last or next) to generate the
164 80189b3a 2022-08-13 op * real pointer value.
165 80189b3a 2022-08-13 op *
166 80189b3a 2022-08-13 op * For details on the use of these macros, see the queue(3) manual page.
167 80189b3a 2022-08-13 op */
168 80189b3a 2022-08-13 op
169 80189b3a 2022-08-13 op #if defined(QUEUE_MACRO_DEBUG) || (defined(_KERNEL) && defined(DIAGNOSTIC))
170 80189b3a 2022-08-13 op #define _Q_INVALID ((void *)-1)
171 80189b3a 2022-08-13 op #define _Q_INVALIDATE(a) (a) = _Q_INVALID
172 80189b3a 2022-08-13 op #else
173 80189b3a 2022-08-13 op #define _Q_INVALIDATE(a)
174 80189b3a 2022-08-13 op #endif
175 80189b3a 2022-08-13 op
176 80189b3a 2022-08-13 op /*
177 80189b3a 2022-08-13 op * Singly-linked List definitions.
178 80189b3a 2022-08-13 op */
179 80189b3a 2022-08-13 op #define SLIST_HEAD(name, type) \
180 80189b3a 2022-08-13 op struct name { \
181 80189b3a 2022-08-13 op struct type *slh_first; /* first element */ \
182 80189b3a 2022-08-13 op }
183 80189b3a 2022-08-13 op
184 80189b3a 2022-08-13 op #define SLIST_HEAD_INITIALIZER(head) \
185 80189b3a 2022-08-13 op { NULL }
186 80189b3a 2022-08-13 op
187 80189b3a 2022-08-13 op #define SLIST_ENTRY(type) \
188 80189b3a 2022-08-13 op struct { \
189 80189b3a 2022-08-13 op struct type *sle_next; /* next element */ \
190 80189b3a 2022-08-13 op }
191 80189b3a 2022-08-13 op
192 80189b3a 2022-08-13 op /*
193 80189b3a 2022-08-13 op * Singly-linked List access methods.
194 80189b3a 2022-08-13 op */
195 80189b3a 2022-08-13 op #define SLIST_FIRST(head) ((head)->slh_first)
196 80189b3a 2022-08-13 op #define SLIST_END(head) NULL
197 80189b3a 2022-08-13 op #define SLIST_EMPTY(head) (SLIST_FIRST(head) == SLIST_END(head))
198 80189b3a 2022-08-13 op #define SLIST_NEXT(elm, field) ((elm)->field.sle_next)
199 80189b3a 2022-08-13 op
200 80189b3a 2022-08-13 op #define SLIST_FOREACH(var, head, field) \
201 80189b3a 2022-08-13 op for((var) = SLIST_FIRST(head); \
202 80189b3a 2022-08-13 op (var) != SLIST_END(head); \
203 80189b3a 2022-08-13 op (var) = SLIST_NEXT(var, field))
204 80189b3a 2022-08-13 op
205 80189b3a 2022-08-13 op #define SLIST_FOREACH_SAFE(var, head, field, tvar) \
206 80189b3a 2022-08-13 op for ((var) = SLIST_FIRST(head); \
207 80189b3a 2022-08-13 op (var) && ((tvar) = SLIST_NEXT(var, field), 1); \
208 80189b3a 2022-08-13 op (var) = (tvar))
209 80189b3a 2022-08-13 op
210 80189b3a 2022-08-13 op /*
211 80189b3a 2022-08-13 op * Singly-linked List functions.
212 80189b3a 2022-08-13 op */
213 80189b3a 2022-08-13 op #define SLIST_INIT(head) { \
214 80189b3a 2022-08-13 op SLIST_FIRST(head) = SLIST_END(head); \
215 80189b3a 2022-08-13 op }
216 80189b3a 2022-08-13 op
217 80189b3a 2022-08-13 op #define SLIST_INSERT_AFTER(slistelm, elm, field) do { \
218 80189b3a 2022-08-13 op (elm)->field.sle_next = (slistelm)->field.sle_next; \
219 80189b3a 2022-08-13 op (slistelm)->field.sle_next = (elm); \
220 80189b3a 2022-08-13 op } while (0)
221 80189b3a 2022-08-13 op
222 80189b3a 2022-08-13 op #define SLIST_INSERT_HEAD(head, elm, field) do { \
223 80189b3a 2022-08-13 op (elm)->field.sle_next = (head)->slh_first; \
224 80189b3a 2022-08-13 op (head)->slh_first = (elm); \
225 80189b3a 2022-08-13 op } while (0)
226 80189b3a 2022-08-13 op
227 80189b3a 2022-08-13 op #define SLIST_REMOVE_AFTER(elm, field) do { \
228 80189b3a 2022-08-13 op (elm)->field.sle_next = (elm)->field.sle_next->field.sle_next; \
229 80189b3a 2022-08-13 op } while (0)
230 80189b3a 2022-08-13 op
231 80189b3a 2022-08-13 op #define SLIST_REMOVE_HEAD(head, field) do { \
232 80189b3a 2022-08-13 op (head)->slh_first = (head)->slh_first->field.sle_next; \
233 80189b3a 2022-08-13 op } while (0)
234 80189b3a 2022-08-13 op
235 80189b3a 2022-08-13 op #define SLIST_REMOVE(head, elm, type, field) do { \
236 80189b3a 2022-08-13 op if ((head)->slh_first == (elm)) { \
237 80189b3a 2022-08-13 op SLIST_REMOVE_HEAD((head), field); \
238 80189b3a 2022-08-13 op } else { \
239 80189b3a 2022-08-13 op struct type *curelm = (head)->slh_first; \
240 80189b3a 2022-08-13 op \
241 80189b3a 2022-08-13 op while (curelm->field.sle_next != (elm)) \
242 80189b3a 2022-08-13 op curelm = curelm->field.sle_next; \
243 80189b3a 2022-08-13 op curelm->field.sle_next = \
244 80189b3a 2022-08-13 op curelm->field.sle_next->field.sle_next; \
245 80189b3a 2022-08-13 op } \
246 80189b3a 2022-08-13 op _Q_INVALIDATE((elm)->field.sle_next); \
247 80189b3a 2022-08-13 op } while (0)
248 80189b3a 2022-08-13 op
249 80189b3a 2022-08-13 op /*
250 80189b3a 2022-08-13 op * List definitions.
251 80189b3a 2022-08-13 op */
252 80189b3a 2022-08-13 op #define LIST_HEAD(name, type) \
253 80189b3a 2022-08-13 op struct name { \
254 80189b3a 2022-08-13 op struct type *lh_first; /* first element */ \
255 80189b3a 2022-08-13 op }
256 80189b3a 2022-08-13 op
257 80189b3a 2022-08-13 op #define LIST_HEAD_INITIALIZER(head) \
258 80189b3a 2022-08-13 op { NULL }
259 80189b3a 2022-08-13 op
260 80189b3a 2022-08-13 op #define LIST_ENTRY(type) \
261 80189b3a 2022-08-13 op struct { \
262 80189b3a 2022-08-13 op struct type *le_next; /* next element */ \
263 80189b3a 2022-08-13 op struct type **le_prev; /* address of previous next element */ \
264 80189b3a 2022-08-13 op }
265 80189b3a 2022-08-13 op
266 80189b3a 2022-08-13 op /*
267 80189b3a 2022-08-13 op * List access methods.
268 80189b3a 2022-08-13 op */
269 80189b3a 2022-08-13 op #define LIST_FIRST(head) ((head)->lh_first)
270 80189b3a 2022-08-13 op #define LIST_END(head) NULL
271 80189b3a 2022-08-13 op #define LIST_EMPTY(head) (LIST_FIRST(head) == LIST_END(head))
272 80189b3a 2022-08-13 op #define LIST_NEXT(elm, field) ((elm)->field.le_next)
273 80189b3a 2022-08-13 op
274 80189b3a 2022-08-13 op #define LIST_FOREACH(var, head, field) \
275 80189b3a 2022-08-13 op for((var) = LIST_FIRST(head); \
276 80189b3a 2022-08-13 op (var)!= LIST_END(head); \
277 80189b3a 2022-08-13 op (var) = LIST_NEXT(var, field))
278 80189b3a 2022-08-13 op
279 80189b3a 2022-08-13 op #define LIST_FOREACH_SAFE(var, head, field, tvar) \
280 80189b3a 2022-08-13 op for ((var) = LIST_FIRST(head); \
281 80189b3a 2022-08-13 op (var) && ((tvar) = LIST_NEXT(var, field), 1); \
282 80189b3a 2022-08-13 op (var) = (tvar))
283 80189b3a 2022-08-13 op
284 80189b3a 2022-08-13 op /*
285 80189b3a 2022-08-13 op * List functions.
286 80189b3a 2022-08-13 op */
287 80189b3a 2022-08-13 op #define LIST_INIT(head) do { \
288 80189b3a 2022-08-13 op LIST_FIRST(head) = LIST_END(head); \
289 80189b3a 2022-08-13 op } while (0)
290 80189b3a 2022-08-13 op
291 80189b3a 2022-08-13 op #define LIST_INSERT_AFTER(listelm, elm, field) do { \
292 80189b3a 2022-08-13 op if (((elm)->field.le_next = (listelm)->field.le_next) != NULL) \
293 80189b3a 2022-08-13 op (listelm)->field.le_next->field.le_prev = \
294 80189b3a 2022-08-13 op &(elm)->field.le_next; \
295 80189b3a 2022-08-13 op (listelm)->field.le_next = (elm); \
296 80189b3a 2022-08-13 op (elm)->field.le_prev = &(listelm)->field.le_next; \
297 80189b3a 2022-08-13 op } while (0)
298 80189b3a 2022-08-13 op
299 80189b3a 2022-08-13 op #define LIST_INSERT_BEFORE(listelm, elm, field) do { \
300 80189b3a 2022-08-13 op (elm)->field.le_prev = (listelm)->field.le_prev; \
301 80189b3a 2022-08-13 op (elm)->field.le_next = (listelm); \
302 80189b3a 2022-08-13 op *(listelm)->field.le_prev = (elm); \
303 80189b3a 2022-08-13 op (listelm)->field.le_prev = &(elm)->field.le_next; \
304 80189b3a 2022-08-13 op } while (0)
305 80189b3a 2022-08-13 op
306 80189b3a 2022-08-13 op #define LIST_INSERT_HEAD(head, elm, field) do { \
307 80189b3a 2022-08-13 op if (((elm)->field.le_next = (head)->lh_first) != NULL) \
308 80189b3a 2022-08-13 op (head)->lh_first->field.le_prev = &(elm)->field.le_next;\
309 80189b3a 2022-08-13 op (head)->lh_first = (elm); \
310 80189b3a 2022-08-13 op (elm)->field.le_prev = &(head)->lh_first; \
311 80189b3a 2022-08-13 op } while (0)
312 80189b3a 2022-08-13 op
313 80189b3a 2022-08-13 op #define LIST_REMOVE(elm, field) do { \
314 80189b3a 2022-08-13 op if ((elm)->field.le_next != NULL) \
315 80189b3a 2022-08-13 op (elm)->field.le_next->field.le_prev = \
316 80189b3a 2022-08-13 op (elm)->field.le_prev; \
317 80189b3a 2022-08-13 op *(elm)->field.le_prev = (elm)->field.le_next; \
318 80189b3a 2022-08-13 op _Q_INVALIDATE((elm)->field.le_prev); \
319 80189b3a 2022-08-13 op _Q_INVALIDATE((elm)->field.le_next); \
320 80189b3a 2022-08-13 op } while (0)
321 80189b3a 2022-08-13 op
322 80189b3a 2022-08-13 op #define LIST_REPLACE(elm, elm2, field) do { \
323 80189b3a 2022-08-13 op if (((elm2)->field.le_next = (elm)->field.le_next) != NULL) \
324 80189b3a 2022-08-13 op (elm2)->field.le_next->field.le_prev = \
325 80189b3a 2022-08-13 op &(elm2)->field.le_next; \
326 80189b3a 2022-08-13 op (elm2)->field.le_prev = (elm)->field.le_prev; \
327 80189b3a 2022-08-13 op *(elm2)->field.le_prev = (elm2); \
328 80189b3a 2022-08-13 op _Q_INVALIDATE((elm)->field.le_prev); \
329 80189b3a 2022-08-13 op _Q_INVALIDATE((elm)->field.le_next); \
330 80189b3a 2022-08-13 op } while (0)
331 80189b3a 2022-08-13 op
332 80189b3a 2022-08-13 op /*
333 80189b3a 2022-08-13 op * Simple queue definitions.
334 80189b3a 2022-08-13 op */
335 80189b3a 2022-08-13 op #define SIMPLEQ_HEAD(name, type) \
336 80189b3a 2022-08-13 op struct name { \
337 80189b3a 2022-08-13 op struct type *sqh_first; /* first element */ \
338 80189b3a 2022-08-13 op struct type **sqh_last; /* addr of last next element */ \
339 80189b3a 2022-08-13 op }
340 80189b3a 2022-08-13 op
341 80189b3a 2022-08-13 op #define SIMPLEQ_HEAD_INITIALIZER(head) \
342 80189b3a 2022-08-13 op { NULL, &(head).sqh_first }
343 80189b3a 2022-08-13 op
344 80189b3a 2022-08-13 op #define SIMPLEQ_ENTRY(type) \
345 80189b3a 2022-08-13 op struct { \
346 80189b3a 2022-08-13 op struct type *sqe_next; /* next element */ \
347 80189b3a 2022-08-13 op }
348 80189b3a 2022-08-13 op
349 80189b3a 2022-08-13 op /*
350 80189b3a 2022-08-13 op * Simple queue access methods.
351 80189b3a 2022-08-13 op */
352 80189b3a 2022-08-13 op #define SIMPLEQ_FIRST(head) ((head)->sqh_first)
353 80189b3a 2022-08-13 op #define SIMPLEQ_END(head) NULL
354 80189b3a 2022-08-13 op #define SIMPLEQ_EMPTY(head) (SIMPLEQ_FIRST(head) == SIMPLEQ_END(head))
355 80189b3a 2022-08-13 op #define SIMPLEQ_NEXT(elm, field) ((elm)->field.sqe_next)
356 80189b3a 2022-08-13 op
357 80189b3a 2022-08-13 op #define SIMPLEQ_FOREACH(var, head, field) \
358 80189b3a 2022-08-13 op for((var) = SIMPLEQ_FIRST(head); \
359 80189b3a 2022-08-13 op (var) != SIMPLEQ_END(head); \
360 80189b3a 2022-08-13 op (var) = SIMPLEQ_NEXT(var, field))
361 80189b3a 2022-08-13 op
362 80189b3a 2022-08-13 op #define SIMPLEQ_FOREACH_SAFE(var, head, field, tvar) \
363 80189b3a 2022-08-13 op for ((var) = SIMPLEQ_FIRST(head); \
364 80189b3a 2022-08-13 op (var) && ((tvar) = SIMPLEQ_NEXT(var, field), 1); \
365 80189b3a 2022-08-13 op (var) = (tvar))
366 80189b3a 2022-08-13 op
367 80189b3a 2022-08-13 op /*
368 80189b3a 2022-08-13 op * Simple queue functions.
369 80189b3a 2022-08-13 op */
370 80189b3a 2022-08-13 op #define SIMPLEQ_INIT(head) do { \
371 80189b3a 2022-08-13 op (head)->sqh_first = NULL; \
372 80189b3a 2022-08-13 op (head)->sqh_last = &(head)->sqh_first; \
373 80189b3a 2022-08-13 op } while (0)
374 80189b3a 2022-08-13 op
375 80189b3a 2022-08-13 op #define SIMPLEQ_INSERT_HEAD(head, elm, field) do { \
376 80189b3a 2022-08-13 op if (((elm)->field.sqe_next = (head)->sqh_first) == NULL) \
377 80189b3a 2022-08-13 op (head)->sqh_last = &(elm)->field.sqe_next; \
378 80189b3a 2022-08-13 op (head)->sqh_first = (elm); \
379 80189b3a 2022-08-13 op } while (0)
380 80189b3a 2022-08-13 op
381 80189b3a 2022-08-13 op #define SIMPLEQ_INSERT_TAIL(head, elm, field) do { \
382 80189b3a 2022-08-13 op (elm)->field.sqe_next = NULL; \
383 80189b3a 2022-08-13 op *(head)->sqh_last = (elm); \
384 80189b3a 2022-08-13 op (head)->sqh_last = &(elm)->field.sqe_next; \
385 80189b3a 2022-08-13 op } while (0)
386 80189b3a 2022-08-13 op
387 80189b3a 2022-08-13 op #define SIMPLEQ_INSERT_AFTER(head, listelm, elm, field) do { \
388 80189b3a 2022-08-13 op if (((elm)->field.sqe_next = (listelm)->field.sqe_next) == NULL)\
389 80189b3a 2022-08-13 op (head)->sqh_last = &(elm)->field.sqe_next; \
390 80189b3a 2022-08-13 op (listelm)->field.sqe_next = (elm); \
391 80189b3a 2022-08-13 op } while (0)
392 80189b3a 2022-08-13 op
393 80189b3a 2022-08-13 op #define SIMPLEQ_REMOVE_HEAD(head, field) do { \
394 80189b3a 2022-08-13 op if (((head)->sqh_first = (head)->sqh_first->field.sqe_next) == NULL) \
395 80189b3a 2022-08-13 op (head)->sqh_last = &(head)->sqh_first; \
396 80189b3a 2022-08-13 op } while (0)
397 80189b3a 2022-08-13 op
398 80189b3a 2022-08-13 op #define SIMPLEQ_REMOVE_AFTER(head, elm, field) do { \
399 80189b3a 2022-08-13 op if (((elm)->field.sqe_next = (elm)->field.sqe_next->field.sqe_next) \
400 80189b3a 2022-08-13 op == NULL) \
401 80189b3a 2022-08-13 op (head)->sqh_last = &(elm)->field.sqe_next; \
402 80189b3a 2022-08-13 op } while (0)
403 80189b3a 2022-08-13 op
404 80189b3a 2022-08-13 op #define SIMPLEQ_CONCAT(head1, head2) do { \
405 80189b3a 2022-08-13 op if (!SIMPLEQ_EMPTY((head2))) { \
406 80189b3a 2022-08-13 op *(head1)->sqh_last = (head2)->sqh_first; \
407 80189b3a 2022-08-13 op (head1)->sqh_last = (head2)->sqh_last; \
408 80189b3a 2022-08-13 op SIMPLEQ_INIT((head2)); \
409 80189b3a 2022-08-13 op } \
410 80189b3a 2022-08-13 op } while (0)
411 80189b3a 2022-08-13 op
412 80189b3a 2022-08-13 op /*
413 80189b3a 2022-08-13 op * XOR Simple queue definitions.
414 80189b3a 2022-08-13 op */
415 80189b3a 2022-08-13 op #define XSIMPLEQ_HEAD(name, type) \
416 80189b3a 2022-08-13 op struct name { \
417 80189b3a 2022-08-13 op struct type *sqx_first; /* first element */ \
418 80189b3a 2022-08-13 op struct type **sqx_last; /* addr of last next element */ \
419 80189b3a 2022-08-13 op unsigned long sqx_cookie; \
420 80189b3a 2022-08-13 op }
421 80189b3a 2022-08-13 op
422 80189b3a 2022-08-13 op #define XSIMPLEQ_ENTRY(type) \
423 80189b3a 2022-08-13 op struct { \
424 80189b3a 2022-08-13 op struct type *sqx_next; /* next element */ \
425 80189b3a 2022-08-13 op }
426 80189b3a 2022-08-13 op
427 80189b3a 2022-08-13 op /*
428 80189b3a 2022-08-13 op * XOR Simple queue access methods.
429 80189b3a 2022-08-13 op */
430 80189b3a 2022-08-13 op #define XSIMPLEQ_XOR(head, ptr) ((__typeof(ptr))((head)->sqx_cookie ^ \
431 80189b3a 2022-08-13 op (unsigned long)(ptr)))
432 80189b3a 2022-08-13 op #define XSIMPLEQ_FIRST(head) XSIMPLEQ_XOR(head, ((head)->sqx_first))
433 80189b3a 2022-08-13 op #define XSIMPLEQ_END(head) NULL
434 80189b3a 2022-08-13 op #define XSIMPLEQ_EMPTY(head) (XSIMPLEQ_FIRST(head) == XSIMPLEQ_END(head))
435 80189b3a 2022-08-13 op #define XSIMPLEQ_NEXT(head, elm, field) XSIMPLEQ_XOR(head, ((elm)->field.sqx_next))
436 80189b3a 2022-08-13 op
437 80189b3a 2022-08-13 op
438 80189b3a 2022-08-13 op #define XSIMPLEQ_FOREACH(var, head, field) \
439 80189b3a 2022-08-13 op for ((var) = XSIMPLEQ_FIRST(head); \
440 80189b3a 2022-08-13 op (var) != XSIMPLEQ_END(head); \
441 80189b3a 2022-08-13 op (var) = XSIMPLEQ_NEXT(head, var, field))
442 80189b3a 2022-08-13 op
443 80189b3a 2022-08-13 op #define XSIMPLEQ_FOREACH_SAFE(var, head, field, tvar) \
444 80189b3a 2022-08-13 op for ((var) = XSIMPLEQ_FIRST(head); \
445 80189b3a 2022-08-13 op (var) && ((tvar) = XSIMPLEQ_NEXT(head, var, field), 1); \
446 80189b3a 2022-08-13 op (var) = (tvar))
447 80189b3a 2022-08-13 op
448 80189b3a 2022-08-13 op /*
449 80189b3a 2022-08-13 op * XOR Simple queue functions.
450 80189b3a 2022-08-13 op */
451 80189b3a 2022-08-13 op #define XSIMPLEQ_INIT(head) do { \
452 80189b3a 2022-08-13 op arc4random_buf(&(head)->sqx_cookie, sizeof((head)->sqx_cookie)); \
453 80189b3a 2022-08-13 op (head)->sqx_first = XSIMPLEQ_XOR(head, NULL); \
454 80189b3a 2022-08-13 op (head)->sqx_last = XSIMPLEQ_XOR(head, &(head)->sqx_first); \
455 80189b3a 2022-08-13 op } while (0)
456 80189b3a 2022-08-13 op
457 80189b3a 2022-08-13 op #define XSIMPLEQ_INSERT_HEAD(head, elm, field) do { \
458 80189b3a 2022-08-13 op if (((elm)->field.sqx_next = (head)->sqx_first) == \
459 80189b3a 2022-08-13 op XSIMPLEQ_XOR(head, NULL)) \
460 80189b3a 2022-08-13 op (head)->sqx_last = XSIMPLEQ_XOR(head, &(elm)->field.sqx_next); \
461 80189b3a 2022-08-13 op (head)->sqx_first = XSIMPLEQ_XOR(head, (elm)); \
462 80189b3a 2022-08-13 op } while (0)
463 80189b3a 2022-08-13 op
464 80189b3a 2022-08-13 op #define XSIMPLEQ_INSERT_TAIL(head, elm, field) do { \
465 80189b3a 2022-08-13 op (elm)->field.sqx_next = XSIMPLEQ_XOR(head, NULL); \
466 80189b3a 2022-08-13 op *(XSIMPLEQ_XOR(head, (head)->sqx_last)) = XSIMPLEQ_XOR(head, (elm)); \
467 80189b3a 2022-08-13 op (head)->sqx_last = XSIMPLEQ_XOR(head, &(elm)->field.sqx_next); \
468 80189b3a 2022-08-13 op } while (0)
469 80189b3a 2022-08-13 op
470 80189b3a 2022-08-13 op #define XSIMPLEQ_INSERT_AFTER(head, listelm, elm, field) do { \
471 80189b3a 2022-08-13 op if (((elm)->field.sqx_next = (listelm)->field.sqx_next) == \
472 80189b3a 2022-08-13 op XSIMPLEQ_XOR(head, NULL)) \
473 80189b3a 2022-08-13 op (head)->sqx_last = XSIMPLEQ_XOR(head, &(elm)->field.sqx_next); \
474 80189b3a 2022-08-13 op (listelm)->field.sqx_next = XSIMPLEQ_XOR(head, (elm)); \
475 80189b3a 2022-08-13 op } while (0)
476 80189b3a 2022-08-13 op
477 80189b3a 2022-08-13 op #define XSIMPLEQ_REMOVE_HEAD(head, field) do { \
478 80189b3a 2022-08-13 op if (((head)->sqx_first = XSIMPLEQ_XOR(head, \
479 80189b3a 2022-08-13 op (head)->sqx_first)->field.sqx_next) == XSIMPLEQ_XOR(head, NULL)) \
480 80189b3a 2022-08-13 op (head)->sqx_last = XSIMPLEQ_XOR(head, &(head)->sqx_first); \
481 80189b3a 2022-08-13 op } while (0)
482 80189b3a 2022-08-13 op
483 80189b3a 2022-08-13 op #define XSIMPLEQ_REMOVE_AFTER(head, elm, field) do { \
484 80189b3a 2022-08-13 op if (((elm)->field.sqx_next = XSIMPLEQ_XOR(head, \
485 80189b3a 2022-08-13 op (elm)->field.sqx_next)->field.sqx_next) \
486 80189b3a 2022-08-13 op == XSIMPLEQ_XOR(head, NULL)) \
487 80189b3a 2022-08-13 op (head)->sqx_last = \
488 80189b3a 2022-08-13 op XSIMPLEQ_XOR(head, &(elm)->field.sqx_next); \
489 80189b3a 2022-08-13 op } while (0)
490 80189b3a 2022-08-13 op
491 80189b3a 2022-08-13 op
492 80189b3a 2022-08-13 op /*
493 80189b3a 2022-08-13 op * Tail queue definitions.
494 80189b3a 2022-08-13 op */
495 80189b3a 2022-08-13 op #define TAILQ_HEAD(name, type) \
496 80189b3a 2022-08-13 op struct name { \
497 80189b3a 2022-08-13 op struct type *tqh_first; /* first element */ \
498 80189b3a 2022-08-13 op struct type **tqh_last; /* addr of last next element */ \
499 80189b3a 2022-08-13 op }
500 80189b3a 2022-08-13 op
501 80189b3a 2022-08-13 op #define TAILQ_HEAD_INITIALIZER(head) \
502 80189b3a 2022-08-13 op { NULL, &(head).tqh_first }
503 80189b3a 2022-08-13 op
504 80189b3a 2022-08-13 op #define TAILQ_ENTRY(type) \
505 80189b3a 2022-08-13 op struct { \
506 80189b3a 2022-08-13 op struct type *tqe_next; /* next element */ \
507 80189b3a 2022-08-13 op struct type **tqe_prev; /* address of previous next element */ \
508 80189b3a 2022-08-13 op }
509 80189b3a 2022-08-13 op
510 80189b3a 2022-08-13 op /*
511 80189b3a 2022-08-13 op * Tail queue access methods.
512 80189b3a 2022-08-13 op */
513 80189b3a 2022-08-13 op #define TAILQ_FIRST(head) ((head)->tqh_first)
514 80189b3a 2022-08-13 op #define TAILQ_END(head) NULL
515 80189b3a 2022-08-13 op #define TAILQ_NEXT(elm, field) ((elm)->field.tqe_next)
516 80189b3a 2022-08-13 op #define TAILQ_LAST(head, headname) \
517 80189b3a 2022-08-13 op (*(((struct headname *)((head)->tqh_last))->tqh_last))
518 80189b3a 2022-08-13 op /* XXX */
519 80189b3a 2022-08-13 op #define TAILQ_PREV(elm, headname, field) \
520 80189b3a 2022-08-13 op (*(((struct headname *)((elm)->field.tqe_prev))->tqh_last))
521 80189b3a 2022-08-13 op #define TAILQ_EMPTY(head) \
522 80189b3a 2022-08-13 op (TAILQ_FIRST(head) == TAILQ_END(head))
523 80189b3a 2022-08-13 op
524 80189b3a 2022-08-13 op #define TAILQ_FOREACH(var, head, field) \
525 80189b3a 2022-08-13 op for((var) = TAILQ_FIRST(head); \
526 80189b3a 2022-08-13 op (var) != TAILQ_END(head); \
527 80189b3a 2022-08-13 op (var) = TAILQ_NEXT(var, field))
528 80189b3a 2022-08-13 op
529 80189b3a 2022-08-13 op #define TAILQ_FOREACH_SAFE(var, head, field, tvar) \
530 80189b3a 2022-08-13 op for ((var) = TAILQ_FIRST(head); \
531 80189b3a 2022-08-13 op (var) != TAILQ_END(head) && \
532 80189b3a 2022-08-13 op ((tvar) = TAILQ_NEXT(var, field), 1); \
533 80189b3a 2022-08-13 op (var) = (tvar))
534 80189b3a 2022-08-13 op
535 80189b3a 2022-08-13 op
536 80189b3a 2022-08-13 op #define TAILQ_FOREACH_REVERSE(var, head, headname, field) \
537 80189b3a 2022-08-13 op for((var) = TAILQ_LAST(head, headname); \
538 80189b3a 2022-08-13 op (var) != TAILQ_END(head); \
539 80189b3a 2022-08-13 op (var) = TAILQ_PREV(var, headname, field))
540 80189b3a 2022-08-13 op
541 80189b3a 2022-08-13 op #define TAILQ_FOREACH_REVERSE_SAFE(var, head, headname, field, tvar) \
542 80189b3a 2022-08-13 op for ((var) = TAILQ_LAST(head, headname); \
543 80189b3a 2022-08-13 op (var) != TAILQ_END(head) && \
544 80189b3a 2022-08-13 op ((tvar) = TAILQ_PREV(var, headname, field), 1); \
545 80189b3a 2022-08-13 op (var) = (tvar))
546 80189b3a 2022-08-13 op
547 80189b3a 2022-08-13 op /*
548 80189b3a 2022-08-13 op * Tail queue functions.
549 80189b3a 2022-08-13 op */
550 80189b3a 2022-08-13 op #define TAILQ_INIT(head) do { \
551 80189b3a 2022-08-13 op (head)->tqh_first = NULL; \
552 80189b3a 2022-08-13 op (head)->tqh_last = &(head)->tqh_first; \
553 80189b3a 2022-08-13 op } while (0)
554 80189b3a 2022-08-13 op
555 80189b3a 2022-08-13 op #define TAILQ_INSERT_HEAD(head, elm, field) do { \
556 80189b3a 2022-08-13 op if (((elm)->field.tqe_next = (head)->tqh_first) != NULL) \
557 80189b3a 2022-08-13 op (head)->tqh_first->field.tqe_prev = \
558 80189b3a 2022-08-13 op &(elm)->field.tqe_next; \
559 80189b3a 2022-08-13 op else \
560 80189b3a 2022-08-13 op (head)->tqh_last = &(elm)->field.tqe_next; \
561 80189b3a 2022-08-13 op (head)->tqh_first = (elm); \
562 80189b3a 2022-08-13 op (elm)->field.tqe_prev = &(head)->tqh_first; \
563 80189b3a 2022-08-13 op } while (0)
564 80189b3a 2022-08-13 op
565 80189b3a 2022-08-13 op #define TAILQ_INSERT_TAIL(head, elm, field) do { \
566 80189b3a 2022-08-13 op (elm)->field.tqe_next = NULL; \
567 80189b3a 2022-08-13 op (elm)->field.tqe_prev = (head)->tqh_last; \
568 80189b3a 2022-08-13 op *(head)->tqh_last = (elm); \
569 80189b3a 2022-08-13 op (head)->tqh_last = &(elm)->field.tqe_next; \
570 80189b3a 2022-08-13 op } while (0)
571 80189b3a 2022-08-13 op
572 80189b3a 2022-08-13 op #define TAILQ_INSERT_AFTER(head, listelm, elm, field) do { \
573 80189b3a 2022-08-13 op if (((elm)->field.tqe_next = (listelm)->field.tqe_next) != NULL)\
574 80189b3a 2022-08-13 op (elm)->field.tqe_next->field.tqe_prev = \
575 80189b3a 2022-08-13 op &(elm)->field.tqe_next; \
576 80189b3a 2022-08-13 op else \
577 80189b3a 2022-08-13 op (head)->tqh_last = &(elm)->field.tqe_next; \
578 80189b3a 2022-08-13 op (listelm)->field.tqe_next = (elm); \
579 80189b3a 2022-08-13 op (elm)->field.tqe_prev = &(listelm)->field.tqe_next; \
580 80189b3a 2022-08-13 op } while (0)
581 80189b3a 2022-08-13 op
582 80189b3a 2022-08-13 op #define TAILQ_INSERT_BEFORE(listelm, elm, field) do { \
583 80189b3a 2022-08-13 op (elm)->field.tqe_prev = (listelm)->field.tqe_prev; \
584 80189b3a 2022-08-13 op (elm)->field.tqe_next = (listelm); \
585 80189b3a 2022-08-13 op *(listelm)->field.tqe_prev = (elm); \
586 80189b3a 2022-08-13 op (listelm)->field.tqe_prev = &(elm)->field.tqe_next; \
587 80189b3a 2022-08-13 op } while (0)
588 80189b3a 2022-08-13 op
589 80189b3a 2022-08-13 op #define TAILQ_REMOVE(head, elm, field) do { \
590 80189b3a 2022-08-13 op if (((elm)->field.tqe_next) != NULL) \
591 80189b3a 2022-08-13 op (elm)->field.tqe_next->field.tqe_prev = \
592 80189b3a 2022-08-13 op (elm)->field.tqe_prev; \
593 80189b3a 2022-08-13 op else \
594 80189b3a 2022-08-13 op (head)->tqh_last = (elm)->field.tqe_prev; \
595 80189b3a 2022-08-13 op *(elm)->field.tqe_prev = (elm)->field.tqe_next; \
596 80189b3a 2022-08-13 op _Q_INVALIDATE((elm)->field.tqe_prev); \
597 80189b3a 2022-08-13 op _Q_INVALIDATE((elm)->field.tqe_next); \
598 80189b3a 2022-08-13 op } while (0)
599 80189b3a 2022-08-13 op
600 80189b3a 2022-08-13 op #define TAILQ_REPLACE(head, elm, elm2, field) do { \
601 80189b3a 2022-08-13 op if (((elm2)->field.tqe_next = (elm)->field.tqe_next) != NULL) \
602 80189b3a 2022-08-13 op (elm2)->field.tqe_next->field.tqe_prev = \
603 80189b3a 2022-08-13 op &(elm2)->field.tqe_next; \
604 80189b3a 2022-08-13 op else \
605 80189b3a 2022-08-13 op (head)->tqh_last = &(elm2)->field.tqe_next; \
606 80189b3a 2022-08-13 op (elm2)->field.tqe_prev = (elm)->field.tqe_prev; \
607 80189b3a 2022-08-13 op *(elm2)->field.tqe_prev = (elm2); \
608 80189b3a 2022-08-13 op _Q_INVALIDATE((elm)->field.tqe_prev); \
609 80189b3a 2022-08-13 op _Q_INVALIDATE((elm)->field.tqe_next); \
610 80189b3a 2022-08-13 op } while (0)
611 80189b3a 2022-08-13 op
612 80189b3a 2022-08-13 op #define TAILQ_CONCAT(head1, head2, field) do { \
613 80189b3a 2022-08-13 op if (!TAILQ_EMPTY(head2)) { \
614 80189b3a 2022-08-13 op *(head1)->tqh_last = (head2)->tqh_first; \
615 80189b3a 2022-08-13 op (head2)->tqh_first->field.tqe_prev = (head1)->tqh_last; \
616 80189b3a 2022-08-13 op (head1)->tqh_last = (head2)->tqh_last; \
617 80189b3a 2022-08-13 op TAILQ_INIT((head2)); \
618 80189b3a 2022-08-13 op } \
619 80189b3a 2022-08-13 op } while (0)