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