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