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