NeoMutt  2019-12-07
Teaching an old dog new tricks
DOXYGEN
queue.h
Go to the documentation of this file.
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: head/sys/sys/queue.h 314436 2017-02-28 23:42:47Z imp $
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 be traversed in either 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  * Below is a summary of implemented functions where:
78  * + means the macro is available
79  * - means the macro is not available
80  * s means the macro is available but is slow (runs in O(n) time)
81  *
82  * SLIST LIST STAILQ TAILQ
83  * _HEAD + + + +
84  * _CLASS_HEAD + + + +
85  * _HEAD_INITIALIZER + + + +
86  * _ENTRY + + + +
87  * _CLASS_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 s s + +
107  * _REMOVE_AFTER + - + -
108  * _REMOVE_HEAD + - + -
109  * _REMOVE s + s +
110  * _SWAP + + + +
111  *
112  */
113 #ifdef QUEUE_MACRO_DEBUG
114 #warn Use QUEUE_MACRO_DEBUG_TRACE and/or QUEUE_MACRO_DEBUG_TRASH
115 #define QUEUE_MACRO_DEBUG_TRACE
116 #define QUEUE_MACRO_DEBUG_TRASH
117 #endif
118 
119 #ifdef QUEUE_MACRO_DEBUG_TRACE
120 /* Store the last 2 places the queue element or head was altered */
121 struct qm_trace {
122  unsigned long lastline;
123  unsigned long prevline;
124  const char *lastfile;
125  const char *prevfile;
126 };
127 
128 #define TRACEBUF struct qm_trace trace;
129 #define TRACEBUF_INITIALIZER { __LINE__, 0, __FILE__, NULL } ,
130 
131 #define QMD_TRACE_HEAD(head) do { \
132  (head)->trace.prevline = (head)->trace.lastline; \
133  (head)->trace.prevfile = (head)->trace.lastfile; \
134  (head)->trace.lastline = __LINE__; \
135  (head)->trace.lastfile = __FILE__; \
136 } while (0)
137 
138 #define QMD_TRACE_ELEM(elem) do { \
139  (elem)->trace.prevline = (elem)->trace.lastline; \
140  (elem)->trace.prevfile = (elem)->trace.lastfile; \
141  (elem)->trace.lastline = __LINE__; \
142  (elem)->trace.lastfile = __FILE__; \
143 } while (0)
144 
145 #else /* !QUEUE_MACRO_DEBUG_TRACE */
146 #define QMD_TRACE_ELEM(elem)
147 #define QMD_TRACE_HEAD(head)
148 #define TRACEBUF
149 #define TRACEBUF_INITIALIZER
150 #endif /* QUEUE_MACRO_DEBUG_TRACE */
151 
152 #ifdef QUEUE_MACRO_DEBUG_TRASH
153 #define TRASHIT(x) do {(x) = (void *)-1;} while (0)
154 #define QMD_IS_TRASHED(x) ((x) == (void *)(intptr_t)-1)
155 #else /* !QUEUE_MACRO_DEBUG_TRASH */
156 #define TRASHIT(x)
157 #define QMD_IS_TRASHED(x) 0
158 #endif /* QUEUE_MACRO_DEBUG_TRASH */
159 
160 #if defined(QUEUE_MACRO_DEBUG_TRACE) || defined(QUEUE_MACRO_DEBUG_TRASH)
161 #define QMD_SAVELINK(name, link) void **name = (void *)&(link)
162 #else /* !QUEUE_MACRO_DEBUG_TRACE && !QUEUE_MACRO_DEBUG_TRASH */
163 #define QMD_SAVELINK(name, link)
164 #endif /* QUEUE_MACRO_DEBUG_TRACE || QUEUE_MACRO_DEBUG_TRASH */
165 
166 #ifdef __cplusplus
167 /*
168  * In C++ there can be structure lists and class lists:
169  */
170 #define QUEUE_TYPEOF(type) type
171 #else
172 #define QUEUE_TYPEOF(type) struct type
173 #endif
174 
175 /*
176  * Singly-linked List declarations.
177  */
178 #define SLIST_HEAD(name, type) \
179 struct name { \
180  struct type *slh_first; /* first element */ \
181 }
182 
183 #define SLIST_CLASS_HEAD(name, type) \
184 struct name { \
185  class type *slh_first; /* first element */ \
186 }
187 
188 #define SLIST_HEAD_INITIALIZER(head) \
189  { NULL }
190 
191 #define SLIST_ENTRY(type) \
192 struct { \
193  struct type *sle_next; /* next element */ \
194 }
195 
196 #define SLIST_CLASS_ENTRY(type) \
197 struct { \
198  class type *sle_next; /* next element */ \
199 }
200 
201 /*
202  * Singly-linked List functions.
203  */
204 #if (defined(_KERNEL) && defined(INVARIANTS))
205 #define QMD_SLIST_CHECK_PREVPTR(prevp, elm) do { \
206  if (*(prevp) != (elm)) \
207  panic("Bad prevptr *(%p) == %p != %p", \
208  (prevp), *(prevp), (elm)); \
209 } while (0)
210 #else
211 #define QMD_SLIST_CHECK_PREVPTR(prevp, elm)
212 #endif
213 
214 #define SLIST_CONCAT(head1, head2, type, field) do { \
215  QUEUE_TYPEOF(type) *curelm = SLIST_FIRST(head1); \
216  if (curelm == NULL) { \
217  if ((SLIST_FIRST(head1) = SLIST_FIRST(head2)) != NULL) \
218  SLIST_INIT(head2); \
219  } else if (SLIST_FIRST(head2) != NULL) { \
220  while (SLIST_NEXT(curelm, field) != NULL) \
221  curelm = SLIST_NEXT(curelm, field); \
222  SLIST_NEXT(curelm, field) = SLIST_FIRST(head2); \
223  SLIST_INIT(head2); \
224  } \
225 } while (0)
226 
227 #define SLIST_EMPTY(head) ((head)->slh_first == NULL)
228 
229 #define SLIST_FIRST(head) ((head)->slh_first)
230 
231 #define SLIST_FOREACH(var, head, field) \
232  for ((var) = SLIST_FIRST((head)); \
233  (var); \
234  (var) = SLIST_NEXT((var), field))
235 
236 #define SLIST_FOREACH_FROM(var, head, field) \
237  for ((var) = ((var) ? (var) : SLIST_FIRST((head))); \
238  (var); \
239  (var) = SLIST_NEXT((var), field))
240 
241 #define SLIST_FOREACH_SAFE(var, head, field, tvar) \
242  for ((var) = SLIST_FIRST((head)); \
243  (var) && ((tvar) = SLIST_NEXT((var), field), 1); \
244  (var) = (tvar))
245 
246 #define SLIST_FOREACH_FROM_SAFE(var, head, field, tvar) \
247  for ((var) = ((var) ? (var) : SLIST_FIRST((head))); \
248  (var) && ((tvar) = SLIST_NEXT((var), field), 1); \
249  (var) = (tvar))
250 
251 #define SLIST_FOREACH_PREVPTR(var, varp, head, field) \
252  for ((varp) = &SLIST_FIRST((head)); \
253  ((var) = *(varp)) != NULL; \
254  (varp) = &SLIST_NEXT((var), field))
255 
256 #define SLIST_INIT(head) do { \
257  SLIST_FIRST((head)) = NULL; \
258 } while (0)
259 
260 #define SLIST_INSERT_AFTER(slistelm, elm, field) do { \
261  SLIST_NEXT((elm), field) = SLIST_NEXT((slistelm), field); \
262  SLIST_NEXT((slistelm), field) = (elm); \
263 } while (0)
264 
265 #define SLIST_INSERT_HEAD(head, elm, field) do { \
266  SLIST_NEXT((elm), field) = SLIST_FIRST((head)); \
267  SLIST_FIRST((head)) = (elm); \
268 } while (0)
269 
270 #define SLIST_NEXT(elm, field) ((elm)->field.sle_next)
271 
272 #define SLIST_REMOVE(head, elm, type, field) do { \
273  QMD_SAVELINK(oldnext, (elm)->field.sle_next); \
274  if (SLIST_FIRST((head)) == (elm)) { \
275  SLIST_REMOVE_HEAD((head), field); \
276  } \
277  else { \
278  QUEUE_TYPEOF(type) *curelm = SLIST_FIRST(head); \
279  while (SLIST_NEXT(curelm, field) != (elm)) \
280  curelm = SLIST_NEXT(curelm, field); \
281  SLIST_REMOVE_AFTER(curelm, field); \
282  } \
283  TRASHIT(*oldnext); \
284 } while (0)
285 
286 #define SLIST_REMOVE_AFTER(elm, field) do { \
287  SLIST_NEXT(elm, field) = \
288  SLIST_NEXT(SLIST_NEXT(elm, field), field); \
289 } while (0)
290 
291 #define SLIST_REMOVE_HEAD(head, field) do { \
292  SLIST_FIRST((head)) = SLIST_NEXT(SLIST_FIRST((head)), field); \
293 } while (0)
294 
295 #define SLIST_REMOVE_PREVPTR(prevp, elm, field) do { \
296  QMD_SLIST_CHECK_PREVPTR(prevp, elm); \
297  *(prevp) = SLIST_NEXT(elm, field); \
298  TRASHIT((elm)->field.sle_next); \
299 } while (0)
300 
301 #define SLIST_SWAP(head1, head2, type) do { \
302  QUEUE_TYPEOF(type) *swap_first = SLIST_FIRST(head1); \
303  SLIST_FIRST(head1) = SLIST_FIRST(head2); \
304  SLIST_FIRST(head2) = swap_first; \
305 } while (0)
306 
307 /*
308  * Singly-linked Tail queue declarations.
309  */
310 #define STAILQ_HEAD(name, type) \
311 struct name { \
312  struct type *stqh_first;/* first element */ \
313  struct type **stqh_last;/* addr of last next element */ \
314 }
315 
316 #define STAILQ_CLASS_HEAD(name, type) \
317 struct name { \
318  class type *stqh_first; /* first element */ \
319  class type **stqh_last; /* addr of last next element */ \
320 }
321 
322 #define STAILQ_HEAD_INITIALIZER(head) \
323  { NULL, &(head).stqh_first }
324 
325 #define STAILQ_ENTRY(type) \
326 struct { \
327  struct type *stqe_next; /* next element */ \
328 }
329 
330 #define STAILQ_CLASS_ENTRY(type) \
331 struct { \
332  class type *stqe_next; /* next element */ \
333 }
334 
335 /*
336  * Singly-linked Tail queue functions.
337  */
338 #define STAILQ_CONCAT(head1, head2) do { \
339  if (!STAILQ_EMPTY((head2))) { \
340  *(head1)->stqh_last = (head2)->stqh_first; \
341  (head1)->stqh_last = (head2)->stqh_last; \
342  STAILQ_INIT((head2)); \
343  } \
344 } while (0)
345 
346 #define STAILQ_EMPTY(head) ((head)->stqh_first == NULL)
347 
348 #define STAILQ_FIRST(head) ((head)->stqh_first)
349 
350 #define STAILQ_FOREACH(var, head, field) \
351  for((var) = STAILQ_FIRST((head)); \
352  (var); \
353  (var) = STAILQ_NEXT((var), field))
354 
355 #define STAILQ_FOREACH_FROM(var, head, field) \
356  for ((var) = ((var) ? (var) : STAILQ_FIRST((head))); \
357  (var); \
358  (var) = STAILQ_NEXT((var), field))
359 
360 #define STAILQ_FOREACH_SAFE(var, head, field, tvar) \
361  for ((var) = STAILQ_FIRST((head)); \
362  (var) && ((tvar) = STAILQ_NEXT((var), field), 1); \
363  (var) = (tvar))
364 
365 #define STAILQ_FOREACH_FROM_SAFE(var, head, field, tvar) \
366  for ((var) = ((var) ? (var) : STAILQ_FIRST((head))); \
367  (var) && ((tvar) = STAILQ_NEXT((var), field), 1); \
368  (var) = (tvar))
369 
370 #define STAILQ_INIT(head) do { \
371  STAILQ_FIRST((head)) = NULL; \
372  (head)->stqh_last = &STAILQ_FIRST((head)); \
373 } while (0)
374 
375 #define STAILQ_INSERT_AFTER(head, tqelm, elm, field) do { \
376  if ((STAILQ_NEXT((elm), field) = STAILQ_NEXT((tqelm), field)) == NULL)\
377  (head)->stqh_last = &STAILQ_NEXT((elm), field); \
378  STAILQ_NEXT((tqelm), field) = (elm); \
379 } while (0)
380 
381 #define STAILQ_INSERT_HEAD(head, elm, field) do { \
382  if ((STAILQ_NEXT((elm), field) = STAILQ_FIRST((head))) == NULL) \
383  (head)->stqh_last = &STAILQ_NEXT((elm), field); \
384  STAILQ_FIRST((head)) = (elm); \
385 } while (0)
386 
387 #define STAILQ_INSERT_TAIL(head, elm, field) do { \
388  STAILQ_NEXT((elm), field) = NULL; \
389  *(head)->stqh_last = (elm); \
390  (head)->stqh_last = &STAILQ_NEXT((elm), field); \
391 } while (0)
392 
393 #define STAILQ_LAST(head, type, field) \
394  (STAILQ_EMPTY((head)) ? NULL : \
395  __containerof((head)->stqh_last, \
396  QUEUE_TYPEOF(type), field.stqe_next))
397 
398 #define STAILQ_NEXT(elm, field) ((elm)->field.stqe_next)
399 
400 #define STAILQ_REMOVE(head, elm, type, field) do { \
401  QMD_SAVELINK(oldnext, (elm)->field.stqe_next); \
402  if (STAILQ_FIRST((head)) == (elm)) { \
403  STAILQ_REMOVE_HEAD((head), field); \
404  } \
405  else { \
406  QUEUE_TYPEOF(type) *curelm = STAILQ_FIRST(head); \
407  while (STAILQ_NEXT(curelm, field) != (elm)) \
408  curelm = STAILQ_NEXT(curelm, field); \
409  STAILQ_REMOVE_AFTER(head, curelm, field); \
410  } \
411  TRASHIT(*oldnext); \
412 } while (0)
413 
414 #define STAILQ_REMOVE_AFTER(head, elm, field) do { \
415  if ((STAILQ_NEXT(elm, field) = \
416  STAILQ_NEXT(STAILQ_NEXT(elm, field), field)) == NULL) \
417  (head)->stqh_last = &STAILQ_NEXT((elm), field); \
418 } while (0)
419 
420 #define STAILQ_REMOVE_HEAD(head, field) do { \
421  if ((STAILQ_FIRST((head)) = \
422  STAILQ_NEXT(STAILQ_FIRST((head)), field)) == NULL) \
423  (head)->stqh_last = &STAILQ_FIRST((head)); \
424 } while (0)
425 
426 #define STAILQ_SWAP(head1, head2, type) do { \
427  QUEUE_TYPEOF(type) *swap_first = STAILQ_FIRST(head1); \
428  QUEUE_TYPEOF(type) **swap_last = (head1)->stqh_last; \
429  STAILQ_FIRST(head1) = STAILQ_FIRST(head2); \
430  (head1)->stqh_last = (head2)->stqh_last; \
431  STAILQ_FIRST(head2) = swap_first; \
432  (head2)->stqh_last = swap_last; \
433  if (STAILQ_EMPTY(head1)) \
434  (head1)->stqh_last = &STAILQ_FIRST(head1); \
435  if (STAILQ_EMPTY(head2)) \
436  (head2)->stqh_last = &STAILQ_FIRST(head2); \
437 } while (0)
438 
439 
440 /*
441  * List declarations.
442  */
443 #define LIST_HEAD(name, type) \
444 struct name { \
445  struct type *lh_first; /* first element */ \
446 }
447 
448 #define LIST_CLASS_HEAD(name, type) \
449 struct name { \
450  class type *lh_first; /* first element */ \
451 }
452 
453 #define LIST_HEAD_INITIALIZER(head) \
454  { NULL }
455 
456 #define LIST_ENTRY(type) \
457 struct { \
458  struct type *le_next; /* next element */ \
459  struct type **le_prev; /* address of previous next element */ \
460 }
461 
462 #define LIST_CLASS_ENTRY(type) \
463 struct { \
464  class type *le_next; /* next element */ \
465  class type **le_prev; /* address of previous next element */ \
466 }
467 
468 /*
469  * List functions.
470  */
471 
472 #if (defined(_KERNEL) && defined(INVARIANTS))
473 /*
474  * QMD_LIST_CHECK_HEAD(LIST_HEAD *head, LIST_ENTRY NAME)
475  *
476  * If the list is non-empty, validates that the first element of the list
477  * points back at 'head.'
478  */
479 #define QMD_LIST_CHECK_HEAD(head, field) do { \
480  if (LIST_FIRST((head)) != NULL && \
481  LIST_FIRST((head))->field.le_prev != \
482  &LIST_FIRST((head))) \
483  panic("Bad list head %p first->prev != head", (head)); \
484 } while (0)
485 
486 /*
487  * QMD_LIST_CHECK_NEXT(TYPE *elm, LIST_ENTRY NAME)
488  *
489  * If an element follows 'elm' in the list, validates that the next element
490  * points back at 'elm.'
491  */
492 #define QMD_LIST_CHECK_NEXT(elm, field) do { \
493  if (LIST_NEXT((elm), field) != NULL && \
494  LIST_NEXT((elm), field)->field.le_prev != \
495  &((elm)->field.le_next)) \
496  panic("Bad link elm %p next->prev != elm", (elm)); \
497 } while (0)
498 
499 /*
500  * QMD_LIST_CHECK_PREV(TYPE *elm, LIST_ENTRY NAME)
501  *
502  * Validates that the previous element (or head of the list) points to 'elm.'
503  */
504 #define QMD_LIST_CHECK_PREV(elm, field) do { \
505  if (*(elm)->field.le_prev != (elm)) \
506  panic("Bad link elm %p prev->next != elm", (elm)); \
507 } while (0)
508 #else
509 #define QMD_LIST_CHECK_HEAD(head, field)
510 #define QMD_LIST_CHECK_NEXT(elm, field)
511 #define QMD_LIST_CHECK_PREV(elm, field)
512 #endif /* (_KERNEL && INVARIANTS) */
513 
514 #define LIST_CONCAT(head1, head2, type, field) do { \
515  QUEUE_TYPEOF(type) *curelm = LIST_FIRST(head1); \
516  if (curelm == NULL) { \
517  if ((LIST_FIRST(head1) = LIST_FIRST(head2)) != NULL) { \
518  LIST_FIRST(head2)->field.le_prev = \
519  &LIST_FIRST((head1)); \
520  LIST_INIT(head2); \
521  } \
522  } else if (LIST_FIRST(head2) != NULL) { \
523  while (LIST_NEXT(curelm, field) != NULL) \
524  curelm = LIST_NEXT(curelm, field); \
525  LIST_NEXT(curelm, field) = LIST_FIRST(head2); \
526  LIST_FIRST(head2)->field.le_prev = &LIST_NEXT(curelm, field); \
527  LIST_INIT(head2); \
528  } \
529 } while (0)
530 
531 #define LIST_EMPTY(head) ((head)->lh_first == NULL)
532 
533 #define LIST_FIRST(head) ((head)->lh_first)
534 
535 #define LIST_FOREACH(var, head, field) \
536  for ((var) = LIST_FIRST((head)); \
537  (var); \
538  (var) = LIST_NEXT((var), field))
539 
540 #define LIST_FOREACH_FROM(var, head, field) \
541  for ((var) = ((var) ? (var) : LIST_FIRST((head))); \
542  (var); \
543  (var) = LIST_NEXT((var), field))
544 
545 #define LIST_FOREACH_SAFE(var, head, field, tvar) \
546  for ((var) = LIST_FIRST((head)); \
547  (var) && ((tvar) = LIST_NEXT((var), field), 1); \
548  (var) = (tvar))
549 
550 #define LIST_FOREACH_FROM_SAFE(var, head, field, tvar) \
551  for ((var) = ((var) ? (var) : LIST_FIRST((head))); \
552  (var) && ((tvar) = LIST_NEXT((var), field), 1); \
553  (var) = (tvar))
554 
555 #define LIST_INIT(head) do { \
556  LIST_FIRST((head)) = NULL; \
557 } while (0)
558 
559 #define LIST_INSERT_AFTER(listelm, elm, field) do { \
560  QMD_LIST_CHECK_NEXT(listelm, field); \
561  if ((LIST_NEXT((elm), field) = LIST_NEXT((listelm), field)) != NULL)\
562  LIST_NEXT((listelm), field)->field.le_prev = \
563  &LIST_NEXT((elm), field); \
564  LIST_NEXT((listelm), field) = (elm); \
565  (elm)->field.le_prev = &LIST_NEXT((listelm), field); \
566 } while (0)
567 
568 #define LIST_INSERT_BEFORE(listelm, elm, field) do { \
569  QMD_LIST_CHECK_PREV(listelm, field); \
570  (elm)->field.le_prev = (listelm)->field.le_prev; \
571  LIST_NEXT((elm), field) = (listelm); \
572  *(listelm)->field.le_prev = (elm); \
573  (listelm)->field.le_prev = &LIST_NEXT((elm), field); \
574 } while (0)
575 
576 #define LIST_INSERT_HEAD(head, elm, field) do { \
577  QMD_LIST_CHECK_HEAD((head), field); \
578  if ((LIST_NEXT((elm), field) = LIST_FIRST((head))) != NULL) \
579  LIST_FIRST((head))->field.le_prev = &LIST_NEXT((elm), field);\
580  LIST_FIRST((head)) = (elm); \
581  (elm)->field.le_prev = &LIST_FIRST((head)); \
582 } while (0)
583 
584 #define LIST_NEXT(elm, field) ((elm)->field.le_next)
585 
586 #define LIST_PREV(elm, head, type, field) \
587  ((elm)->field.le_prev == &LIST_FIRST((head)) ? NULL : \
588  __containerof((elm)->field.le_prev, \
589  QUEUE_TYPEOF(type), field.le_next))
590 
591 #define LIST_REMOVE(elm, field) do { \
592  QMD_SAVELINK(oldnext, (elm)->field.le_next); \
593  QMD_SAVELINK(oldprev, (elm)->field.le_prev); \
594  QMD_LIST_CHECK_NEXT(elm, field); \
595  QMD_LIST_CHECK_PREV(elm, field); \
596  if (LIST_NEXT((elm), field) != NULL) \
597  LIST_NEXT((elm), field)->field.le_prev = \
598  (elm)->field.le_prev; \
599  *(elm)->field.le_prev = LIST_NEXT((elm), field); \
600  TRASHIT(*oldnext); \
601  TRASHIT(*oldprev); \
602 } while (0)
603 
604 #define LIST_SWAP(head1, head2, type, field) do { \
605  QUEUE_TYPEOF(type) *swap_tmp = LIST_FIRST(head1); \
606  LIST_FIRST((head1)) = LIST_FIRST((head2)); \
607  LIST_FIRST((head2)) = swap_tmp; \
608  if ((swap_tmp = LIST_FIRST((head1))) != NULL) \
609  swap_tmp->field.le_prev = &LIST_FIRST((head1)); \
610  if ((swap_tmp = LIST_FIRST((head2))) != NULL) \
611  swap_tmp->field.le_prev = &LIST_FIRST((head2)); \
612 } while (0)
613 
614 /*
615  * Tail queue declarations.
616  */
617 #define TAILQ_HEAD(name, type) \
618 struct name { \
619  struct type *tqh_first; /* first element */ \
620  struct type **tqh_last; /* addr of last next element */ \
621  TRACEBUF \
622 }
623 
624 #define TAILQ_CLASS_HEAD(name, type) \
625 struct name { \
626  class type *tqh_first; /* first element */ \
627  class type **tqh_last; /* addr of last next element */ \
628  TRACEBUF \
629 }
630 
631 #define TAILQ_HEAD_INITIALIZER(head) \
632  { NULL, &(head).tqh_first, TRACEBUF_INITIALIZER }
633 
634 #define TAILQ_ENTRY(type) \
635 struct { \
636  struct type *tqe_next; /* next element */ \
637  struct type **tqe_prev; /* address of previous next element */ \
638  TRACEBUF \
639 }
640 
641 #define TAILQ_CLASS_ENTRY(type) \
642 struct { \
643  class type *tqe_next; /* next element */ \
644  class type **tqe_prev; /* address of previous next element */ \
645  TRACEBUF \
646 }
647 
648 /*
649  * Tail queue functions.
650  */
651 #if (defined(_KERNEL) && defined(INVARIANTS))
652 /*
653  * QMD_TAILQ_CHECK_HEAD(TAILQ_HEAD *head, TAILQ_ENTRY NAME)
654  *
655  * If the tailq is non-empty, validates that the first element of the tailq
656  * points back at 'head.'
657  */
658 #define QMD_TAILQ_CHECK_HEAD(head, field) do { \
659  if (!TAILQ_EMPTY(head) && \
660  TAILQ_FIRST((head))->field.tqe_prev != \
661  &TAILQ_FIRST((head))) \
662  panic("Bad tailq head %p first->prev != head", (head)); \
663 } while (0)
664 
665 /*
666  * QMD_TAILQ_CHECK_TAIL(TAILQ_HEAD *head, TAILQ_ENTRY NAME)
667  *
668  * Validates that the tail of the tailq is a pointer to pointer to NULL.
669  */
670 #define QMD_TAILQ_CHECK_TAIL(head, field) do { \
671  if (*(head)->tqh_last != NULL) \
672  panic("Bad tailq NEXT(%p->tqh_last) != NULL", (head)); \
673 } while (0)
674 
675 /*
676  * QMD_TAILQ_CHECK_NEXT(TYPE *elm, TAILQ_ENTRY NAME)
677  *
678  * If an element follows 'elm' in the tailq, validates that the next element
679  * points back at 'elm.'
680  */
681 #define QMD_TAILQ_CHECK_NEXT(elm, field) do { \
682  if (TAILQ_NEXT((elm), field) != NULL && \
683  TAILQ_NEXT((elm), field)->field.tqe_prev != \
684  &((elm)->field.tqe_next)) \
685  panic("Bad link elm %p next->prev != elm", (elm)); \
686 } while (0)
687 
688 /*
689  * QMD_TAILQ_CHECK_PREV(TYPE *elm, TAILQ_ENTRY NAME)
690  *
691  * Validates that the previous element (or head of the tailq) points to 'elm.'
692  */
693 #define QMD_TAILQ_CHECK_PREV(elm, field) do { \
694  if (*(elm)->field.tqe_prev != (elm)) \
695  panic("Bad link elm %p prev->next != elm", (elm)); \
696 } while (0)
697 #else
698 #define QMD_TAILQ_CHECK_HEAD(head, field)
699 #define QMD_TAILQ_CHECK_TAIL(head, headname)
700 #define QMD_TAILQ_CHECK_NEXT(elm, field)
701 #define QMD_TAILQ_CHECK_PREV(elm, field)
702 #endif /* (_KERNEL && INVARIANTS) */
703 
704 #define TAILQ_CONCAT(head1, head2, field) do { \
705  if (!TAILQ_EMPTY(head2)) { \
706  *(head1)->tqh_last = (head2)->tqh_first; \
707  (head2)->tqh_first->field.tqe_prev = (head1)->tqh_last; \
708  (head1)->tqh_last = (head2)->tqh_last; \
709  TAILQ_INIT((head2)); \
710  QMD_TRACE_HEAD(head1); \
711  QMD_TRACE_HEAD(head2); \
712  } \
713 } while (0)
714 
715 #define TAILQ_EMPTY(head) ((head)->tqh_first == NULL)
716 
717 #define TAILQ_FIRST(head) ((head)->tqh_first)
718 
719 #define TAILQ_FOREACH(var, head, field) \
720  for ((var) = TAILQ_FIRST((head)); \
721  (var); \
722  (var) = TAILQ_NEXT((var), field))
723 
724 #define TAILQ_FOREACH_FROM(var, head, field) \
725  for ((var) = ((var) ? (var) : TAILQ_FIRST((head))); \
726  (var); \
727  (var) = TAILQ_NEXT((var), field))
728 
729 #define TAILQ_FOREACH_SAFE(var, head, field, tvar) \
730  for ((var) = TAILQ_FIRST((head)); \
731  (var) && ((tvar) = TAILQ_NEXT((var), field), 1); \
732  (var) = (tvar))
733 
734 #define TAILQ_FOREACH_FROM_SAFE(var, head, field, tvar) \
735  for ((var) = ((var) ? (var) : TAILQ_FIRST((head))); \
736  (var) && ((tvar) = TAILQ_NEXT((var), field), 1); \
737  (var) = (tvar))
738 
739 #define TAILQ_FOREACH_REVERSE(var, head, headname, field) \
740  for ((var) = TAILQ_LAST((head), headname); \
741  (var); \
742  (var) = TAILQ_PREV((var), headname, field))
743 
744 #define TAILQ_FOREACH_REVERSE_FROM(var, head, headname, field) \
745  for ((var) = ((var) ? (var) : TAILQ_LAST((head), headname)); \
746  (var); \
747  (var) = TAILQ_PREV((var), headname, field))
748 
749 #define TAILQ_FOREACH_REVERSE_SAFE(var, head, headname, field, tvar) \
750  for ((var) = TAILQ_LAST((head), headname); \
751  (var) && ((tvar) = TAILQ_PREV((var), headname, field), 1); \
752  (var) = (tvar))
753 
754 #define TAILQ_FOREACH_REVERSE_FROM_SAFE(var, head, headname, field, tvar) \
755  for ((var) = ((var) ? (var) : TAILQ_LAST((head), headname)); \
756  (var) && ((tvar) = TAILQ_PREV((var), headname, field), 1); \
757  (var) = (tvar))
758 
759 #define TAILQ_INIT(head) do { \
760  TAILQ_FIRST((head)) = NULL; \
761  (head)->tqh_last = &TAILQ_FIRST((head)); \
762  QMD_TRACE_HEAD(head); \
763 } while (0)
764 
765 #define TAILQ_INSERT_AFTER(head, listelm, elm, field) do { \
766  QMD_TAILQ_CHECK_NEXT(listelm, field); \
767  if ((TAILQ_NEXT((elm), field) = TAILQ_NEXT((listelm), field)) != NULL)\
768  TAILQ_NEXT((elm), field)->field.tqe_prev = \
769  &TAILQ_NEXT((elm), field); \
770  else { \
771  (head)->tqh_last = &TAILQ_NEXT((elm), field); \
772  QMD_TRACE_HEAD(head); \
773  } \
774  TAILQ_NEXT((listelm), field) = (elm); \
775  (elm)->field.tqe_prev = &TAILQ_NEXT((listelm), field); \
776  QMD_TRACE_ELEM(&(elm)->field); \
777  QMD_TRACE_ELEM(&(listelm)->field); \
778 } while (0)
779 
780 #define TAILQ_INSERT_BEFORE(listelm, elm, field) do { \
781  QMD_TAILQ_CHECK_PREV(listelm, field); \
782  (elm)->field.tqe_prev = (listelm)->field.tqe_prev; \
783  TAILQ_NEXT((elm), field) = (listelm); \
784  *(listelm)->field.tqe_prev = (elm); \
785  (listelm)->field.tqe_prev = &TAILQ_NEXT((elm), field); \
786  QMD_TRACE_ELEM(&(elm)->field); \
787  QMD_TRACE_ELEM(&(listelm)->field); \
788 } while (0)
789 
790 #define TAILQ_INSERT_HEAD(head, elm, field) do { \
791  QMD_TAILQ_CHECK_HEAD(head, field); \
792  if ((TAILQ_NEXT((elm), field) = TAILQ_FIRST((head))) != NULL) \
793  TAILQ_FIRST((head))->field.tqe_prev = \
794  &TAILQ_NEXT((elm), field); \
795  else \
796  (head)->tqh_last = &TAILQ_NEXT((elm), field); \
797  TAILQ_FIRST((head)) = (elm); \
798  (elm)->field.tqe_prev = &TAILQ_FIRST((head)); \
799  QMD_TRACE_HEAD(head); \
800  QMD_TRACE_ELEM(&(elm)->field); \
801 } while (0)
802 
803 #define TAILQ_INSERT_TAIL(head, elm, field) do { \
804  QMD_TAILQ_CHECK_TAIL(head, field); \
805  TAILQ_NEXT((elm), field) = NULL; \
806  (elm)->field.tqe_prev = (head)->tqh_last; \
807  *(head)->tqh_last = (elm); \
808  (head)->tqh_last = &TAILQ_NEXT((elm), field); \
809  QMD_TRACE_HEAD(head); \
810  QMD_TRACE_ELEM(&(elm)->field); \
811 } while (0)
812 
813 #define TAILQ_LAST(head, headname) \
814  (*(((struct headname *)((head)->tqh_last))->tqh_last))
815 
816 #define TAILQ_NEXT(elm, field) ((elm)->field.tqe_next)
817 
818 #define TAILQ_PREV(elm, headname, field) \
819  (*(((struct headname *)((elm)->field.tqe_prev))->tqh_last))
820 
821 #define TAILQ_REMOVE(head, elm, field) do { \
822  QMD_SAVELINK(oldnext, (elm)->field.tqe_next); \
823  QMD_SAVELINK(oldprev, (elm)->field.tqe_prev); \
824  QMD_TAILQ_CHECK_NEXT(elm, field); \
825  QMD_TAILQ_CHECK_PREV(elm, field); \
826  if ((TAILQ_NEXT((elm), field)) != NULL) \
827  TAILQ_NEXT((elm), field)->field.tqe_prev = \
828  (elm)->field.tqe_prev; \
829  else { \
830  (head)->tqh_last = (elm)->field.tqe_prev; \
831  QMD_TRACE_HEAD(head); \
832  } \
833  *(elm)->field.tqe_prev = TAILQ_NEXT((elm), field); \
834  TRASHIT(*oldnext); \
835  TRASHIT(*oldprev); \
836  QMD_TRACE_ELEM(&(elm)->field); \
837 } while (0)
838 
839 #define TAILQ_SWAP(head1, head2, type, field) do { \
840  QUEUE_TYPEOF(type) *swap_first = (head1)->tqh_first; \
841  QUEUE_TYPEOF(type) **swap_last = (head1)->tqh_last; \
842  (head1)->tqh_first = (head2)->tqh_first; \
843  (head1)->tqh_last = (head2)->tqh_last; \
844  (head2)->tqh_first = swap_first; \
845  (head2)->tqh_last = swap_last; \
846  if ((swap_first = (head1)->tqh_first) != NULL) \
847  swap_first->field.tqe_prev = &(head1)->tqh_first; \
848  else \
849  (head1)->tqh_last = &(head1)->tqh_first; \
850  if ((swap_first = (head2)->tqh_first) != NULL) \
851  swap_first->field.tqe_prev = &(head2)->tqh_first; \
852  else \
853  (head2)->tqh_last = &(head2)->tqh_first; \
854 } while (0)
855 
856 #endif /* !SYS_QUEUE_H_ */