]>
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@ |
1c79356b | 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. | |
8f6c56a5 | 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. | |
17 | * | |
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. | |
8f6c56a5 | 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_ | |
60 | #define _SYS_QUEUE_H_ | |
61 | ||
62 | /* | |
63 | * This file defines five types of data structures: singly-linked lists, | |
2d21ac55 | 64 | * singly-linked tail queues, lists, tail queues, and circular queues. |
1c79356b A |
65 | * |
66 | * A singly-linked list is headed by a single forward pointer. The elements | |
67 | * are singly linked for minimum space and pointer manipulation overhead at | |
68 | * the expense of O(n) removal for arbitrary elements. New elements can be | |
69 | * added to the list after an existing element or at the head of the list. | |
70 | * Elements being removed from the head of the list should use the explicit | |
71 | * macro for this purpose for optimum efficiency. A singly-linked list may | |
72 | * only be traversed in the forward direction. Singly-linked lists are ideal | |
73 | * for applications with large datasets and few or no removals or for | |
74 | * implementing a LIFO queue. | |
75 | * | |
76 | * A singly-linked tail queue is headed by a pair of pointers, one to the | |
77 | * head of the list and the other to the tail of the list. The elements are | |
78 | * singly linked for minimum space and pointer manipulation overhead at the | |
79 | * expense of O(n) removal for arbitrary elements. New elements can be added | |
80 | * to the list after an existing element, at the head of the list, or at the | |
81 | * end of the list. Elements being removed from the head of the tail queue | |
82 | * should use the explicit macro for this purpose for optimum efficiency. | |
83 | * A singly-linked tail queue may only be traversed in the forward direction. | |
84 | * Singly-linked tail queues are ideal for applications with large datasets | |
85 | * and few or no removals or for implementing a FIFO queue. | |
86 | * | |
87 | * A list is headed by a single forward pointer (or an array of forward | |
88 | * pointers for a hash table header). The elements are doubly linked | |
89 | * so that an arbitrary element can be removed without a need to | |
90 | * traverse the list. New elements can be added to the list before | |
91 | * or after an existing element or at the head of the list. A list | |
92 | * may only be traversed in the forward direction. | |
93 | * | |
94 | * A tail queue is headed by a pair of pointers, one to the head of the | |
95 | * list and the other to the tail of the list. The elements are doubly | |
96 | * linked so that an arbitrary element can be removed without a need to | |
97 | * traverse the list. New elements can be added to the list before or | |
98 | * after an existing element, at the head of the list, or at the end of | |
2d21ac55 | 99 | * the list. A tail queue may be traversed in either direction. |
1c79356b A |
100 | * |
101 | * A circle queue is headed by a pair of pointers, one to the head of the | |
102 | * list and the other to the tail of the list. The elements are doubly | |
103 | * linked so that an arbitrary element can be removed without a need to | |
104 | * traverse the list. New elements can be added to the list before or after | |
105 | * an existing element, at the head of the list, or at the end of the list. | |
106 | * A circle queue may be traversed in either direction, but has a more | |
107 | * complex end of list detection. | |
2d21ac55 A |
108 | * Note that circle queues are deprecated, because, as the removal log |
109 | * in FreeBSD states, "CIRCLEQs are a disgrace to everything Knuth taught | |
110 | * us in Volume 1 Chapter 2. [...] Use TAILQ instead, it provides the same | |
111 | * functionality." Code using them will continue to compile, but they | |
112 | * are no longer documented on the man page. | |
1c79356b A |
113 | * |
114 | * For details on the use of these macros, see the queue(3) manual page. | |
115 | * | |
116 | * | |
2d21ac55 A |
117 | * SLIST LIST STAILQ TAILQ CIRCLEQ |
118 | * _HEAD + + + + + | |
119 | * _HEAD_INITIALIZER + + + + - | |
120 | * _ENTRY + + + + + | |
121 | * _INIT + + + + + | |
122 | * _EMPTY + + + + + | |
123 | * _FIRST + + + + + | |
124 | * _NEXT + + + + + | |
125 | * _PREV - - - + + | |
126 | * _LAST - - + + + | |
127 | * _FOREACH + + + + + | |
128 | * _FOREACH_SAFE + + + + - | |
129 | * _FOREACH_REVERSE - - - + - | |
130 | * _FOREACH_REVERSE_SAFE - - - + - | |
131 | * _INSERT_HEAD + + + + + | |
132 | * _INSERT_BEFORE - + - + + | |
133 | * _INSERT_AFTER + + + + + | |
134 | * _INSERT_TAIL - - + + + | |
135 | * _CONCAT - - + + - | |
6d2010ae | 136 | * _REMOVE_AFTER + - + - - |
2d21ac55 | 137 | * _REMOVE_HEAD + - + - - |
6d2010ae | 138 | * _REMOVE_HEAD_UNTIL - - + - - |
2d21ac55 | 139 | * _REMOVE + + + + + |
6d2010ae | 140 | * _SWAP - + + + - |
1c79356b A |
141 | * |
142 | */ | |
2d21ac55 A |
143 | #ifdef QUEUE_MACRO_DEBUG |
144 | /* Store the last 2 places the queue element or head was altered */ | |
145 | struct qm_trace { | |
146 | char * lastfile; | |
147 | int lastline; | |
148 | char * prevfile; | |
149 | int prevline; | |
150 | }; | |
151 | ||
152 | #define TRACEBUF struct qm_trace trace; | |
153 | #define TRASHIT(x) do {(x) = (void *)-1;} while (0) | |
154 | ||
155 | #define QMD_TRACE_HEAD(head) do { \ | |
156 | (head)->trace.prevline = (head)->trace.lastline; \ | |
157 | (head)->trace.prevfile = (head)->trace.lastfile; \ | |
158 | (head)->trace.lastline = __LINE__; \ | |
159 | (head)->trace.lastfile = __FILE__; \ | |
160 | } while (0) | |
161 | ||
162 | #define QMD_TRACE_ELEM(elem) do { \ | |
163 | (elem)->trace.prevline = (elem)->trace.lastline; \ | |
164 | (elem)->trace.prevfile = (elem)->trace.lastfile; \ | |
165 | (elem)->trace.lastline = __LINE__; \ | |
166 | (elem)->trace.lastfile = __FILE__; \ | |
167 | } while (0) | |
168 | ||
169 | #else | |
170 | #define QMD_TRACE_ELEM(elem) | |
171 | #define QMD_TRACE_HEAD(head) | |
172 | #define TRACEBUF | |
173 | #define TRASHIT(x) | |
174 | #endif /* QUEUE_MACRO_DEBUG */ | |
1c79356b | 175 | |
39236c6e A |
176 | /* |
177 | * Horrible macros to enable use of code that was meant to be C-specific | |
178 | * (and which push struct onto type) in C++; without these, C++ code | |
179 | * that uses these macros in the context of a class will blow up | |
180 | * due to "struct" being preprended to "type" by the macros, causing | |
181 | * inconsistent use of tags. | |
182 | * | |
183 | * This approach is necessary because these are macros; we have to use | |
184 | * these on a per-macro basis (because the queues are implemented as | |
185 | * macros, disabling this warning in the scope of the header file is | |
186 | * insufficient), whuch means we can't use #pragma, and have to use | |
187 | * _Pragma. We only need to use these for the queue macros that | |
188 | * prepend "struct" to "type" and will cause C++ to blow up. | |
189 | */ | |
190 | #if defined(__clang__) && defined(__cplusplus) | |
191 | #define __MISMATCH_TAGS_PUSH \ | |
192 | _Pragma("clang diagnostic push") \ | |
193 | _Pragma("clang diagnostic ignored \"-Wmismatched-tags\"") | |
194 | #define __MISMATCH_TAGS_POP \ | |
195 | _Pragma("clang diagnostic pop") | |
196 | #else | |
197 | #define __MISMATCH_TAGS_PUSH | |
198 | #define __MISMATCH_TAGS_POP | |
199 | #endif | |
200 | ||
1c79356b | 201 | /* |
2d21ac55 | 202 | * Singly-linked List declarations. |
1c79356b | 203 | */ |
2d21ac55 | 204 | #define SLIST_HEAD(name, type) \ |
39236c6e | 205 | __MISMATCH_TAGS_PUSH \ |
1c79356b A |
206 | struct name { \ |
207 | struct type *slh_first; /* first element */ \ | |
39236c6e A |
208 | } \ |
209 | __MISMATCH_TAGS_POP | |
2d21ac55 A |
210 | |
211 | #define SLIST_HEAD_INITIALIZER(head) \ | |
212 | { NULL } | |
213 | ||
214 | #define SLIST_ENTRY(type) \ | |
39236c6e | 215 | __MISMATCH_TAGS_PUSH \ |
1c79356b A |
216 | struct { \ |
217 | struct type *sle_next; /* next element */ \ | |
39236c6e A |
218 | } \ |
219 | __MISMATCH_TAGS_POP | |
2d21ac55 | 220 | |
1c79356b A |
221 | /* |
222 | * Singly-linked List functions. | |
223 | */ | |
224 | #define SLIST_EMPTY(head) ((head)->slh_first == NULL) | |
225 | ||
226 | #define SLIST_FIRST(head) ((head)->slh_first) | |
227 | ||
2d21ac55 A |
228 | #define SLIST_FOREACH(var, head, field) \ |
229 | for ((var) = SLIST_FIRST((head)); \ | |
230 | (var); \ | |
231 | (var) = SLIST_NEXT((var), field)) | |
1c79356b | 232 | |
2d21ac55 A |
233 | #define SLIST_FOREACH_SAFE(var, head, field, tvar) \ |
234 | for ((var) = SLIST_FIRST((head)); \ | |
235 | (var) && ((tvar) = SLIST_NEXT((var), field), 1); \ | |
236 | (var) = (tvar)) | |
1c79356b | 237 | |
2d21ac55 A |
238 | #define SLIST_FOREACH_PREVPTR(var, varp, head, field) \ |
239 | for ((varp) = &SLIST_FIRST((head)); \ | |
240 | ((var) = *(varp)) != NULL; \ | |
241 | (varp) = &SLIST_NEXT((var), field)) | |
1c79356b | 242 | |
2d21ac55 A |
243 | #define SLIST_INIT(head) do { \ |
244 | SLIST_FIRST((head)) = NULL; \ | |
1c79356b A |
245 | } while (0) |
246 | ||
2d21ac55 A |
247 | #define SLIST_INSERT_AFTER(slistelm, elm, field) do { \ |
248 | SLIST_NEXT((elm), field) = SLIST_NEXT((slistelm), field); \ | |
249 | SLIST_NEXT((slistelm), field) = (elm); \ | |
250 | } while (0) | |
1c79356b | 251 | |
2d21ac55 A |
252 | #define SLIST_INSERT_HEAD(head, elm, field) do { \ |
253 | SLIST_NEXT((elm), field) = SLIST_FIRST((head)); \ | |
254 | SLIST_FIRST((head)) = (elm); \ | |
1c79356b A |
255 | } while (0) |
256 | ||
2d21ac55 A |
257 | #define SLIST_NEXT(elm, field) ((elm)->field.sle_next) |
258 | ||
39236c6e A |
259 | #define SLIST_REMOVE(head, elm, type, field) \ |
260 | __MISMATCH_TAGS_PUSH \ | |
261 | do { \ | |
2d21ac55 | 262 | if (SLIST_FIRST((head)) == (elm)) { \ |
1c79356b A |
263 | SLIST_REMOVE_HEAD((head), field); \ |
264 | } \ | |
265 | else { \ | |
2d21ac55 A |
266 | struct type *curelm = SLIST_FIRST((head)); \ |
267 | while (SLIST_NEXT(curelm, field) != (elm)) \ | |
268 | curelm = SLIST_NEXT(curelm, field); \ | |
6d2010ae | 269 | SLIST_REMOVE_AFTER(curelm, field); \ |
1c79356b | 270 | } \ |
2d21ac55 | 271 | TRASHIT((elm)->field.sle_next); \ |
39236c6e A |
272 | } while (0) \ |
273 | __MISMATCH_TAGS_POP | |
2d21ac55 | 274 | |
6d2010ae A |
275 | #define SLIST_REMOVE_AFTER(elm, field) do { \ |
276 | SLIST_NEXT(elm, field) = \ | |
277 | SLIST_NEXT(SLIST_NEXT(elm, field), field); \ | |
278 | } while (0) | |
279 | ||
2d21ac55 A |
280 | #define SLIST_REMOVE_HEAD(head, field) do { \ |
281 | SLIST_FIRST((head)) = SLIST_NEXT(SLIST_FIRST((head)), field); \ | |
1c79356b A |
282 | } while (0) |
283 | ||
284 | /* | |
2d21ac55 | 285 | * Singly-linked Tail queue declarations. |
1c79356b | 286 | */ |
2d21ac55 | 287 | #define STAILQ_HEAD(name, type) \ |
39236c6e | 288 | __MISMATCH_TAGS_PUSH \ |
1c79356b A |
289 | struct name { \ |
290 | struct type *stqh_first;/* first element */ \ | |
291 | struct type **stqh_last;/* addr of last next element */ \ | |
39236c6e A |
292 | } \ |
293 | __MISMATCH_TAGS_POP | |
1c79356b | 294 | |
2d21ac55 | 295 | #define STAILQ_HEAD_INITIALIZER(head) \ |
1c79356b A |
296 | { NULL, &(head).stqh_first } |
297 | ||
2d21ac55 | 298 | #define STAILQ_ENTRY(type) \ |
39236c6e | 299 | __MISMATCH_TAGS_PUSH \ |
1c79356b A |
300 | struct { \ |
301 | struct type *stqe_next; /* next element */ \ | |
39236c6e A |
302 | } \ |
303 | __MISMATCH_TAGS_POP | |
1c79356b A |
304 | |
305 | /* | |
306 | * Singly-linked Tail queue functions. | |
307 | */ | |
2d21ac55 A |
308 | #define STAILQ_CONCAT(head1, head2) do { \ |
309 | if (!STAILQ_EMPTY((head2))) { \ | |
310 | *(head1)->stqh_last = (head2)->stqh_first; \ | |
311 | (head1)->stqh_last = (head2)->stqh_last; \ | |
312 | STAILQ_INIT((head2)); \ | |
313 | } \ | |
1c79356b A |
314 | } while (0) |
315 | ||
2d21ac55 | 316 | #define STAILQ_EMPTY(head) ((head)->stqh_first == NULL) |
1c79356b | 317 | |
2d21ac55 A |
318 | #define STAILQ_FIRST(head) ((head)->stqh_first) |
319 | ||
320 | #define STAILQ_FOREACH(var, head, field) \ | |
321 | for((var) = STAILQ_FIRST((head)); \ | |
322 | (var); \ | |
323 | (var) = STAILQ_NEXT((var), field)) | |
1c79356b | 324 | |
1c79356b | 325 | |
2d21ac55 A |
326 | #define STAILQ_FOREACH_SAFE(var, head, field, tvar) \ |
327 | for ((var) = STAILQ_FIRST((head)); \ | |
328 | (var) && ((tvar) = STAILQ_NEXT((var), field), 1); \ | |
329 | (var) = (tvar)) | |
330 | ||
331 | #define STAILQ_INIT(head) do { \ | |
332 | STAILQ_FIRST((head)) = NULL; \ | |
333 | (head)->stqh_last = &STAILQ_FIRST((head)); \ | |
1c79356b A |
334 | } while (0) |
335 | ||
2d21ac55 A |
336 | #define STAILQ_INSERT_AFTER(head, tqelm, elm, field) do { \ |
337 | if ((STAILQ_NEXT((elm), field) = STAILQ_NEXT((tqelm), field)) == NULL)\ | |
338 | (head)->stqh_last = &STAILQ_NEXT((elm), field); \ | |
339 | STAILQ_NEXT((tqelm), field) = (elm); \ | |
340 | } while (0) | |
1c79356b | 341 | |
2d21ac55 A |
342 | #define STAILQ_INSERT_HEAD(head, elm, field) do { \ |
343 | if ((STAILQ_NEXT((elm), field) = STAILQ_FIRST((head))) == NULL) \ | |
344 | (head)->stqh_last = &STAILQ_NEXT((elm), field); \ | |
345 | STAILQ_FIRST((head)) = (elm); \ | |
1c79356b A |
346 | } while (0) |
347 | ||
2d21ac55 A |
348 | #define STAILQ_INSERT_TAIL(head, elm, field) do { \ |
349 | STAILQ_NEXT((elm), field) = NULL; \ | |
350 | *(head)->stqh_last = (elm); \ | |
351 | (head)->stqh_last = &STAILQ_NEXT((elm), field); \ | |
1c79356b A |
352 | } while (0) |
353 | ||
2d21ac55 | 354 | #define STAILQ_LAST(head, type, field) \ |
39236c6e | 355 | __MISMATCH_TAGS_PUSH \ |
2d21ac55 A |
356 | (STAILQ_EMPTY((head)) ? \ |
357 | NULL : \ | |
358 | ((struct type *)(void *) \ | |
39236c6e A |
359 | ((char *)((head)->stqh_last) - __offsetof(struct type, field))))\ |
360 | __MISMATCH_TAGS_POP | |
1c79356b | 361 | |
2d21ac55 A |
362 | #define STAILQ_NEXT(elm, field) ((elm)->field.stqe_next) |
363 | ||
39236c6e A |
364 | #define STAILQ_REMOVE(head, elm, type, field) \ |
365 | __MISMATCH_TAGS_PUSH \ | |
366 | do { \ | |
2d21ac55 A |
367 | if (STAILQ_FIRST((head)) == (elm)) { \ |
368 | STAILQ_REMOVE_HEAD((head), field); \ | |
1c79356b A |
369 | } \ |
370 | else { \ | |
2d21ac55 A |
371 | struct type *curelm = STAILQ_FIRST((head)); \ |
372 | while (STAILQ_NEXT(curelm, field) != (elm)) \ | |
373 | curelm = STAILQ_NEXT(curelm, field); \ | |
6d2010ae | 374 | STAILQ_REMOVE_AFTER(head, curelm, field); \ |
1c79356b | 375 | } \ |
2d21ac55 | 376 | TRASHIT((elm)->field.stqe_next); \ |
39236c6e A |
377 | } while (0) \ |
378 | __MISMATCH_TAGS_POP | |
2d21ac55 A |
379 | |
380 | #define STAILQ_REMOVE_HEAD(head, field) do { \ | |
381 | if ((STAILQ_FIRST((head)) = \ | |
382 | STAILQ_NEXT(STAILQ_FIRST((head)), field)) == NULL) \ | |
383 | (head)->stqh_last = &STAILQ_FIRST((head)); \ | |
384 | } while (0) | |
385 | ||
6d2010ae A |
386 | #define STAILQ_REMOVE_HEAD_UNTIL(head, elm, field) do { \ |
387 | if ((STAILQ_FIRST((head)) = STAILQ_NEXT((elm), field)) == NULL) \ | |
388 | (head)->stqh_last = &STAILQ_FIRST((head)); \ | |
1c79356b A |
389 | } while (0) |
390 | ||
6d2010ae A |
391 | #define STAILQ_REMOVE_AFTER(head, elm, field) do { \ |
392 | if ((STAILQ_NEXT(elm, field) = \ | |
393 | STAILQ_NEXT(STAILQ_NEXT(elm, field), field)) == NULL) \ | |
394 | (head)->stqh_last = &STAILQ_NEXT((elm), field); \ | |
395 | } while (0) | |
396 | ||
39236c6e A |
397 | #define STAILQ_SWAP(head1, head2, type) \ |
398 | __MISMATCH_TAGS_PUSH \ | |
399 | do { \ | |
6d2010ae A |
400 | struct type *swap_first = STAILQ_FIRST(head1); \ |
401 | struct type **swap_last = (head1)->stqh_last; \ | |
402 | STAILQ_FIRST(head1) = STAILQ_FIRST(head2); \ | |
403 | (head1)->stqh_last = (head2)->stqh_last; \ | |
404 | STAILQ_FIRST(head2) = swap_first; \ | |
405 | (head2)->stqh_last = swap_last; \ | |
406 | if (STAILQ_EMPTY(head1)) \ | |
407 | (head1)->stqh_last = &STAILQ_FIRST(head1); \ | |
408 | if (STAILQ_EMPTY(head2)) \ | |
409 | (head2)->stqh_last = &STAILQ_FIRST(head2); \ | |
39236c6e A |
410 | } while (0) \ |
411 | __MISMATCH_TAGS_POP | |
6d2010ae A |
412 | |
413 | ||
1c79356b | 414 | /* |
2d21ac55 | 415 | * List declarations. |
1c79356b | 416 | */ |
2d21ac55 | 417 | #define LIST_HEAD(name, type) \ |
39236c6e | 418 | __MISMATCH_TAGS_PUSH \ |
1c79356b A |
419 | struct name { \ |
420 | struct type *lh_first; /* first element */ \ | |
39236c6e A |
421 | } \ |
422 | __MISMATCH_TAGS_POP | |
1c79356b | 423 | |
2d21ac55 | 424 | #define LIST_HEAD_INITIALIZER(head) \ |
1c79356b A |
425 | { NULL } |
426 | ||
2d21ac55 | 427 | #define LIST_ENTRY(type) \ |
39236c6e | 428 | __MISMATCH_TAGS_PUSH \ |
1c79356b A |
429 | struct { \ |
430 | struct type *le_next; /* next element */ \ | |
431 | struct type **le_prev; /* address of previous next element */ \ | |
39236c6e A |
432 | } \ |
433 | __MISMATCH_TAGS_POP | |
1c79356b A |
434 | |
435 | /* | |
436 | * List functions. | |
437 | */ | |
438 | ||
2d21ac55 A |
439 | #if (defined(_KERNEL) && defined(INVARIANTS)) || defined(QUEUE_MACRO_DEBUG) |
440 | #define QMD_LIST_CHECK_HEAD(head, field) do { \ | |
441 | if (LIST_FIRST((head)) != NULL && \ | |
442 | LIST_FIRST((head))->field.le_prev != \ | |
443 | &LIST_FIRST((head))) \ | |
444 | panic("Bad list head %p first->prev != head", (head)); \ | |
445 | } while (0) | |
1c79356b | 446 | |
2d21ac55 A |
447 | #define QMD_LIST_CHECK_NEXT(elm, field) do { \ |
448 | if (LIST_NEXT((elm), field) != NULL && \ | |
449 | LIST_NEXT((elm), field)->field.le_prev != \ | |
450 | &((elm)->field.le_next)) \ | |
451 | panic("Bad link elm %p next->prev != elm", (elm)); \ | |
452 | } while (0) | |
453 | ||
454 | #define QMD_LIST_CHECK_PREV(elm, field) do { \ | |
455 | if (*(elm)->field.le_prev != (elm)) \ | |
456 | panic("Bad link elm %p prev->next != elm", (elm)); \ | |
457 | } while (0) | |
458 | #else | |
459 | #define QMD_LIST_CHECK_HEAD(head, field) | |
460 | #define QMD_LIST_CHECK_NEXT(elm, field) | |
461 | #define QMD_LIST_CHECK_PREV(elm, field) | |
462 | #endif /* (_KERNEL && INVARIANTS) || QUEUE_MACRO_DEBUG */ | |
1c79356b | 463 | |
2d21ac55 A |
464 | #define LIST_EMPTY(head) ((head)->lh_first == NULL) |
465 | ||
466 | #define LIST_FIRST(head) ((head)->lh_first) | |
467 | ||
468 | #define LIST_FOREACH(var, head, field) \ | |
469 | for ((var) = LIST_FIRST((head)); \ | |
470 | (var); \ | |
471 | (var) = LIST_NEXT((var), field)) | |
472 | ||
473 | #define LIST_FOREACH_SAFE(var, head, field, tvar) \ | |
474 | for ((var) = LIST_FIRST((head)); \ | |
475 | (var) && ((tvar) = LIST_NEXT((var), field), 1); \ | |
476 | (var) = (tvar)) | |
1c79356b A |
477 | |
478 | #define LIST_INIT(head) do { \ | |
2d21ac55 | 479 | LIST_FIRST((head)) = NULL; \ |
1c79356b A |
480 | } while (0) |
481 | ||
2d21ac55 A |
482 | #define LIST_INSERT_AFTER(listelm, elm, field) do { \ |
483 | QMD_LIST_CHECK_NEXT(listelm, field); \ | |
484 | if ((LIST_NEXT((elm), field) = LIST_NEXT((listelm), field)) != NULL)\ | |
485 | LIST_NEXT((listelm), field)->field.le_prev = \ | |
486 | &LIST_NEXT((elm), field); \ | |
487 | LIST_NEXT((listelm), field) = (elm); \ | |
488 | (elm)->field.le_prev = &LIST_NEXT((listelm), field); \ | |
1c79356b A |
489 | } while (0) |
490 | ||
2d21ac55 A |
491 | #define LIST_INSERT_BEFORE(listelm, elm, field) do { \ |
492 | QMD_LIST_CHECK_PREV(listelm, field); \ | |
1c79356b | 493 | (elm)->field.le_prev = (listelm)->field.le_prev; \ |
2d21ac55 | 494 | LIST_NEXT((elm), field) = (listelm); \ |
1c79356b | 495 | *(listelm)->field.le_prev = (elm); \ |
2d21ac55 | 496 | (listelm)->field.le_prev = &LIST_NEXT((elm), field); \ |
1c79356b A |
497 | } while (0) |
498 | ||
2d21ac55 A |
499 | #define LIST_INSERT_HEAD(head, elm, field) do { \ |
500 | QMD_LIST_CHECK_HEAD((head), field); \ | |
501 | if ((LIST_NEXT((elm), field) = LIST_FIRST((head))) != NULL) \ | |
502 | LIST_FIRST((head))->field.le_prev = &LIST_NEXT((elm), field);\ | |
503 | LIST_FIRST((head)) = (elm); \ | |
504 | (elm)->field.le_prev = &LIST_FIRST((head)); \ | |
1c79356b A |
505 | } while (0) |
506 | ||
2d21ac55 | 507 | #define LIST_NEXT(elm, field) ((elm)->field.le_next) |
1c79356b | 508 | |
2d21ac55 A |
509 | #define LIST_REMOVE(elm, field) do { \ |
510 | QMD_LIST_CHECK_NEXT(elm, field); \ | |
511 | QMD_LIST_CHECK_PREV(elm, field); \ | |
512 | if (LIST_NEXT((elm), field) != NULL) \ | |
513 | LIST_NEXT((elm), field)->field.le_prev = \ | |
1c79356b | 514 | (elm)->field.le_prev; \ |
2d21ac55 A |
515 | *(elm)->field.le_prev = LIST_NEXT((elm), field); \ |
516 | TRASHIT((elm)->field.le_next); \ | |
517 | TRASHIT((elm)->field.le_prev); \ | |
1c79356b A |
518 | } while (0) |
519 | ||
39236c6e A |
520 | #define LIST_SWAP(head1, head2, type, field) \ |
521 | __MISMATCH_TAGS_PUSH \ | |
522 | do { \ | |
6d2010ae A |
523 | struct type *swap_tmp = LIST_FIRST((head1)); \ |
524 | LIST_FIRST((head1)) = LIST_FIRST((head2)); \ | |
525 | LIST_FIRST((head2)) = swap_tmp; \ | |
526 | if ((swap_tmp = LIST_FIRST((head1))) != NULL) \ | |
527 | swap_tmp->field.le_prev = &LIST_FIRST((head1)); \ | |
528 | if ((swap_tmp = LIST_FIRST((head2))) != NULL) \ | |
529 | swap_tmp->field.le_prev = &LIST_FIRST((head2)); \ | |
39236c6e A |
530 | } while (0) \ |
531 | __MISMATCH_TAGS_POP | |
6d2010ae | 532 | |
1c79356b | 533 | /* |
2d21ac55 | 534 | * Tail queue declarations. |
1c79356b | 535 | */ |
2d21ac55 | 536 | #define TAILQ_HEAD(name, type) \ |
39236c6e | 537 | __MISMATCH_TAGS_PUSH \ |
1c79356b A |
538 | struct name { \ |
539 | struct type *tqh_first; /* first element */ \ | |
540 | struct type **tqh_last; /* addr of last next element */ \ | |
2d21ac55 | 541 | TRACEBUF \ |
39236c6e A |
542 | } \ |
543 | __MISMATCH_TAGS_POP | |
1c79356b | 544 | |
2d21ac55 | 545 | #define TAILQ_HEAD_INITIALIZER(head) \ |
1c79356b A |
546 | { NULL, &(head).tqh_first } |
547 | ||
2d21ac55 | 548 | #define TAILQ_ENTRY(type) \ |
39236c6e | 549 | __MISMATCH_TAGS_PUSH \ |
1c79356b A |
550 | struct { \ |
551 | struct type *tqe_next; /* next element */ \ | |
552 | struct type **tqe_prev; /* address of previous next element */ \ | |
2d21ac55 | 553 | TRACEBUF \ |
39236c6e A |
554 | } \ |
555 | __MISMATCH_TAGS_POP | |
1c79356b A |
556 | |
557 | /* | |
558 | * Tail queue functions. | |
559 | */ | |
2d21ac55 A |
560 | #define TAILQ_CONCAT(head1, head2, field) do { \ |
561 | if (!TAILQ_EMPTY(head2)) { \ | |
562 | *(head1)->tqh_last = (head2)->tqh_first; \ | |
563 | (head2)->tqh_first->field.tqe_prev = (head1)->tqh_last; \ | |
564 | (head1)->tqh_last = (head2)->tqh_last; \ | |
565 | TAILQ_INIT((head2)); \ | |
566 | QMD_TRACE_HEAD(head1); \ | |
567 | QMD_TRACE_HEAD(head2); \ | |
568 | } \ | |
569 | } while (0) | |
1c79356b | 570 | |
2d21ac55 | 571 | #define TAILQ_EMPTY(head) ((head)->tqh_first == NULL) |
1c79356b | 572 | |
2d21ac55 | 573 | #define TAILQ_FIRST(head) ((head)->tqh_first) |
1c79356b | 574 | |
2d21ac55 A |
575 | #define TAILQ_FOREACH(var, head, field) \ |
576 | for ((var) = TAILQ_FIRST((head)); \ | |
577 | (var); \ | |
578 | (var) = TAILQ_NEXT((var), field)) | |
1c79356b | 579 | |
2d21ac55 A |
580 | #define TAILQ_FOREACH_SAFE(var, head, field, tvar) \ |
581 | for ((var) = TAILQ_FIRST((head)); \ | |
582 | (var) && ((tvar) = TAILQ_NEXT((var), field), 1); \ | |
583 | (var) = (tvar)) | |
1c79356b | 584 | |
2d21ac55 A |
585 | #define TAILQ_FOREACH_REVERSE(var, head, headname, field) \ |
586 | for ((var) = TAILQ_LAST((head), headname); \ | |
587 | (var); \ | |
588 | (var) = TAILQ_PREV((var), headname, field)) | |
1c79356b | 589 | |
2d21ac55 A |
590 | #define TAILQ_FOREACH_REVERSE_SAFE(var, head, headname, field, tvar) \ |
591 | for ((var) = TAILQ_LAST((head), headname); \ | |
592 | (var) && ((tvar) = TAILQ_PREV((var), headname, field), 1); \ | |
593 | (var) = (tvar)) | |
1c79356b A |
594 | |
595 | #define TAILQ_INIT(head) do { \ | |
2d21ac55 A |
596 | TAILQ_FIRST((head)) = NULL; \ |
597 | (head)->tqh_last = &TAILQ_FIRST((head)); \ | |
598 | QMD_TRACE_HEAD(head); \ | |
1c79356b A |
599 | } while (0) |
600 | ||
2d21ac55 A |
601 | #define TAILQ_INSERT_AFTER(head, listelm, elm, field) do { \ |
602 | if ((TAILQ_NEXT((elm), field) = TAILQ_NEXT((listelm), field)) != NULL)\ | |
603 | TAILQ_NEXT((elm), field)->field.tqe_prev = \ | |
604 | &TAILQ_NEXT((elm), field); \ | |
605 | else { \ | |
606 | (head)->tqh_last = &TAILQ_NEXT((elm), field); \ | |
607 | QMD_TRACE_HEAD(head); \ | |
608 | } \ | |
609 | TAILQ_NEXT((listelm), field) = (elm); \ | |
610 | (elm)->field.tqe_prev = &TAILQ_NEXT((listelm), field); \ | |
611 | QMD_TRACE_ELEM(&(elm)->field); \ | |
612 | QMD_TRACE_ELEM(&listelm->field); \ | |
1c79356b A |
613 | } while (0) |
614 | ||
2d21ac55 A |
615 | #define TAILQ_INSERT_BEFORE(listelm, elm, field) do { \ |
616 | (elm)->field.tqe_prev = (listelm)->field.tqe_prev; \ | |
617 | TAILQ_NEXT((elm), field) = (listelm); \ | |
618 | *(listelm)->field.tqe_prev = (elm); \ | |
619 | (listelm)->field.tqe_prev = &TAILQ_NEXT((elm), field); \ | |
620 | QMD_TRACE_ELEM(&(elm)->field); \ | |
621 | QMD_TRACE_ELEM(&listelm->field); \ | |
1c79356b A |
622 | } while (0) |
623 | ||
2d21ac55 A |
624 | #define TAILQ_INSERT_HEAD(head, elm, field) do { \ |
625 | if ((TAILQ_NEXT((elm), field) = TAILQ_FIRST((head))) != NULL) \ | |
626 | TAILQ_FIRST((head))->field.tqe_prev = \ | |
627 | &TAILQ_NEXT((elm), field); \ | |
1c79356b | 628 | else \ |
2d21ac55 A |
629 | (head)->tqh_last = &TAILQ_NEXT((elm), field); \ |
630 | TAILQ_FIRST((head)) = (elm); \ | |
631 | (elm)->field.tqe_prev = &TAILQ_FIRST((head)); \ | |
632 | QMD_TRACE_HEAD(head); \ | |
633 | QMD_TRACE_ELEM(&(elm)->field); \ | |
1c79356b A |
634 | } while (0) |
635 | ||
2d21ac55 A |
636 | #define TAILQ_INSERT_TAIL(head, elm, field) do { \ |
637 | TAILQ_NEXT((elm), field) = NULL; \ | |
638 | (elm)->field.tqe_prev = (head)->tqh_last; \ | |
639 | *(head)->tqh_last = (elm); \ | |
640 | (head)->tqh_last = &TAILQ_NEXT((elm), field); \ | |
641 | QMD_TRACE_HEAD(head); \ | |
642 | QMD_TRACE_ELEM(&(elm)->field); \ | |
1c79356b A |
643 | } while (0) |
644 | ||
2d21ac55 | 645 | #define TAILQ_LAST(head, headname) \ |
39236c6e A |
646 | __MISMATCH_TAGS_PUSH \ |
647 | (*(((struct headname *)((head)->tqh_last))->tqh_last)) \ | |
648 | __MISMATCH_TAGS_POP | |
2d21ac55 A |
649 | |
650 | #define TAILQ_NEXT(elm, field) ((elm)->field.tqe_next) | |
651 | ||
652 | #define TAILQ_PREV(elm, headname, field) \ | |
39236c6e A |
653 | __MISMATCH_TAGS_PUSH \ |
654 | (*(((struct headname *)((elm)->field.tqe_prev))->tqh_last)) \ | |
655 | __MISMATCH_TAGS_POP | |
2d21ac55 A |
656 | |
657 | #define TAILQ_REMOVE(head, elm, field) do { \ | |
658 | if ((TAILQ_NEXT((elm), field)) != NULL) \ | |
659 | TAILQ_NEXT((elm), field)->field.tqe_prev = \ | |
1c79356b | 660 | (elm)->field.tqe_prev; \ |
2d21ac55 | 661 | else { \ |
1c79356b | 662 | (head)->tqh_last = (elm)->field.tqe_prev; \ |
2d21ac55 A |
663 | QMD_TRACE_HEAD(head); \ |
664 | } \ | |
665 | *(elm)->field.tqe_prev = TAILQ_NEXT((elm), field); \ | |
666 | TRASHIT((elm)->field.tqe_next); \ | |
667 | TRASHIT((elm)->field.tqe_prev); \ | |
668 | QMD_TRACE_ELEM(&(elm)->field); \ | |
1c79356b A |
669 | } while (0) |
670 | ||
39236c6e A |
671 | /* |
672 | * Why did they switch to spaces for this one macro? | |
673 | */ | |
674 | #define TAILQ_SWAP(head1, head2, type, field) \ | |
675 | __MISMATCH_TAGS_PUSH \ | |
676 | do { \ | |
6d2010ae A |
677 | struct type *swap_first = (head1)->tqh_first; \ |
678 | struct type **swap_last = (head1)->tqh_last; \ | |
679 | (head1)->tqh_first = (head2)->tqh_first; \ | |
680 | (head1)->tqh_last = (head2)->tqh_last; \ | |
681 | (head2)->tqh_first = swap_first; \ | |
682 | (head2)->tqh_last = swap_last; \ | |
683 | if ((swap_first = (head1)->tqh_first) != NULL) \ | |
684 | swap_first->field.tqe_prev = &(head1)->tqh_first; \ | |
685 | else \ | |
686 | (head1)->tqh_last = &(head1)->tqh_first; \ | |
687 | if ((swap_first = (head2)->tqh_first) != NULL) \ | |
688 | swap_first->field.tqe_prev = &(head2)->tqh_first; \ | |
689 | else \ | |
690 | (head2)->tqh_last = &(head2)->tqh_first; \ | |
39236c6e A |
691 | } while (0) \ |
692 | __MISMATCH_TAGS_POP | |
6d2010ae | 693 | |
1c79356b A |
694 | /* |
695 | * Circular queue definitions. | |
696 | */ | |
697 | #define CIRCLEQ_HEAD(name, type) \ | |
39236c6e | 698 | __MISMATCH_TAGS_PUSH \ |
1c79356b A |
699 | struct name { \ |
700 | struct type *cqh_first; /* first element */ \ | |
701 | struct type *cqh_last; /* last element */ \ | |
39236c6e A |
702 | } \ |
703 | __MISMATCH_TAGS_POP | |
1c79356b A |
704 | |
705 | #define CIRCLEQ_ENTRY(type) \ | |
39236c6e | 706 | __MISMATCH_TAGS_PUSH \ |
1c79356b A |
707 | struct { \ |
708 | struct type *cqe_next; /* next element */ \ | |
709 | struct type *cqe_prev; /* previous element */ \ | |
39236c6e A |
710 | } \ |
711 | __MISMATCH_TAGS_POP | |
1c79356b A |
712 | |
713 | /* | |
714 | * Circular queue functions. | |
715 | */ | |
716 | #define CIRCLEQ_EMPTY(head) ((head)->cqh_first == (void *)(head)) | |
717 | ||
718 | #define CIRCLEQ_FIRST(head) ((head)->cqh_first) | |
719 | ||
720 | #define CIRCLEQ_FOREACH(var, head, field) \ | |
721 | for((var) = (head)->cqh_first; \ | |
722 | (var) != (void *)(head); \ | |
723 | (var) = (var)->field.cqe_next) | |
724 | ||
725 | #define CIRCLEQ_INIT(head) do { \ | |
726 | (head)->cqh_first = (void *)(head); \ | |
727 | (head)->cqh_last = (void *)(head); \ | |
728 | } while (0) | |
729 | ||
730 | #define CIRCLEQ_INSERT_AFTER(head, listelm, elm, field) do { \ | |
731 | (elm)->field.cqe_next = (listelm)->field.cqe_next; \ | |
732 | (elm)->field.cqe_prev = (listelm); \ | |
733 | if ((listelm)->field.cqe_next == (void *)(head)) \ | |
734 | (head)->cqh_last = (elm); \ | |
735 | else \ | |
736 | (listelm)->field.cqe_next->field.cqe_prev = (elm); \ | |
737 | (listelm)->field.cqe_next = (elm); \ | |
738 | } while (0) | |
739 | ||
740 | #define CIRCLEQ_INSERT_BEFORE(head, listelm, elm, field) do { \ | |
741 | (elm)->field.cqe_next = (listelm); \ | |
742 | (elm)->field.cqe_prev = (listelm)->field.cqe_prev; \ | |
743 | if ((listelm)->field.cqe_prev == (void *)(head)) \ | |
744 | (head)->cqh_first = (elm); \ | |
745 | else \ | |
746 | (listelm)->field.cqe_prev->field.cqe_next = (elm); \ | |
747 | (listelm)->field.cqe_prev = (elm); \ | |
748 | } while (0) | |
749 | ||
750 | #define CIRCLEQ_INSERT_HEAD(head, elm, field) do { \ | |
751 | (elm)->field.cqe_next = (head)->cqh_first; \ | |
752 | (elm)->field.cqe_prev = (void *)(head); \ | |
753 | if ((head)->cqh_last == (void *)(head)) \ | |
754 | (head)->cqh_last = (elm); \ | |
755 | else \ | |
756 | (head)->cqh_first->field.cqe_prev = (elm); \ | |
757 | (head)->cqh_first = (elm); \ | |
758 | } while (0) | |
759 | ||
760 | #define CIRCLEQ_INSERT_TAIL(head, elm, field) do { \ | |
761 | (elm)->field.cqe_next = (void *)(head); \ | |
762 | (elm)->field.cqe_prev = (head)->cqh_last; \ | |
763 | if ((head)->cqh_first == (void *)(head)) \ | |
764 | (head)->cqh_first = (elm); \ | |
765 | else \ | |
766 | (head)->cqh_last->field.cqe_next = (elm); \ | |
767 | (head)->cqh_last = (elm); \ | |
768 | } while (0) | |
769 | ||
770 | #define CIRCLEQ_LAST(head) ((head)->cqh_last) | |
771 | ||
772 | #define CIRCLEQ_NEXT(elm,field) ((elm)->field.cqe_next) | |
773 | ||
774 | #define CIRCLEQ_PREV(elm,field) ((elm)->field.cqe_prev) | |
775 | ||
776 | #define CIRCLEQ_REMOVE(head, elm, field) do { \ | |
777 | if ((elm)->field.cqe_next == (void *)(head)) \ | |
778 | (head)->cqh_last = (elm)->field.cqe_prev; \ | |
779 | else \ | |
780 | (elm)->field.cqe_next->field.cqe_prev = \ | |
781 | (elm)->field.cqe_prev; \ | |
782 | if ((elm)->field.cqe_prev == (void *)(head)) \ | |
783 | (head)->cqh_first = (elm)->field.cqe_next; \ | |
784 | else \ | |
785 | (elm)->field.cqe_prev->field.cqe_next = \ | |
786 | (elm)->field.cqe_next; \ | |
787 | } while (0) | |
788 | ||
2d21ac55 | 789 | #ifdef _KERNEL |
1c79356b A |
790 | |
791 | #if NOTFB31 | |
792 | ||
793 | /* | |
794 | * XXX insque() and remque() are an old way of handling certain queues. | |
795 | * They bogusly assumes that all queue heads look alike. | |
796 | */ | |
797 | ||
798 | struct quehead { | |
799 | struct quehead *qh_link; | |
800 | struct quehead *qh_rlink; | |
801 | }; | |
802 | ||
2d21ac55 | 803 | #ifdef __GNUC__ |
1c79356b A |
804 | |
805 | static __inline void | |
806 | insque(void *a, void *b) | |
807 | { | |
2d21ac55 A |
808 | struct quehead *element = (struct quehead *)a, |
809 | *head = (struct quehead *)b; | |
1c79356b A |
810 | |
811 | element->qh_link = head->qh_link; | |
812 | element->qh_rlink = head; | |
813 | head->qh_link = element; | |
814 | element->qh_link->qh_rlink = element; | |
815 | } | |
816 | ||
817 | static __inline void | |
818 | remque(void *a) | |
819 | { | |
2d21ac55 | 820 | struct quehead *element = (struct quehead *)a; |
1c79356b A |
821 | |
822 | element->qh_link->qh_rlink = element->qh_rlink; | |
823 | element->qh_rlink->qh_link = element->qh_link; | |
824 | element->qh_rlink = 0; | |
825 | } | |
826 | ||
827 | #else /* !__GNUC__ */ | |
828 | ||
91447636 A |
829 | void insque(void *a, void *b); |
830 | void remque(void *a); | |
1c79356b A |
831 | |
832 | #endif /* __GNUC__ */ | |
833 | ||
834 | #endif | |
2d21ac55 | 835 | #endif /* _KERNEL */ |
1c79356b A |
836 | |
837 | #endif /* !_SYS_QUEUE_H_ */ |