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