]>
Commit | Line | Data |
---|---|---|
8429abe0 RW |
1 | /* $OpenBSD: queue.h,v 1.43 2015/12/28 19:38:40 millert Exp $ */ |
2 | /* $NetBSD: queue.h,v 1.11 1996/05/16 05:17:14 mycroft Exp $ */ | |
3 | ||
4 | /* | |
5 | * Copyright (c) 1991, 1993 | |
6 | * The Regents of the University of California. All rights reserved. | |
7 | * | |
8 | * Redistribution and use in source and binary forms, with or without | |
9 | * modification, are permitted provided that the following conditions | |
10 | * are met: | |
11 | * 1. Redistributions of source code must retain the above copyright | |
12 | * notice, this list of conditions and the following disclaimer. | |
13 | * 2. Redistributions in binary form must reproduce the above copyright | |
14 | * notice, this list of conditions and the following disclaimer in the | |
15 | * documentation and/or other materials provided with the distribution. | |
16 | * 3. Neither the name of the University nor the names of its contributors | |
17 | * may be used to endorse or promote products derived from this software | |
18 | * without specific prior written permission. | |
19 | * | |
20 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
21 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
23 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
26 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
27 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
28 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
29 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
30 | * SUCH DAMAGE. | |
31 | * | |
32 | * @(#)queue.h 8.5 (Berkeley) 8/20/94 | |
33 | */ | |
34 | ||
d62a17ae | 35 | #ifndef _SYS_QUEUE_H_ |
8429abe0 RW |
36 | #define _SYS_QUEUE_H_ |
37 | ||
5e244469 RW |
38 | #ifdef __cplusplus |
39 | extern "C" { | |
40 | #endif | |
41 | ||
8429abe0 RW |
42 | /* |
43 | * This file defines five types of data structures: singly-linked lists, | |
44 | * lists, simple queues, tail queues and XOR simple queues. | |
45 | * | |
46 | * | |
47 | * A singly-linked list is headed by a single forward pointer. The elements | |
48 | * are singly linked for minimum space and pointer manipulation overhead at | |
49 | * the expense of O(n) removal for arbitrary elements. New elements can be | |
50 | * added to the list after an existing element or at the head of the list. | |
51 | * Elements being removed from the head of the list should use the explicit | |
52 | * macro for this purpose for optimum efficiency. A singly-linked list may | |
53 | * only be traversed in the forward direction. Singly-linked lists are ideal | |
54 | * for applications with large datasets and few or no removals or for | |
55 | * implementing a LIFO queue. | |
56 | * | |
57 | * A list is headed by a single forward pointer (or an array of forward | |
58 | * pointers for a hash table header). The elements are doubly linked | |
59 | * so that an arbitrary element can be removed without a need to | |
60 | * traverse the list. New elements can be added to the list before | |
61 | * or after an existing element or at the head of the list. A list | |
62 | * may only be traversed in the forward direction. | |
63 | * | |
64 | * A simple queue is headed by a pair of pointers, one to the head of the | |
65 | * list and the other to the tail of the list. The elements are singly | |
66 | * linked to save space, so elements can only be removed from the | |
67 | * head of the list. New elements can be added to the list before or after | |
68 | * an existing element, at the head of the list, or at the end of the | |
69 | * list. A simple queue may only be traversed in the forward direction. | |
70 | * | |
71 | * A tail queue is headed by a pair of pointers, one to the head of the | |
72 | * list and the other to the tail of the list. The elements are doubly | |
73 | * linked so that an arbitrary element can be removed without a need to | |
74 | * traverse the list. New elements can be added to the list before or | |
75 | * after an existing element, at the head of the list, or at the end of | |
76 | * the list. A tail queue may be traversed in either direction. | |
77 | * | |
78 | * An XOR simple queue is used in the same way as a regular simple queue. | |
79 | * The difference is that the head structure also includes a "cookie" that | |
80 | * is XOR'd with the queue pointer (first, last or next) to generate the | |
81 | * real pointer value. | |
82 | * | |
83 | * For details on the use of these macros, see the queue(3) manual page. | |
84 | */ | |
85 | ||
86 | #if defined(QUEUE_MACRO_DEBUG) || (defined(_KERNEL) && defined(DIAGNOSTIC)) | |
87 | #define _Q_INVALIDATE(a) (a) = ((void *)-1) | |
88 | #else | |
89 | #define _Q_INVALIDATE(a) | |
90 | #endif | |
91 | ||
92 | /* | |
93 | * Singly-linked List definitions. | |
94 | */ | |
d62a17ae | 95 | #define SLIST_HEAD(name, type) \ |
96 | struct name { \ | |
97 | struct type *slh_first; /* first element */ \ | |
98 | } | |
8429abe0 | 99 | |
d62a17ae | 100 | #define SLIST_HEAD_INITIALIZER(head) \ |
101 | { \ | |
102 | NULL \ | |
103 | } | |
8429abe0 | 104 | |
d62a17ae | 105 | #define SLIST_ENTRY(type) \ |
106 | struct { \ | |
107 | struct type *sle_next; /* next element */ \ | |
108 | } | |
8429abe0 RW |
109 | |
110 | /* | |
111 | * Singly-linked List access methods. | |
112 | */ | |
113 | #define SLIST_FIRST(head) ((head)->slh_first) | |
114 | #define SLIST_END(head) NULL | |
115 | #define SLIST_EMPTY(head) (SLIST_FIRST(head) == SLIST_END(head)) | |
116 | #define SLIST_NEXT(elm, field) ((elm)->field.sle_next) | |
117 | ||
d62a17ae | 118 | #define SLIST_FOREACH(var, head, field) \ |
119 | for ((var) = SLIST_FIRST(head); (var) != SLIST_END(head); \ | |
120 | (var) = SLIST_NEXT(var, field)) | |
8429abe0 | 121 | |
d62a17ae | 122 | #define SLIST_FOREACH_SAFE(var, head, field, tvar) \ |
123 | for ((var) = SLIST_FIRST(head); \ | |
124 | (var) && ((tvar) = SLIST_NEXT(var, field), 1); (var) = (tvar)) | |
8429abe0 RW |
125 | |
126 | /* | |
127 | * Singly-linked List functions. | |
128 | */ | |
d62a17ae | 129 | #define SLIST_INIT(head) \ |
130 | { \ | |
131 | SLIST_FIRST(head) = SLIST_END(head); \ | |
132 | } | |
133 | ||
134 | #define SLIST_INSERT_AFTER(slistelm, elm, field) \ | |
135 | do { \ | |
136 | (elm)->field.sle_next = (slistelm)->field.sle_next; \ | |
137 | (slistelm)->field.sle_next = (elm); \ | |
138 | } while (0) | |
139 | ||
140 | #define SLIST_INSERT_HEAD(head, elm, field) \ | |
141 | do { \ | |
142 | (elm)->field.sle_next = (head)->slh_first; \ | |
143 | (head)->slh_first = (elm); \ | |
144 | } while (0) | |
145 | ||
146 | #define SLIST_REMOVE_AFTER(elm, field) \ | |
147 | do { \ | |
148 | (elm)->field.sle_next = (elm)->field.sle_next->field.sle_next; \ | |
149 | } while (0) | |
150 | ||
151 | #define SLIST_REMOVE_HEAD(head, field) \ | |
152 | do { \ | |
153 | (head)->slh_first = (head)->slh_first->field.sle_next; \ | |
154 | } while (0) | |
155 | ||
156 | #define SLIST_REMOVE(head, elm, type, field) \ | |
157 | do { \ | |
158 | if ((head)->slh_first == (elm)) { \ | |
159 | SLIST_REMOVE_HEAD((head), field); \ | |
160 | } else { \ | |
161 | struct type *curelm = (head)->slh_first; \ | |
162 | \ | |
163 | while (curelm->field.sle_next != (elm)) \ | |
164 | curelm = curelm->field.sle_next; \ | |
165 | curelm->field.sle_next = \ | |
166 | curelm->field.sle_next->field.sle_next; \ | |
167 | } \ | |
168 | _Q_INVALIDATE((elm)->field.sle_next); \ | |
169 | } while (0) | |
8429abe0 RW |
170 | |
171 | /* | |
172 | * List definitions. | |
173 | */ | |
d62a17ae | 174 | #define LIST_HEAD(name, type) \ |
175 | struct name { \ | |
176 | struct type *lh_first; /* first element */ \ | |
177 | } | |
178 | ||
179 | #define LIST_HEAD_INITIALIZER(head) \ | |
180 | { \ | |
181 | NULL \ | |
182 | } | |
183 | ||
184 | #define LIST_ENTRY(type) \ | |
185 | struct { \ | |
186 | struct type *le_next; /* next element */ \ | |
187 | struct type **le_prev; /* address of previous next element */ \ | |
188 | } | |
8429abe0 RW |
189 | |
190 | /* | |
191 | * List access methods. | |
192 | */ | |
193 | #define LIST_FIRST(head) ((head)->lh_first) | |
194 | #define LIST_END(head) NULL | |
195 | #define LIST_EMPTY(head) (LIST_FIRST(head) == LIST_END(head)) | |
196 | #define LIST_NEXT(elm, field) ((elm)->field.le_next) | |
197 | ||
d62a17ae | 198 | #define LIST_FOREACH(var, head, field) \ |
199 | for ((var) = LIST_FIRST(head); (var) != LIST_END(head); \ | |
200 | (var) = LIST_NEXT(var, field)) | |
8429abe0 | 201 | |
d62a17ae | 202 | #define LIST_FOREACH_SAFE(var, head, field, tvar) \ |
203 | for ((var) = LIST_FIRST(head); \ | |
204 | (var) && ((tvar) = LIST_NEXT(var, field), 1); (var) = (tvar)) | |
8429abe0 RW |
205 | |
206 | /* | |
207 | * List functions. | |
208 | */ | |
d62a17ae | 209 | #define LIST_INIT(head) \ |
210 | do { \ | |
211 | LIST_FIRST(head) = LIST_END(head); \ | |
212 | } while (0) | |
213 | ||
214 | #define LIST_INSERT_AFTER(listelm, elm, field) \ | |
215 | do { \ | |
216 | if (((elm)->field.le_next = (listelm)->field.le_next) != NULL) \ | |
217 | (listelm)->field.le_next->field.le_prev = \ | |
218 | &(elm)->field.le_next; \ | |
219 | (listelm)->field.le_next = (elm); \ | |
220 | (elm)->field.le_prev = &(listelm)->field.le_next; \ | |
221 | } while (0) | |
222 | ||
223 | #define LIST_INSERT_BEFORE(listelm, elm, field) \ | |
224 | do { \ | |
225 | (elm)->field.le_prev = (listelm)->field.le_prev; \ | |
226 | (elm)->field.le_next = (listelm); \ | |
227 | *(listelm)->field.le_prev = (elm); \ | |
228 | (listelm)->field.le_prev = &(elm)->field.le_next; \ | |
229 | } while (0) | |
230 | ||
231 | #define LIST_INSERT_HEAD(head, elm, field) \ | |
232 | do { \ | |
233 | if (((elm)->field.le_next = (head)->lh_first) != NULL) \ | |
234 | (head)->lh_first->field.le_prev = \ | |
235 | &(elm)->field.le_next; \ | |
236 | (head)->lh_first = (elm); \ | |
237 | (elm)->field.le_prev = &(head)->lh_first; \ | |
238 | } while (0) | |
239 | ||
240 | #define LIST_REMOVE(elm, field) \ | |
241 | do { \ | |
242 | if ((elm)->field.le_next != NULL) \ | |
243 | (elm)->field.le_next->field.le_prev = \ | |
244 | (elm)->field.le_prev; \ | |
245 | *(elm)->field.le_prev = (elm)->field.le_next; \ | |
246 | _Q_INVALIDATE((elm)->field.le_prev); \ | |
247 | _Q_INVALIDATE((elm)->field.le_next); \ | |
248 | } while (0) | |
249 | ||
250 | #define LIST_REPLACE(elm, elm2, field) \ | |
251 | do { \ | |
252 | if (((elm2)->field.le_next = (elm)->field.le_next) != NULL) \ | |
253 | (elm2)->field.le_next->field.le_prev = \ | |
254 | &(elm2)->field.le_next; \ | |
255 | (elm2)->field.le_prev = (elm)->field.le_prev; \ | |
256 | *(elm2)->field.le_prev = (elm2); \ | |
257 | _Q_INVALIDATE((elm)->field.le_prev); \ | |
258 | _Q_INVALIDATE((elm)->field.le_next); \ | |
259 | } while (0) | |
8429abe0 RW |
260 | |
261 | /* | |
262 | * Simple queue definitions. | |
263 | */ | |
d62a17ae | 264 | #define SIMPLEQ_HEAD(name, type) \ |
265 | struct name { \ | |
266 | struct type *sqh_first; /* first element */ \ | |
267 | struct type **sqh_last; /* addr of last next element */ \ | |
268 | } | |
269 | ||
270 | #define SIMPLEQ_HEAD_INITIALIZER(head) \ | |
271 | { \ | |
272 | NULL, &(head).sqh_first \ | |
273 | } | |
274 | ||
275 | #define SIMPLEQ_ENTRY(type) \ | |
276 | struct { \ | |
277 | struct type *sqe_next; /* next element */ \ | |
278 | } | |
8429abe0 RW |
279 | |
280 | /* | |
281 | * Simple queue access methods. | |
282 | */ | |
283 | #define SIMPLEQ_FIRST(head) ((head)->sqh_first) | |
284 | #define SIMPLEQ_END(head) NULL | |
285 | #define SIMPLEQ_EMPTY(head) (SIMPLEQ_FIRST(head) == SIMPLEQ_END(head)) | |
286 | #define SIMPLEQ_NEXT(elm, field) ((elm)->field.sqe_next) | |
287 | ||
d62a17ae | 288 | #define SIMPLEQ_FOREACH(var, head, field) \ |
289 | for ((var) = SIMPLEQ_FIRST(head); (var) != SIMPLEQ_END(head); \ | |
290 | (var) = SIMPLEQ_NEXT(var, field)) | |
8429abe0 | 291 | |
d62a17ae | 292 | #define SIMPLEQ_FOREACH_SAFE(var, head, field, tvar) \ |
293 | for ((var) = SIMPLEQ_FIRST(head); \ | |
294 | (var) && ((tvar) = SIMPLEQ_NEXT(var, field), 1); (var) = (tvar)) | |
8429abe0 RW |
295 | |
296 | /* | |
297 | * Simple queue functions. | |
298 | */ | |
d62a17ae | 299 | #define SIMPLEQ_INIT(head) \ |
300 | do { \ | |
301 | (head)->sqh_first = NULL; \ | |
302 | (head)->sqh_last = &(head)->sqh_first; \ | |
303 | } while (0) | |
304 | ||
305 | #define SIMPLEQ_INSERT_HEAD(head, elm, field) \ | |
306 | do { \ | |
307 | if (((elm)->field.sqe_next = (head)->sqh_first) == NULL) \ | |
308 | (head)->sqh_last = &(elm)->field.sqe_next; \ | |
309 | (head)->sqh_first = (elm); \ | |
310 | } while (0) | |
311 | ||
312 | #define SIMPLEQ_INSERT_TAIL(head, elm, field) \ | |
313 | do { \ | |
314 | (elm)->field.sqe_next = NULL; \ | |
315 | *(head)->sqh_last = (elm); \ | |
316 | (head)->sqh_last = &(elm)->field.sqe_next; \ | |
317 | } while (0) | |
318 | ||
319 | #define SIMPLEQ_INSERT_AFTER(head, listelm, elm, field) \ | |
320 | do { \ | |
321 | if (((elm)->field.sqe_next = (listelm)->field.sqe_next) \ | |
322 | == NULL) \ | |
323 | (head)->sqh_last = &(elm)->field.sqe_next; \ | |
324 | (listelm)->field.sqe_next = (elm); \ | |
325 | } while (0) | |
326 | ||
327 | #define SIMPLEQ_REMOVE_HEAD(head, field) \ | |
328 | do { \ | |
329 | if (((head)->sqh_first = (head)->sqh_first->field.sqe_next) \ | |
330 | == NULL) \ | |
331 | (head)->sqh_last = &(head)->sqh_first; \ | |
332 | } while (0) | |
333 | ||
334 | #define SIMPLEQ_REMOVE_AFTER(head, elm, field) \ | |
335 | do { \ | |
336 | if (((elm)->field.sqe_next = \ | |
337 | (elm)->field.sqe_next->field.sqe_next) \ | |
338 | == NULL) \ | |
339 | (head)->sqh_last = &(elm)->field.sqe_next; \ | |
340 | } while (0) | |
341 | ||
342 | #define SIMPLEQ_CONCAT(head1, head2) \ | |
343 | do { \ | |
344 | if (!SIMPLEQ_EMPTY((head2))) { \ | |
345 | *(head1)->sqh_last = (head2)->sqh_first; \ | |
346 | (head1)->sqh_last = (head2)->sqh_last; \ | |
347 | SIMPLEQ_INIT((head2)); \ | |
348 | } \ | |
349 | } while (0) | |
8429abe0 RW |
350 | |
351 | /* | |
352 | * XOR Simple queue definitions. | |
353 | */ | |
d62a17ae | 354 | #define XSIMPLEQ_HEAD(name, type) \ |
355 | struct name { \ | |
356 | struct type *sqx_first; /* first element */ \ | |
357 | struct type **sqx_last; /* addr of last next element */ \ | |
358 | unsigned long sqx_cookie; \ | |
359 | } | |
360 | ||
361 | #define XSIMPLEQ_ENTRY(type) \ | |
362 | struct { \ | |
363 | struct type *sqx_next; /* next element */ \ | |
364 | } | |
8429abe0 RW |
365 | |
366 | /* | |
367 | * XOR Simple queue access methods. | |
368 | */ | |
d62a17ae | 369 | #define XSIMPLEQ_XOR(head, ptr) \ |
370 | ((__typeof(ptr))((head)->sqx_cookie ^ (unsigned long)(ptr))) | |
8429abe0 RW |
371 | #define XSIMPLEQ_FIRST(head) XSIMPLEQ_XOR(head, ((head)->sqx_first)) |
372 | #define XSIMPLEQ_END(head) NULL | |
373 | #define XSIMPLEQ_EMPTY(head) (XSIMPLEQ_FIRST(head) == XSIMPLEQ_END(head)) | |
374 | #define XSIMPLEQ_NEXT(head, elm, field) XSIMPLEQ_XOR(head, ((elm)->field.sqx_next)) | |
375 | ||
d62a17ae | 376 | #define XSIMPLEQ_FOREACH(var, head, field) \ |
377 | for ((var) = XSIMPLEQ_FIRST(head); (var) != XSIMPLEQ_END(head); \ | |
378 | (var) = XSIMPLEQ_NEXT(head, var, field)) | |
8429abe0 | 379 | |
d62a17ae | 380 | #define XSIMPLEQ_FOREACH_SAFE(var, head, field, tvar) \ |
381 | for ((var) = XSIMPLEQ_FIRST(head); \ | |
382 | (var) && ((tvar) = XSIMPLEQ_NEXT(head, var, field), 1); \ | |
383 | (var) = (tvar)) | |
8429abe0 RW |
384 | |
385 | /* | |
386 | * XOR Simple queue functions. | |
387 | */ | |
d62a17ae | 388 | #define XSIMPLEQ_INIT(head) \ |
389 | do { \ | |
390 | arc4random_buf(&(head)->sqx_cookie, \ | |
391 | sizeof((head)->sqx_cookie)); \ | |
392 | (head)->sqx_first = XSIMPLEQ_XOR(head, NULL); \ | |
393 | (head)->sqx_last = XSIMPLEQ_XOR(head, &(head)->sqx_first); \ | |
394 | } while (0) | |
395 | ||
396 | #define XSIMPLEQ_INSERT_HEAD(head, elm, field) \ | |
397 | do { \ | |
398 | if (((elm)->field.sqx_next = (head)->sqx_first) \ | |
399 | == XSIMPLEQ_XOR(head, NULL)) \ | |
400 | (head)->sqx_last = \ | |
401 | XSIMPLEQ_XOR(head, &(elm)->field.sqx_next); \ | |
402 | (head)->sqx_first = XSIMPLEQ_XOR(head, (elm)); \ | |
403 | } while (0) | |
404 | ||
405 | #define XSIMPLEQ_INSERT_TAIL(head, elm, field) \ | |
406 | do { \ | |
407 | (elm)->field.sqx_next = XSIMPLEQ_XOR(head, NULL); \ | |
408 | *(XSIMPLEQ_XOR(head, (head)->sqx_last)) = \ | |
409 | XSIMPLEQ_XOR(head, (elm)); \ | |
8429abe0 | 410 | (head)->sqx_last = XSIMPLEQ_XOR(head, &(elm)->field.sqx_next); \ |
d62a17ae | 411 | } while (0) |
412 | ||
413 | #define XSIMPLEQ_INSERT_AFTER(head, listelm, elm, field) \ | |
414 | do { \ | |
415 | if (((elm)->field.sqx_next = (listelm)->field.sqx_next) \ | |
416 | == XSIMPLEQ_XOR(head, NULL)) \ | |
417 | (head)->sqx_last = \ | |
418 | XSIMPLEQ_XOR(head, &(elm)->field.sqx_next); \ | |
419 | (listelm)->field.sqx_next = XSIMPLEQ_XOR(head, (elm)); \ | |
420 | } while (0) | |
421 | ||
422 | #define XSIMPLEQ_REMOVE_HEAD(head, field) \ | |
423 | do { \ | |
424 | if (((head)->sqx_first = XSIMPLEQ_XOR(head, (head)->sqx_first) \ | |
425 | ->field.sqx_next) \ | |
426 | == XSIMPLEQ_XOR(head, NULL)) \ | |
427 | (head)->sqx_last = \ | |
428 | XSIMPLEQ_XOR(head, &(head)->sqx_first); \ | |
429 | } while (0) | |
430 | ||
431 | #define XSIMPLEQ_REMOVE_AFTER(head, elm, field) \ | |
432 | do { \ | |
433 | if (((elm)->field.sqx_next = \ | |
434 | XSIMPLEQ_XOR(head, (elm)->field.sqx_next) \ | |
435 | ->field.sqx_next) \ | |
436 | == XSIMPLEQ_XOR(head, NULL)) \ | |
437 | (head)->sqx_last = \ | |
438 | XSIMPLEQ_XOR(head, &(elm)->field.sqx_next); \ | |
439 | } while (0) | |
8429abe0 RW |
440 | |
441 | ||
442 | /* | |
443 | * Tail queue definitions. | |
444 | */ | |
d62a17ae | 445 | #define TAILQ_HEAD(name, type) \ |
446 | struct name { \ | |
447 | struct type *tqh_first; /* first element */ \ | |
448 | struct type **tqh_last; /* addr of last next element */ \ | |
449 | } | |
450 | ||
451 | #define TAILQ_HEAD_INITIALIZER(head) \ | |
452 | { \ | |
453 | NULL, &(head).tqh_first \ | |
454 | } | |
455 | ||
456 | #define TAILQ_ENTRY(type) \ | |
457 | struct { \ | |
458 | struct type *tqe_next; /* next element */ \ | |
459 | struct type **tqe_prev; /* address of previous next element */ \ | |
460 | } | |
8429abe0 RW |
461 | |
462 | /* | |
463 | * Tail queue access methods. | |
464 | */ | |
465 | #define TAILQ_FIRST(head) ((head)->tqh_first) | |
466 | #define TAILQ_END(head) NULL | |
467 | #define TAILQ_NEXT(elm, field) ((elm)->field.tqe_next) | |
d62a17ae | 468 | #define TAILQ_LAST(head, headname) \ |
8429abe0 RW |
469 | (*(((struct headname *)((head)->tqh_last))->tqh_last)) |
470 | /* XXX */ | |
d62a17ae | 471 | #define TAILQ_PREV(elm, headname, field) \ |
8429abe0 | 472 | (*(((struct headname *)((elm)->field.tqe_prev))->tqh_last)) |
d62a17ae | 473 | #define TAILQ_EMPTY(head) (TAILQ_FIRST(head) == TAILQ_END(head)) |
8429abe0 | 474 | |
d62a17ae | 475 | #define TAILQ_FOREACH(var, head, field) \ |
476 | for ((var) = TAILQ_FIRST(head); (var) != TAILQ_END(head); \ | |
477 | (var) = TAILQ_NEXT(var, field)) | |
8429abe0 | 478 | |
d62a17ae | 479 | #define TAILQ_FOREACH_SAFE(var, head, field, tvar) \ |
480 | for ((var) = TAILQ_FIRST(head); \ | |
481 | (var) != TAILQ_END(head) && ((tvar) = TAILQ_NEXT(var, field), 1); \ | |
482 | (var) = (tvar)) | |
8429abe0 RW |
483 | |
484 | ||
d62a17ae | 485 | #define TAILQ_FOREACH_REVERSE(var, head, headname, field) \ |
486 | for ((var) = TAILQ_LAST(head, headname); (var) != TAILQ_END(head); \ | |
487 | (var) = TAILQ_PREV(var, headname, field)) | |
8429abe0 | 488 | |
d62a17ae | 489 | #define TAILQ_FOREACH_REVERSE_SAFE(var, head, headname, field, tvar) \ |
490 | for ((var) = TAILQ_LAST(head, headname); \ | |
491 | (var) != TAILQ_END(head) \ | |
492 | && ((tvar) = TAILQ_PREV(var, headname, field), 1); \ | |
493 | (var) = (tvar)) | |
8429abe0 RW |
494 | |
495 | /* | |
496 | * Tail queue functions. | |
497 | */ | |
d62a17ae | 498 | #define TAILQ_INIT(head) \ |
499 | do { \ | |
500 | (head)->tqh_first = NULL; \ | |
501 | (head)->tqh_last = &(head)->tqh_first; \ | |
502 | } while (0) | |
503 | ||
504 | #define TAILQ_INSERT_HEAD(head, elm, field) \ | |
505 | do { \ | |
506 | if (((elm)->field.tqe_next = (head)->tqh_first) != NULL) \ | |
507 | (head)->tqh_first->field.tqe_prev = \ | |
508 | &(elm)->field.tqe_next; \ | |
509 | else \ | |
510 | (head)->tqh_last = &(elm)->field.tqe_next; \ | |
511 | (head)->tqh_first = (elm); \ | |
512 | (elm)->field.tqe_prev = &(head)->tqh_first; \ | |
513 | } while (0) | |
514 | ||
515 | #define TAILQ_INSERT_TAIL(head, elm, field) \ | |
516 | do { \ | |
517 | (elm)->field.tqe_next = NULL; \ | |
518 | (elm)->field.tqe_prev = (head)->tqh_last; \ | |
519 | *(head)->tqh_last = (elm); \ | |
520 | (head)->tqh_last = &(elm)->field.tqe_next; \ | |
521 | } while (0) | |
522 | ||
523 | #define TAILQ_INSERT_AFTER(head, listelm, elm, field) \ | |
524 | do { \ | |
525 | if (((elm)->field.tqe_next = (listelm)->field.tqe_next) \ | |
526 | != NULL) \ | |
527 | (elm)->field.tqe_next->field.tqe_prev = \ | |
528 | &(elm)->field.tqe_next; \ | |
529 | else \ | |
530 | (head)->tqh_last = &(elm)->field.tqe_next; \ | |
531 | (listelm)->field.tqe_next = (elm); \ | |
532 | (elm)->field.tqe_prev = &(listelm)->field.tqe_next; \ | |
533 | } while (0) | |
534 | ||
535 | #define TAILQ_INSERT_BEFORE(listelm, elm, field) \ | |
536 | do { \ | |
537 | (elm)->field.tqe_prev = (listelm)->field.tqe_prev; \ | |
538 | (elm)->field.tqe_next = (listelm); \ | |
539 | *(listelm)->field.tqe_prev = (elm); \ | |
540 | (listelm)->field.tqe_prev = &(elm)->field.tqe_next; \ | |
541 | } while (0) | |
542 | ||
543 | #define TAILQ_REMOVE(head, elm, field) \ | |
544 | do { \ | |
545 | if (((elm)->field.tqe_next) != NULL) \ | |
546 | (elm)->field.tqe_next->field.tqe_prev = \ | |
547 | (elm)->field.tqe_prev; \ | |
548 | else \ | |
549 | (head)->tqh_last = (elm)->field.tqe_prev; \ | |
550 | *(elm)->field.tqe_prev = (elm)->field.tqe_next; \ | |
551 | _Q_INVALIDATE((elm)->field.tqe_prev); \ | |
552 | _Q_INVALIDATE((elm)->field.tqe_next); \ | |
553 | } while (0) | |
554 | ||
555 | #define TAILQ_REPLACE(head, elm, elm2, field) \ | |
556 | do { \ | |
557 | if (((elm2)->field.tqe_next = (elm)->field.tqe_next) != NULL) \ | |
558 | (elm2)->field.tqe_next->field.tqe_prev = \ | |
559 | &(elm2)->field.tqe_next; \ | |
560 | else \ | |
561 | (head)->tqh_last = &(elm2)->field.tqe_next; \ | |
562 | (elm2)->field.tqe_prev = (elm)->field.tqe_prev; \ | |
563 | *(elm2)->field.tqe_prev = (elm2); \ | |
564 | _Q_INVALIDATE((elm)->field.tqe_prev); \ | |
565 | _Q_INVALIDATE((elm)->field.tqe_next); \ | |
566 | } while (0) | |
567 | ||
568 | #define TAILQ_CONCAT(head1, head2, field) \ | |
569 | do { \ | |
570 | if (!TAILQ_EMPTY(head2)) { \ | |
571 | *(head1)->tqh_last = (head2)->tqh_first; \ | |
572 | (head2)->tqh_first->field.tqe_prev = \ | |
573 | (head1)->tqh_last; \ | |
574 | (head1)->tqh_last = (head2)->tqh_last; \ | |
575 | TAILQ_INIT((head2)); \ | |
576 | } \ | |
577 | } while (0) | |
578 | ||
5e244469 RW |
579 | #ifdef __cplusplus |
580 | } | |
581 | #endif | |
582 | ||
d62a17ae | 583 | #endif /* !_SYS_QUEUE_H_ */ |