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