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