1 0d6fb46a 2022-01-09 op /* $OpenBSD: queue.h,v 1.46 2020/12/30 13:33:12 millert Exp $ */
2 0d6fb46a 2022-01-09 op /* $NetBSD: queue.h,v 1.11 1996/05/16 05:17:14 mycroft Exp $ */
5 0d6fb46a 2022-01-09 op * Copyright (c) 1991, 1993
6 0d6fb46a 2022-01-09 op * The Regents of the University of California. All rights reserved.
8 0d6fb46a 2022-01-09 op * Redistribution and use in source and binary forms, with or without
9 0d6fb46a 2022-01-09 op * modification, are permitted provided that the following conditions
11 0d6fb46a 2022-01-09 op * 1. Redistributions of source code must retain the above copyright
12 0d6fb46a 2022-01-09 op * notice, this list of conditions and the following disclaimer.
13 0d6fb46a 2022-01-09 op * 2. Redistributions in binary form must reproduce the above copyright
14 0d6fb46a 2022-01-09 op * notice, this list of conditions and the following disclaimer in the
15 0d6fb46a 2022-01-09 op * documentation and/or other materials provided with the distribution.
16 0d6fb46a 2022-01-09 op * 3. Neither the name of the University nor the names of its contributors
17 0d6fb46a 2022-01-09 op * may be used to endorse or promote products derived from this software
18 0d6fb46a 2022-01-09 op * without specific prior written permission.
20 0d6fb46a 2022-01-09 op * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 0d6fb46a 2022-01-09 op * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 0d6fb46a 2022-01-09 op * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 0d6fb46a 2022-01-09 op * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 0d6fb46a 2022-01-09 op * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 0d6fb46a 2022-01-09 op * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 0d6fb46a 2022-01-09 op * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 0d6fb46a 2022-01-09 op * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 0d6fb46a 2022-01-09 op * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 0d6fb46a 2022-01-09 op * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 0d6fb46a 2022-01-09 op * SUCH DAMAGE.
32 0d6fb46a 2022-01-09 op * @(#)queue.h 8.5 (Berkeley) 8/20/94
35 0d6fb46a 2022-01-09 op #ifndef _SYS_QUEUE_H_
36 0d6fb46a 2022-01-09 op #define _SYS_QUEUE_H_
39 0d6fb46a 2022-01-09 op * This file defines five types of data structures: singly-linked lists,
40 0d6fb46a 2022-01-09 op * lists, simple queues, tail queues and XOR simple queues.
43 0d6fb46a 2022-01-09 op * A singly-linked list is headed by a single forward pointer. The elements
44 0d6fb46a 2022-01-09 op * are singly linked for minimum space and pointer manipulation overhead at
45 0d6fb46a 2022-01-09 op * the expense of O(n) removal for arbitrary elements. New elements can be
46 0d6fb46a 2022-01-09 op * added to the list after an existing element or at the head of the list.
47 0d6fb46a 2022-01-09 op * Elements being removed from the head of the list should use the explicit
48 0d6fb46a 2022-01-09 op * macro for this purpose for optimum efficiency. A singly-linked list may
49 0d6fb46a 2022-01-09 op * only be traversed in the forward direction. Singly-linked lists are ideal
50 0d6fb46a 2022-01-09 op * for applications with large datasets and few or no removals or for
51 0d6fb46a 2022-01-09 op * implementing a LIFO queue.
53 0d6fb46a 2022-01-09 op * A list is headed by a single forward pointer (or an array of forward
54 0d6fb46a 2022-01-09 op * pointers for a hash table header). The elements are doubly linked
55 0d6fb46a 2022-01-09 op * so that an arbitrary element can be removed without a need to
56 0d6fb46a 2022-01-09 op * traverse the list. New elements can be added to the list before
57 0d6fb46a 2022-01-09 op * or after an existing element or at the head of the list. A list
58 0d6fb46a 2022-01-09 op * may only be traversed in the forward direction.
60 0d6fb46a 2022-01-09 op * A simple queue is headed by a pair of pointers, one to the head of the
61 0d6fb46a 2022-01-09 op * list and the other to the tail of the list. The elements are singly
62 0d6fb46a 2022-01-09 op * linked to save space, so elements can only be removed from the
63 0d6fb46a 2022-01-09 op * head of the list. New elements can be added to the list before or after
64 0d6fb46a 2022-01-09 op * an existing element, at the head of the list, or at the end of the
65 0d6fb46a 2022-01-09 op * list. A simple queue may only be traversed in the forward direction.
67 0d6fb46a 2022-01-09 op * A tail queue is headed by a pair of pointers, one to the head of the
68 0d6fb46a 2022-01-09 op * list and the other to the tail of the list. The elements are doubly
69 0d6fb46a 2022-01-09 op * linked so that an arbitrary element can be removed without a need to
70 0d6fb46a 2022-01-09 op * traverse the list. New elements can be added to the list before or
71 0d6fb46a 2022-01-09 op * after an existing element, at the head of the list, or at the end of
72 0d6fb46a 2022-01-09 op * the list. A tail queue may be traversed in either direction.
74 0d6fb46a 2022-01-09 op * An XOR simple queue is used in the same way as a regular simple queue.
75 0d6fb46a 2022-01-09 op * The difference is that the head structure also includes a "cookie" that
76 0d6fb46a 2022-01-09 op * is XOR'd with the queue pointer (first, last or next) to generate the
77 0d6fb46a 2022-01-09 op * real pointer value.
79 0d6fb46a 2022-01-09 op * For details on the use of these macros, see the queue(3) manual page.
82 0d6fb46a 2022-01-09 op #if defined(QUEUE_MACRO_DEBUG) || (defined(_KERNEL) && defined(DIAGNOSTIC))
83 0d6fb46a 2022-01-09 op #define _Q_INVALID ((void *)-1)
84 0d6fb46a 2022-01-09 op #define _Q_INVALIDATE(a) (a) = _Q_INVALID
86 0d6fb46a 2022-01-09 op #define _Q_INVALIDATE(a)
90 0d6fb46a 2022-01-09 op * Singly-linked List definitions.
92 0d6fb46a 2022-01-09 op #define SLIST_HEAD(name, type) \
93 0d6fb46a 2022-01-09 op struct name { \
94 0d6fb46a 2022-01-09 op struct type *slh_first; /* first element */ \
97 0d6fb46a 2022-01-09 op #define SLIST_HEAD_INITIALIZER(head) \
100 0d6fb46a 2022-01-09 op #define SLIST_ENTRY(type) \
102 0d6fb46a 2022-01-09 op struct type *sle_next; /* next element */ \
106 0d6fb46a 2022-01-09 op * Singly-linked List access methods.
108 0d6fb46a 2022-01-09 op #define SLIST_FIRST(head) ((head)->slh_first)
109 0d6fb46a 2022-01-09 op #define SLIST_END(head) NULL
110 0d6fb46a 2022-01-09 op #define SLIST_EMPTY(head) (SLIST_FIRST(head) == SLIST_END(head))
111 0d6fb46a 2022-01-09 op #define SLIST_NEXT(elm, field) ((elm)->field.sle_next)
113 0d6fb46a 2022-01-09 op #define SLIST_FOREACH(var, head, field) \
114 0d6fb46a 2022-01-09 op for((var) = SLIST_FIRST(head); \
115 0d6fb46a 2022-01-09 op (var) != SLIST_END(head); \
116 0d6fb46a 2022-01-09 op (var) = SLIST_NEXT(var, field))
118 0d6fb46a 2022-01-09 op #define SLIST_FOREACH_SAFE(var, head, field, tvar) \
119 0d6fb46a 2022-01-09 op for ((var) = SLIST_FIRST(head); \
120 0d6fb46a 2022-01-09 op (var) && ((tvar) = SLIST_NEXT(var, field), 1); \
121 0d6fb46a 2022-01-09 op (var) = (tvar))
124 0d6fb46a 2022-01-09 op * Singly-linked List functions.
126 0d6fb46a 2022-01-09 op #define SLIST_INIT(head) { \
127 0d6fb46a 2022-01-09 op SLIST_FIRST(head) = SLIST_END(head); \
130 0d6fb46a 2022-01-09 op #define SLIST_INSERT_AFTER(slistelm, elm, field) do { \
131 0d6fb46a 2022-01-09 op (elm)->field.sle_next = (slistelm)->field.sle_next; \
132 0d6fb46a 2022-01-09 op (slistelm)->field.sle_next = (elm); \
135 0d6fb46a 2022-01-09 op #define SLIST_INSERT_HEAD(head, elm, field) do { \
136 0d6fb46a 2022-01-09 op (elm)->field.sle_next = (head)->slh_first; \
137 0d6fb46a 2022-01-09 op (head)->slh_first = (elm); \
140 0d6fb46a 2022-01-09 op #define SLIST_REMOVE_AFTER(elm, field) do { \
141 0d6fb46a 2022-01-09 op (elm)->field.sle_next = (elm)->field.sle_next->field.sle_next; \
144 0d6fb46a 2022-01-09 op #define SLIST_REMOVE_HEAD(head, field) do { \
145 0d6fb46a 2022-01-09 op (head)->slh_first = (head)->slh_first->field.sle_next; \
148 0d6fb46a 2022-01-09 op #define SLIST_REMOVE(head, elm, type, field) do { \
149 0d6fb46a 2022-01-09 op if ((head)->slh_first == (elm)) { \
150 0d6fb46a 2022-01-09 op SLIST_REMOVE_HEAD((head), field); \
152 0d6fb46a 2022-01-09 op struct type *curelm = (head)->slh_first; \
154 0d6fb46a 2022-01-09 op while (curelm->field.sle_next != (elm)) \
155 0d6fb46a 2022-01-09 op curelm = curelm->field.sle_next; \
156 0d6fb46a 2022-01-09 op curelm->field.sle_next = \
157 0d6fb46a 2022-01-09 op curelm->field.sle_next->field.sle_next; \
159 0d6fb46a 2022-01-09 op _Q_INVALIDATE((elm)->field.sle_next); \
163 0d6fb46a 2022-01-09 op * List definitions.
165 0d6fb46a 2022-01-09 op #define LIST_HEAD(name, type) \
166 0d6fb46a 2022-01-09 op struct name { \
167 0d6fb46a 2022-01-09 op struct type *lh_first; /* first element */ \
170 0d6fb46a 2022-01-09 op #define LIST_HEAD_INITIALIZER(head) \
173 0d6fb46a 2022-01-09 op #define LIST_ENTRY(type) \
175 0d6fb46a 2022-01-09 op struct type *le_next; /* next element */ \
176 0d6fb46a 2022-01-09 op struct type **le_prev; /* address of previous next element */ \
180 0d6fb46a 2022-01-09 op * List access methods.
182 0d6fb46a 2022-01-09 op #define LIST_FIRST(head) ((head)->lh_first)
183 0d6fb46a 2022-01-09 op #define LIST_END(head) NULL
184 0d6fb46a 2022-01-09 op #define LIST_EMPTY(head) (LIST_FIRST(head) == LIST_END(head))
185 0d6fb46a 2022-01-09 op #define LIST_NEXT(elm, field) ((elm)->field.le_next)
187 0d6fb46a 2022-01-09 op #define LIST_FOREACH(var, head, field) \
188 0d6fb46a 2022-01-09 op for((var) = LIST_FIRST(head); \
189 0d6fb46a 2022-01-09 op (var)!= LIST_END(head); \
190 0d6fb46a 2022-01-09 op (var) = LIST_NEXT(var, field))
192 0d6fb46a 2022-01-09 op #define LIST_FOREACH_SAFE(var, head, field, tvar) \
193 0d6fb46a 2022-01-09 op for ((var) = LIST_FIRST(head); \
194 0d6fb46a 2022-01-09 op (var) && ((tvar) = LIST_NEXT(var, field), 1); \
195 0d6fb46a 2022-01-09 op (var) = (tvar))
198 0d6fb46a 2022-01-09 op * List functions.
200 0d6fb46a 2022-01-09 op #define LIST_INIT(head) do { \
201 0d6fb46a 2022-01-09 op LIST_FIRST(head) = LIST_END(head); \
204 0d6fb46a 2022-01-09 op #define LIST_INSERT_AFTER(listelm, elm, field) do { \
205 0d6fb46a 2022-01-09 op if (((elm)->field.le_next = (listelm)->field.le_next) != NULL) \
206 0d6fb46a 2022-01-09 op (listelm)->field.le_next->field.le_prev = \
207 0d6fb46a 2022-01-09 op &(elm)->field.le_next; \
208 0d6fb46a 2022-01-09 op (listelm)->field.le_next = (elm); \
209 0d6fb46a 2022-01-09 op (elm)->field.le_prev = &(listelm)->field.le_next; \
212 0d6fb46a 2022-01-09 op #define LIST_INSERT_BEFORE(listelm, elm, field) do { \
213 0d6fb46a 2022-01-09 op (elm)->field.le_prev = (listelm)->field.le_prev; \
214 0d6fb46a 2022-01-09 op (elm)->field.le_next = (listelm); \
215 0d6fb46a 2022-01-09 op *(listelm)->field.le_prev = (elm); \
216 0d6fb46a 2022-01-09 op (listelm)->field.le_prev = &(elm)->field.le_next; \
219 0d6fb46a 2022-01-09 op #define LIST_INSERT_HEAD(head, elm, field) do { \
220 0d6fb46a 2022-01-09 op if (((elm)->field.le_next = (head)->lh_first) != NULL) \
221 0d6fb46a 2022-01-09 op (head)->lh_first->field.le_prev = &(elm)->field.le_next;\
222 0d6fb46a 2022-01-09 op (head)->lh_first = (elm); \
223 0d6fb46a 2022-01-09 op (elm)->field.le_prev = &(head)->lh_first; \
226 0d6fb46a 2022-01-09 op #define LIST_REMOVE(elm, field) do { \
227 0d6fb46a 2022-01-09 op if ((elm)->field.le_next != NULL) \
228 0d6fb46a 2022-01-09 op (elm)->field.le_next->field.le_prev = \
229 0d6fb46a 2022-01-09 op (elm)->field.le_prev; \
230 0d6fb46a 2022-01-09 op *(elm)->field.le_prev = (elm)->field.le_next; \
231 0d6fb46a 2022-01-09 op _Q_INVALIDATE((elm)->field.le_prev); \
232 0d6fb46a 2022-01-09 op _Q_INVALIDATE((elm)->field.le_next); \
235 0d6fb46a 2022-01-09 op #define LIST_REPLACE(elm, elm2, field) do { \
236 0d6fb46a 2022-01-09 op if (((elm2)->field.le_next = (elm)->field.le_next) != NULL) \
237 0d6fb46a 2022-01-09 op (elm2)->field.le_next->field.le_prev = \
238 0d6fb46a 2022-01-09 op &(elm2)->field.le_next; \
239 0d6fb46a 2022-01-09 op (elm2)->field.le_prev = (elm)->field.le_prev; \
240 0d6fb46a 2022-01-09 op *(elm2)->field.le_prev = (elm2); \
241 0d6fb46a 2022-01-09 op _Q_INVALIDATE((elm)->field.le_prev); \
242 0d6fb46a 2022-01-09 op _Q_INVALIDATE((elm)->field.le_next); \
246 0d6fb46a 2022-01-09 op * Simple queue definitions.
248 0d6fb46a 2022-01-09 op #define SIMPLEQ_HEAD(name, type) \
249 0d6fb46a 2022-01-09 op struct name { \
250 0d6fb46a 2022-01-09 op struct type *sqh_first; /* first element */ \
251 0d6fb46a 2022-01-09 op struct type **sqh_last; /* addr of last next element */ \
254 0d6fb46a 2022-01-09 op #define SIMPLEQ_HEAD_INITIALIZER(head) \
255 0d6fb46a 2022-01-09 op { NULL, &(head).sqh_first }
257 0d6fb46a 2022-01-09 op #define SIMPLEQ_ENTRY(type) \
259 0d6fb46a 2022-01-09 op struct type *sqe_next; /* next element */ \
263 0d6fb46a 2022-01-09 op * Simple queue access methods.
265 0d6fb46a 2022-01-09 op #define SIMPLEQ_FIRST(head) ((head)->sqh_first)
266 0d6fb46a 2022-01-09 op #define SIMPLEQ_END(head) NULL
267 0d6fb46a 2022-01-09 op #define SIMPLEQ_EMPTY(head) (SIMPLEQ_FIRST(head) == SIMPLEQ_END(head))
268 0d6fb46a 2022-01-09 op #define SIMPLEQ_NEXT(elm, field) ((elm)->field.sqe_next)
270 0d6fb46a 2022-01-09 op #define SIMPLEQ_FOREACH(var, head, field) \
271 0d6fb46a 2022-01-09 op for((var) = SIMPLEQ_FIRST(head); \
272 0d6fb46a 2022-01-09 op (var) != SIMPLEQ_END(head); \
273 0d6fb46a 2022-01-09 op (var) = SIMPLEQ_NEXT(var, field))
275 0d6fb46a 2022-01-09 op #define SIMPLEQ_FOREACH_SAFE(var, head, field, tvar) \
276 0d6fb46a 2022-01-09 op for ((var) = SIMPLEQ_FIRST(head); \
277 0d6fb46a 2022-01-09 op (var) && ((tvar) = SIMPLEQ_NEXT(var, field), 1); \
278 0d6fb46a 2022-01-09 op (var) = (tvar))
281 0d6fb46a 2022-01-09 op * Simple queue functions.
283 0d6fb46a 2022-01-09 op #define SIMPLEQ_INIT(head) do { \
284 0d6fb46a 2022-01-09 op (head)->sqh_first = NULL; \
285 0d6fb46a 2022-01-09 op (head)->sqh_last = &(head)->sqh_first; \
288 0d6fb46a 2022-01-09 op #define SIMPLEQ_INSERT_HEAD(head, elm, field) do { \
289 0d6fb46a 2022-01-09 op if (((elm)->field.sqe_next = (head)->sqh_first) == NULL) \
290 0d6fb46a 2022-01-09 op (head)->sqh_last = &(elm)->field.sqe_next; \
291 0d6fb46a 2022-01-09 op (head)->sqh_first = (elm); \
294 0d6fb46a 2022-01-09 op #define SIMPLEQ_INSERT_TAIL(head, elm, field) do { \
295 0d6fb46a 2022-01-09 op (elm)->field.sqe_next = NULL; \
296 0d6fb46a 2022-01-09 op *(head)->sqh_last = (elm); \
297 0d6fb46a 2022-01-09 op (head)->sqh_last = &(elm)->field.sqe_next; \
300 0d6fb46a 2022-01-09 op #define SIMPLEQ_INSERT_AFTER(head, listelm, elm, field) do { \
301 0d6fb46a 2022-01-09 op if (((elm)->field.sqe_next = (listelm)->field.sqe_next) == NULL)\
302 0d6fb46a 2022-01-09 op (head)->sqh_last = &(elm)->field.sqe_next; \
303 0d6fb46a 2022-01-09 op (listelm)->field.sqe_next = (elm); \
306 0d6fb46a 2022-01-09 op #define SIMPLEQ_REMOVE_HEAD(head, field) do { \
307 0d6fb46a 2022-01-09 op if (((head)->sqh_first = (head)->sqh_first->field.sqe_next) == NULL) \
308 0d6fb46a 2022-01-09 op (head)->sqh_last = &(head)->sqh_first; \
311 0d6fb46a 2022-01-09 op #define SIMPLEQ_REMOVE_AFTER(head, elm, field) do { \
312 0d6fb46a 2022-01-09 op if (((elm)->field.sqe_next = (elm)->field.sqe_next->field.sqe_next) \
314 0d6fb46a 2022-01-09 op (head)->sqh_last = &(elm)->field.sqe_next; \
317 0d6fb46a 2022-01-09 op #define SIMPLEQ_CONCAT(head1, head2) do { \
318 0d6fb46a 2022-01-09 op if (!SIMPLEQ_EMPTY((head2))) { \
319 0d6fb46a 2022-01-09 op *(head1)->sqh_last = (head2)->sqh_first; \
320 0d6fb46a 2022-01-09 op (head1)->sqh_last = (head2)->sqh_last; \
321 0d6fb46a 2022-01-09 op SIMPLEQ_INIT((head2)); \
326 0d6fb46a 2022-01-09 op * XOR Simple queue definitions.
328 0d6fb46a 2022-01-09 op #define XSIMPLEQ_HEAD(name, type) \
329 0d6fb46a 2022-01-09 op struct name { \
330 0d6fb46a 2022-01-09 op struct type *sqx_first; /* first element */ \
331 0d6fb46a 2022-01-09 op struct type **sqx_last; /* addr of last next element */ \
332 0d6fb46a 2022-01-09 op unsigned long sqx_cookie; \
335 0d6fb46a 2022-01-09 op #define XSIMPLEQ_ENTRY(type) \
337 0d6fb46a 2022-01-09 op struct type *sqx_next; /* next element */ \
341 0d6fb46a 2022-01-09 op * XOR Simple queue access methods.
343 0d6fb46a 2022-01-09 op #define XSIMPLEQ_XOR(head, ptr) ((__typeof(ptr))((head)->sqx_cookie ^ \
344 0d6fb46a 2022-01-09 op (unsigned long)(ptr)))
345 0d6fb46a 2022-01-09 op #define XSIMPLEQ_FIRST(head) XSIMPLEQ_XOR(head, ((head)->sqx_first))
346 0d6fb46a 2022-01-09 op #define XSIMPLEQ_END(head) NULL
347 0d6fb46a 2022-01-09 op #define XSIMPLEQ_EMPTY(head) (XSIMPLEQ_FIRST(head) == XSIMPLEQ_END(head))
348 0d6fb46a 2022-01-09 op #define XSIMPLEQ_NEXT(head, elm, field) XSIMPLEQ_XOR(head, ((elm)->field.sqx_next))
351 0d6fb46a 2022-01-09 op #define XSIMPLEQ_FOREACH(var, head, field) \
352 0d6fb46a 2022-01-09 op for ((var) = XSIMPLEQ_FIRST(head); \
353 0d6fb46a 2022-01-09 op (var) != XSIMPLEQ_END(head); \
354 0d6fb46a 2022-01-09 op (var) = XSIMPLEQ_NEXT(head, var, field))
356 0d6fb46a 2022-01-09 op #define XSIMPLEQ_FOREACH_SAFE(var, head, field, tvar) \
357 0d6fb46a 2022-01-09 op for ((var) = XSIMPLEQ_FIRST(head); \
358 0d6fb46a 2022-01-09 op (var) && ((tvar) = XSIMPLEQ_NEXT(head, var, field), 1); \
359 0d6fb46a 2022-01-09 op (var) = (tvar))
362 0d6fb46a 2022-01-09 op * XOR Simple queue functions.
364 0d6fb46a 2022-01-09 op #define XSIMPLEQ_INIT(head) do { \
365 0d6fb46a 2022-01-09 op arc4random_buf(&(head)->sqx_cookie, sizeof((head)->sqx_cookie)); \
366 0d6fb46a 2022-01-09 op (head)->sqx_first = XSIMPLEQ_XOR(head, NULL); \
367 0d6fb46a 2022-01-09 op (head)->sqx_last = XSIMPLEQ_XOR(head, &(head)->sqx_first); \
370 0d6fb46a 2022-01-09 op #define XSIMPLEQ_INSERT_HEAD(head, elm, field) do { \
371 0d6fb46a 2022-01-09 op if (((elm)->field.sqx_next = (head)->sqx_first) == \
372 0d6fb46a 2022-01-09 op XSIMPLEQ_XOR(head, NULL)) \
373 0d6fb46a 2022-01-09 op (head)->sqx_last = XSIMPLEQ_XOR(head, &(elm)->field.sqx_next); \
374 0d6fb46a 2022-01-09 op (head)->sqx_first = XSIMPLEQ_XOR(head, (elm)); \
377 0d6fb46a 2022-01-09 op #define XSIMPLEQ_INSERT_TAIL(head, elm, field) do { \
378 0d6fb46a 2022-01-09 op (elm)->field.sqx_next = XSIMPLEQ_XOR(head, NULL); \
379 0d6fb46a 2022-01-09 op *(XSIMPLEQ_XOR(head, (head)->sqx_last)) = XSIMPLEQ_XOR(head, (elm)); \
380 0d6fb46a 2022-01-09 op (head)->sqx_last = XSIMPLEQ_XOR(head, &(elm)->field.sqx_next); \
383 0d6fb46a 2022-01-09 op #define XSIMPLEQ_INSERT_AFTER(head, listelm, elm, field) do { \
384 0d6fb46a 2022-01-09 op if (((elm)->field.sqx_next = (listelm)->field.sqx_next) == \
385 0d6fb46a 2022-01-09 op XSIMPLEQ_XOR(head, NULL)) \
386 0d6fb46a 2022-01-09 op (head)->sqx_last = XSIMPLEQ_XOR(head, &(elm)->field.sqx_next); \
387 0d6fb46a 2022-01-09 op (listelm)->field.sqx_next = XSIMPLEQ_XOR(head, (elm)); \
390 0d6fb46a 2022-01-09 op #define XSIMPLEQ_REMOVE_HEAD(head, field) do { \
391 0d6fb46a 2022-01-09 op if (((head)->sqx_first = XSIMPLEQ_XOR(head, \
392 0d6fb46a 2022-01-09 op (head)->sqx_first)->field.sqx_next) == XSIMPLEQ_XOR(head, NULL)) \
393 0d6fb46a 2022-01-09 op (head)->sqx_last = XSIMPLEQ_XOR(head, &(head)->sqx_first); \
396 0d6fb46a 2022-01-09 op #define XSIMPLEQ_REMOVE_AFTER(head, elm, field) do { \
397 0d6fb46a 2022-01-09 op if (((elm)->field.sqx_next = XSIMPLEQ_XOR(head, \
398 0d6fb46a 2022-01-09 op (elm)->field.sqx_next)->field.sqx_next) \
399 0d6fb46a 2022-01-09 op == XSIMPLEQ_XOR(head, NULL)) \
400 0d6fb46a 2022-01-09 op (head)->sqx_last = \
401 0d6fb46a 2022-01-09 op XSIMPLEQ_XOR(head, &(elm)->field.sqx_next); \
406 0d6fb46a 2022-01-09 op * Tail queue definitions.
408 0d6fb46a 2022-01-09 op #define TAILQ_HEAD(name, type) \
409 0d6fb46a 2022-01-09 op struct name { \
410 0d6fb46a 2022-01-09 op struct type *tqh_first; /* first element */ \
411 0d6fb46a 2022-01-09 op struct type **tqh_last; /* addr of last next element */ \
414 0d6fb46a 2022-01-09 op #define TAILQ_HEAD_INITIALIZER(head) \
415 0d6fb46a 2022-01-09 op { NULL, &(head).tqh_first }
417 0d6fb46a 2022-01-09 op #define TAILQ_ENTRY(type) \
419 0d6fb46a 2022-01-09 op struct type *tqe_next; /* next element */ \
420 0d6fb46a 2022-01-09 op struct type **tqe_prev; /* address of previous next element */ \
424 0d6fb46a 2022-01-09 op * Tail queue access methods.
426 0d6fb46a 2022-01-09 op #define TAILQ_FIRST(head) ((head)->tqh_first)
427 0d6fb46a 2022-01-09 op #define TAILQ_END(head) NULL
428 0d6fb46a 2022-01-09 op #define TAILQ_NEXT(elm, field) ((elm)->field.tqe_next)
429 0d6fb46a 2022-01-09 op #define TAILQ_LAST(head, headname) \
430 0d6fb46a 2022-01-09 op (*(((struct headname *)((head)->tqh_last))->tqh_last))
432 0d6fb46a 2022-01-09 op #define TAILQ_PREV(elm, headname, field) \
433 0d6fb46a 2022-01-09 op (*(((struct headname *)((elm)->field.tqe_prev))->tqh_last))
434 0d6fb46a 2022-01-09 op #define TAILQ_EMPTY(head) \
435 0d6fb46a 2022-01-09 op (TAILQ_FIRST(head) == TAILQ_END(head))
437 0d6fb46a 2022-01-09 op #define TAILQ_FOREACH(var, head, field) \
438 0d6fb46a 2022-01-09 op for((var) = TAILQ_FIRST(head); \
439 0d6fb46a 2022-01-09 op (var) != TAILQ_END(head); \
440 0d6fb46a 2022-01-09 op (var) = TAILQ_NEXT(var, field))
442 0d6fb46a 2022-01-09 op #define TAILQ_FOREACH_SAFE(var, head, field, tvar) \
443 0d6fb46a 2022-01-09 op for ((var) = TAILQ_FIRST(head); \
444 0d6fb46a 2022-01-09 op (var) != TAILQ_END(head) && \
445 0d6fb46a 2022-01-09 op ((tvar) = TAILQ_NEXT(var, field), 1); \
446 0d6fb46a 2022-01-09 op (var) = (tvar))
449 0d6fb46a 2022-01-09 op #define TAILQ_FOREACH_REVERSE(var, head, headname, field) \
450 0d6fb46a 2022-01-09 op for((var) = TAILQ_LAST(head, headname); \
451 0d6fb46a 2022-01-09 op (var) != TAILQ_END(head); \
452 0d6fb46a 2022-01-09 op (var) = TAILQ_PREV(var, headname, field))
454 0d6fb46a 2022-01-09 op #define TAILQ_FOREACH_REVERSE_SAFE(var, head, headname, field, tvar) \
455 0d6fb46a 2022-01-09 op for ((var) = TAILQ_LAST(head, headname); \
456 0d6fb46a 2022-01-09 op (var) != TAILQ_END(head) && \
457 0d6fb46a 2022-01-09 op ((tvar) = TAILQ_PREV(var, headname, field), 1); \
458 0d6fb46a 2022-01-09 op (var) = (tvar))
461 0d6fb46a 2022-01-09 op * Tail queue functions.
463 0d6fb46a 2022-01-09 op #define TAILQ_INIT(head) do { \
464 0d6fb46a 2022-01-09 op (head)->tqh_first = NULL; \
465 0d6fb46a 2022-01-09 op (head)->tqh_last = &(head)->tqh_first; \
468 0d6fb46a 2022-01-09 op #define TAILQ_INSERT_HEAD(head, elm, field) do { \
469 0d6fb46a 2022-01-09 op if (((elm)->field.tqe_next = (head)->tqh_first) != NULL) \
470 0d6fb46a 2022-01-09 op (head)->tqh_first->field.tqe_prev = \
471 0d6fb46a 2022-01-09 op &(elm)->field.tqe_next; \
473 0d6fb46a 2022-01-09 op (head)->tqh_last = &(elm)->field.tqe_next; \
474 0d6fb46a 2022-01-09 op (head)->tqh_first = (elm); \
475 0d6fb46a 2022-01-09 op (elm)->field.tqe_prev = &(head)->tqh_first; \
478 0d6fb46a 2022-01-09 op #define TAILQ_INSERT_TAIL(head, elm, field) do { \
479 0d6fb46a 2022-01-09 op (elm)->field.tqe_next = NULL; \
480 0d6fb46a 2022-01-09 op (elm)->field.tqe_prev = (head)->tqh_last; \
481 0d6fb46a 2022-01-09 op *(head)->tqh_last = (elm); \
482 0d6fb46a 2022-01-09 op (head)->tqh_last = &(elm)->field.tqe_next; \
485 0d6fb46a 2022-01-09 op #define TAILQ_INSERT_AFTER(head, listelm, elm, field) do { \
486 0d6fb46a 2022-01-09 op if (((elm)->field.tqe_next = (listelm)->field.tqe_next) != NULL)\
487 0d6fb46a 2022-01-09 op (elm)->field.tqe_next->field.tqe_prev = \
488 0d6fb46a 2022-01-09 op &(elm)->field.tqe_next; \
490 0d6fb46a 2022-01-09 op (head)->tqh_last = &(elm)->field.tqe_next; \
491 0d6fb46a 2022-01-09 op (listelm)->field.tqe_next = (elm); \
492 0d6fb46a 2022-01-09 op (elm)->field.tqe_prev = &(listelm)->field.tqe_next; \
495 0d6fb46a 2022-01-09 op #define TAILQ_INSERT_BEFORE(listelm, elm, field) do { \
496 0d6fb46a 2022-01-09 op (elm)->field.tqe_prev = (listelm)->field.tqe_prev; \
497 0d6fb46a 2022-01-09 op (elm)->field.tqe_next = (listelm); \
498 0d6fb46a 2022-01-09 op *(listelm)->field.tqe_prev = (elm); \
499 0d6fb46a 2022-01-09 op (listelm)->field.tqe_prev = &(elm)->field.tqe_next; \
502 0d6fb46a 2022-01-09 op #define TAILQ_REMOVE(head, elm, field) do { \
503 0d6fb46a 2022-01-09 op if (((elm)->field.tqe_next) != NULL) \
504 0d6fb46a 2022-01-09 op (elm)->field.tqe_next->field.tqe_prev = \
505 0d6fb46a 2022-01-09 op (elm)->field.tqe_prev; \
507 0d6fb46a 2022-01-09 op (head)->tqh_last = (elm)->field.tqe_prev; \
508 0d6fb46a 2022-01-09 op *(elm)->field.tqe_prev = (elm)->field.tqe_next; \
509 0d6fb46a 2022-01-09 op _Q_INVALIDATE((elm)->field.tqe_prev); \
510 0d6fb46a 2022-01-09 op _Q_INVALIDATE((elm)->field.tqe_next); \
513 0d6fb46a 2022-01-09 op #define TAILQ_REPLACE(head, elm, elm2, field) do { \
514 0d6fb46a 2022-01-09 op if (((elm2)->field.tqe_next = (elm)->field.tqe_next) != NULL) \
515 0d6fb46a 2022-01-09 op (elm2)->field.tqe_next->field.tqe_prev = \
516 0d6fb46a 2022-01-09 op &(elm2)->field.tqe_next; \
518 0d6fb46a 2022-01-09 op (head)->tqh_last = &(elm2)->field.tqe_next; \
519 0d6fb46a 2022-01-09 op (elm2)->field.tqe_prev = (elm)->field.tqe_prev; \
520 0d6fb46a 2022-01-09 op *(elm2)->field.tqe_prev = (elm2); \
521 0d6fb46a 2022-01-09 op _Q_INVALIDATE((elm)->field.tqe_prev); \
522 0d6fb46a 2022-01-09 op _Q_INVALIDATE((elm)->field.tqe_next); \
525 0d6fb46a 2022-01-09 op #define TAILQ_CONCAT(head1, head2, field) do { \
526 0d6fb46a 2022-01-09 op if (!TAILQ_EMPTY(head2)) { \
527 0d6fb46a 2022-01-09 op *(head1)->tqh_last = (head2)->tqh_first; \
528 0d6fb46a 2022-01-09 op (head2)->tqh_first->field.tqe_prev = (head1)->tqh_last; \
529 0d6fb46a 2022-01-09 op (head1)->tqh_last = (head2)->tqh_last; \
530 0d6fb46a 2022-01-09 op TAILQ_INIT((head2)); \
535 0d6fb46a 2022-01-09 op * Singly-linked Tail queue declarations.
537 0d6fb46a 2022-01-09 op #define STAILQ_HEAD(name, type) \
538 0d6fb46a 2022-01-09 op struct name { \
539 0d6fb46a 2022-01-09 op struct type *stqh_first; /* first element */ \
540 0d6fb46a 2022-01-09 op struct type **stqh_last; /* addr of last next element */ \
543 0d6fb46a 2022-01-09 op #define STAILQ_HEAD_INITIALIZER(head) \
544 0d6fb46a 2022-01-09 op { NULL, &(head).stqh_first }
546 0d6fb46a 2022-01-09 op #define STAILQ_ENTRY(type) \
548 0d6fb46a 2022-01-09 op struct type *stqe_next; /* next element */ \
552 0d6fb46a 2022-01-09 op * Singly-linked Tail queue access methods.
554 0d6fb46a 2022-01-09 op #define STAILQ_FIRST(head) ((head)->stqh_first)
555 0d6fb46a 2022-01-09 op #define STAILQ_END(head) NULL
556 0d6fb46a 2022-01-09 op #define STAILQ_EMPTY(head) (STAILQ_FIRST(head) == STAILQ_END(head))
557 0d6fb46a 2022-01-09 op #define STAILQ_NEXT(elm, field) ((elm)->field.stqe_next)
559 0d6fb46a 2022-01-09 op #define STAILQ_FOREACH(var, head, field) \
560 0d6fb46a 2022-01-09 op for ((var) = STAILQ_FIRST(head); \
561 0d6fb46a 2022-01-09 op (var) != STAILQ_END(head); \
562 0d6fb46a 2022-01-09 op (var) = STAILQ_NEXT(var, field))
564 0d6fb46a 2022-01-09 op #define STAILQ_FOREACH_SAFE(var, head, field, tvar) \
565 0d6fb46a 2022-01-09 op for ((var) = STAILQ_FIRST(head); \
566 0d6fb46a 2022-01-09 op (var) && ((tvar) = STAILQ_NEXT(var, field), 1); \
567 0d6fb46a 2022-01-09 op (var) = (tvar))
570 0d6fb46a 2022-01-09 op * Singly-linked Tail queue functions.
572 0d6fb46a 2022-01-09 op #define STAILQ_INIT(head) do { \
573 0d6fb46a 2022-01-09 op STAILQ_FIRST((head)) = NULL; \
574 0d6fb46a 2022-01-09 op (head)->stqh_last = &STAILQ_FIRST((head)); \
577 0d6fb46a 2022-01-09 op #define STAILQ_INSERT_HEAD(head, elm, field) do { \
578 0d6fb46a 2022-01-09 op if ((STAILQ_NEXT((elm), field) = STAILQ_FIRST((head))) == NULL) \
579 0d6fb46a 2022-01-09 op (head)->stqh_last = &STAILQ_NEXT((elm), field); \
580 0d6fb46a 2022-01-09 op STAILQ_FIRST((head)) = (elm); \
583 0d6fb46a 2022-01-09 op #define STAILQ_INSERT_TAIL(head, elm, field) do { \
584 0d6fb46a 2022-01-09 op STAILQ_NEXT((elm), field) = NULL; \
585 0d6fb46a 2022-01-09 op *(head)->stqh_last = (elm); \
586 0d6fb46a 2022-01-09 op (head)->stqh_last = &STAILQ_NEXT((elm), field); \
589 0d6fb46a 2022-01-09 op #define STAILQ_INSERT_AFTER(head, listelm, elm, field) do { \
590 0d6fb46a 2022-01-09 op if ((STAILQ_NEXT((elm), field) = STAILQ_NEXT((elm), field)) == NULL)\
591 0d6fb46a 2022-01-09 op (head)->stqh_last = &STAILQ_NEXT((elm), field); \
592 0d6fb46a 2022-01-09 op STAILQ_NEXT((elm), field) = (elm); \
595 0d6fb46a 2022-01-09 op #define STAILQ_REMOVE_HEAD(head, field) do { \
596 0d6fb46a 2022-01-09 op if ((STAILQ_FIRST((head)) = \
597 0d6fb46a 2022-01-09 op STAILQ_NEXT(STAILQ_FIRST((head)), field)) == NULL) \
598 0d6fb46a 2022-01-09 op (head)->stqh_last = &STAILQ_FIRST((head)); \
601 0d6fb46a 2022-01-09 op #define STAILQ_REMOVE_AFTER(head, elm, field) do { \
602 0d6fb46a 2022-01-09 op if ((STAILQ_NEXT(elm, field) = \
603 0d6fb46a 2022-01-09 op STAILQ_NEXT(STAILQ_NEXT(elm, field), field)) == NULL) \
604 0d6fb46a 2022-01-09 op (head)->stqh_last = &STAILQ_NEXT((elm), field); \
607 0d6fb46a 2022-01-09 op #define STAILQ_REMOVE(head, elm, type, field) do { \
608 0d6fb46a 2022-01-09 op if (STAILQ_FIRST((head)) == (elm)) { \
609 0d6fb46a 2022-01-09 op STAILQ_REMOVE_HEAD((head), field); \
611 0d6fb46a 2022-01-09 op struct type *curelm = (head)->stqh_first; \
612 0d6fb46a 2022-01-09 op while (STAILQ_NEXT(curelm, field) != (elm)) \
613 0d6fb46a 2022-01-09 op curelm = STAILQ_NEXT(curelm, field); \
614 0d6fb46a 2022-01-09 op STAILQ_REMOVE_AFTER(head, curelm, field); \
618 0d6fb46a 2022-01-09 op #define STAILQ_CONCAT(head1, head2) do { \
619 0d6fb46a 2022-01-09 op if (!STAILQ_EMPTY((head2))) { \
620 0d6fb46a 2022-01-09 op *(head1)->stqh_last = (head2)->stqh_first; \
621 0d6fb46a 2022-01-09 op (head1)->stqh_last = (head2)->stqh_last; \
622 0d6fb46a 2022-01-09 op STAILQ_INIT((head2)); \
626 0d6fb46a 2022-01-09 op #define STAILQ_LAST(head, type, field) \
627 0d6fb46a 2022-01-09 op (STAILQ_EMPTY((head)) ? NULL : \
628 0d6fb46a 2022-01-09 op ((struct type *)(void *) \
629 0d6fb46a 2022-01-09 op ((char *)((head)->stqh_last) - offsetof(struct type, field))))
631 0d6fb46a 2022-01-09 op #endif /* !_SYS_QUEUE_H_ */