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