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 * 3. 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 #include <sys/cdefs.h>
37
38 /*
39 * This file defines four types of data structures: singly-linked lists,
40 * singly-linked tail queues, lists and tail queues.
41 *
42 * A singly-linked list is headed by a single forward pointer. The elements
43 * are singly linked for minimum space and pointer manipulation overhead at
44 * the expense of O(n) removal for arbitrary elements. New elements can be
45 * added to the list after an existing element or at the head of the list.
46 * Elements being removed from the head of the list should use the explicit
47 * macro for this purpose for optimum efficiency. A singly-linked list may
48 * only be traversed in the forward direction. Singly-linked lists are ideal
49 * for applications with large datasets and few or no removals or for
50 * implementing a LIFO queue.
51 *
52 * A singly-linked tail queue is headed by a pair of pointers, one to the
53 * head of the list and the other to the tail of the list. The elements are
54 * singly linked for minimum space and pointer manipulation overhead at the
55 * expense of O(n) removal for arbitrary elements. New elements can be added
56 * to the list after an existing element, at the head of the list, or at the
57 * end of the list. Elements being removed from the head of the tail queue
58 * should use the explicit macro for this purpose for optimum efficiency.
59 * A singly-linked tail queue may only be traversed in the forward direction.
60 * Singly-linked tail queues are ideal for applications with large datasets
61 * and few or no removals or for implementing a FIFO queue.
62 *
63 * A list is headed by a single forward pointer (or an array of forward
64 * pointers for a hash table header). The elements are doubly linked
65 * so that an arbitrary element can be removed without a need to
66 * traverse the list. New elements can be added to the list before
67 * or after an existing element or at the head of the list. A list
68 * may be traversed in either direction.
69 *
70 * A tail queue is headed by a pair of pointers, one to the head of the
71 * list and the other to the tail of the list. The elements are doubly
72 * linked so that an arbitrary element can be removed without a need to
73 * traverse the list. New elements can be added to the list before or
74 * after an existing element, at the head of the list, or at the end of
75 * the list. A tail queue may be traversed in either direction.
76 *
77 * For details on the use of these macros, see the queue(3) manual page.
78 *
79 * Below is a summary of implemented functions where:
80 * + means the macro is available
81 * - means the macro is not available
82 * s means the macro is available but is slow (runs in O(n) time)
83 *
84 * SLIST LIST STAILQ TAILQ
85 * _HEAD + + + +
86 * _CLASS_HEAD + + + +
87 * _HEAD_INITIALIZER + + + +
88 * _ENTRY + + + +
89 * _CLASS_ENTRY + + + +
90 * _INIT + + + +
91 * _EMPTY + + + +
92 * _FIRST + + + +
93 * _NEXT + + + +
94 * _PREV - + - +
95 * _LAST - - + +
96 * _LAST_FAST - - - +
97 * _FOREACH + + + +
98 * _FOREACH_FROM + + + +
99 * _FOREACH_SAFE + + + +
100 * _FOREACH_FROM_SAFE + + + +
101 * _FOREACH_REVERSE - - - +
102 * _FOREACH_REVERSE_FROM - - - +
103 * _FOREACH_REVERSE_SAFE - - - +
104 * _FOREACH_REVERSE_FROM_SAFE - - - +
105 * _INSERT_HEAD + + + +
106 * _INSERT_BEFORE - + - +
107 * _INSERT_AFTER + + + +
108 * _INSERT_TAIL - - + +
109 * _CONCAT s s + +
110 * _REMOVE_AFTER + - + -
111 * _REMOVE_HEAD + - + -
112 * _REMOVE s + s +
113 * _SWAP + + + +
114 *
115 */
116 #ifdef QUEUE_MACRO_DEBUG
117 #warn Use QUEUE_MACRO_DEBUG_TRACE and/or QUEUE_MACRO_DEBUG_TRASH
118 #define QUEUE_MACRO_DEBUG_TRACE
119 #define QUEUE_MACRO_DEBUG_TRASH
120 #endif
121
122 #ifdef QUEUE_MACRO_DEBUG_TRACE
123 /* Store the last 2 places the queue element or head was altered */
124 struct qm_trace {
125 unsigned long lastline;
126 unsigned long prevline;
127 const char *lastfile;
128 const char *prevfile;
129 };
130
131 #define TRACEBUF struct qm_trace trace;
132 #define TRACEBUF_INITIALIZER { __LINE__, 0, __FILE__, NULL } ,
133
134 #define QMD_TRACE_HEAD(head) do { \
135 (head)->trace.prevline = (head)->trace.lastline; \
136 (head)->trace.prevfile = (head)->trace.lastfile; \
137 (head)->trace.lastline = __LINE__; \
138 (head)->trace.lastfile = __FILE__; \
139 } while (0)
140
141 #define QMD_TRACE_ELEM(elem) do { \
142 (elem)->trace.prevline = (elem)->trace.lastline; \
143 (elem)->trace.prevfile = (elem)->trace.lastfile; \
144 (elem)->trace.lastline = __LINE__; \
145 (elem)->trace.lastfile = __FILE__; \
146 } while (0)
147
148 #else /* !QUEUE_MACRO_DEBUG_TRACE */
149 #define QMD_TRACE_ELEM(elem)
150 #define QMD_TRACE_HEAD(head)
151 #define TRACEBUF
152 #define TRACEBUF_INITIALIZER
153 #endif /* QUEUE_MACRO_DEBUG_TRACE */
154
155 #ifdef QUEUE_MACRO_DEBUG_TRASH
156 #define TRASHIT(x) do {(x) = (void *)-1;} while (0)
157 #define QMD_IS_TRASHED(x) ((x) == (void *)(intptr_t)-1)
158 #else /* !QUEUE_MACRO_DEBUG_TRASH */
159 #define TRASHIT(x)
160 #define QMD_IS_TRASHED(x) 0
161 #endif /* QUEUE_MACRO_DEBUG_TRASH */
162
163 #if defined(QUEUE_MACRO_DEBUG_TRACE) || defined(QUEUE_MACRO_DEBUG_TRASH)
164 #define QMD_SAVELINK(name, link) void **name = (void *)&(link)
165 #else /* !QUEUE_MACRO_DEBUG_TRACE && !QUEUE_MACRO_DEBUG_TRASH */
166 #define QMD_SAVELINK(name, link)
167 #endif /* QUEUE_MACRO_DEBUG_TRACE || QUEUE_MACRO_DEBUG_TRASH */
168
169 #ifdef __cplusplus
170 /*
171 * In C++ there can be structure lists and class lists:
172 */
173 #define QUEUE_TYPEOF(type) type
174 #else
175 #define QUEUE_TYPEOF(type) struct type
176 #endif
177
178 /*
179 * Singly-linked List declarations.
180 */
181 #define SLIST_HEAD(name, type) \
182 struct name { \
183 struct type *slh_first; /* first element */ \
184 }
185
186 #define SLIST_CLASS_HEAD(name, type) \
187 struct name { \
188 class type *slh_first; /* first element */ \
189 }
190
191 #define SLIST_HEAD_INITIALIZER(head) \
192 { NULL }
193
194 #define SLIST_ENTRY(type) \
195 struct { \
196 struct type *sle_next; /* next element */ \
197 }
198
199 #define SLIST_CLASS_ENTRY(type) \
200 struct { \
201 class type *sle_next; /* next element */ \
202 }
203
204 /*
205 * Singly-linked List functions.
206 */
207 #if (defined(_KERNEL) && defined(INVARIANTS))
208 #define QMD_SLIST_CHECK_PREVPTR(prevp, elm) do { \
209 if (*(prevp) != (elm)) \
210 panic("Bad prevptr *(%p) == %p != %p", \
211 (prevp), *(prevp), (elm)); \
212 } while (0)
213 #else
214 #define QMD_SLIST_CHECK_PREVPTR(prevp, elm)
215 #endif
216
217 #define SLIST_CONCAT(head1, head2, type, field) do { \
218 QUEUE_TYPEOF(type) *curelm = SLIST_FIRST(head1); \
219 if (curelm == NULL) { \
220 if ((SLIST_FIRST(head1) = SLIST_FIRST(head2)) != NULL) \
221 SLIST_INIT(head2); \
222 } else if (SLIST_FIRST(head2) != NULL) { \
223 while (SLIST_NEXT(curelm, field) != NULL) \
224 curelm = SLIST_NEXT(curelm, field); \
225 SLIST_NEXT(curelm, field) = SLIST_FIRST(head2); \
226 SLIST_INIT(head2); \
227 } \
228 } while (0)
229
230 #define SLIST_EMPTY(head) ((head)->slh_first == NULL)
231
232 #define SLIST_FIRST(head) ((head)->slh_first)
233
234 #define SLIST_FOREACH(var, head, field) \
235 for ((var) = SLIST_FIRST((head)); \
236 (var); \
237 (var) = SLIST_NEXT((var), field))
238
239 #define SLIST_FOREACH_FROM(var, head, field) \
240 for ((var) = ((var) ? (var) : SLIST_FIRST((head))); \
241 (var); \
242 (var) = SLIST_NEXT((var), field))
243
244 #define SLIST_FOREACH_SAFE(var, head, field, tvar) \
245 for ((var) = SLIST_FIRST((head)); \
246 (var) && ((tvar) = SLIST_NEXT((var), field), 1); \
247 (var) = (tvar))
248
249 #define SLIST_FOREACH_FROM_SAFE(var, head, field, tvar) \
250 for ((var) = ((var) ? (var) : SLIST_FIRST((head))); \
251 (var) && ((tvar) = SLIST_NEXT((var), field), 1); \
252 (var) = (tvar))
253
254 #define SLIST_FOREACH_PREVPTR(var, varp, head, field) \
255 for ((varp) = &SLIST_FIRST((head)); \
256 ((var) = *(varp)) != NULL; \
257 (varp) = &SLIST_NEXT((var), field))
258
259 #define SLIST_INIT(head) do { \
260 SLIST_FIRST((head)) = NULL; \
261 } while (0)
262
263 #define SLIST_INSERT_AFTER(slistelm, elm, field) do { \
264 SLIST_NEXT((elm), field) = SLIST_NEXT((slistelm), field); \
265 SLIST_NEXT((slistelm), field) = (elm); \
266 } while (0)
267
268 #define SLIST_INSERT_HEAD(head, elm, field) do { \
269 SLIST_NEXT((elm), field) = SLIST_FIRST((head)); \
270 SLIST_FIRST((head)) = (elm); \
271 } while (0)
272
273 #define SLIST_NEXT(elm, field) ((elm)->field.sle_next)
274
275 #define SLIST_REMOVE(head, elm, type, field) do { \
276 QMD_SAVELINK(oldnext, (elm)->field.sle_next); \
277 if (SLIST_FIRST((head)) == (elm)) { \
278 SLIST_REMOVE_HEAD((head), field); \
279 } \
280 else { \
281 QUEUE_TYPEOF(type) *curelm = SLIST_FIRST(head); \
282 while (SLIST_NEXT(curelm, field) != (elm)) \
283 curelm = SLIST_NEXT(curelm, field); \
284 SLIST_REMOVE_AFTER(curelm, field); \
285 } \
286 TRASHIT(*oldnext); \
287 } while (0)
288
289 #define SLIST_REMOVE_AFTER(elm, field) do { \
290 SLIST_NEXT(elm, field) = \
291 SLIST_NEXT(SLIST_NEXT(elm, field), field); \
292 } while (0)
293
294 #define SLIST_REMOVE_HEAD(head, field) do { \
295 SLIST_FIRST((head)) = SLIST_NEXT(SLIST_FIRST((head)), field); \
296 } while (0)
297
298 #define SLIST_REMOVE_PREVPTR(prevp, elm, field) do { \
299 QMD_SLIST_CHECK_PREVPTR(prevp, elm); \
300 *(prevp) = SLIST_NEXT(elm, field); \
301 TRASHIT((elm)->field.sle_next); \
302 } while (0)
303
304 #define SLIST_SWAP(head1, head2, type) do { \
305 QUEUE_TYPEOF(type) *swap_first = SLIST_FIRST(head1); \
306 SLIST_FIRST(head1) = SLIST_FIRST(head2); \
307 SLIST_FIRST(head2) = swap_first; \
308 } while (0)
309
310 /*
311 * Singly-linked Tail queue declarations.
312 */
313 #define STAILQ_HEAD(name, type) \
314 struct name { \
315 struct type *stqh_first;/* first element */ \
316 struct type **stqh_last;/* addr of last next element */ \
317 }
318
319 #define STAILQ_CLASS_HEAD(name, type) \
320 struct name { \
321 class type *stqh_first; /* first element */ \
322 class type **stqh_last; /* addr of last next element */ \
323 }
324
325 #define STAILQ_HEAD_INITIALIZER(head) \
326 { NULL, &(head).stqh_first }
327
328 #define STAILQ_ENTRY(type) \
329 struct { \
330 struct type *stqe_next; /* next element */ \
331 }
332
333 #define STAILQ_CLASS_ENTRY(type) \
334 struct { \
335 class type *stqe_next; /* next element */ \
336 }
337
338 /*
339 * Singly-linked Tail queue functions.
340 */
341 #define STAILQ_CONCAT(head1, head2) do { \
342 if (!STAILQ_EMPTY((head2))) { \
343 *(head1)->stqh_last = (head2)->stqh_first; \
344 (head1)->stqh_last = (head2)->stqh_last; \
345 STAILQ_INIT((head2)); \
346 } \
347 } while (0)
348
349 #define STAILQ_EMPTY(head) ((head)->stqh_first == NULL)
350
351 #define STAILQ_FIRST(head) ((head)->stqh_first)
352
353 #define STAILQ_FOREACH(var, head, field) \
354 for((var) = STAILQ_FIRST((head)); \
355 (var); \
356 (var) = STAILQ_NEXT((var), field))
357
358 #define STAILQ_FOREACH_FROM(var, head, field) \
359 for ((var) = ((var) ? (var) : STAILQ_FIRST((head))); \
360 (var); \
361 (var) = STAILQ_NEXT((var), field))
362
363 #define STAILQ_FOREACH_SAFE(var, head, field, tvar) \
364 for ((var) = STAILQ_FIRST((head)); \
365 (var) && ((tvar) = STAILQ_NEXT((var), field), 1); \
366 (var) = (tvar))
367
368 #define STAILQ_FOREACH_FROM_SAFE(var, head, field, tvar) \
369 for ((var) = ((var) ? (var) : STAILQ_FIRST((head))); \
370 (var) && ((tvar) = STAILQ_NEXT((var), field), 1); \
371 (var) = (tvar))
372
373 #define STAILQ_INIT(head) do { \
374 STAILQ_FIRST((head)) = NULL; \
375 (head)->stqh_last = &STAILQ_FIRST((head)); \
376 } while (0)
377
378 #define STAILQ_INSERT_AFTER(head, tqelm, elm, field) do { \
379 if ((STAILQ_NEXT((elm), field) = STAILQ_NEXT((tqelm), field)) == NULL)\
380 (head)->stqh_last = &STAILQ_NEXT((elm), field); \
381 STAILQ_NEXT((tqelm), field) = (elm); \
382 } while (0)
383
384 #define STAILQ_INSERT_HEAD(head, elm, field) do { \
385 if ((STAILQ_NEXT((elm), field) = STAILQ_FIRST((head))) == NULL) \
386 (head)->stqh_last = &STAILQ_NEXT((elm), field); \
387 STAILQ_FIRST((head)) = (elm); \
388 } while (0)
389
390 #define STAILQ_INSERT_TAIL(head, elm, field) do { \
391 STAILQ_NEXT((elm), field) = NULL; \
392 *(head)->stqh_last = (elm); \
393 (head)->stqh_last = &STAILQ_NEXT((elm), field); \
394 } while (0)
395
396 #define STAILQ_LAST(head, type, field) \
397 (STAILQ_EMPTY((head)) ? NULL : \
398 __containerof((head)->stqh_last, \
399 QUEUE_TYPEOF(type), field.stqe_next))
400
401 #define STAILQ_NEXT(elm, field) ((elm)->field.stqe_next)
402
403 #define STAILQ_REMOVE(head, elm, type, field) do { \
404 QMD_SAVELINK(oldnext, (elm)->field.stqe_next); \
405 if (STAILQ_FIRST((head)) == (elm)) { \
406 STAILQ_REMOVE_HEAD((head), field); \
407 } \
408 else { \
409 QUEUE_TYPEOF(type) *curelm = STAILQ_FIRST(head); \
410 while (STAILQ_NEXT(curelm, field) != (elm)) \
411 curelm = STAILQ_NEXT(curelm, field); \
412 STAILQ_REMOVE_AFTER(head, curelm, field); \
413 } \
414 TRASHIT(*oldnext); \
415 } while (0)
416
417 #define STAILQ_REMOVE_AFTER(head, elm, field) do { \
418 if ((STAILQ_NEXT(elm, field) = \
419 STAILQ_NEXT(STAILQ_NEXT(elm, field), field)) == NULL) \
420 (head)->stqh_last = &STAILQ_NEXT((elm), field); \
421 } while (0)
422
423 #define STAILQ_REMOVE_HEAD(head, field) do { \
424 if ((STAILQ_FIRST((head)) = \
425 STAILQ_NEXT(STAILQ_FIRST((head)), field)) == NULL) \
426 (head)->stqh_last = &STAILQ_FIRST((head)); \
427 } while (0)
428
429 #define STAILQ_REMOVE_HEAD_UNTIL(head, elm, field) do { \
430 if ((STAILQ_FIRST((head)) = STAILQ_NEXT((elm), field)) == NULL) \
431 (head)->stqh_last = &STAILQ_FIRST((head)); \
432 } while (0)
433
434 #define STAILQ_SWAP(head1, head2, type) do { \
435 QUEUE_TYPEOF(type) *swap_first = STAILQ_FIRST(head1); \
436 QUEUE_TYPEOF(type) **swap_last = (head1)->stqh_last; \
437 STAILQ_FIRST(head1) = STAILQ_FIRST(head2); \
438 (head1)->stqh_last = (head2)->stqh_last; \
439 STAILQ_FIRST(head2) = swap_first; \
440 (head2)->stqh_last = swap_last; \
441 if (STAILQ_EMPTY(head1)) \
442 (head1)->stqh_last = &STAILQ_FIRST(head1); \
443 if (STAILQ_EMPTY(head2)) \
444 (head2)->stqh_last = &STAILQ_FIRST(head2); \
445 } while (0)
446
447
448 /*
449 * List declarations.
450 */
451 #define LIST_HEAD(name, type) \
452 struct name { \
453 struct type *lh_first; /* first element */ \
454 }
455
456 #define LIST_CLASS_HEAD(name, type) \
457 struct name { \
458 class type *lh_first; /* first element */ \
459 }
460
461 #define LIST_HEAD_INITIALIZER(head) \
462 { NULL }
463
464 #define LIST_ENTRY(type) \
465 struct { \
466 struct type *le_next; /* next element */ \
467 struct type **le_prev; /* address of previous next element */ \
468 }
469
470 #define LIST_CLASS_ENTRY(type) \
471 struct { \
472 class type *le_next; /* next element */ \
473 class type **le_prev; /* address of previous next element */ \
474 }
475
476 /*
477 * List functions.
478 */
479
480 #if (defined(_KERNEL) && defined(INVARIANTS))
481 /*
482 * QMD_LIST_CHECK_HEAD(LIST_HEAD *head, LIST_ENTRY NAME)
483 *
484 * If the list is non-empty, validates that the first element of the list
485 * points back at 'head.'
486 */
487 #define QMD_LIST_CHECK_HEAD(head, field) do { \
488 if (LIST_FIRST((head)) != NULL && \
489 LIST_FIRST((head))->field.le_prev != \
490 &LIST_FIRST((head))) \
491 panic("Bad list head %p first->prev != head", (head)); \
492 } while (0)
493
494 /*
495 * QMD_LIST_CHECK_NEXT(TYPE *elm, LIST_ENTRY NAME)
496 *
497 * If an element follows 'elm' in the list, validates that the next element
498 * points back at 'elm.'
499 */
500 #define QMD_LIST_CHECK_NEXT(elm, field) do { \
501 if (LIST_NEXT((elm), field) != NULL && \
502 LIST_NEXT((elm), field)->field.le_prev != \
503 &((elm)->field.le_next)) \
504 panic("Bad link elm %p next->prev != elm", (elm)); \
505 } while (0)
506
507 /*
508 * QMD_LIST_CHECK_PREV(TYPE *elm, LIST_ENTRY NAME)
509 *
510 * Validates that the previous element (or head of the list) points to 'elm.'
511 */
512 #define QMD_LIST_CHECK_PREV(elm, field) do { \
513 if (*(elm)->field.le_prev != (elm)) \
514 panic("Bad link elm %p prev->next != elm", (elm)); \
515 } while (0)
516 #else
517 #define QMD_LIST_CHECK_HEAD(head, field)
518 #define QMD_LIST_CHECK_NEXT(elm, field)
519 #define QMD_LIST_CHECK_PREV(elm, field)
520 #endif /* (_KERNEL && INVARIANTS) */
521
522 #define LIST_CONCAT(head1, head2, type, field) do { \
523 QUEUE_TYPEOF(type) *curelm = LIST_FIRST(head1); \
524 if (curelm == NULL) { \
525 if ((LIST_FIRST(head1) = LIST_FIRST(head2)) != NULL) { \
526 LIST_FIRST(head2)->field.le_prev = \
527 &LIST_FIRST((head1)); \
528 LIST_INIT(head2); \
529 } \
530 } else if (LIST_FIRST(head2) != NULL) { \
531 while (LIST_NEXT(curelm, field) != NULL) \
532 curelm = LIST_NEXT(curelm, field); \
533 LIST_NEXT(curelm, field) = LIST_FIRST(head2); \
534 LIST_FIRST(head2)->field.le_prev = &LIST_NEXT(curelm, field); \
535 LIST_INIT(head2); \
536 } \
537 } while (0)
538
539 #define LIST_EMPTY(head) ((head)->lh_first == NULL)
540
541 #define LIST_FIRST(head) ((head)->lh_first)
542
543 #define LIST_FOREACH(var, head, field) \
544 for ((var) = LIST_FIRST((head)); \
545 (var); \
546 (var) = LIST_NEXT((var), field))
547
548 #define LIST_FOREACH_FROM(var, head, field) \
549 for ((var) = ((var) ? (var) : LIST_FIRST((head))); \
550 (var); \
551 (var) = LIST_NEXT((var), field))
552
553 #define LIST_FOREACH_SAFE(var, head, field, tvar) \
554 for ((var) = LIST_FIRST((head)); \
555 (var) && ((tvar) = LIST_NEXT((var), field), 1); \
556 (var) = (tvar))
557
558 #define LIST_FOREACH_FROM_SAFE(var, head, field, tvar) \
559 for ((var) = ((var) ? (var) : LIST_FIRST((head))); \
560 (var) && ((tvar) = LIST_NEXT((var), field), 1); \
561 (var) = (tvar))
562
563 #define LIST_INIT(head) do { \
564 LIST_FIRST((head)) = NULL; \
565 } while (0)
566
567 #define LIST_INSERT_AFTER(listelm, elm, field) do { \
568 QMD_LIST_CHECK_NEXT(listelm, field); \
569 if ((LIST_NEXT((elm), field) = LIST_NEXT((listelm), field)) != NULL)\
570 LIST_NEXT((listelm), field)->field.le_prev = \
571 &LIST_NEXT((elm), field); \
572 LIST_NEXT((listelm), field) = (elm); \
573 (elm)->field.le_prev = &LIST_NEXT((listelm), field); \
574 } while (0)
575
576 #define LIST_INSERT_BEFORE(listelm, elm, field) do { \
577 QMD_LIST_CHECK_PREV(listelm, field); \
578 (elm)->field.le_prev = (listelm)->field.le_prev; \
579 LIST_NEXT((elm), field) = (listelm); \
580 *(listelm)->field.le_prev = (elm); \
581 (listelm)->field.le_prev = &LIST_NEXT((elm), field); \
582 } while (0)
583
584 #define LIST_INSERT_HEAD(head, elm, field) do { \
585 QMD_LIST_CHECK_HEAD((head), field); \
586 if ((LIST_NEXT((elm), field) = LIST_FIRST((head))) != NULL) \
587 LIST_FIRST((head))->field.le_prev = &LIST_NEXT((elm), field);\
588 LIST_FIRST((head)) = (elm); \
589 (elm)->field.le_prev = &LIST_FIRST((head)); \
590 } while (0)
591
592 #define LIST_NEXT(elm, field) ((elm)->field.le_next)
593
594 #define LIST_PREV(elm, head, type, field) \
595 ((elm)->field.le_prev == &LIST_FIRST((head)) ? NULL : \
596 __containerof((elm)->field.le_prev, \
597 QUEUE_TYPEOF(type), field.le_next))
598
599 #define LIST_REMOVE(elm, field) do { \
600 QMD_SAVELINK(oldnext, (elm)->field.le_next); \
601 QMD_SAVELINK(oldprev, (elm)->field.le_prev); \
602 QMD_LIST_CHECK_NEXT(elm, field); \
603 QMD_LIST_CHECK_PREV(elm, field); \
604 if (LIST_NEXT((elm), field) != NULL) \
605 LIST_NEXT((elm), field)->field.le_prev = \
606 (elm)->field.le_prev; \
607 *(elm)->field.le_prev = LIST_NEXT((elm), field); \
608 TRASHIT(*oldnext); \
609 TRASHIT(*oldprev); \
610 } while (0)
611
612 #define LIST_SWAP(head1, head2, type, field) do { \
613 QUEUE_TYPEOF(type) *swap_tmp = LIST_FIRST(head1); \
614 LIST_FIRST((head1)) = LIST_FIRST((head2)); \
615 LIST_FIRST((head2)) = swap_tmp; \
616 if ((swap_tmp = LIST_FIRST((head1))) != NULL) \
617 swap_tmp->field.le_prev = &LIST_FIRST((head1)); \
618 if ((swap_tmp = LIST_FIRST((head2))) != NULL) \
619 swap_tmp->field.le_prev = &LIST_FIRST((head2)); \
620 } while (0)
621
622 /*
623 * Tail queue declarations.
624 */
625 #define TAILQ_HEAD(name, type) \
626 struct name { \
627 struct type *tqh_first; /* first element */ \
628 struct type **tqh_last; /* addr of last next element */ \
629 TRACEBUF \
630 }
631
632 #define TAILQ_CLASS_HEAD(name, type) \
633 struct name { \
634 class type *tqh_first; /* first element */ \
635 class type **tqh_last; /* addr of last next element */ \
636 TRACEBUF \
637 }
638
639 #define TAILQ_HEAD_INITIALIZER(head) \
640 { NULL, &(head).tqh_first, TRACEBUF_INITIALIZER }
641
642 #define TAILQ_ENTRY(type) \
643 struct { \
644 struct type *tqe_next; /* next element */ \
645 struct type **tqe_prev; /* address of previous next element */ \
646 TRACEBUF \
647 }
648
649 #define TAILQ_CLASS_ENTRY(type) \
650 struct { \
651 class type *tqe_next; /* next element */ \
652 class type **tqe_prev; /* address of previous next element */ \
653 TRACEBUF \
654 }
655
656 /*
657 * Tail queue functions.
658 */
659 #if (defined(_KERNEL) && defined(INVARIANTS))
660 /*
661 * QMD_TAILQ_CHECK_HEAD(TAILQ_HEAD *head, TAILQ_ENTRY NAME)
662 *
663 * If the tailq is non-empty, validates that the first element of the tailq
664 * points back at 'head.'
665 */
666 #define QMD_TAILQ_CHECK_HEAD(head, field) do { \
667 if (!TAILQ_EMPTY(head) && \
668 TAILQ_FIRST((head))->field.tqe_prev != \
669 &TAILQ_FIRST((head))) \
670 panic("Bad tailq head %p first->prev != head", (head)); \
671 } while (0)
672
673 /*
674 * QMD_TAILQ_CHECK_TAIL(TAILQ_HEAD *head, TAILQ_ENTRY NAME)
675 *
676 * Validates that the tail of the tailq is a pointer to pointer to NULL.
677 */
678 #define QMD_TAILQ_CHECK_TAIL(head, field) do { \
679 if (*(head)->tqh_last != NULL) \
680 panic("Bad tailq NEXT(%p->tqh_last) != NULL", (head)); \
681 } while (0)
682
683 /*
684 * QMD_TAILQ_CHECK_NEXT(TYPE *elm, TAILQ_ENTRY NAME)
685 *
686 * If an element follows 'elm' in the tailq, validates that the next element
687 * points back at 'elm.'
688 */
689 #define QMD_TAILQ_CHECK_NEXT(elm, field) do { \
690 if (TAILQ_NEXT((elm), field) != NULL && \
691 TAILQ_NEXT((elm), field)->field.tqe_prev != \
692 &((elm)->field.tqe_next)) \
693 panic("Bad link elm %p next->prev != elm", (elm)); \
694 } while (0)
695
696 /*
697 * QMD_TAILQ_CHECK_PREV(TYPE *elm, TAILQ_ENTRY NAME)
698 *
699 * Validates that the previous element (or head of the tailq) points to 'elm.'
700 */
701 #define QMD_TAILQ_CHECK_PREV(elm, field) do { \
702 if (*(elm)->field.tqe_prev != (elm)) \
703 panic("Bad link elm %p prev->next != elm", (elm)); \
704 } while (0)
705 #else
706 #define QMD_TAILQ_CHECK_HEAD(head, field)
707 #define QMD_TAILQ_CHECK_TAIL(head, headname)
708 #define QMD_TAILQ_CHECK_NEXT(elm, field)
709 #define QMD_TAILQ_CHECK_PREV(elm, field)
710 #endif /* (_KERNEL && INVARIANTS) */
711
712 #define TAILQ_CONCAT(head1, head2, field) do { \
713 if (!TAILQ_EMPTY(head2)) { \
714 *(head1)->tqh_last = (head2)->tqh_first; \
715 (head2)->tqh_first->field.tqe_prev = (head1)->tqh_last; \
716 (head1)->tqh_last = (head2)->tqh_last; \
717 TAILQ_INIT((head2)); \
718 QMD_TRACE_HEAD(head1); \
719 QMD_TRACE_HEAD(head2); \
720 } \
721 } while (0)
722
723 #define TAILQ_EMPTY(head) ((head)->tqh_first == NULL)
724
725 #define TAILQ_FIRST(head) ((head)->tqh_first)
726
727 #define TAILQ_FOREACH(var, head, field) \
728 for ((var) = TAILQ_FIRST((head)); \
729 (var); \
730 (var) = TAILQ_NEXT((var), field))
731
732 #define TAILQ_FOREACH_FROM(var, head, field) \
733 for ((var) = ((var) ? (var) : TAILQ_FIRST((head))); \
734 (var); \
735 (var) = TAILQ_NEXT((var), field))
736
737 #define TAILQ_FOREACH_SAFE(var, head, field, tvar) \
738 for ((var) = TAILQ_FIRST((head)); \
739 (var) && ((tvar) = TAILQ_NEXT((var), field), 1); \
740 (var) = (tvar))
741
742 #define TAILQ_FOREACH_FROM_SAFE(var, head, field, tvar) \
743 for ((var) = ((var) ? (var) : TAILQ_FIRST((head))); \
744 (var) && ((tvar) = TAILQ_NEXT((var), field), 1); \
745 (var) = (tvar))
746
747 #define TAILQ_FOREACH_REVERSE(var, head, headname, field) \
748 for ((var) = TAILQ_LAST((head), headname); \
749 (var); \
750 (var) = TAILQ_PREV((var), headname, field))
751
752 #define TAILQ_FOREACH_REVERSE_FROM(var, head, headname, field) \
753 for ((var) = ((var) ? (var) : TAILQ_LAST((head), headname)); \
754 (var); \
755 (var) = TAILQ_PREV((var), headname, field))
756
757 #define TAILQ_FOREACH_REVERSE_SAFE(var, head, headname, field, tvar) \
758 for ((var) = TAILQ_LAST((head), headname); \
759 (var) && ((tvar) = TAILQ_PREV((var), headname, field), 1); \
760 (var) = (tvar))
761
762 #define TAILQ_FOREACH_REVERSE_FROM_SAFE(var, head, headname, field, tvar) \
763 for ((var) = ((var) ? (var) : TAILQ_LAST((head), headname)); \
764 (var) && ((tvar) = TAILQ_PREV((var), headname, field), 1); \
765 (var) = (tvar))
766
767 #define TAILQ_INIT(head) do { \
768 TAILQ_FIRST((head)) = NULL; \
769 (head)->tqh_last = &TAILQ_FIRST((head)); \
770 QMD_TRACE_HEAD(head); \
771 } while (0)
772
773 #define TAILQ_INSERT_AFTER(head, listelm, elm, field) do { \
774 QMD_TAILQ_CHECK_NEXT(listelm, field); \
775 if ((TAILQ_NEXT((elm), field) = TAILQ_NEXT((listelm), field)) != NULL)\
776 TAILQ_NEXT((elm), field)->field.tqe_prev = \
777 &TAILQ_NEXT((elm), field); \
778 else { \
779 (head)->tqh_last = &TAILQ_NEXT((elm), field); \
780 QMD_TRACE_HEAD(head); \
781 } \
782 TAILQ_NEXT((listelm), field) = (elm); \
783 (elm)->field.tqe_prev = &TAILQ_NEXT((listelm), field); \
784 QMD_TRACE_ELEM(&(elm)->field); \
785 QMD_TRACE_ELEM(&(listelm)->field); \
786 } while (0)
787
788 #define TAILQ_INSERT_BEFORE(listelm, elm, field) do { \
789 QMD_TAILQ_CHECK_PREV(listelm, field); \
790 (elm)->field.tqe_prev = (listelm)->field.tqe_prev; \
791 TAILQ_NEXT((elm), field) = (listelm); \
792 *(listelm)->field.tqe_prev = (elm); \
793 (listelm)->field.tqe_prev = &TAILQ_NEXT((elm), field); \
794 QMD_TRACE_ELEM(&(elm)->field); \
795 QMD_TRACE_ELEM(&(listelm)->field); \
796 } while (0)
797
798 #define TAILQ_INSERT_HEAD(head, elm, field) do { \
799 QMD_TAILQ_CHECK_HEAD(head, field); \
800 if ((TAILQ_NEXT((elm), field) = TAILQ_FIRST((head))) != NULL) \
801 TAILQ_FIRST((head))->field.tqe_prev = \
802 &TAILQ_NEXT((elm), field); \
803 else \
804 (head)->tqh_last = &TAILQ_NEXT((elm), field); \
805 TAILQ_FIRST((head)) = (elm); \
806 (elm)->field.tqe_prev = &TAILQ_FIRST((head)); \
807 QMD_TRACE_HEAD(head); \
808 QMD_TRACE_ELEM(&(elm)->field); \
809 } while (0)
810
811 #define TAILQ_INSERT_TAIL(head, elm, field) do { \
812 QMD_TAILQ_CHECK_TAIL(head, field); \
813 TAILQ_NEXT((elm), field) = NULL; \
814 (elm)->field.tqe_prev = (head)->tqh_last; \
815 *(head)->tqh_last = (elm); \
816 (head)->tqh_last = &TAILQ_NEXT((elm), field); \
817 QMD_TRACE_HEAD(head); \
818 QMD_TRACE_ELEM(&(elm)->field); \
819 } while (0)
820
821 #define TAILQ_LAST(head, headname) \
822 (*(((struct headname *)((head)->tqh_last))->tqh_last))
823
824 /*
825 * The FAST function is fast in that it causes no data access other
826 * then the access to the head. The standard LAST function above
827 * will cause a data access of both the element you want and
828 * the previous element. FAST is very useful for instances when
829 * you may want to prefetch the last data element.
830 */
831 #define TAILQ_LAST_FAST(head, type, field) \
832 (TAILQ_EMPTY(head) ? NULL : __containerof((head)->tqh_last, QUEUE_TYPEOF(type), field.tqe_next))
833
834 #define TAILQ_NEXT(elm, field) ((elm)->field.tqe_next)
835
836 #define TAILQ_PREV(elm, headname, field) \
837 (*(((struct headname *)((elm)->field.tqe_prev))->tqh_last))
838
839 #define TAILQ_REMOVE(head, elm, field) do { \
840 QMD_SAVELINK(oldnext, (elm)->field.tqe_next); \
841 QMD_SAVELINK(oldprev, (elm)->field.tqe_prev); \
842 QMD_TAILQ_CHECK_NEXT(elm, field); \
843 QMD_TAILQ_CHECK_PREV(elm, field); \
844 if ((TAILQ_NEXT((elm), field)) != NULL) \
845 TAILQ_NEXT((elm), field)->field.tqe_prev = \
846 (elm)->field.tqe_prev; \
847 else { \
848 (head)->tqh_last = (elm)->field.tqe_prev; \
849 QMD_TRACE_HEAD(head); \
850 } \
851 *(elm)->field.tqe_prev = TAILQ_NEXT((elm), field); \
852 TRASHIT(*oldnext); \
853 TRASHIT(*oldprev); \
854 QMD_TRACE_ELEM(&(elm)->field); \
855 } while (0)
856
857 #define TAILQ_SWAP(head1, head2, type, field) do { \
858 QUEUE_TYPEOF(type) *swap_first = (head1)->tqh_first; \
859 QUEUE_TYPEOF(type) **swap_last = (head1)->tqh_last; \
860 (head1)->tqh_first = (head2)->tqh_first; \
861 (head1)->tqh_last = (head2)->tqh_last; \
862 (head2)->tqh_first = swap_first; \
863 (head2)->tqh_last = swap_last; \
864 if ((swap_first = (head1)->tqh_first) != NULL) \
865 swap_first->field.tqe_prev = &(head1)->tqh_first; \
866 else \
867 (head1)->tqh_last = &(head1)->tqh_first; \
868 if ((swap_first = (head2)->tqh_first) != NULL) \
869 swap_first->field.tqe_prev = &(head2)->tqh_first; \
870 else \
871 (head2)->tqh_last = &(head2)->tqh_first; \
872 } while (0)
873
874 #ifdef _KERNEL
875
876 /*
877 * XXX insque() and remque() are an old way of handling certain queues.
878 * They bogusly assumes that all queue heads look alike.
879 */
880
881 struct quehead {
882 struct quehead *qh_link;
883 struct quehead *qh_rlink;
884 };
885
886 #ifdef __GNUC__
887
888 static __inline void
insque(void * a,void * b)889 insque(void *a, void *b)
890 {
891 struct quehead *element = (struct quehead *)a,
892 *head = (struct quehead *)b;
893
894 element->qh_link = head->qh_link;
895 element->qh_rlink = head;
896 head->qh_link = element;
897 element->qh_link->qh_rlink = element;
898 }
899
900 static __inline void
remque(void * a)901 remque(void *a)
902 {
903 struct quehead *element = (struct quehead *)a;
904
905 element->qh_link->qh_rlink = element->qh_rlink;
906 element->qh_rlink->qh_link = element->qh_link;
907 element->qh_rlink = 0;
908 }
909
910 #else /* !__GNUC__ */
911
912 void insque(void *a, void *b);
913 void remque(void *a);
914
915 #endif /* __GNUC__ */
916
917 #endif /* _KERNEL */
918
919 #endif /* !_SYS_QUEUE_H_ */
920