xref: /xnu-11417.140.69/bsd/sys/queue.h (revision 43a90889846e00bfb5cf1d255cdc0a701a1e05a4)
1*43a90889SApple OSS Distributions /*
2*43a90889SApple OSS Distributions  * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3*43a90889SApple OSS Distributions  *
4*43a90889SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*43a90889SApple OSS Distributions  *
6*43a90889SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*43a90889SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*43a90889SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*43a90889SApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*43a90889SApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*43a90889SApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*43a90889SApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*43a90889SApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*43a90889SApple OSS Distributions  *
15*43a90889SApple OSS Distributions  * Please obtain a copy of the License at
16*43a90889SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*43a90889SApple OSS Distributions  *
18*43a90889SApple OSS Distributions  * The Original Code and all software distributed under the License are
19*43a90889SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*43a90889SApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*43a90889SApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*43a90889SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*43a90889SApple OSS Distributions  * Please see the License for the specific language governing rights and
24*43a90889SApple OSS Distributions  * limitations under the License.
25*43a90889SApple OSS Distributions  *
26*43a90889SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*43a90889SApple OSS Distributions  */
28*43a90889SApple OSS Distributions /*-
29*43a90889SApple OSS Distributions  * Copyright (c) 1991, 1993
30*43a90889SApple OSS Distributions  *	The Regents of the University of California.  All rights reserved.
31*43a90889SApple OSS Distributions  *
32*43a90889SApple OSS Distributions  * Redistribution and use in source and binary forms, with or without
33*43a90889SApple OSS Distributions  * modification, are permitted provided that the following conditions
34*43a90889SApple OSS Distributions  * are met:
35*43a90889SApple OSS Distributions  * 1. Redistributions of source code must retain the above copyright
36*43a90889SApple OSS Distributions  *    notice, this list of conditions and the following disclaimer.
37*43a90889SApple OSS Distributions  * 2. Redistributions in binary form must reproduce the above copyright
38*43a90889SApple OSS Distributions  *    notice, this list of conditions and the following disclaimer in the
39*43a90889SApple OSS Distributions  *    documentation and/or other materials provided with the distribution.
40*43a90889SApple OSS Distributions  * 4. Neither the name of the University nor the names of its contributors
41*43a90889SApple OSS Distributions  *    may be used to endorse or promote products derived from this software
42*43a90889SApple OSS Distributions  *    without specific prior written permission.
43*43a90889SApple OSS Distributions  *
44*43a90889SApple OSS Distributions  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
45*43a90889SApple OSS Distributions  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46*43a90889SApple OSS Distributions  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
47*43a90889SApple OSS Distributions  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
48*43a90889SApple OSS Distributions  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
49*43a90889SApple OSS Distributions  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
50*43a90889SApple OSS Distributions  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
51*43a90889SApple OSS Distributions  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
52*43a90889SApple OSS Distributions  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
53*43a90889SApple OSS Distributions  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
54*43a90889SApple OSS Distributions  * SUCH DAMAGE.
55*43a90889SApple OSS Distributions  *
56*43a90889SApple OSS Distributions  *	@(#)queue.h	8.5 (Berkeley) 8/20/94
57*43a90889SApple OSS Distributions  */
58*43a90889SApple OSS Distributions 
59*43a90889SApple OSS Distributions #ifndef _SYS_QUEUE_H_
60*43a90889SApple OSS Distributions #define _SYS_QUEUE_H_
61*43a90889SApple OSS Distributions 
62*43a90889SApple OSS Distributions #ifdef KERNEL_PRIVATE
63*43a90889SApple OSS Distributions #include <kern/debug.h> /* panic function call */
64*43a90889SApple OSS Distributions #include <machine/trap.h>
65*43a90889SApple OSS Distributions #include <sys/cdefs.h>  /* __improbable in kernelspace */
66*43a90889SApple OSS Distributions #else
67*43a90889SApple OSS Distributions #ifndef __improbable
68*43a90889SApple OSS Distributions #define __improbable(x) (x)             /* noop in userspace */
69*43a90889SApple OSS Distributions #endif /* __improbable */
70*43a90889SApple OSS Distributions #endif /* KERNEL_PRIVATE */
71*43a90889SApple OSS Distributions 
72*43a90889SApple OSS Distributions /*
73*43a90889SApple OSS Distributions  * This file defines five types of data structures: singly-linked lists,
74*43a90889SApple OSS Distributions  * singly-linked tail queues, lists, tail queues, and circular queues.
75*43a90889SApple OSS Distributions  *
76*43a90889SApple OSS Distributions  * A singly-linked list is headed by a single forward pointer. The elements
77*43a90889SApple OSS Distributions  * are singly linked for minimum space and pointer manipulation overhead at
78*43a90889SApple OSS Distributions  * the expense of O(n) removal for arbitrary elements. New elements can be
79*43a90889SApple OSS Distributions  * added to the list after an existing element or at the head of the list.
80*43a90889SApple OSS Distributions  * Elements being removed from the head of the list should use the explicit
81*43a90889SApple OSS Distributions  * macro for this purpose for optimum efficiency. A singly-linked list may
82*43a90889SApple OSS Distributions  * only be traversed in the forward direction.  Singly-linked lists are ideal
83*43a90889SApple OSS Distributions  * for applications with large datasets and few or no removals or for
84*43a90889SApple OSS Distributions  * implementing a LIFO queue.
85*43a90889SApple OSS Distributions  *
86*43a90889SApple OSS Distributions  * A singly-linked tail queue is headed by a pair of pointers, one to the
87*43a90889SApple OSS Distributions  * head of the list and the other to the tail of the list. The elements are
88*43a90889SApple OSS Distributions  * singly linked for minimum space and pointer manipulation overhead at the
89*43a90889SApple OSS Distributions  * expense of O(n) removal for arbitrary elements. New elements can be added
90*43a90889SApple OSS Distributions  * to the list after an existing element, at the head of the list, or at the
91*43a90889SApple OSS Distributions  * end of the list. Elements being removed from the head of the tail queue
92*43a90889SApple OSS Distributions  * should use the explicit macro for this purpose for optimum efficiency.
93*43a90889SApple OSS Distributions  * A singly-linked tail queue may only be traversed in the forward direction.
94*43a90889SApple OSS Distributions  * Singly-linked tail queues are ideal for applications with large datasets
95*43a90889SApple OSS Distributions  * and few or no removals or for implementing a FIFO queue.
96*43a90889SApple OSS Distributions  *
97*43a90889SApple OSS Distributions  * A list is headed by a single forward pointer (or an array of forward
98*43a90889SApple OSS Distributions  * pointers for a hash table header). The elements are doubly linked
99*43a90889SApple OSS Distributions  * so that an arbitrary element can be removed without a need to
100*43a90889SApple OSS Distributions  * traverse the list. New elements can be added to the list before
101*43a90889SApple OSS Distributions  * or after an existing element or at the head of the list. A list
102*43a90889SApple OSS Distributions  * may only be traversed in the forward direction.
103*43a90889SApple OSS Distributions  *
104*43a90889SApple OSS Distributions  * A tail queue is headed by a pair of pointers, one to the head of the
105*43a90889SApple OSS Distributions  * list and the other to the tail of the list. The elements are doubly
106*43a90889SApple OSS Distributions  * linked so that an arbitrary element can be removed without a need to
107*43a90889SApple OSS Distributions  * traverse the list. New elements can be added to the list before or
108*43a90889SApple OSS Distributions  * after an existing element, at the head of the list, or at the end of
109*43a90889SApple OSS Distributions  * the list. A tail queue may be traversed in either direction.
110*43a90889SApple OSS Distributions  *
111*43a90889SApple OSS Distributions  * A circle queue is headed by a pair of pointers, one to the head of the
112*43a90889SApple OSS Distributions  * list and the other to the tail of the list. The elements are doubly
113*43a90889SApple OSS Distributions  * linked so that an arbitrary element can be removed without a need to
114*43a90889SApple OSS Distributions  * traverse the list. New elements can be added to the list before or after
115*43a90889SApple OSS Distributions  * an existing element, at the head of the list, or at the end of the list.
116*43a90889SApple OSS Distributions  * A circle queue may be traversed in either direction, but has a more
117*43a90889SApple OSS Distributions  * complex end of list detection.
118*43a90889SApple OSS Distributions  * Note that circle queues are deprecated, because, as the removal log
119*43a90889SApple OSS Distributions  * in FreeBSD states, "CIRCLEQs are a disgrace to everything Knuth taught
120*43a90889SApple OSS Distributions  * us in Volume 1 Chapter 2. [...] Use TAILQ instead, it provides the same
121*43a90889SApple OSS Distributions  * functionality." Code using them will continue to compile, but they
122*43a90889SApple OSS Distributions  * are no longer documented on the man page.
123*43a90889SApple OSS Distributions  *
124*43a90889SApple OSS Distributions  * For details on the use of these macros, see the queue(3) manual page.
125*43a90889SApple OSS Distributions  *
126*43a90889SApple OSS Distributions  *
127*43a90889SApple OSS Distributions  *                        SLIST   LIST   STAILQ   TAILQ   CIRCLEQ
128*43a90889SApple OSS Distributions  * _HEAD                   +       +      +        +       +
129*43a90889SApple OSS Distributions  * _HEAD_INITIALIZER       +       +      +        +       -
130*43a90889SApple OSS Distributions  * _ENTRY                  +       +      +        +       +
131*43a90889SApple OSS Distributions  * _INIT                   +       +      +        +       +
132*43a90889SApple OSS Distributions  * _EMPTY                  +       +      +        +       +
133*43a90889SApple OSS Distributions  * _FIRST                  +       +      +        +       +
134*43a90889SApple OSS Distributions  * _NEXT                   +       +      +        +       +
135*43a90889SApple OSS Distributions  * _PREV                   -       -      -        +       +
136*43a90889SApple OSS Distributions  * _LAST                   -       -      +        +       +
137*43a90889SApple OSS Distributions  * _FOREACH                +       +      +        +       +
138*43a90889SApple OSS Distributions  * _FOREACH_SAFE           +       +      +        +       -
139*43a90889SApple OSS Distributions  * _FOREACH_REVERSE        -       -      -        +       -
140*43a90889SApple OSS Distributions  * _FOREACH_REVERSE_SAFE   -       -      -        +       -
141*43a90889SApple OSS Distributions  * _INSERT_HEAD            +       +      +        +       +
142*43a90889SApple OSS Distributions  * _INSERT_BEFORE          -       +      -        +       +
143*43a90889SApple OSS Distributions  * _INSERT_AFTER           +       +      +        +       +
144*43a90889SApple OSS Distributions  * _INSERT_TAIL            -       -      +        +       +
145*43a90889SApple OSS Distributions  * _CONCAT                 -       -      +        +       -
146*43a90889SApple OSS Distributions  * _REMOVE_AFTER           +       -      +        -       -
147*43a90889SApple OSS Distributions  * _REMOVE_HEAD            +       -      +        -       -
148*43a90889SApple OSS Distributions  * _REMOVE_HEAD_UNTIL      -       -      +        -       -
149*43a90889SApple OSS Distributions  * _REMOVE                 +       +      +        +       +
150*43a90889SApple OSS Distributions  * _SWAP                   -       +      +        +       -
151*43a90889SApple OSS Distributions  *
152*43a90889SApple OSS Distributions  */
153*43a90889SApple OSS Distributions #ifdef QUEUE_MACRO_DEBUG
154*43a90889SApple OSS Distributions /* Store the last 2 places the queue element or head was altered */
155*43a90889SApple OSS Distributions struct qm_trace {
156*43a90889SApple OSS Distributions 	char * lastfile;
157*43a90889SApple OSS Distributions 	int lastline;
158*43a90889SApple OSS Distributions 	char * prevfile;
159*43a90889SApple OSS Distributions 	int prevline;
160*43a90889SApple OSS Distributions };
161*43a90889SApple OSS Distributions 
162*43a90889SApple OSS Distributions #define TRACEBUF        struct qm_trace trace;
163*43a90889SApple OSS Distributions #define TRASHIT(x)      do {(x) = (void *)-1;} while (0)
164*43a90889SApple OSS Distributions 
165*43a90889SApple OSS Distributions #define QMD_TRACE_HEAD(head) do {                                       \
166*43a90889SApple OSS Distributions 	(head)->trace.prevline = (head)->trace.lastline;                \
167*43a90889SApple OSS Distributions 	(head)->trace.prevfile = (head)->trace.lastfile;                \
168*43a90889SApple OSS Distributions 	(head)->trace.lastline = __LINE__;                              \
169*43a90889SApple OSS Distributions 	(head)->trace.lastfile = __FILE__;                              \
170*43a90889SApple OSS Distributions } while (0)
171*43a90889SApple OSS Distributions 
172*43a90889SApple OSS Distributions #define QMD_TRACE_ELEM(elem) do {                                       \
173*43a90889SApple OSS Distributions 	(elem)->trace.prevline = (elem)->trace.lastline;                \
174*43a90889SApple OSS Distributions 	(elem)->trace.prevfile = (elem)->trace.lastfile;                \
175*43a90889SApple OSS Distributions 	(elem)->trace.lastline = __LINE__;                              \
176*43a90889SApple OSS Distributions 	(elem)->trace.lastfile = __FILE__;                              \
177*43a90889SApple OSS Distributions } while (0)
178*43a90889SApple OSS Distributions 
179*43a90889SApple OSS Distributions #else
180*43a90889SApple OSS Distributions #define QMD_TRACE_ELEM(elem)
181*43a90889SApple OSS Distributions #define QMD_TRACE_HEAD(head)
182*43a90889SApple OSS Distributions #define TRACEBUF
183*43a90889SApple OSS Distributions #define TRASHIT(x)
184*43a90889SApple OSS Distributions #endif  /* QUEUE_MACRO_DEBUG */
185*43a90889SApple OSS Distributions 
186*43a90889SApple OSS Distributions /*
187*43a90889SApple OSS Distributions  * Horrible macros to enable use of code that was meant to be C-specific
188*43a90889SApple OSS Distributions  *   (and which push struct onto type) in C++; without these, C++ code
189*43a90889SApple OSS Distributions  *   that uses these macros in the context of a class will blow up
190*43a90889SApple OSS Distributions  *   due to "struct" being preprended to "type" by the macros, causing
191*43a90889SApple OSS Distributions  *   inconsistent use of tags.
192*43a90889SApple OSS Distributions  *
193*43a90889SApple OSS Distributions  * This approach is necessary because these are macros; we have to use
194*43a90889SApple OSS Distributions  *   these on a per-macro basis (because the queues are implemented as
195*43a90889SApple OSS Distributions  *   macros, disabling this warning in the scope of the header file is
196*43a90889SApple OSS Distributions  *   insufficient), whuch means we can't use #pragma, and have to use
197*43a90889SApple OSS Distributions  *   _Pragma.  We only need to use these for the queue macros that
198*43a90889SApple OSS Distributions  *   prepend "struct" to "type" and will cause C++ to blow up.
199*43a90889SApple OSS Distributions  */
200*43a90889SApple OSS Distributions #if defined(__clang__) && defined(__cplusplus)
201*43a90889SApple OSS Distributions #define __MISMATCH_TAGS_PUSH                                            \
202*43a90889SApple OSS Distributions 	_Pragma("clang diagnostic push")                                \
203*43a90889SApple OSS Distributions 	_Pragma("clang diagnostic ignored \"-Wmismatched-tags\"")
204*43a90889SApple OSS Distributions #define __MISMATCH_TAGS_POP                                             \
205*43a90889SApple OSS Distributions 	_Pragma("clang diagnostic pop")
206*43a90889SApple OSS Distributions #else
207*43a90889SApple OSS Distributions #define __MISMATCH_TAGS_PUSH
208*43a90889SApple OSS Distributions #define __MISMATCH_TAGS_POP
209*43a90889SApple OSS Distributions #endif
210*43a90889SApple OSS Distributions 
211*43a90889SApple OSS Distributions /*!
212*43a90889SApple OSS Distributions  * Ensures that these macros can safely be used in structs when compiling with
213*43a90889SApple OSS Distributions  * clang. The macros do not allow for nullability attributes to be specified due
214*43a90889SApple OSS Distributions  * to how they are expanded. For example:
215*43a90889SApple OSS Distributions  *
216*43a90889SApple OSS Distributions  *     SLIST_HEAD(, foo _Nullable) bar;
217*43a90889SApple OSS Distributions  *
218*43a90889SApple OSS Distributions  * expands to
219*43a90889SApple OSS Distributions  *
220*43a90889SApple OSS Distributions  *     struct {
221*43a90889SApple OSS Distributions  *         struct foo _Nullable *slh_first;
222*43a90889SApple OSS Distributions  *     }
223*43a90889SApple OSS Distributions  *
224*43a90889SApple OSS Distributions  * which is not valid because the nullability specifier has to apply to the
225*43a90889SApple OSS Distributions  * pointer. So just ignore nullability completeness in all the places where this
226*43a90889SApple OSS Distributions  * is an issue.
227*43a90889SApple OSS Distributions  */
228*43a90889SApple OSS Distributions #if defined(__clang__)
229*43a90889SApple OSS Distributions #define __NULLABILITY_COMPLETENESS_PUSH \
230*43a90889SApple OSS Distributions 	_Pragma("clang diagnostic push") \
231*43a90889SApple OSS Distributions 	_Pragma("clang diagnostic ignored \"-Wnullability-completeness\"")
232*43a90889SApple OSS Distributions #define __NULLABILITY_COMPLETENESS_POP \
233*43a90889SApple OSS Distributions 	_Pragma("clang diagnostic pop")
234*43a90889SApple OSS Distributions #else
235*43a90889SApple OSS Distributions #define __NULLABILITY_COMPLETENESS_PUSH
236*43a90889SApple OSS Distributions #define __NULLABILITY_COMPLETENESS_POP
237*43a90889SApple OSS Distributions #endif
238*43a90889SApple OSS Distributions 
239*43a90889SApple OSS Distributions /*
240*43a90889SApple OSS Distributions  * Singly-linked List declarations.
241*43a90889SApple OSS Distributions  */
242*43a90889SApple OSS Distributions #define SLIST_HEAD(name, type)                                          \
243*43a90889SApple OSS Distributions __MISMATCH_TAGS_PUSH                                                    \
244*43a90889SApple OSS Distributions __NULLABILITY_COMPLETENESS_PUSH                                         \
245*43a90889SApple OSS Distributions struct name {                                                           \
246*43a90889SApple OSS Distributions 	struct type *slh_first; /* first element */                     \
247*43a90889SApple OSS Distributions }                                                                       \
248*43a90889SApple OSS Distributions __NULLABILITY_COMPLETENESS_POP                                          \
249*43a90889SApple OSS Distributions __MISMATCH_TAGS_POP
250*43a90889SApple OSS Distributions 
251*43a90889SApple OSS Distributions #define SLIST_HEAD_INITIALIZER(head)                                    \
252*43a90889SApple OSS Distributions 	{ NULL }
253*43a90889SApple OSS Distributions 
254*43a90889SApple OSS Distributions #define SLIST_ENTRY(type)                                               \
255*43a90889SApple OSS Distributions __MISMATCH_TAGS_PUSH                                                    \
256*43a90889SApple OSS Distributions __NULLABILITY_COMPLETENESS_PUSH                                         \
257*43a90889SApple OSS Distributions struct {                                                                \
258*43a90889SApple OSS Distributions 	struct type *sle_next;  /* next element */                      \
259*43a90889SApple OSS Distributions }                                                                       \
260*43a90889SApple OSS Distributions __NULLABILITY_COMPLETENESS_POP                                          \
261*43a90889SApple OSS Distributions __MISMATCH_TAGS_POP
262*43a90889SApple OSS Distributions 
263*43a90889SApple OSS Distributions /*
264*43a90889SApple OSS Distributions  * Singly-linked List functions.
265*43a90889SApple OSS Distributions  */
266*43a90889SApple OSS Distributions #define SLIST_EMPTY(head)       ((head)->slh_first == NULL)
267*43a90889SApple OSS Distributions 
268*43a90889SApple OSS Distributions #define SLIST_FIRST(head)       ((head)->slh_first)
269*43a90889SApple OSS Distributions 
270*43a90889SApple OSS Distributions #define SLIST_FOREACH(var, head, field)                                 \
271*43a90889SApple OSS Distributions 	for ((var) = SLIST_FIRST((head));                               \
272*43a90889SApple OSS Distributions 	    (var);                                                      \
273*43a90889SApple OSS Distributions 	    (var) = SLIST_NEXT((var), field))
274*43a90889SApple OSS Distributions 
275*43a90889SApple OSS Distributions #define SLIST_FOREACH_SAFE(var, head, field, tvar)                      \
276*43a90889SApple OSS Distributions 	for ((var) = SLIST_FIRST((head));                               \
277*43a90889SApple OSS Distributions 	    (var) && ((tvar) = SLIST_NEXT((var), field), 1);            \
278*43a90889SApple OSS Distributions 	    (var) = (tvar))
279*43a90889SApple OSS Distributions 
280*43a90889SApple OSS Distributions #define SLIST_FOREACH_PREVPTR(var, varp, head, field)                   \
281*43a90889SApple OSS Distributions 	for ((varp) = &SLIST_FIRST((head));                             \
282*43a90889SApple OSS Distributions 	    ((var) = *(varp)) != NULL;                                  \
283*43a90889SApple OSS Distributions 	    (varp) = &SLIST_NEXT((var), field))
284*43a90889SApple OSS Distributions 
285*43a90889SApple OSS Distributions #define SLIST_INIT(head) do {                                           \
286*43a90889SApple OSS Distributions 	SLIST_FIRST((head)) = NULL;                                     \
287*43a90889SApple OSS Distributions } while (0)
288*43a90889SApple OSS Distributions 
289*43a90889SApple OSS Distributions #define SLIST_INSERT_AFTER(slistelm, elm, field) do {                   \
290*43a90889SApple OSS Distributions 	SLIST_NEXT((elm), field) = SLIST_NEXT((slistelm), field);       \
291*43a90889SApple OSS Distributions 	SLIST_NEXT((slistelm), field) = (elm);                          \
292*43a90889SApple OSS Distributions } while (0)
293*43a90889SApple OSS Distributions 
294*43a90889SApple OSS Distributions #define SLIST_INSERT_HEAD(head, elm, field) do {                        \
295*43a90889SApple OSS Distributions 	SLIST_NEXT((elm), field) = SLIST_FIRST((head));                 \
296*43a90889SApple OSS Distributions 	SLIST_FIRST((head)) = (elm);                                    \
297*43a90889SApple OSS Distributions } while (0)
298*43a90889SApple OSS Distributions 
299*43a90889SApple OSS Distributions #define SLIST_NEXT(elm, field)  ((elm)->field.sle_next)
300*43a90889SApple OSS Distributions 
301*43a90889SApple OSS Distributions #define SLIST_REMOVE(head, elm, type, field)                            \
302*43a90889SApple OSS Distributions __MISMATCH_TAGS_PUSH                                                    \
303*43a90889SApple OSS Distributions __NULLABILITY_COMPLETENESS_PUSH                                         \
304*43a90889SApple OSS Distributions do {                                                                    \
305*43a90889SApple OSS Distributions 	if (SLIST_FIRST((head)) == (elm)) {                             \
306*43a90889SApple OSS Distributions 	        SLIST_REMOVE_HEAD((head), field);                       \
307*43a90889SApple OSS Distributions 	}                                                               \
308*43a90889SApple OSS Distributions 	else {                                                          \
309*43a90889SApple OSS Distributions 	        struct type *curelm = SLIST_FIRST((head));              \
310*43a90889SApple OSS Distributions 	        while (SLIST_NEXT(curelm, field) != (elm))              \
311*43a90889SApple OSS Distributions 	                curelm = SLIST_NEXT(curelm, field);             \
312*43a90889SApple OSS Distributions 	        SLIST_REMOVE_AFTER(curelm, field);                      \
313*43a90889SApple OSS Distributions 	}                                                               \
314*43a90889SApple OSS Distributions 	TRASHIT((elm)->field.sle_next);                                 \
315*43a90889SApple OSS Distributions } while (0)                                                             \
316*43a90889SApple OSS Distributions __NULLABILITY_COMPLETENESS_POP                                      \
317*43a90889SApple OSS Distributions __MISMATCH_TAGS_POP
318*43a90889SApple OSS Distributions 
319*43a90889SApple OSS Distributions #define SLIST_REMOVE_AFTER(elm, field) do {                             \
320*43a90889SApple OSS Distributions 	SLIST_NEXT(elm, field) =                                        \
321*43a90889SApple OSS Distributions 	    SLIST_NEXT(SLIST_NEXT(elm, field), field);                  \
322*43a90889SApple OSS Distributions } while (0)
323*43a90889SApple OSS Distributions 
324*43a90889SApple OSS Distributions #define SLIST_REMOVE_HEAD(head, field) do {                             \
325*43a90889SApple OSS Distributions 	SLIST_FIRST((head)) = SLIST_NEXT(SLIST_FIRST((head)), field);   \
326*43a90889SApple OSS Distributions } while (0)
327*43a90889SApple OSS Distributions 
328*43a90889SApple OSS Distributions /*
329*43a90889SApple OSS Distributions  * Singly-linked Tail queue declarations.
330*43a90889SApple OSS Distributions  */
331*43a90889SApple OSS Distributions #define STAILQ_HEAD(name, type)                                         \
332*43a90889SApple OSS Distributions __MISMATCH_TAGS_PUSH                                                    \
333*43a90889SApple OSS Distributions __NULLABILITY_COMPLETENESS_PUSH                                         \
334*43a90889SApple OSS Distributions struct name {                                                           \
335*43a90889SApple OSS Distributions 	struct type *stqh_first;/* first element */                     \
336*43a90889SApple OSS Distributions 	struct type **stqh_last;/* addr of last next element */         \
337*43a90889SApple OSS Distributions }                                                                       \
338*43a90889SApple OSS Distributions __NULLABILITY_COMPLETENESS_POP                                          \
339*43a90889SApple OSS Distributions __MISMATCH_TAGS_POP
340*43a90889SApple OSS Distributions 
341*43a90889SApple OSS Distributions #define STAILQ_HEAD_INITIALIZER(head)                                   \
342*43a90889SApple OSS Distributions 	{ NULL, &(head).stqh_first }
343*43a90889SApple OSS Distributions 
344*43a90889SApple OSS Distributions #define STAILQ_ENTRY(type)                                              \
345*43a90889SApple OSS Distributions __MISMATCH_TAGS_PUSH                                                    \
346*43a90889SApple OSS Distributions __NULLABILITY_COMPLETENESS_PUSH                                         \
347*43a90889SApple OSS Distributions struct {                                                                \
348*43a90889SApple OSS Distributions 	struct type *stqe_next; /* next element */                      \
349*43a90889SApple OSS Distributions }                                                                       \
350*43a90889SApple OSS Distributions __NULLABILITY_COMPLETENESS_POP                                         \
351*43a90889SApple OSS Distributions __MISMATCH_TAGS_POP
352*43a90889SApple OSS Distributions 
353*43a90889SApple OSS Distributions /*
354*43a90889SApple OSS Distributions  * Singly-linked Tail queue functions.
355*43a90889SApple OSS Distributions  */
356*43a90889SApple OSS Distributions #define STAILQ_CONCAT(head1, head2) do {                                \
357*43a90889SApple OSS Distributions 	if (!STAILQ_EMPTY((head2))) {                                   \
358*43a90889SApple OSS Distributions 	        *(head1)->stqh_last = (head2)->stqh_first;              \
359*43a90889SApple OSS Distributions 	        (head1)->stqh_last = (head2)->stqh_last;                \
360*43a90889SApple OSS Distributions 	        STAILQ_INIT((head2));                                   \
361*43a90889SApple OSS Distributions 	}                                                               \
362*43a90889SApple OSS Distributions } while (0)
363*43a90889SApple OSS Distributions 
364*43a90889SApple OSS Distributions #define STAILQ_EMPTY(head)      ((head)->stqh_first == NULL)
365*43a90889SApple OSS Distributions 
366*43a90889SApple OSS Distributions #define STAILQ_FIRST(head)      ((head)->stqh_first)
367*43a90889SApple OSS Distributions 
368*43a90889SApple OSS Distributions #define STAILQ_FOREACH(var, head, field)                                \
369*43a90889SApple OSS Distributions 	for((var) = STAILQ_FIRST((head));                               \
370*43a90889SApple OSS Distributions 	   (var);                                                       \
371*43a90889SApple OSS Distributions 	   (var) = STAILQ_NEXT((var), field))
372*43a90889SApple OSS Distributions 
373*43a90889SApple OSS Distributions 
374*43a90889SApple OSS Distributions #define STAILQ_FOREACH_SAFE(var, head, field, tvar)                     \
375*43a90889SApple OSS Distributions 	for ((var) = STAILQ_FIRST((head));                              \
376*43a90889SApple OSS Distributions 	    (var) && ((tvar) = STAILQ_NEXT((var), field), 1);           \
377*43a90889SApple OSS Distributions 	    (var) = (tvar))
378*43a90889SApple OSS Distributions 
379*43a90889SApple OSS Distributions #define STAILQ_INIT(head) do {                                          \
380*43a90889SApple OSS Distributions 	STAILQ_FIRST((head)) = NULL;                                    \
381*43a90889SApple OSS Distributions 	(head)->stqh_last = &STAILQ_FIRST((head));                      \
382*43a90889SApple OSS Distributions } while (0)
383*43a90889SApple OSS Distributions 
384*43a90889SApple OSS Distributions #define STAILQ_INSERT_AFTER(head, tqelm, elm, field) do {               \
385*43a90889SApple OSS Distributions 	if ((STAILQ_NEXT((elm), field) = STAILQ_NEXT((tqelm), field)) == NULL)\
386*43a90889SApple OSS Distributions 	        (head)->stqh_last = &STAILQ_NEXT((elm), field);         \
387*43a90889SApple OSS Distributions 	STAILQ_NEXT((tqelm), field) = (elm);                            \
388*43a90889SApple OSS Distributions } while (0)
389*43a90889SApple OSS Distributions 
390*43a90889SApple OSS Distributions #define STAILQ_INSERT_HEAD(head, elm, field) do {                       \
391*43a90889SApple OSS Distributions 	if ((STAILQ_NEXT((elm), field) = STAILQ_FIRST((head))) == NULL) \
392*43a90889SApple OSS Distributions 	        (head)->stqh_last = &STAILQ_NEXT((elm), field);         \
393*43a90889SApple OSS Distributions 	STAILQ_FIRST((head)) = (elm);                                   \
394*43a90889SApple OSS Distributions } while (0)
395*43a90889SApple OSS Distributions 
396*43a90889SApple OSS Distributions #define STAILQ_INSERT_TAIL(head, elm, field) do {                       \
397*43a90889SApple OSS Distributions 	STAILQ_NEXT((elm), field) = NULL;                               \
398*43a90889SApple OSS Distributions 	*(head)->stqh_last = (elm);                                     \
399*43a90889SApple OSS Distributions 	(head)->stqh_last = &STAILQ_NEXT((elm), field);                 \
400*43a90889SApple OSS Distributions } while (0)
401*43a90889SApple OSS Distributions 
402*43a90889SApple OSS Distributions #define STAILQ_LAST(head, type, field)                                  \
403*43a90889SApple OSS Distributions __MISMATCH_TAGS_PUSH                                                    \
404*43a90889SApple OSS Distributions __NULLABILITY_COMPLETENESS_PUSH                                         \
405*43a90889SApple OSS Distributions 	(STAILQ_EMPTY((head)) ?                                         \
406*43a90889SApple OSS Distributions 	        NULL :                                                  \
407*43a90889SApple OSS Distributions 	        ((struct type *)(void *)                                \
408*43a90889SApple OSS Distributions 	        ((char *)((head)->stqh_last) - __offsetof(struct type, field))))\
409*43a90889SApple OSS Distributions __NULLABILITY_COMPLETENESS_POP                                         \
410*43a90889SApple OSS Distributions __MISMATCH_TAGS_POP
411*43a90889SApple OSS Distributions 
412*43a90889SApple OSS Distributions #define STAILQ_NEXT(elm, field) ((elm)->field.stqe_next)
413*43a90889SApple OSS Distributions 
414*43a90889SApple OSS Distributions #define STAILQ_REMOVE(head, elm, type, field)                           \
415*43a90889SApple OSS Distributions __MISMATCH_TAGS_PUSH                                                    \
416*43a90889SApple OSS Distributions __NULLABILITY_COMPLETENESS_PUSH                                         \
417*43a90889SApple OSS Distributions do {                                                                    \
418*43a90889SApple OSS Distributions 	if (STAILQ_FIRST((head)) == (elm)) {                            \
419*43a90889SApple OSS Distributions 	        STAILQ_REMOVE_HEAD((head), field);                      \
420*43a90889SApple OSS Distributions 	}                                                               \
421*43a90889SApple OSS Distributions 	else {                                                          \
422*43a90889SApple OSS Distributions 	        struct type *curelm = STAILQ_FIRST((head));             \
423*43a90889SApple OSS Distributions 	        while (STAILQ_NEXT(curelm, field) != (elm))             \
424*43a90889SApple OSS Distributions 	                curelm = STAILQ_NEXT(curelm, field);            \
425*43a90889SApple OSS Distributions 	        STAILQ_REMOVE_AFTER(head, curelm, field);               \
426*43a90889SApple OSS Distributions 	}                                                               \
427*43a90889SApple OSS Distributions 	TRASHIT((elm)->field.stqe_next);                                \
428*43a90889SApple OSS Distributions } while (0)                                                             \
429*43a90889SApple OSS Distributions __NULLABILITY_COMPLETENESS_POP                                      \
430*43a90889SApple OSS Distributions __MISMATCH_TAGS_POP
431*43a90889SApple OSS Distributions 
432*43a90889SApple OSS Distributions #define STAILQ_REMOVE_HEAD(head, field) do {                            \
433*43a90889SApple OSS Distributions 	if ((STAILQ_FIRST((head)) =                                     \
434*43a90889SApple OSS Distributions 	     STAILQ_NEXT(STAILQ_FIRST((head)), field)) == NULL)         \
435*43a90889SApple OSS Distributions 	        (head)->stqh_last = &STAILQ_FIRST((head));              \
436*43a90889SApple OSS Distributions } while (0)
437*43a90889SApple OSS Distributions 
438*43a90889SApple OSS Distributions #define STAILQ_REMOVE_HEAD_UNTIL(head, elm, field) do {                 \
439*43a90889SApple OSS Distributions        if ((STAILQ_FIRST((head)) = STAILQ_NEXT((elm), field)) == NULL) \
440*43a90889SApple OSS Distributions 	       (head)->stqh_last = &STAILQ_FIRST((head));              \
441*43a90889SApple OSS Distributions } while (0)
442*43a90889SApple OSS Distributions 
443*43a90889SApple OSS Distributions #define STAILQ_REMOVE_AFTER(head, elm, field) do {                      \
444*43a90889SApple OSS Distributions 	if ((STAILQ_NEXT(elm, field) =                                  \
445*43a90889SApple OSS Distributions 	     STAILQ_NEXT(STAILQ_NEXT(elm, field), field)) == NULL)      \
446*43a90889SApple OSS Distributions 	        (head)->stqh_last = &STAILQ_NEXT((elm), field);         \
447*43a90889SApple OSS Distributions } while (0)
448*43a90889SApple OSS Distributions 
449*43a90889SApple OSS Distributions #define STAILQ_SWAP(head1, head2, type)                                 \
450*43a90889SApple OSS Distributions __MISMATCH_TAGS_PUSH                                                    \
451*43a90889SApple OSS Distributions __NULLABILITY_COMPLETENESS_PUSH                                         \
452*43a90889SApple OSS Distributions do {                                                                    \
453*43a90889SApple OSS Distributions 	struct type *swap_first = STAILQ_FIRST(head1);                  \
454*43a90889SApple OSS Distributions 	struct type **swap_last = (head1)->stqh_last;                   \
455*43a90889SApple OSS Distributions 	STAILQ_FIRST(head1) = STAILQ_FIRST(head2);                      \
456*43a90889SApple OSS Distributions 	(head1)->stqh_last = (head2)->stqh_last;                        \
457*43a90889SApple OSS Distributions 	STAILQ_FIRST(head2) = swap_first;                               \
458*43a90889SApple OSS Distributions 	(head2)->stqh_last = swap_last;                                 \
459*43a90889SApple OSS Distributions 	if (STAILQ_EMPTY(head1))                                        \
460*43a90889SApple OSS Distributions 	        (head1)->stqh_last = &STAILQ_FIRST(head1);              \
461*43a90889SApple OSS Distributions 	if (STAILQ_EMPTY(head2))                                        \
462*43a90889SApple OSS Distributions 	        (head2)->stqh_last = &STAILQ_FIRST(head2);              \
463*43a90889SApple OSS Distributions } while (0)                                                             \
464*43a90889SApple OSS Distributions __NULLABILITY_COMPLETENESS_POP                                          \
465*43a90889SApple OSS Distributions __MISMATCH_TAGS_POP
466*43a90889SApple OSS Distributions 
467*43a90889SApple OSS Distributions 
468*43a90889SApple OSS Distributions /*
469*43a90889SApple OSS Distributions  * List declarations.
470*43a90889SApple OSS Distributions  */
471*43a90889SApple OSS Distributions #define LIST_HEAD(name, type)                                           \
472*43a90889SApple OSS Distributions __MISMATCH_TAGS_PUSH                                                    \
473*43a90889SApple OSS Distributions __NULLABILITY_COMPLETENESS_PUSH                                         \
474*43a90889SApple OSS Distributions struct name {                                                           \
475*43a90889SApple OSS Distributions 	struct type *lh_first;  /* first element */                     \
476*43a90889SApple OSS Distributions }                                                                       \
477*43a90889SApple OSS Distributions __NULLABILITY_COMPLETENESS_POP                                          \
478*43a90889SApple OSS Distributions __MISMATCH_TAGS_POP
479*43a90889SApple OSS Distributions 
480*43a90889SApple OSS Distributions #define LIST_HEAD_INITIALIZER(head)                                     \
481*43a90889SApple OSS Distributions 	{ NULL }
482*43a90889SApple OSS Distributions 
483*43a90889SApple OSS Distributions #define LIST_ENTRY(type)                                                \
484*43a90889SApple OSS Distributions __MISMATCH_TAGS_PUSH                                                    \
485*43a90889SApple OSS Distributions __NULLABILITY_COMPLETENESS_PUSH                                         \
486*43a90889SApple OSS Distributions struct {                                                                \
487*43a90889SApple OSS Distributions 	struct type *le_next;   /* next element */                      \
488*43a90889SApple OSS Distributions 	struct type **le_prev;  /* address of previous next element */  \
489*43a90889SApple OSS Distributions }                                                                       \
490*43a90889SApple OSS Distributions __NULLABILITY_COMPLETENESS_POP                                          \
491*43a90889SApple OSS Distributions __MISMATCH_TAGS_POP
492*43a90889SApple OSS Distributions 
493*43a90889SApple OSS Distributions /*
494*43a90889SApple OSS Distributions  * List functions.
495*43a90889SApple OSS Distributions  */
496*43a90889SApple OSS Distributions 
497*43a90889SApple OSS Distributions #ifdef KERNEL_PRIVATE
498*43a90889SApple OSS Distributions #define LIST_CHECK_HEAD(head, field) do {                               \
499*43a90889SApple OSS Distributions 	if (__improbable(                                               \
500*43a90889SApple OSS Distributions 	    LIST_FIRST((head)) != NULL &&                               \
501*43a90889SApple OSS Distributions 	    LIST_FIRST((head))->field.le_prev !=                        \
502*43a90889SApple OSS Distributions 	    &LIST_FIRST((head))))                                       \
503*43a90889SApple OSS Distributions 	        ml_fatal_trap_invalid_list_linkage((uintptr_t)(head));  \
504*43a90889SApple OSS Distributions } while (0)
505*43a90889SApple OSS Distributions 
506*43a90889SApple OSS Distributions #define LIST_CHECK_NEXT(elm, field) do {                                \
507*43a90889SApple OSS Distributions 	if (__improbable(                                               \
508*43a90889SApple OSS Distributions 	    LIST_NEXT((elm), field) != NULL &&                          \
509*43a90889SApple OSS Distributions 	    LIST_NEXT((elm), field)->field.le_prev !=                   \
510*43a90889SApple OSS Distributions 	    &((elm)->field.le_next)))                                   \
511*43a90889SApple OSS Distributions 	        ml_fatal_trap_invalid_list_linkage((uintptr_t)(elm));   \
512*43a90889SApple OSS Distributions } while (0)
513*43a90889SApple OSS Distributions 
514*43a90889SApple OSS Distributions #define LIST_CHECK_PREV(elm, field) do {                                \
515*43a90889SApple OSS Distributions 	if (__improbable(*(elm)->field.le_prev != (elm)))               \
516*43a90889SApple OSS Distributions 	        ml_fatal_trap_invalid_list_linkage((uintptr_t)(elm));   \
517*43a90889SApple OSS Distributions } while (0)
518*43a90889SApple OSS Distributions #else
519*43a90889SApple OSS Distributions #define LIST_CHECK_HEAD(head, field)
520*43a90889SApple OSS Distributions #define LIST_CHECK_NEXT(elm, field)
521*43a90889SApple OSS Distributions #define LIST_CHECK_PREV(elm, field)
522*43a90889SApple OSS Distributions #endif /* KERNEL_PRIVATE */
523*43a90889SApple OSS Distributions 
524*43a90889SApple OSS Distributions #define LIST_EMPTY(head)        ((head)->lh_first == NULL)
525*43a90889SApple OSS Distributions 
526*43a90889SApple OSS Distributions #define LIST_FIRST(head)        ((head)->lh_first)
527*43a90889SApple OSS Distributions 
528*43a90889SApple OSS Distributions #define LIST_FOREACH(var, head, field)                                  \
529*43a90889SApple OSS Distributions 	for ((var) = LIST_FIRST((head));                                \
530*43a90889SApple OSS Distributions 	    (var);                                                      \
531*43a90889SApple OSS Distributions 	    (var) = LIST_NEXT((var), field))
532*43a90889SApple OSS Distributions 
533*43a90889SApple OSS Distributions #define LIST_FOREACH_SAFE(var, head, field, tvar)                       \
534*43a90889SApple OSS Distributions 	for ((var) = LIST_FIRST((head));                                \
535*43a90889SApple OSS Distributions 	    (var) && ((tvar) = LIST_NEXT((var), field), 1);             \
536*43a90889SApple OSS Distributions 	    (var) = (tvar))
537*43a90889SApple OSS Distributions 
538*43a90889SApple OSS Distributions #define LIST_INIT(head) do {                                            \
539*43a90889SApple OSS Distributions 	LIST_FIRST((head)) = NULL;                                      \
540*43a90889SApple OSS Distributions } while (0)
541*43a90889SApple OSS Distributions 
542*43a90889SApple OSS Distributions #define LIST_INSERT_AFTER(listelm, elm, field) do {                     \
543*43a90889SApple OSS Distributions 	LIST_CHECK_NEXT(listelm, field);                                \
544*43a90889SApple OSS Distributions 	if ((LIST_NEXT((elm), field) = LIST_NEXT((listelm), field)) != NULL)\
545*43a90889SApple OSS Distributions 	        LIST_NEXT((listelm), field)->field.le_prev =            \
546*43a90889SApple OSS Distributions 	            &LIST_NEXT((elm), field);                           \
547*43a90889SApple OSS Distributions 	LIST_NEXT((listelm), field) = (elm);                            \
548*43a90889SApple OSS Distributions 	(elm)->field.le_prev = &LIST_NEXT((listelm), field);            \
549*43a90889SApple OSS Distributions } while (0)
550*43a90889SApple OSS Distributions 
551*43a90889SApple OSS Distributions #define LIST_INSERT_BEFORE(listelm, elm, field) do {                    \
552*43a90889SApple OSS Distributions 	LIST_CHECK_PREV(listelm, field);                                \
553*43a90889SApple OSS Distributions 	(elm)->field.le_prev = (listelm)->field.le_prev;                \
554*43a90889SApple OSS Distributions 	LIST_NEXT((elm), field) = (listelm);                            \
555*43a90889SApple OSS Distributions 	*(listelm)->field.le_prev = (elm);                              \
556*43a90889SApple OSS Distributions 	(listelm)->field.le_prev = &LIST_NEXT((elm), field);            \
557*43a90889SApple OSS Distributions } while (0)
558*43a90889SApple OSS Distributions 
559*43a90889SApple OSS Distributions #define LIST_INSERT_HEAD(head, elm, field) do {                         \
560*43a90889SApple OSS Distributions 	LIST_CHECK_HEAD((head), field);                         \
561*43a90889SApple OSS Distributions 	if ((LIST_NEXT((elm), field) = LIST_FIRST((head))) != NULL)     \
562*43a90889SApple OSS Distributions 	        LIST_FIRST((head))->field.le_prev = &LIST_NEXT((elm), field);\
563*43a90889SApple OSS Distributions 	LIST_FIRST((head)) = (elm);                                     \
564*43a90889SApple OSS Distributions 	(elm)->field.le_prev = &LIST_FIRST((head));                     \
565*43a90889SApple OSS Distributions } while (0)
566*43a90889SApple OSS Distributions 
567*43a90889SApple OSS Distributions #define LIST_NEXT(elm, field)   ((elm)->field.le_next)
568*43a90889SApple OSS Distributions 
569*43a90889SApple OSS Distributions #define LIST_REMOVE(elm, field) do {                                    \
570*43a90889SApple OSS Distributions 	LIST_CHECK_NEXT(elm, field);                            \
571*43a90889SApple OSS Distributions 	LIST_CHECK_PREV(elm, field);                            \
572*43a90889SApple OSS Distributions 	if (LIST_NEXT((elm), field) != NULL)                            \
573*43a90889SApple OSS Distributions 	        LIST_NEXT((elm), field)->field.le_prev =                \
574*43a90889SApple OSS Distributions 	            (elm)->field.le_prev;                               \
575*43a90889SApple OSS Distributions 	*(elm)->field.le_prev = LIST_NEXT((elm), field);                \
576*43a90889SApple OSS Distributions 	TRASHIT((elm)->field.le_next);                                  \
577*43a90889SApple OSS Distributions 	TRASHIT((elm)->field.le_prev);                                  \
578*43a90889SApple OSS Distributions } while (0)
579*43a90889SApple OSS Distributions 
580*43a90889SApple OSS Distributions #define LIST_SWAP(head1, head2, type, field)                            \
581*43a90889SApple OSS Distributions __MISMATCH_TAGS_PUSH                                                    \
582*43a90889SApple OSS Distributions __NULLABILITY_COMPLETENESS_PUSH                                         \
583*43a90889SApple OSS Distributions do {                                                                    \
584*43a90889SApple OSS Distributions 	struct type *swap_tmp = LIST_FIRST((head1));                    \
585*43a90889SApple OSS Distributions 	LIST_FIRST((head1)) = LIST_FIRST((head2));                      \
586*43a90889SApple OSS Distributions 	LIST_FIRST((head2)) = swap_tmp;                                 \
587*43a90889SApple OSS Distributions 	if ((swap_tmp = LIST_FIRST((head1))) != NULL)                   \
588*43a90889SApple OSS Distributions 	        swap_tmp->field.le_prev = &LIST_FIRST((head1));         \
589*43a90889SApple OSS Distributions 	if ((swap_tmp = LIST_FIRST((head2))) != NULL)                   \
590*43a90889SApple OSS Distributions 	        swap_tmp->field.le_prev = &LIST_FIRST((head2));         \
591*43a90889SApple OSS Distributions } while (0)                                                             \
592*43a90889SApple OSS Distributions __NULLABILITY_COMPLETENESS_POP                                          \
593*43a90889SApple OSS Distributions __MISMATCH_TAGS_POP
594*43a90889SApple OSS Distributions 
595*43a90889SApple OSS Distributions /*
596*43a90889SApple OSS Distributions  * Tail queue declarations.
597*43a90889SApple OSS Distributions  */
598*43a90889SApple OSS Distributions #define TAILQ_HEAD(name, type)                                          \
599*43a90889SApple OSS Distributions __MISMATCH_TAGS_PUSH                                                    \
600*43a90889SApple OSS Distributions __NULLABILITY_COMPLETENESS_PUSH                                         \
601*43a90889SApple OSS Distributions struct name {                                                           \
602*43a90889SApple OSS Distributions 	struct type *tqh_first; /* first element */                     \
603*43a90889SApple OSS Distributions 	struct type **tqh_last; /* addr of last next element */         \
604*43a90889SApple OSS Distributions 	TRACEBUF                                                        \
605*43a90889SApple OSS Distributions }                                                                       \
606*43a90889SApple OSS Distributions __NULLABILITY_COMPLETENESS_POP                                          \
607*43a90889SApple OSS Distributions __MISMATCH_TAGS_POP
608*43a90889SApple OSS Distributions 
609*43a90889SApple OSS Distributions #define TAILQ_HEAD_INITIALIZER(head)                                    \
610*43a90889SApple OSS Distributions 	{ NULL, &(head).tqh_first }
611*43a90889SApple OSS Distributions 
612*43a90889SApple OSS Distributions #define TAILQ_ENTRY(type)                                               \
613*43a90889SApple OSS Distributions __MISMATCH_TAGS_PUSH                                                    \
614*43a90889SApple OSS Distributions __NULLABILITY_COMPLETENESS_PUSH                                         \
615*43a90889SApple OSS Distributions struct {                                                                \
616*43a90889SApple OSS Distributions 	struct type *tqe_next;  /* next element */                      \
617*43a90889SApple OSS Distributions 	struct type **tqe_prev; /* address of previous next element */  \
618*43a90889SApple OSS Distributions 	TRACEBUF                                                        \
619*43a90889SApple OSS Distributions }                                                                       \
620*43a90889SApple OSS Distributions __NULLABILITY_COMPLETENESS_POP                                          \
621*43a90889SApple OSS Distributions __MISMATCH_TAGS_POP
622*43a90889SApple OSS Distributions 
623*43a90889SApple OSS Distributions /*
624*43a90889SApple OSS Distributions  * Tail queue functions.
625*43a90889SApple OSS Distributions  */
626*43a90889SApple OSS Distributions #ifdef KERNEL_PRIVATE
627*43a90889SApple OSS Distributions #define TAILQ_CHECK_HEAD(head, field) do {                              \
628*43a90889SApple OSS Distributions 	if (__improbable(                                               \
629*43a90889SApple OSS Distributions 	    TAILQ_FIRST((head)) != NULL &&                              \
630*43a90889SApple OSS Distributions 	    TAILQ_FIRST((head))->field.tqe_prev !=                      \
631*43a90889SApple OSS Distributions 	    &TAILQ_FIRST((head))))                                      \
632*43a90889SApple OSS Distributions 	        ml_fatal_trap_invalid_list_linkage((uintptr_t)(head));  \
633*43a90889SApple OSS Distributions } while (0)
634*43a90889SApple OSS Distributions 
635*43a90889SApple OSS Distributions #define TAILQ_CHECK_NEXT(elm, field) do {                               \
636*43a90889SApple OSS Distributions 	if (__improbable(                                               \
637*43a90889SApple OSS Distributions 	    TAILQ_NEXT((elm), field) != NULL &&                         \
638*43a90889SApple OSS Distributions 	    TAILQ_NEXT((elm), field)->field.tqe_prev !=                 \
639*43a90889SApple OSS Distributions 	    &((elm)->field.tqe_next)))                                  \
640*43a90889SApple OSS Distributions 	        ml_fatal_trap_invalid_list_linkage((uintptr_t)(elm));   \
641*43a90889SApple OSS Distributions } while(0)
642*43a90889SApple OSS Distributions 
643*43a90889SApple OSS Distributions #define TAILQ_CHECK_PREV(elm, field) do {                               \
644*43a90889SApple OSS Distributions 	if (__improbable(*(elm)->field.tqe_prev != (elm)))              \
645*43a90889SApple OSS Distributions 	        ml_fatal_trap_invalid_list_linkage((uintptr_t)(elm));   \
646*43a90889SApple OSS Distributions } while(0)
647*43a90889SApple OSS Distributions #else
648*43a90889SApple OSS Distributions #define TAILQ_CHECK_HEAD(head, field)
649*43a90889SApple OSS Distributions #define TAILQ_CHECK_NEXT(elm, field)
650*43a90889SApple OSS Distributions #define TAILQ_CHECK_PREV(elm, field)
651*43a90889SApple OSS Distributions #endif /* KERNEL_PRIVATE */
652*43a90889SApple OSS Distributions 
653*43a90889SApple OSS Distributions #define TAILQ_CONCAT(head1, head2, field) do {                          \
654*43a90889SApple OSS Distributions 	if (!TAILQ_EMPTY(head2)) {                                      \
655*43a90889SApple OSS Distributions 	        *(head1)->tqh_last = (head2)->tqh_first;                \
656*43a90889SApple OSS Distributions 	        (head2)->tqh_first->field.tqe_prev = (head1)->tqh_last; \
657*43a90889SApple OSS Distributions 	        (head1)->tqh_last = (head2)->tqh_last;                  \
658*43a90889SApple OSS Distributions 	        TAILQ_INIT((head2));                                    \
659*43a90889SApple OSS Distributions 	        QMD_TRACE_HEAD(head1);                                  \
660*43a90889SApple OSS Distributions 	        QMD_TRACE_HEAD(head2);                                  \
661*43a90889SApple OSS Distributions 	}                                                               \
662*43a90889SApple OSS Distributions } while (0)
663*43a90889SApple OSS Distributions 
664*43a90889SApple OSS Distributions #define TAILQ_EMPTY(head)       ((head)->tqh_first == NULL)
665*43a90889SApple OSS Distributions 
666*43a90889SApple OSS Distributions #define TAILQ_FIRST(head)       ((head)->tqh_first)
667*43a90889SApple OSS Distributions 
668*43a90889SApple OSS Distributions #define TAILQ_FOREACH(var, head, field)                                 \
669*43a90889SApple OSS Distributions 	for ((var) = TAILQ_FIRST((head));                               \
670*43a90889SApple OSS Distributions 	    (var);                                                      \
671*43a90889SApple OSS Distributions 	    (var) = TAILQ_NEXT((var), field))
672*43a90889SApple OSS Distributions 
673*43a90889SApple OSS Distributions #define TAILQ_FOREACH_SAFE(var, head, field, tvar)                      \
674*43a90889SApple OSS Distributions 	for ((var) = TAILQ_FIRST((head));                               \
675*43a90889SApple OSS Distributions 	    (var) && ((tvar) = TAILQ_NEXT((var), field), 1);            \
676*43a90889SApple OSS Distributions 	    (var) = (tvar))
677*43a90889SApple OSS Distributions 
678*43a90889SApple OSS Distributions #define TAILQ_FOREACH_REVERSE(var, head, headname, field)               \
679*43a90889SApple OSS Distributions 	for ((var) = TAILQ_LAST((head), headname);                      \
680*43a90889SApple OSS Distributions 	    (var);                                                      \
681*43a90889SApple OSS Distributions 	    (var) = TAILQ_PREV((var), headname, field))
682*43a90889SApple OSS Distributions 
683*43a90889SApple OSS Distributions #define TAILQ_FOREACH_REVERSE_SAFE(var, head, headname, field, tvar)    \
684*43a90889SApple OSS Distributions 	for ((var) = TAILQ_LAST((head), headname);                      \
685*43a90889SApple OSS Distributions 	    (var) && ((tvar) = TAILQ_PREV((var), headname, field), 1);  \
686*43a90889SApple OSS Distributions 	    (var) = (tvar))
687*43a90889SApple OSS Distributions 
688*43a90889SApple OSS Distributions #if XNU_KERNEL_PRIVATE
689*43a90889SApple OSS Distributions /*
690*43a90889SApple OSS Distributions  * Can be used when the initialized HEAD was just bzeroed
691*43a90889SApple OSS Distributions  * Works around deficiencies in clang analysis of initialization patterns.
692*43a90889SApple OSS Distributions  * See: <rdar://problem/47939050>
693*43a90889SApple OSS Distributions  */
694*43a90889SApple OSS Distributions #define TAILQ_INIT_AFTER_BZERO(head) do {                               \
695*43a90889SApple OSS Distributions 	(head)->tqh_last = &TAILQ_FIRST((head));                        \
696*43a90889SApple OSS Distributions } while (0)
697*43a90889SApple OSS Distributions #endif /* XNU_KERNEL_PRIVATE */
698*43a90889SApple OSS Distributions 
699*43a90889SApple OSS Distributions #define TAILQ_INIT(head) do {                                           \
700*43a90889SApple OSS Distributions 	TAILQ_FIRST((head)) = NULL;                                     \
701*43a90889SApple OSS Distributions 	(head)->tqh_last = &TAILQ_FIRST((head));                        \
702*43a90889SApple OSS Distributions 	QMD_TRACE_HEAD(head);                                           \
703*43a90889SApple OSS Distributions } while (0)
704*43a90889SApple OSS Distributions 
705*43a90889SApple OSS Distributions 
706*43a90889SApple OSS Distributions #define TAILQ_INSERT_AFTER(head, listelm, elm, field) do {              \
707*43a90889SApple OSS Distributions 	TAILQ_CHECK_NEXT(listelm, field);                               \
708*43a90889SApple OSS Distributions 	if ((TAILQ_NEXT((elm), field) = TAILQ_NEXT((listelm), field)) != NULL)\
709*43a90889SApple OSS Distributions 	        TAILQ_NEXT((elm), field)->field.tqe_prev =              \
710*43a90889SApple OSS Distributions 	            &TAILQ_NEXT((elm), field);                          \
711*43a90889SApple OSS Distributions 	else {                                                          \
712*43a90889SApple OSS Distributions 	        (head)->tqh_last = &TAILQ_NEXT((elm), field);           \
713*43a90889SApple OSS Distributions 	        QMD_TRACE_HEAD(head);                                   \
714*43a90889SApple OSS Distributions 	}                                                               \
715*43a90889SApple OSS Distributions 	TAILQ_NEXT((listelm), field) = (elm);                           \
716*43a90889SApple OSS Distributions 	(elm)->field.tqe_prev = &TAILQ_NEXT((listelm), field);          \
717*43a90889SApple OSS Distributions 	QMD_TRACE_ELEM(&(elm)->field);                                  \
718*43a90889SApple OSS Distributions 	QMD_TRACE_ELEM(&listelm->field);                                \
719*43a90889SApple OSS Distributions } while (0)
720*43a90889SApple OSS Distributions 
721*43a90889SApple OSS Distributions #define TAILQ_INSERT_BEFORE(listelm, elm, field) do {                   \
722*43a90889SApple OSS Distributions 	TAILQ_CHECK_PREV(listelm, field);                               \
723*43a90889SApple OSS Distributions 	(elm)->field.tqe_prev = (listelm)->field.tqe_prev;              \
724*43a90889SApple OSS Distributions 	TAILQ_NEXT((elm), field) = (listelm);                           \
725*43a90889SApple OSS Distributions 	*(listelm)->field.tqe_prev = (elm);                             \
726*43a90889SApple OSS Distributions 	(listelm)->field.tqe_prev = &TAILQ_NEXT((elm), field);          \
727*43a90889SApple OSS Distributions 	QMD_TRACE_ELEM(&(elm)->field);                                  \
728*43a90889SApple OSS Distributions 	QMD_TRACE_ELEM(&listelm->field);                                \
729*43a90889SApple OSS Distributions } while (0)
730*43a90889SApple OSS Distributions 
731*43a90889SApple OSS Distributions #define TAILQ_INSERT_HEAD(head, elm, field) do {                        \
732*43a90889SApple OSS Distributions 	TAILQ_CHECK_HEAD(head, field);                                  \
733*43a90889SApple OSS Distributions 	if ((TAILQ_NEXT((elm), field) = TAILQ_FIRST((head))) != NULL)   \
734*43a90889SApple OSS Distributions 	        TAILQ_FIRST((head))->field.tqe_prev =                   \
735*43a90889SApple OSS Distributions 	            &TAILQ_NEXT((elm), field);                          \
736*43a90889SApple OSS Distributions 	else                                                            \
737*43a90889SApple OSS Distributions 	        (head)->tqh_last = &TAILQ_NEXT((elm), field);           \
738*43a90889SApple OSS Distributions 	TAILQ_FIRST((head)) = (elm);                                    \
739*43a90889SApple OSS Distributions 	(elm)->field.tqe_prev = &TAILQ_FIRST((head));                   \
740*43a90889SApple OSS Distributions 	QMD_TRACE_HEAD(head);                                           \
741*43a90889SApple OSS Distributions 	QMD_TRACE_ELEM(&(elm)->field);                                  \
742*43a90889SApple OSS Distributions } while (0)
743*43a90889SApple OSS Distributions 
744*43a90889SApple OSS Distributions #define TAILQ_INSERT_TAIL(head, elm, field) do {                        \
745*43a90889SApple OSS Distributions 	TAILQ_NEXT((elm), field) = NULL;                                \
746*43a90889SApple OSS Distributions 	(elm)->field.tqe_prev = (head)->tqh_last;                       \
747*43a90889SApple OSS Distributions 	*(head)->tqh_last = (elm);                                      \
748*43a90889SApple OSS Distributions 	(head)->tqh_last = &TAILQ_NEXT((elm), field);                   \
749*43a90889SApple OSS Distributions 	QMD_TRACE_HEAD(head);                                           \
750*43a90889SApple OSS Distributions 	QMD_TRACE_ELEM(&(elm)->field);                                  \
751*43a90889SApple OSS Distributions } while (0)
752*43a90889SApple OSS Distributions 
753*43a90889SApple OSS Distributions #define TAILQ_LAST(head, headname)                                      \
754*43a90889SApple OSS Distributions __MISMATCH_TAGS_PUSH                                                    \
755*43a90889SApple OSS Distributions __NULLABILITY_COMPLETENESS_PUSH                                         \
756*43a90889SApple OSS Distributions 	(*(((struct headname *)((head)->tqh_last))->tqh_last))          \
757*43a90889SApple OSS Distributions __NULLABILITY_COMPLETENESS_POP                                          \
758*43a90889SApple OSS Distributions __MISMATCH_TAGS_POP
759*43a90889SApple OSS Distributions 
760*43a90889SApple OSS Distributions #define TAILQ_NEXT(elm, field) ((elm)->field.tqe_next)
761*43a90889SApple OSS Distributions 
762*43a90889SApple OSS Distributions #define TAILQ_PREV(elm, headname, field)                                \
763*43a90889SApple OSS Distributions __MISMATCH_TAGS_PUSH                                                    \
764*43a90889SApple OSS Distributions __NULLABILITY_COMPLETENESS_PUSH                                         \
765*43a90889SApple OSS Distributions 	(*(((struct headname *)((elm)->field.tqe_prev))->tqh_last))     \
766*43a90889SApple OSS Distributions __NULLABILITY_COMPLETENESS_POP                                          \
767*43a90889SApple OSS Distributions __MISMATCH_TAGS_POP
768*43a90889SApple OSS Distributions 
769*43a90889SApple OSS Distributions #define TAILQ_REMOVE(head, elm, field) do {                             \
770*43a90889SApple OSS Distributions 	TAILQ_CHECK_NEXT(elm, field);                                   \
771*43a90889SApple OSS Distributions 	TAILQ_CHECK_PREV(elm, field);                                   \
772*43a90889SApple OSS Distributions 	if ((TAILQ_NEXT((elm), field)) != NULL)                         \
773*43a90889SApple OSS Distributions 	        TAILQ_NEXT((elm), field)->field.tqe_prev =              \
774*43a90889SApple OSS Distributions 	            (elm)->field.tqe_prev;                              \
775*43a90889SApple OSS Distributions 	else {                                                          \
776*43a90889SApple OSS Distributions 	        (head)->tqh_last = (elm)->field.tqe_prev;               \
777*43a90889SApple OSS Distributions 	        QMD_TRACE_HEAD(head);                                   \
778*43a90889SApple OSS Distributions 	}                                                               \
779*43a90889SApple OSS Distributions 	*(elm)->field.tqe_prev = TAILQ_NEXT((elm), field);              \
780*43a90889SApple OSS Distributions 	TRASHIT((elm)->field.tqe_next);                                 \
781*43a90889SApple OSS Distributions 	TRASHIT((elm)->field.tqe_prev);                                 \
782*43a90889SApple OSS Distributions 	QMD_TRACE_ELEM(&(elm)->field);                                  \
783*43a90889SApple OSS Distributions } while (0)
784*43a90889SApple OSS Distributions 
785*43a90889SApple OSS Distributions /*
786*43a90889SApple OSS Distributions  * Why did they switch to spaces for this one macro?
787*43a90889SApple OSS Distributions  */
788*43a90889SApple OSS Distributions #define TAILQ_SWAP(head1, head2, type, field)                           \
789*43a90889SApple OSS Distributions __MISMATCH_TAGS_PUSH                                                    \
790*43a90889SApple OSS Distributions __NULLABILITY_COMPLETENESS_PUSH                                         \
791*43a90889SApple OSS Distributions do {                                                                    \
792*43a90889SApple OSS Distributions 	struct type *swap_first = (head1)->tqh_first;                   \
793*43a90889SApple OSS Distributions 	struct type **swap_last = (head1)->tqh_last;                    \
794*43a90889SApple OSS Distributions 	(head1)->tqh_first = (head2)->tqh_first;                        \
795*43a90889SApple OSS Distributions 	(head1)->tqh_last = (head2)->tqh_last;                          \
796*43a90889SApple OSS Distributions 	(head2)->tqh_first = swap_first;                                \
797*43a90889SApple OSS Distributions 	(head2)->tqh_last = swap_last;                                  \
798*43a90889SApple OSS Distributions 	if ((swap_first = (head1)->tqh_first) != NULL)                  \
799*43a90889SApple OSS Distributions 	        swap_first->field.tqe_prev = &(head1)->tqh_first;       \
800*43a90889SApple OSS Distributions 	else                                                            \
801*43a90889SApple OSS Distributions 	        (head1)->tqh_last = &(head1)->tqh_first;                \
802*43a90889SApple OSS Distributions 	if ((swap_first = (head2)->tqh_first) != NULL)                  \
803*43a90889SApple OSS Distributions 	        swap_first->field.tqe_prev = &(head2)->tqh_first;       \
804*43a90889SApple OSS Distributions 	else                                                            \
805*43a90889SApple OSS Distributions 	        (head2)->tqh_last = &(head2)->tqh_first;                \
806*43a90889SApple OSS Distributions } while (0)                                                             \
807*43a90889SApple OSS Distributions __NULLABILITY_COMPLETENESS_POP                                          \
808*43a90889SApple OSS Distributions __MISMATCH_TAGS_POP
809*43a90889SApple OSS Distributions 
810*43a90889SApple OSS Distributions /*
811*43a90889SApple OSS Distributions  * Circular queue definitions.
812*43a90889SApple OSS Distributions  */
813*43a90889SApple OSS Distributions #define CIRCLEQ_HEAD(name, type)                                        \
814*43a90889SApple OSS Distributions __MISMATCH_TAGS_PUSH                                                    \
815*43a90889SApple OSS Distributions __NULLABILITY_COMPLETENESS_PUSH                                         \
816*43a90889SApple OSS Distributions struct name {                                                           \
817*43a90889SApple OSS Distributions 	struct type *cqh_first;         /* first element */             \
818*43a90889SApple OSS Distributions 	struct type *cqh_last;          /* last element */              \
819*43a90889SApple OSS Distributions }                                                                       \
820*43a90889SApple OSS Distributions __NULLABILITY_COMPLETENESS_POP                                          \
821*43a90889SApple OSS Distributions __MISMATCH_TAGS_POP
822*43a90889SApple OSS Distributions 
823*43a90889SApple OSS Distributions #define CIRCLEQ_ENTRY(type)                                             \
824*43a90889SApple OSS Distributions __MISMATCH_TAGS_PUSH                                                    \
825*43a90889SApple OSS Distributions __NULLABILITY_COMPLETENESS_PUSH                                         \
826*43a90889SApple OSS Distributions struct {                                                                \
827*43a90889SApple OSS Distributions 	struct type *cqe_next;          /* next element */              \
828*43a90889SApple OSS Distributions 	struct type *cqe_prev;          /* previous element */          \
829*43a90889SApple OSS Distributions }                                                                       \
830*43a90889SApple OSS Distributions __NULLABILITY_COMPLETENESS_POP                                         \
831*43a90889SApple OSS Distributions __MISMATCH_TAGS_POP
832*43a90889SApple OSS Distributions 
833*43a90889SApple OSS Distributions /*
834*43a90889SApple OSS Distributions  * Circular queue functions.
835*43a90889SApple OSS Distributions  */
836*43a90889SApple OSS Distributions #ifdef KERNEL_PRIVATE
837*43a90889SApple OSS Distributions #define CIRCLEQ_CHECK_HEAD(head, field) do {                            \
838*43a90889SApple OSS Distributions 	if (__improbable(                                               \
839*43a90889SApple OSS Distributions 	    CIRCLEQ_FIRST((head)) != ((void*)(head)) &&                 \
840*43a90889SApple OSS Distributions 	    CIRCLEQ_FIRST((head))->field.cqe_prev != ((void*)(head))))  \
841*43a90889SApple OSS Distributions 	        ml_fatal_trap_invalid_list_linkage((uintptr_t)(head));  \
842*43a90889SApple OSS Distributions } while(0)
843*43a90889SApple OSS Distributions #define CIRCLEQ_CHECK_NEXT(head, elm, field) do {                       \
844*43a90889SApple OSS Distributions 	if (__improbable(                                               \
845*43a90889SApple OSS Distributions 	    CIRCLEQ_NEXT((elm), field) != ((void*)(head)) &&            \
846*43a90889SApple OSS Distributions 	    CIRCLEQ_NEXT((elm), field)->field.cqe_prev != (elm)))       \
847*43a90889SApple OSS Distributions 	        ml_fatal_trap_invalid_list_linkage((uintptr_t)(elm));   \
848*43a90889SApple OSS Distributions } while(0)
849*43a90889SApple OSS Distributions #define CIRCLEQ_CHECK_PREV(head, elm, field) do {                       \
850*43a90889SApple OSS Distributions 	if (__improbable(                                               \
851*43a90889SApple OSS Distributions 	    CIRCLEQ_PREV((elm), field) != ((void*)(head)) &&            \
852*43a90889SApple OSS Distributions 	    CIRCLEQ_PREV((elm), field)->field.cqe_next != (elm)))       \
853*43a90889SApple OSS Distributions 	        ml_fatal_trap_invalid_list_linkage((uintptr_t)(elm));   \
854*43a90889SApple OSS Distributions } while(0)
855*43a90889SApple OSS Distributions #else
856*43a90889SApple OSS Distributions #define CIRCLEQ_CHECK_HEAD(head, field)
857*43a90889SApple OSS Distributions #define CIRCLEQ_CHECK_NEXT(head, elm, field)
858*43a90889SApple OSS Distributions #define CIRCLEQ_CHECK_PREV(head, elm, field)
859*43a90889SApple OSS Distributions #endif /* KERNEL_PRIVATE */
860*43a90889SApple OSS Distributions 
861*43a90889SApple OSS Distributions #define CIRCLEQ_EMPTY(head) ((head)->cqh_first == (void *)(head))
862*43a90889SApple OSS Distributions 
863*43a90889SApple OSS Distributions #define CIRCLEQ_FIRST(head) ((head)->cqh_first)
864*43a90889SApple OSS Distributions 
865*43a90889SApple OSS Distributions #define CIRCLEQ_FOREACH(var, head, field)                               \
866*43a90889SApple OSS Distributions 	for((var) = (head)->cqh_first;                                  \
867*43a90889SApple OSS Distributions 	    (var) != (void *)(head);                                    \
868*43a90889SApple OSS Distributions 	    (var) = (var)->field.cqe_next)
869*43a90889SApple OSS Distributions 
870*43a90889SApple OSS Distributions #define CIRCLEQ_INIT(head) do {                                         \
871*43a90889SApple OSS Distributions 	(head)->cqh_first = (void *)(head);                             \
872*43a90889SApple OSS Distributions 	(head)->cqh_last = (void *)(head);                              \
873*43a90889SApple OSS Distributions } while (0)
874*43a90889SApple OSS Distributions 
875*43a90889SApple OSS Distributions #define CIRCLEQ_INSERT_AFTER(head, listelm, elm, field) do {            \
876*43a90889SApple OSS Distributions 	CIRCLEQ_CHECK_NEXT(head, listelm, field);                       \
877*43a90889SApple OSS Distributions 	(elm)->field.cqe_next = (listelm)->field.cqe_next;              \
878*43a90889SApple OSS Distributions 	(elm)->field.cqe_prev = (listelm);                              \
879*43a90889SApple OSS Distributions 	if ((listelm)->field.cqe_next == (void *)(head))                \
880*43a90889SApple OSS Distributions 	        (head)->cqh_last = (elm);                               \
881*43a90889SApple OSS Distributions 	else                                                            \
882*43a90889SApple OSS Distributions 	        (listelm)->field.cqe_next->field.cqe_prev = (elm);      \
883*43a90889SApple OSS Distributions 	(listelm)->field.cqe_next = (elm);                              \
884*43a90889SApple OSS Distributions } while (0)
885*43a90889SApple OSS Distributions 
886*43a90889SApple OSS Distributions #define CIRCLEQ_INSERT_BEFORE(head, listelm, elm, field) do {           \
887*43a90889SApple OSS Distributions 	CIRCLEQ_CHECK_PREV(head, listelm, field);                       \
888*43a90889SApple OSS Distributions 	(elm)->field.cqe_next = (listelm);                              \
889*43a90889SApple OSS Distributions 	(elm)->field.cqe_prev = (listelm)->field.cqe_prev;              \
890*43a90889SApple OSS Distributions 	if ((listelm)->field.cqe_prev == (void *)(head))                \
891*43a90889SApple OSS Distributions 	        (head)->cqh_first = (elm);                              \
892*43a90889SApple OSS Distributions 	else                                                            \
893*43a90889SApple OSS Distributions 	        (listelm)->field.cqe_prev->field.cqe_next = (elm);      \
894*43a90889SApple OSS Distributions 	(listelm)->field.cqe_prev = (elm);                              \
895*43a90889SApple OSS Distributions } while (0)
896*43a90889SApple OSS Distributions 
897*43a90889SApple OSS Distributions #define CIRCLEQ_INSERT_HEAD(head, elm, field) do {                      \
898*43a90889SApple OSS Distributions 	CIRCLEQ_CHECK_HEAD(head, field);                                \
899*43a90889SApple OSS Distributions 	(elm)->field.cqe_next = (head)->cqh_first;                      \
900*43a90889SApple OSS Distributions 	(elm)->field.cqe_prev = (void *)(head);                         \
901*43a90889SApple OSS Distributions 	if ((head)->cqh_last == (void *)(head))                         \
902*43a90889SApple OSS Distributions 	        (head)->cqh_last = (elm);                               \
903*43a90889SApple OSS Distributions 	else                                                            \
904*43a90889SApple OSS Distributions 	        (head)->cqh_first->field.cqe_prev = (elm);              \
905*43a90889SApple OSS Distributions 	(head)->cqh_first = (elm);                                      \
906*43a90889SApple OSS Distributions } while (0)
907*43a90889SApple OSS Distributions 
908*43a90889SApple OSS Distributions #define CIRCLEQ_INSERT_TAIL(head, elm, field) do {                      \
909*43a90889SApple OSS Distributions 	(elm)->field.cqe_next = (void *)(head);                         \
910*43a90889SApple OSS Distributions 	(elm)->field.cqe_prev = (head)->cqh_last;                       \
911*43a90889SApple OSS Distributions 	if ((head)->cqh_first == (void *)(head))                        \
912*43a90889SApple OSS Distributions 	        (head)->cqh_first = (elm);                              \
913*43a90889SApple OSS Distributions 	else                                                            \
914*43a90889SApple OSS Distributions 	        (head)->cqh_last->field.cqe_next = (elm);               \
915*43a90889SApple OSS Distributions 	(head)->cqh_last = (elm);                                       \
916*43a90889SApple OSS Distributions } while (0)
917*43a90889SApple OSS Distributions 
918*43a90889SApple OSS Distributions #define CIRCLEQ_LAST(head) ((head)->cqh_last)
919*43a90889SApple OSS Distributions 
920*43a90889SApple OSS Distributions #define CIRCLEQ_NEXT(elm, field) ((elm)->field.cqe_next)
921*43a90889SApple OSS Distributions 
922*43a90889SApple OSS Distributions #define CIRCLEQ_PREV(elm, field) ((elm)->field.cqe_prev)
923*43a90889SApple OSS Distributions 
924*43a90889SApple OSS Distributions #define CIRCLEQ_REMOVE(head, elm, field) do {                           \
925*43a90889SApple OSS Distributions 	CIRCLEQ_CHECK_NEXT(head, elm, field);                           \
926*43a90889SApple OSS Distributions 	CIRCLEQ_CHECK_PREV(head, elm, field);                           \
927*43a90889SApple OSS Distributions 	if ((elm)->field.cqe_next == (void *)(head))                    \
928*43a90889SApple OSS Distributions 	        (head)->cqh_last = (elm)->field.cqe_prev;               \
929*43a90889SApple OSS Distributions 	else                                                            \
930*43a90889SApple OSS Distributions 	        (elm)->field.cqe_next->field.cqe_prev =                 \
931*43a90889SApple OSS Distributions 	            (elm)->field.cqe_prev;                              \
932*43a90889SApple OSS Distributions 	if ((elm)->field.cqe_prev == (void *)(head))                    \
933*43a90889SApple OSS Distributions 	        (head)->cqh_first = (elm)->field.cqe_next;              \
934*43a90889SApple OSS Distributions 	else                                                            \
935*43a90889SApple OSS Distributions 	        (elm)->field.cqe_prev->field.cqe_next =                 \
936*43a90889SApple OSS Distributions 	            (elm)->field.cqe_next;                              \
937*43a90889SApple OSS Distributions } while (0)
938*43a90889SApple OSS Distributions 
939*43a90889SApple OSS Distributions #ifdef _KERNEL
940*43a90889SApple OSS Distributions 
941*43a90889SApple OSS Distributions #if NOTFB31
942*43a90889SApple OSS Distributions 
943*43a90889SApple OSS Distributions /*
944*43a90889SApple OSS Distributions  * XXX insque() and remque() are an old way of handling certain queues.
945*43a90889SApple OSS Distributions  * They bogusly assumes that all queue heads look alike.
946*43a90889SApple OSS Distributions  */
947*43a90889SApple OSS Distributions 
948*43a90889SApple OSS Distributions struct quehead {
949*43a90889SApple OSS Distributions 	struct quehead *qh_link;
950*43a90889SApple OSS Distributions 	struct quehead *qh_rlink;
951*43a90889SApple OSS Distributions };
952*43a90889SApple OSS Distributions 
953*43a90889SApple OSS Distributions #ifdef __GNUC__
954*43a90889SApple OSS Distributions #ifdef KERNEL_PRIVATE
955*43a90889SApple OSS Distributions static __inline void
chkquenext(void * a)956*43a90889SApple OSS Distributions chkquenext(void *a)
957*43a90889SApple OSS Distributions {
958*43a90889SApple OSS Distributions 	struct quehead *element = (struct quehead *)a;
959*43a90889SApple OSS Distributions 	if (__improbable(element->qh_link != NULL &&
960*43a90889SApple OSS Distributions 	    element->qh_link->qh_rlink != element)) {
961*43a90889SApple OSS Distributions 		ml_fatal_trap_invalid_list_linkage((uintptr_t)(a));
962*43a90889SApple OSS Distributions 	}
963*43a90889SApple OSS Distributions }
964*43a90889SApple OSS Distributions 
965*43a90889SApple OSS Distributions static __inline void
chkqueprev(void * a)966*43a90889SApple OSS Distributions chkqueprev(void *a)
967*43a90889SApple OSS Distributions {
968*43a90889SApple OSS Distributions 	struct quehead *element = (struct quehead *)a;
969*43a90889SApple OSS Distributions 	if (__improbable(element->qh_rlink != NULL &&
970*43a90889SApple OSS Distributions 	    element->qh_rlink->qh_link != element)) {
971*43a90889SApple OSS Distributions 		ml_fatal_trap_invalid_list_linkage((uintptr_t)(a));
972*43a90889SApple OSS Distributions 	}
973*43a90889SApple OSS Distributions }
974*43a90889SApple OSS Distributions #else /* !KERNEL_PRIVATE */
975*43a90889SApple OSS Distributions #define chkquenext(a)
976*43a90889SApple OSS Distributions #define chkqueprev(a)
977*43a90889SApple OSS Distributions #endif /* KERNEL_PRIVATE */
978*43a90889SApple OSS Distributions 
979*43a90889SApple OSS Distributions static __inline void
insque(void * a,void * b)980*43a90889SApple OSS Distributions insque(void *a, void *b)
981*43a90889SApple OSS Distributions {
982*43a90889SApple OSS Distributions 	struct quehead *element = (struct quehead *)a,
983*43a90889SApple OSS Distributions 	    *head = (struct quehead *)b;
984*43a90889SApple OSS Distributions 	chkquenext(head);
985*43a90889SApple OSS Distributions 
986*43a90889SApple OSS Distributions 	element->qh_link = head->qh_link;
987*43a90889SApple OSS Distributions 	element->qh_rlink = head;
988*43a90889SApple OSS Distributions 	head->qh_link = element;
989*43a90889SApple OSS Distributions 	element->qh_link->qh_rlink = element;
990*43a90889SApple OSS Distributions }
991*43a90889SApple OSS Distributions 
992*43a90889SApple OSS Distributions static __inline void
remque(void * a)993*43a90889SApple OSS Distributions remque(void *a)
994*43a90889SApple OSS Distributions {
995*43a90889SApple OSS Distributions 	struct quehead *element = (struct quehead *)a;
996*43a90889SApple OSS Distributions 	chkquenext(element);
997*43a90889SApple OSS Distributions 	chkqueprev(element);
998*43a90889SApple OSS Distributions 
999*43a90889SApple OSS Distributions 	element->qh_link->qh_rlink = element->qh_rlink;
1000*43a90889SApple OSS Distributions 	element->qh_rlink->qh_link = element->qh_link;
1001*43a90889SApple OSS Distributions 	element->qh_rlink = 0;
1002*43a90889SApple OSS Distributions }
1003*43a90889SApple OSS Distributions 
1004*43a90889SApple OSS Distributions #else /* !__GNUC__ */
1005*43a90889SApple OSS Distributions 
1006*43a90889SApple OSS Distributions void    insque(void *a, void *b);
1007*43a90889SApple OSS Distributions void    remque(void *a);
1008*43a90889SApple OSS Distributions 
1009*43a90889SApple OSS Distributions #endif /* __GNUC__ */
1010*43a90889SApple OSS Distributions 
1011*43a90889SApple OSS Distributions #endif /* NOTFB31 */
1012*43a90889SApple OSS Distributions #endif /* _KERNEL */
1013*43a90889SApple OSS Distributions 
1014*43a90889SApple OSS Distributions #endif /* !_SYS_QUEUE_H_ */
1015