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