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