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