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