2 80189b3a 2022-08-13 op * A compatible version of OpenBSD <sys/queue.h>.
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.
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
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.
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.
32 80189b3a 2022-08-13 op * @(#)queue.h 8.5 (Berkeley) 8/20/94
35 80189b3a 2022-08-13 op /* OPENBSD ORIGINAL: sys/sys/queue.h */
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>.
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
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.
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.
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.
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.
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.
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.
166 80189b3a 2022-08-13 op * For details on the use of these macros, see the queue(3) manual page.
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
173 80189b3a 2022-08-13 op #define _Q_INVALIDATE(a)
177 80189b3a 2022-08-13 op * Singly-linked List definitions.
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 */ \
184 80189b3a 2022-08-13 op #define SLIST_HEAD_INITIALIZER(head) \
187 80189b3a 2022-08-13 op #define SLIST_ENTRY(type) \
189 80189b3a 2022-08-13 op struct type *sle_next; /* next element */ \
193 80189b3a 2022-08-13 op * Singly-linked List access methods.
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)
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))
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))
211 80189b3a 2022-08-13 op * Singly-linked List functions.
213 80189b3a 2022-08-13 op #define SLIST_INIT(head) { \
214 80189b3a 2022-08-13 op SLIST_FIRST(head) = SLIST_END(head); \
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); \
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); \
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; \
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; \
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); \
239 80189b3a 2022-08-13 op struct type *curelm = (head)->slh_first; \
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; \
246 80189b3a 2022-08-13 op _Q_INVALIDATE((elm)->field.sle_next); \
250 80189b3a 2022-08-13 op * List definitions.
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 */ \
257 80189b3a 2022-08-13 op #define LIST_HEAD_INITIALIZER(head) \
260 80189b3a 2022-08-13 op #define LIST_ENTRY(type) \
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 */ \
267 80189b3a 2022-08-13 op * List access methods.
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)
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))
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))
285 80189b3a 2022-08-13 op * List functions.
287 80189b3a 2022-08-13 op #define LIST_INIT(head) do { \
288 80189b3a 2022-08-13 op LIST_FIRST(head) = LIST_END(head); \
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; \
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; \
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; \
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); \
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); \
333 80189b3a 2022-08-13 op * Simple queue definitions.
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 */ \
341 80189b3a 2022-08-13 op #define SIMPLEQ_HEAD_INITIALIZER(head) \
342 80189b3a 2022-08-13 op { NULL, &(head).sqh_first }
344 80189b3a 2022-08-13 op #define SIMPLEQ_ENTRY(type) \
346 80189b3a 2022-08-13 op struct type *sqe_next; /* next element */ \
350 80189b3a 2022-08-13 op * Simple queue access methods.
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)
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))
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))
368 80189b3a 2022-08-13 op * Simple queue functions.
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; \
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); \
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; \
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); \
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; \
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) \
401 80189b3a 2022-08-13 op (head)->sqh_last = &(elm)->field.sqe_next; \
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)); \
413 80189b3a 2022-08-13 op * XOR Simple queue definitions.
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; \
422 80189b3a 2022-08-13 op #define XSIMPLEQ_ENTRY(type) \
424 80189b3a 2022-08-13 op struct type *sqx_next; /* next element */ \
428 80189b3a 2022-08-13 op * XOR Simple queue access methods.
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))
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))
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))
449 80189b3a 2022-08-13 op * XOR Simple queue functions.
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); \
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)); \
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); \
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)); \
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); \
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); \
493 80189b3a 2022-08-13 op * Tail queue definitions.
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 */ \
501 80189b3a 2022-08-13 op #define TAILQ_HEAD_INITIALIZER(head) \
502 80189b3a 2022-08-13 op { NULL, &(head).tqh_first }
504 80189b3a 2022-08-13 op #define TAILQ_ENTRY(type) \
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 */ \
511 80189b3a 2022-08-13 op * Tail queue access methods.
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))
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))
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))
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))
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))
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))
548 80189b3a 2022-08-13 op * Tail queue functions.
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; \
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; \
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; \
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; \
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; \
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; \
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; \
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; \
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); \
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; \
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); \
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)); \