xref: /haiku/src/libs/compat/freebsd_network/compat/sys/queue.h (revision dba28784c21beab5d397068303881fe024a76859)
1*dba28784SAugustin Cavalier /*-
2*dba28784SAugustin Cavalier  * Copyright (c) 1991, 1993
3*dba28784SAugustin Cavalier  *	The Regents of the University of California.  All rights reserved.
4*dba28784SAugustin Cavalier  *
5*dba28784SAugustin Cavalier  * Redistribution and use in source and binary forms, with or without
6*dba28784SAugustin Cavalier  * modification, are permitted provided that the following conditions
7*dba28784SAugustin Cavalier  * are met:
8*dba28784SAugustin Cavalier  * 1. Redistributions of source code must retain the above copyright
9*dba28784SAugustin Cavalier  *    notice, this list of conditions and the following disclaimer.
10*dba28784SAugustin Cavalier  * 2. Redistributions in binary form must reproduce the above copyright
11*dba28784SAugustin Cavalier  *    notice, this list of conditions and the following disclaimer in the
12*dba28784SAugustin Cavalier  *    documentation and/or other materials provided with the distribution.
13*dba28784SAugustin Cavalier  * 4. Neither the name of the University nor the names of its contributors
14*dba28784SAugustin Cavalier  *    may be used to endorse or promote products derived from this software
15*dba28784SAugustin Cavalier  *    without specific prior written permission.
16*dba28784SAugustin Cavalier  *
17*dba28784SAugustin Cavalier  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18*dba28784SAugustin Cavalier  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19*dba28784SAugustin Cavalier  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20*dba28784SAugustin Cavalier  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21*dba28784SAugustin Cavalier  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22*dba28784SAugustin Cavalier  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23*dba28784SAugustin Cavalier  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24*dba28784SAugustin Cavalier  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25*dba28784SAugustin Cavalier  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26*dba28784SAugustin Cavalier  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27*dba28784SAugustin Cavalier  * SUCH DAMAGE.
28*dba28784SAugustin Cavalier  *
29*dba28784SAugustin Cavalier  *	@(#)queue.h	8.5 (Berkeley) 8/20/94
30*dba28784SAugustin Cavalier  * $FreeBSD: releng/11.1/sys/sys/queue.h 307533 2016-10-17 21:44:41Z mckusick $
31*dba28784SAugustin Cavalier  */
32*dba28784SAugustin Cavalier 
33*dba28784SAugustin Cavalier #ifndef _SYS_QUEUE_H_
34*dba28784SAugustin Cavalier #define	_SYS_QUEUE_H_
35*dba28784SAugustin Cavalier 
36*dba28784SAugustin Cavalier #include <sys/cdefs.h>
37*dba28784SAugustin Cavalier 
38*dba28784SAugustin Cavalier /*
39*dba28784SAugustin Cavalier  * This file defines four types of data structures: singly-linked lists,
40*dba28784SAugustin Cavalier  * singly-linked tail queues, lists and tail queues.
41*dba28784SAugustin Cavalier  *
42*dba28784SAugustin Cavalier  * A singly-linked list is headed by a single forward pointer. The elements
43*dba28784SAugustin Cavalier  * are singly linked for minimum space and pointer manipulation overhead at
44*dba28784SAugustin Cavalier  * the expense of O(n) removal for arbitrary elements. New elements can be
45*dba28784SAugustin Cavalier  * added to the list after an existing element or at the head of the list.
46*dba28784SAugustin Cavalier  * Elements being removed from the head of the list should use the explicit
47*dba28784SAugustin Cavalier  * macro for this purpose for optimum efficiency. A singly-linked list may
48*dba28784SAugustin Cavalier  * only be traversed in the forward direction.  Singly-linked lists are ideal
49*dba28784SAugustin Cavalier  * for applications with large datasets and few or no removals or for
50*dba28784SAugustin Cavalier  * implementing a LIFO queue.
51*dba28784SAugustin Cavalier  *
52*dba28784SAugustin Cavalier  * A singly-linked tail queue is headed by a pair of pointers, one to the
53*dba28784SAugustin Cavalier  * head of the list and the other to the tail of the list. The elements are
54*dba28784SAugustin Cavalier  * singly linked for minimum space and pointer manipulation overhead at the
55*dba28784SAugustin Cavalier  * expense of O(n) removal for arbitrary elements. New elements can be added
56*dba28784SAugustin Cavalier  * to the list after an existing element, at the head of the list, or at the
57*dba28784SAugustin Cavalier  * end of the list. Elements being removed from the head of the tail queue
58*dba28784SAugustin Cavalier  * should use the explicit macro for this purpose for optimum efficiency.
59*dba28784SAugustin Cavalier  * A singly-linked tail queue may only be traversed in the forward direction.
60*dba28784SAugustin Cavalier  * Singly-linked tail queues are ideal for applications with large datasets
61*dba28784SAugustin Cavalier  * and few or no removals or for implementing a FIFO queue.
62*dba28784SAugustin Cavalier  *
63*dba28784SAugustin Cavalier  * A list is headed by a single forward pointer (or an array of forward
64*dba28784SAugustin Cavalier  * pointers for a hash table header). The elements are doubly linked
65*dba28784SAugustin Cavalier  * so that an arbitrary element can be removed without a need to
66*dba28784SAugustin Cavalier  * traverse the list. New elements can be added to the list before
67*dba28784SAugustin Cavalier  * or after an existing element or at the head of the list. A list
68*dba28784SAugustin Cavalier  * may be traversed in either direction.
69*dba28784SAugustin Cavalier  *
70*dba28784SAugustin Cavalier  * A tail queue is headed by a pair of pointers, one to the head of the
71*dba28784SAugustin Cavalier  * list and the other to the tail of the list. The elements are doubly
72*dba28784SAugustin Cavalier  * linked so that an arbitrary element can be removed without a need to
73*dba28784SAugustin Cavalier  * traverse the list. New elements can be added to the list before or
74*dba28784SAugustin Cavalier  * after an existing element, at the head of the list, or at the end of
75*dba28784SAugustin Cavalier  * the list. A tail queue may be traversed in either direction.
76*dba28784SAugustin Cavalier  *
77*dba28784SAugustin Cavalier  * For details on the use of these macros, see the queue(3) manual page.
78*dba28784SAugustin Cavalier  *
79*dba28784SAugustin Cavalier  * Below is a summary of implemented functions where:
80*dba28784SAugustin Cavalier  *  +  means the macro is available
81*dba28784SAugustin Cavalier  *  -  means the macro is not available
82*dba28784SAugustin Cavalier  *  s  means the macro is available but is slow (runs in O(n) time)
83*dba28784SAugustin Cavalier  *
84*dba28784SAugustin Cavalier  *				SLIST	LIST	STAILQ	TAILQ
85*dba28784SAugustin Cavalier  * _HEAD			+	+	+	+
86*dba28784SAugustin Cavalier  * _CLASS_HEAD			+	+	+	+
87*dba28784SAugustin Cavalier  * _HEAD_INITIALIZER		+	+	+	+
88*dba28784SAugustin Cavalier  * _ENTRY			+	+	+	+
89*dba28784SAugustin Cavalier  * _CLASS_ENTRY			+	+	+	+
90*dba28784SAugustin Cavalier  * _INIT			+	+	+	+
91*dba28784SAugustin Cavalier  * _EMPTY			+	+	+	+
92*dba28784SAugustin Cavalier  * _FIRST			+	+	+	+
93*dba28784SAugustin Cavalier  * _NEXT			+	+	+	+
94*dba28784SAugustin Cavalier  * _PREV			-	+	-	+
95*dba28784SAugustin Cavalier  * _LAST			-	-	+	+
96*dba28784SAugustin Cavalier  * _FOREACH			+	+	+	+
97*dba28784SAugustin Cavalier  * _FOREACH_FROM		+	+	+	+
98*dba28784SAugustin Cavalier  * _FOREACH_SAFE		+	+	+	+
99*dba28784SAugustin Cavalier  * _FOREACH_FROM_SAFE		+	+	+	+
100*dba28784SAugustin Cavalier  * _FOREACH_REVERSE		-	-	-	+
101*dba28784SAugustin Cavalier  * _FOREACH_REVERSE_FROM	-	-	-	+
102*dba28784SAugustin Cavalier  * _FOREACH_REVERSE_SAFE	-	-	-	+
103*dba28784SAugustin Cavalier  * _FOREACH_REVERSE_FROM_SAFE	-	-	-	+
104*dba28784SAugustin Cavalier  * _INSERT_HEAD			+	+	+	+
105*dba28784SAugustin Cavalier  * _INSERT_BEFORE		-	+	-	+
106*dba28784SAugustin Cavalier  * _INSERT_AFTER		+	+	+	+
107*dba28784SAugustin Cavalier  * _INSERT_TAIL			-	-	+	+
108*dba28784SAugustin Cavalier  * _CONCAT			s	s	+	+
109*dba28784SAugustin Cavalier  * _REMOVE_AFTER		+	-	+	-
110*dba28784SAugustin Cavalier  * _REMOVE_HEAD			+	-	+	-
111*dba28784SAugustin Cavalier  * _REMOVE			s	+	s	+
112*dba28784SAugustin Cavalier  * _SWAP			+	+	+	+
113*dba28784SAugustin Cavalier  *
114*dba28784SAugustin Cavalier  */
115*dba28784SAugustin Cavalier #ifdef QUEUE_MACRO_DEBUG
116*dba28784SAugustin Cavalier /* Store the last 2 places the queue element or head was altered */
117*dba28784SAugustin Cavalier struct qm_trace {
118*dba28784SAugustin Cavalier 	unsigned long	 lastline;
119*dba28784SAugustin Cavalier 	unsigned long	 prevline;
120*dba28784SAugustin Cavalier 	const char	*lastfile;
121*dba28784SAugustin Cavalier 	const char	*prevfile;
122*dba28784SAugustin Cavalier };
123*dba28784SAugustin Cavalier 
124*dba28784SAugustin Cavalier #define	TRACEBUF	struct qm_trace trace;
125*dba28784SAugustin Cavalier #define	TRACEBUF_INITIALIZER	{ __LINE__, 0, __FILE__, NULL } ,
126*dba28784SAugustin Cavalier #define	TRASHIT(x)	do {(x) = (void *)-1;} while (0)
127*dba28784SAugustin Cavalier #define	QMD_SAVELINK(name, link)	void **name = (void *)&(link)
128*dba28784SAugustin Cavalier 
129*dba28784SAugustin Cavalier #define	QMD_TRACE_HEAD(head) do {					\
130*dba28784SAugustin Cavalier 	(head)->trace.prevline = (head)->trace.lastline;		\
131*dba28784SAugustin Cavalier 	(head)->trace.prevfile = (head)->trace.lastfile;		\
132*dba28784SAugustin Cavalier 	(head)->trace.lastline = __LINE__;				\
133*dba28784SAugustin Cavalier 	(head)->trace.lastfile = __FILE__;				\
134*dba28784SAugustin Cavalier } while (0)
135*dba28784SAugustin Cavalier 
136*dba28784SAugustin Cavalier #define	QMD_TRACE_ELEM(elem) do {					\
137*dba28784SAugustin Cavalier 	(elem)->trace.prevline = (elem)->trace.lastline;		\
138*dba28784SAugustin Cavalier 	(elem)->trace.prevfile = (elem)->trace.lastfile;		\
139*dba28784SAugustin Cavalier 	(elem)->trace.lastline = __LINE__;				\
140*dba28784SAugustin Cavalier 	(elem)->trace.lastfile = __FILE__;				\
141*dba28784SAugustin Cavalier } while (0)
142*dba28784SAugustin Cavalier 
143*dba28784SAugustin Cavalier #else
144*dba28784SAugustin Cavalier #define	QMD_TRACE_ELEM(elem)
145*dba28784SAugustin Cavalier #define	QMD_TRACE_HEAD(head)
146*dba28784SAugustin Cavalier #define	QMD_SAVELINK(name, link)
147*dba28784SAugustin Cavalier #define	TRACEBUF
148*dba28784SAugustin Cavalier #define	TRACEBUF_INITIALIZER
149*dba28784SAugustin Cavalier #define	TRASHIT(x)
150*dba28784SAugustin Cavalier #endif	/* QUEUE_MACRO_DEBUG */
151*dba28784SAugustin Cavalier 
152*dba28784SAugustin Cavalier #ifdef __cplusplus
153*dba28784SAugustin Cavalier /*
154*dba28784SAugustin Cavalier  * In C++ there can be structure lists and class lists:
155*dba28784SAugustin Cavalier  */
156*dba28784SAugustin Cavalier #define	QUEUE_TYPEOF(type) type
157*dba28784SAugustin Cavalier #else
158*dba28784SAugustin Cavalier #define	QUEUE_TYPEOF(type) struct type
159*dba28784SAugustin Cavalier #endif
160*dba28784SAugustin Cavalier 
161*dba28784SAugustin Cavalier /*
162*dba28784SAugustin Cavalier  * Singly-linked List declarations.
163*dba28784SAugustin Cavalier  */
164*dba28784SAugustin Cavalier #define	SLIST_HEAD(name, type)						\
165*dba28784SAugustin Cavalier struct name {								\
166*dba28784SAugustin Cavalier 	struct type *slh_first;	/* first element */			\
167*dba28784SAugustin Cavalier }
168*dba28784SAugustin Cavalier 
169*dba28784SAugustin Cavalier #define	SLIST_CLASS_HEAD(name, type)					\
170*dba28784SAugustin Cavalier struct name {								\
171*dba28784SAugustin Cavalier 	class type *slh_first;	/* first element */			\
172*dba28784SAugustin Cavalier }
173*dba28784SAugustin Cavalier 
174*dba28784SAugustin Cavalier #define	SLIST_HEAD_INITIALIZER(head)					\
175*dba28784SAugustin Cavalier 	{ NULL }
176*dba28784SAugustin Cavalier 
177*dba28784SAugustin Cavalier #define	SLIST_ENTRY(type)						\
178*dba28784SAugustin Cavalier struct {								\
179*dba28784SAugustin Cavalier 	struct type *sle_next;	/* next element */			\
180*dba28784SAugustin Cavalier }
181*dba28784SAugustin Cavalier 
182*dba28784SAugustin Cavalier #define	SLIST_CLASS_ENTRY(type)						\
183*dba28784SAugustin Cavalier struct {								\
184*dba28784SAugustin Cavalier 	class type *sle_next;		/* next element */		\
185*dba28784SAugustin Cavalier }
186*dba28784SAugustin Cavalier 
187*dba28784SAugustin Cavalier /*
188*dba28784SAugustin Cavalier  * Singly-linked List functions.
189*dba28784SAugustin Cavalier  */
190*dba28784SAugustin Cavalier #define SLIST_CONCAT(head1, head2, type, field) do {			\
191*dba28784SAugustin Cavalier 	QUEUE_TYPEOF(type) *curelm = SLIST_FIRST(head1);		\
192*dba28784SAugustin Cavalier 	if (curelm == NULL) {						\
193*dba28784SAugustin Cavalier 		if ((SLIST_FIRST(head1) = SLIST_FIRST(head2)) != NULL)	\
194*dba28784SAugustin Cavalier 			SLIST_INIT(head2);				\
195*dba28784SAugustin Cavalier 	} else if (SLIST_FIRST(head2) != NULL) {			\
196*dba28784SAugustin Cavalier 		while (SLIST_NEXT(curelm, field) != NULL)		\
197*dba28784SAugustin Cavalier 			curelm = SLIST_NEXT(curelm, field);		\
198*dba28784SAugustin Cavalier 		SLIST_NEXT(curelm, field) = SLIST_FIRST(head2);		\
199*dba28784SAugustin Cavalier 		SLIST_INIT(head2);					\
200*dba28784SAugustin Cavalier 	}								\
201*dba28784SAugustin Cavalier } while (0)
202*dba28784SAugustin Cavalier 
203*dba28784SAugustin Cavalier #define	SLIST_EMPTY(head)	((head)->slh_first == NULL)
204*dba28784SAugustin Cavalier 
205*dba28784SAugustin Cavalier #define	SLIST_FIRST(head)	((head)->slh_first)
206*dba28784SAugustin Cavalier 
207*dba28784SAugustin Cavalier #define	SLIST_FOREACH(var, head, field)					\
208*dba28784SAugustin Cavalier 	for ((var) = SLIST_FIRST((head));				\
209*dba28784SAugustin Cavalier 	    (var);							\
210*dba28784SAugustin Cavalier 	    (var) = SLIST_NEXT((var), field))
211*dba28784SAugustin Cavalier 
212*dba28784SAugustin Cavalier #define	SLIST_FOREACH_FROM(var, head, field)				\
213*dba28784SAugustin Cavalier 	for ((var) = ((var) ? (var) : SLIST_FIRST((head)));		\
214*dba28784SAugustin Cavalier 	    (var);							\
215*dba28784SAugustin Cavalier 	    (var) = SLIST_NEXT((var), field))
216*dba28784SAugustin Cavalier 
217*dba28784SAugustin Cavalier #define	SLIST_FOREACH_SAFE(var, head, field, tvar)			\
218*dba28784SAugustin Cavalier 	for ((var) = SLIST_FIRST((head));				\
219*dba28784SAugustin Cavalier 	    (var) && ((tvar) = SLIST_NEXT((var), field), 1);		\
220*dba28784SAugustin Cavalier 	    (var) = (tvar))
221*dba28784SAugustin Cavalier 
222*dba28784SAugustin Cavalier #define	SLIST_FOREACH_FROM_SAFE(var, head, field, tvar)			\
223*dba28784SAugustin Cavalier 	for ((var) = ((var) ? (var) : SLIST_FIRST((head)));		\
224*dba28784SAugustin Cavalier 	    (var) && ((tvar) = SLIST_NEXT((var), field), 1);		\
225*dba28784SAugustin Cavalier 	    (var) = (tvar))
226*dba28784SAugustin Cavalier 
227*dba28784SAugustin Cavalier #define	SLIST_FOREACH_PREVPTR(var, varp, head, field)			\
228*dba28784SAugustin Cavalier 	for ((varp) = &SLIST_FIRST((head));				\
229*dba28784SAugustin Cavalier 	    ((var) = *(varp)) != NULL;					\
230*dba28784SAugustin Cavalier 	    (varp) = &SLIST_NEXT((var), field))
231*dba28784SAugustin Cavalier 
232*dba28784SAugustin Cavalier #define	SLIST_INIT(head) do {						\
233*dba28784SAugustin Cavalier 	SLIST_FIRST((head)) = NULL;					\
234*dba28784SAugustin Cavalier } while (0)
235*dba28784SAugustin Cavalier 
236*dba28784SAugustin Cavalier #define	SLIST_INSERT_AFTER(slistelm, elm, field) do {			\
237*dba28784SAugustin Cavalier 	SLIST_NEXT((elm), field) = SLIST_NEXT((slistelm), field);	\
238*dba28784SAugustin Cavalier 	SLIST_NEXT((slistelm), field) = (elm);				\
239*dba28784SAugustin Cavalier } while (0)
240*dba28784SAugustin Cavalier 
241*dba28784SAugustin Cavalier #define	SLIST_INSERT_HEAD(head, elm, field) do {			\
242*dba28784SAugustin Cavalier 	SLIST_NEXT((elm), field) = SLIST_FIRST((head));			\
243*dba28784SAugustin Cavalier 	SLIST_FIRST((head)) = (elm);					\
244*dba28784SAugustin Cavalier } while (0)
245*dba28784SAugustin Cavalier 
246*dba28784SAugustin Cavalier #define	SLIST_NEXT(elm, field)	((elm)->field.sle_next)
247*dba28784SAugustin Cavalier 
248*dba28784SAugustin Cavalier #define	SLIST_REMOVE(head, elm, type, field) do {			\
249*dba28784SAugustin Cavalier 	QMD_SAVELINK(oldnext, (elm)->field.sle_next);			\
250*dba28784SAugustin Cavalier 	if (SLIST_FIRST((head)) == (elm)) {				\
251*dba28784SAugustin Cavalier 		SLIST_REMOVE_HEAD((head), field);			\
252*dba28784SAugustin Cavalier 	}								\
253*dba28784SAugustin Cavalier 	else {								\
254*dba28784SAugustin Cavalier 		QUEUE_TYPEOF(type) *curelm = SLIST_FIRST(head);		\
255*dba28784SAugustin Cavalier 		while (SLIST_NEXT(curelm, field) != (elm))		\
256*dba28784SAugustin Cavalier 			curelm = SLIST_NEXT(curelm, field);		\
257*dba28784SAugustin Cavalier 		SLIST_REMOVE_AFTER(curelm, field);			\
258*dba28784SAugustin Cavalier 	}								\
259*dba28784SAugustin Cavalier 	TRASHIT(*oldnext);						\
260*dba28784SAugustin Cavalier } while (0)
261*dba28784SAugustin Cavalier 
262*dba28784SAugustin Cavalier #define SLIST_REMOVE_AFTER(elm, field) do {				\
263*dba28784SAugustin Cavalier 	SLIST_NEXT(elm, field) =					\
264*dba28784SAugustin Cavalier 	    SLIST_NEXT(SLIST_NEXT(elm, field), field);			\
265*dba28784SAugustin Cavalier } while (0)
266*dba28784SAugustin Cavalier 
267*dba28784SAugustin Cavalier #define	SLIST_REMOVE_HEAD(head, field) do {				\
268*dba28784SAugustin Cavalier 	SLIST_FIRST((head)) = SLIST_NEXT(SLIST_FIRST((head)), field);	\
269*dba28784SAugustin Cavalier } while (0)
270*dba28784SAugustin Cavalier 
271*dba28784SAugustin Cavalier #define SLIST_SWAP(head1, head2, type) do {				\
272*dba28784SAugustin Cavalier 	QUEUE_TYPEOF(type) *swap_first = SLIST_FIRST(head1);		\
273*dba28784SAugustin Cavalier 	SLIST_FIRST(head1) = SLIST_FIRST(head2);			\
274*dba28784SAugustin Cavalier 	SLIST_FIRST(head2) = swap_first;				\
275*dba28784SAugustin Cavalier } while (0)
276*dba28784SAugustin Cavalier 
277*dba28784SAugustin Cavalier /*
278*dba28784SAugustin Cavalier  * Singly-linked Tail queue declarations.
279*dba28784SAugustin Cavalier  */
280*dba28784SAugustin Cavalier #define	STAILQ_HEAD(name, type)						\
281*dba28784SAugustin Cavalier struct name {								\
282*dba28784SAugustin Cavalier 	struct type *stqh_first;/* first element */			\
283*dba28784SAugustin Cavalier 	struct type **stqh_last;/* addr of last next element */		\
284*dba28784SAugustin Cavalier }
285*dba28784SAugustin Cavalier 
286*dba28784SAugustin Cavalier #define	STAILQ_CLASS_HEAD(name, type)					\
287*dba28784SAugustin Cavalier struct name {								\
288*dba28784SAugustin Cavalier 	class type *stqh_first;	/* first element */			\
289*dba28784SAugustin Cavalier 	class type **stqh_last;	/* addr of last next element */		\
290*dba28784SAugustin Cavalier }
291*dba28784SAugustin Cavalier 
292*dba28784SAugustin Cavalier #define	STAILQ_HEAD_INITIALIZER(head)					\
293*dba28784SAugustin Cavalier 	{ NULL, &(head).stqh_first }
294*dba28784SAugustin Cavalier 
295*dba28784SAugustin Cavalier #define	STAILQ_ENTRY(type)						\
296*dba28784SAugustin Cavalier struct {								\
297*dba28784SAugustin Cavalier 	struct type *stqe_next;	/* next element */			\
298*dba28784SAugustin Cavalier }
299*dba28784SAugustin Cavalier 
300*dba28784SAugustin Cavalier #define	STAILQ_CLASS_ENTRY(type)					\
301*dba28784SAugustin Cavalier struct {								\
302*dba28784SAugustin Cavalier 	class type *stqe_next;	/* next element */			\
303*dba28784SAugustin Cavalier }
304*dba28784SAugustin Cavalier 
305*dba28784SAugustin Cavalier /*
306*dba28784SAugustin Cavalier  * Singly-linked Tail queue functions.
307*dba28784SAugustin Cavalier  */
308*dba28784SAugustin Cavalier #define	STAILQ_CONCAT(head1, head2) do {				\
309*dba28784SAugustin Cavalier 	if (!STAILQ_EMPTY((head2))) {					\
310*dba28784SAugustin Cavalier 		*(head1)->stqh_last = (head2)->stqh_first;		\
311*dba28784SAugustin Cavalier 		(head1)->stqh_last = (head2)->stqh_last;		\
312*dba28784SAugustin Cavalier 		STAILQ_INIT((head2));					\
313*dba28784SAugustin Cavalier 	}								\
314*dba28784SAugustin Cavalier } while (0)
315*dba28784SAugustin Cavalier 
316*dba28784SAugustin Cavalier #define	STAILQ_EMPTY(head)	((head)->stqh_first == NULL)
317*dba28784SAugustin Cavalier 
318*dba28784SAugustin Cavalier #define	STAILQ_FIRST(head)	((head)->stqh_first)
319*dba28784SAugustin Cavalier 
320*dba28784SAugustin Cavalier #define	STAILQ_FOREACH(var, head, field)				\
321*dba28784SAugustin Cavalier 	for((var) = STAILQ_FIRST((head));				\
322*dba28784SAugustin Cavalier 	   (var);							\
323*dba28784SAugustin Cavalier 	   (var) = STAILQ_NEXT((var), field))
324*dba28784SAugustin Cavalier 
325*dba28784SAugustin Cavalier #define	STAILQ_FOREACH_FROM(var, head, field)				\
326*dba28784SAugustin Cavalier 	for ((var) = ((var) ? (var) : STAILQ_FIRST((head)));		\
327*dba28784SAugustin Cavalier 	   (var);							\
328*dba28784SAugustin Cavalier 	   (var) = STAILQ_NEXT((var), field))
329*dba28784SAugustin Cavalier 
330*dba28784SAugustin Cavalier #define	STAILQ_FOREACH_SAFE(var, head, field, tvar)			\
331*dba28784SAugustin Cavalier 	for ((var) = STAILQ_FIRST((head));				\
332*dba28784SAugustin Cavalier 	    (var) && ((tvar) = STAILQ_NEXT((var), field), 1);		\
333*dba28784SAugustin Cavalier 	    (var) = (tvar))
334*dba28784SAugustin Cavalier 
335*dba28784SAugustin Cavalier #define	STAILQ_FOREACH_FROM_SAFE(var, head, field, tvar)		\
336*dba28784SAugustin Cavalier 	for ((var) = ((var) ? (var) : STAILQ_FIRST((head)));		\
337*dba28784SAugustin Cavalier 	    (var) && ((tvar) = STAILQ_NEXT((var), field), 1);		\
338*dba28784SAugustin Cavalier 	    (var) = (tvar))
339*dba28784SAugustin Cavalier 
340*dba28784SAugustin Cavalier #define	STAILQ_INIT(head) do {						\
341*dba28784SAugustin Cavalier 	STAILQ_FIRST((head)) = NULL;					\
342*dba28784SAugustin Cavalier 	(head)->stqh_last = &STAILQ_FIRST((head));			\
343*dba28784SAugustin Cavalier } while (0)
344*dba28784SAugustin Cavalier 
345*dba28784SAugustin Cavalier #define	STAILQ_INSERT_AFTER(head, tqelm, elm, field) do {		\
346*dba28784SAugustin Cavalier 	if ((STAILQ_NEXT((elm), field) = STAILQ_NEXT((tqelm), field)) == NULL)\
347*dba28784SAugustin Cavalier 		(head)->stqh_last = &STAILQ_NEXT((elm), field);		\
348*dba28784SAugustin Cavalier 	STAILQ_NEXT((tqelm), field) = (elm);				\
349*dba28784SAugustin Cavalier } while (0)
350*dba28784SAugustin Cavalier 
351*dba28784SAugustin Cavalier #define	STAILQ_INSERT_HEAD(head, elm, field) do {			\
352*dba28784SAugustin Cavalier 	if ((STAILQ_NEXT((elm), field) = STAILQ_FIRST((head))) == NULL)	\
353*dba28784SAugustin Cavalier 		(head)->stqh_last = &STAILQ_NEXT((elm), field);		\
354*dba28784SAugustin Cavalier 	STAILQ_FIRST((head)) = (elm);					\
355*dba28784SAugustin Cavalier } while (0)
356*dba28784SAugustin Cavalier 
357*dba28784SAugustin Cavalier #define	STAILQ_INSERT_TAIL(head, elm, field) do {			\
358*dba28784SAugustin Cavalier 	STAILQ_NEXT((elm), field) = NULL;				\
359*dba28784SAugustin Cavalier 	*(head)->stqh_last = (elm);					\
360*dba28784SAugustin Cavalier 	(head)->stqh_last = &STAILQ_NEXT((elm), field);			\
361*dba28784SAugustin Cavalier } while (0)
362*dba28784SAugustin Cavalier 
363*dba28784SAugustin Cavalier #define	STAILQ_LAST(head, type, field)				\
364*dba28784SAugustin Cavalier 	(STAILQ_EMPTY((head)) ? NULL :				\
365*dba28784SAugustin Cavalier 	    __containerof((head)->stqh_last,			\
366*dba28784SAugustin Cavalier 	    QUEUE_TYPEOF(type), field.stqe_next))
367*dba28784SAugustin Cavalier 
368*dba28784SAugustin Cavalier #define	STAILQ_NEXT(elm, field)	((elm)->field.stqe_next)
369*dba28784SAugustin Cavalier 
370*dba28784SAugustin Cavalier #define	STAILQ_REMOVE(head, elm, type, field) do {			\
371*dba28784SAugustin Cavalier 	QMD_SAVELINK(oldnext, (elm)->field.stqe_next);			\
372*dba28784SAugustin Cavalier 	if (STAILQ_FIRST((head)) == (elm)) {				\
373*dba28784SAugustin Cavalier 		STAILQ_REMOVE_HEAD((head), field);			\
374*dba28784SAugustin Cavalier 	}								\
375*dba28784SAugustin Cavalier 	else {								\
376*dba28784SAugustin Cavalier 		QUEUE_TYPEOF(type) *curelm = STAILQ_FIRST(head);	\
377*dba28784SAugustin Cavalier 		while (STAILQ_NEXT(curelm, field) != (elm))		\
378*dba28784SAugustin Cavalier 			curelm = STAILQ_NEXT(curelm, field);		\
379*dba28784SAugustin Cavalier 		STAILQ_REMOVE_AFTER(head, curelm, field);		\
380*dba28784SAugustin Cavalier 	}								\
381*dba28784SAugustin Cavalier 	TRASHIT(*oldnext);						\
382*dba28784SAugustin Cavalier } while (0)
383*dba28784SAugustin Cavalier 
384*dba28784SAugustin Cavalier #define STAILQ_REMOVE_AFTER(head, elm, field) do {			\
385*dba28784SAugustin Cavalier 	if ((STAILQ_NEXT(elm, field) =					\
386*dba28784SAugustin Cavalier 	     STAILQ_NEXT(STAILQ_NEXT(elm, field), field)) == NULL)	\
387*dba28784SAugustin Cavalier 		(head)->stqh_last = &STAILQ_NEXT((elm), field);		\
388*dba28784SAugustin Cavalier } while (0)
389*dba28784SAugustin Cavalier 
390*dba28784SAugustin Cavalier #define	STAILQ_REMOVE_HEAD(head, field) do {				\
391*dba28784SAugustin Cavalier 	if ((STAILQ_FIRST((head)) =					\
392*dba28784SAugustin Cavalier 	     STAILQ_NEXT(STAILQ_FIRST((head)), field)) == NULL)		\
393*dba28784SAugustin Cavalier 		(head)->stqh_last = &STAILQ_FIRST((head));		\
394*dba28784SAugustin Cavalier } while (0)
395*dba28784SAugustin Cavalier 
396*dba28784SAugustin Cavalier #define STAILQ_SWAP(head1, head2, type) do {				\
397*dba28784SAugustin Cavalier 	QUEUE_TYPEOF(type) *swap_first = STAILQ_FIRST(head1);		\
398*dba28784SAugustin Cavalier 	QUEUE_TYPEOF(type) **swap_last = (head1)->stqh_last;		\
399*dba28784SAugustin Cavalier 	STAILQ_FIRST(head1) = STAILQ_FIRST(head2);			\
400*dba28784SAugustin Cavalier 	(head1)->stqh_last = (head2)->stqh_last;			\
401*dba28784SAugustin Cavalier 	STAILQ_FIRST(head2) = swap_first;				\
402*dba28784SAugustin Cavalier 	(head2)->stqh_last = swap_last;					\
403*dba28784SAugustin Cavalier 	if (STAILQ_EMPTY(head1))					\
404*dba28784SAugustin Cavalier 		(head1)->stqh_last = &STAILQ_FIRST(head1);		\
405*dba28784SAugustin Cavalier 	if (STAILQ_EMPTY(head2))					\
406*dba28784SAugustin Cavalier 		(head2)->stqh_last = &STAILQ_FIRST(head2);		\
407*dba28784SAugustin Cavalier } while (0)
408*dba28784SAugustin Cavalier 
409*dba28784SAugustin Cavalier 
410*dba28784SAugustin Cavalier /*
411*dba28784SAugustin Cavalier  * List declarations.
412*dba28784SAugustin Cavalier  */
413*dba28784SAugustin Cavalier #define	LIST_HEAD(name, type)						\
414*dba28784SAugustin Cavalier struct name {								\
415*dba28784SAugustin Cavalier 	struct type *lh_first;	/* first element */			\
416*dba28784SAugustin Cavalier }
417*dba28784SAugustin Cavalier 
418*dba28784SAugustin Cavalier #define	LIST_CLASS_HEAD(name, type)					\
419*dba28784SAugustin Cavalier struct name {								\
420*dba28784SAugustin Cavalier 	class type *lh_first;	/* first element */			\
421*dba28784SAugustin Cavalier }
422*dba28784SAugustin Cavalier 
423*dba28784SAugustin Cavalier #define	LIST_HEAD_INITIALIZER(head)					\
424*dba28784SAugustin Cavalier 	{ NULL }
425*dba28784SAugustin Cavalier 
426*dba28784SAugustin Cavalier #define	LIST_ENTRY(type)						\
427*dba28784SAugustin Cavalier struct {								\
428*dba28784SAugustin Cavalier 	struct type *le_next;	/* next element */			\
429*dba28784SAugustin Cavalier 	struct type **le_prev;	/* address of previous next element */	\
430*dba28784SAugustin Cavalier }
431*dba28784SAugustin Cavalier 
432*dba28784SAugustin Cavalier #define	LIST_CLASS_ENTRY(type)						\
433*dba28784SAugustin Cavalier struct {								\
434*dba28784SAugustin Cavalier 	class type *le_next;	/* next element */			\
435*dba28784SAugustin Cavalier 	class type **le_prev;	/* address of previous next element */	\
436*dba28784SAugustin Cavalier }
437*dba28784SAugustin Cavalier 
438*dba28784SAugustin Cavalier /*
439*dba28784SAugustin Cavalier  * List functions.
440*dba28784SAugustin Cavalier  */
441*dba28784SAugustin Cavalier 
442*dba28784SAugustin Cavalier #if (defined(_KERNEL) && defined(INVARIANTS))
443*dba28784SAugustin Cavalier #define	QMD_LIST_CHECK_HEAD(head, field) do {				\
444*dba28784SAugustin Cavalier 	if (LIST_FIRST((head)) != NULL &&				\
445*dba28784SAugustin Cavalier 	    LIST_FIRST((head))->field.le_prev !=			\
446*dba28784SAugustin Cavalier 	     &LIST_FIRST((head)))					\
447*dba28784SAugustin Cavalier 		panic("Bad list head %p first->prev != head", (head));	\
448*dba28784SAugustin Cavalier } while (0)
449*dba28784SAugustin Cavalier 
450*dba28784SAugustin Cavalier #define	QMD_LIST_CHECK_NEXT(elm, field) do {				\
451*dba28784SAugustin Cavalier 	if (LIST_NEXT((elm), field) != NULL &&				\
452*dba28784SAugustin Cavalier 	    LIST_NEXT((elm), field)->field.le_prev !=			\
453*dba28784SAugustin Cavalier 	     &((elm)->field.le_next))					\
454*dba28784SAugustin Cavalier 	     	panic("Bad link elm %p next->prev != elm", (elm));	\
455*dba28784SAugustin Cavalier } while (0)
456*dba28784SAugustin Cavalier 
457*dba28784SAugustin Cavalier #define	QMD_LIST_CHECK_PREV(elm, field) do {				\
458*dba28784SAugustin Cavalier 	if (*(elm)->field.le_prev != (elm))				\
459*dba28784SAugustin Cavalier 		panic("Bad link elm %p prev->next != elm", (elm));	\
460*dba28784SAugustin Cavalier } while (0)
461*dba28784SAugustin Cavalier #else
462*dba28784SAugustin Cavalier #define	QMD_LIST_CHECK_HEAD(head, field)
463*dba28784SAugustin Cavalier #define	QMD_LIST_CHECK_NEXT(elm, field)
464*dba28784SAugustin Cavalier #define	QMD_LIST_CHECK_PREV(elm, field)
465*dba28784SAugustin Cavalier #endif /* (_KERNEL && INVARIANTS) */
466*dba28784SAugustin Cavalier 
467*dba28784SAugustin Cavalier #define LIST_CONCAT(head1, head2, type, field) do {			      \
468*dba28784SAugustin Cavalier 	QUEUE_TYPEOF(type) *curelm = LIST_FIRST(head1);			      \
469*dba28784SAugustin Cavalier 	if (curelm == NULL) {						      \
470*dba28784SAugustin Cavalier 		if ((LIST_FIRST(head1) = LIST_FIRST(head2)) != NULL) {	      \
471*dba28784SAugustin Cavalier 			LIST_FIRST(head2)->field.le_prev =		      \
472*dba28784SAugustin Cavalier 			    &LIST_FIRST((head1));			      \
473*dba28784SAugustin Cavalier 			LIST_INIT(head2);				      \
474*dba28784SAugustin Cavalier 		}							      \
475*dba28784SAugustin Cavalier 	} else if (LIST_FIRST(head2) != NULL) {				      \
476*dba28784SAugustin Cavalier 		while (LIST_NEXT(curelm, field) != NULL)		      \
477*dba28784SAugustin Cavalier 			curelm = LIST_NEXT(curelm, field);		      \
478*dba28784SAugustin Cavalier 		LIST_NEXT(curelm, field) = LIST_FIRST(head2);		      \
479*dba28784SAugustin Cavalier 		LIST_FIRST(head2)->field.le_prev = &LIST_NEXT(curelm, field); \
480*dba28784SAugustin Cavalier 		LIST_INIT(head2);					      \
481*dba28784SAugustin Cavalier 	}								      \
482*dba28784SAugustin Cavalier } while (0)
483*dba28784SAugustin Cavalier 
484*dba28784SAugustin Cavalier #define	LIST_EMPTY(head)	((head)->lh_first == NULL)
485*dba28784SAugustin Cavalier 
486*dba28784SAugustin Cavalier #define	LIST_FIRST(head)	((head)->lh_first)
487*dba28784SAugustin Cavalier 
488*dba28784SAugustin Cavalier #define	LIST_FOREACH(var, head, field)					\
489*dba28784SAugustin Cavalier 	for ((var) = LIST_FIRST((head));				\
490*dba28784SAugustin Cavalier 	    (var);							\
491*dba28784SAugustin Cavalier 	    (var) = LIST_NEXT((var), field))
492*dba28784SAugustin Cavalier 
493*dba28784SAugustin Cavalier #define	LIST_FOREACH_FROM(var, head, field)				\
494*dba28784SAugustin Cavalier 	for ((var) = ((var) ? (var) : LIST_FIRST((head)));		\
495*dba28784SAugustin Cavalier 	    (var);							\
496*dba28784SAugustin Cavalier 	    (var) = LIST_NEXT((var), field))
497*dba28784SAugustin Cavalier 
498*dba28784SAugustin Cavalier #define	LIST_FOREACH_SAFE(var, head, field, tvar)			\
499*dba28784SAugustin Cavalier 	for ((var) = LIST_FIRST((head));				\
500*dba28784SAugustin Cavalier 	    (var) && ((tvar) = LIST_NEXT((var), field), 1);		\
501*dba28784SAugustin Cavalier 	    (var) = (tvar))
502*dba28784SAugustin Cavalier 
503*dba28784SAugustin Cavalier #define	LIST_FOREACH_FROM_SAFE(var, head, field, tvar)			\
504*dba28784SAugustin Cavalier 	for ((var) = ((var) ? (var) : LIST_FIRST((head)));		\
505*dba28784SAugustin Cavalier 	    (var) && ((tvar) = LIST_NEXT((var), field), 1);		\
506*dba28784SAugustin Cavalier 	    (var) = (tvar))
507*dba28784SAugustin Cavalier 
508*dba28784SAugustin Cavalier #define	LIST_INIT(head) do {						\
509*dba28784SAugustin Cavalier 	LIST_FIRST((head)) = NULL;					\
510*dba28784SAugustin Cavalier } while (0)
511*dba28784SAugustin Cavalier 
512*dba28784SAugustin Cavalier #define	LIST_INSERT_AFTER(listelm, elm, field) do {			\
513*dba28784SAugustin Cavalier 	QMD_LIST_CHECK_NEXT(listelm, field);				\
514*dba28784SAugustin Cavalier 	if ((LIST_NEXT((elm), field) = LIST_NEXT((listelm), field)) != NULL)\
515*dba28784SAugustin Cavalier 		LIST_NEXT((listelm), field)->field.le_prev =		\
516*dba28784SAugustin Cavalier 		    &LIST_NEXT((elm), field);				\
517*dba28784SAugustin Cavalier 	LIST_NEXT((listelm), field) = (elm);				\
518*dba28784SAugustin Cavalier 	(elm)->field.le_prev = &LIST_NEXT((listelm), field);		\
519*dba28784SAugustin Cavalier } while (0)
520*dba28784SAugustin Cavalier 
521*dba28784SAugustin Cavalier #define	LIST_INSERT_BEFORE(listelm, elm, field) do {			\
522*dba28784SAugustin Cavalier 	QMD_LIST_CHECK_PREV(listelm, field);				\
523*dba28784SAugustin Cavalier 	(elm)->field.le_prev = (listelm)->field.le_prev;		\
524*dba28784SAugustin Cavalier 	LIST_NEXT((elm), field) = (listelm);				\
525*dba28784SAugustin Cavalier 	*(listelm)->field.le_prev = (elm);				\
526*dba28784SAugustin Cavalier 	(listelm)->field.le_prev = &LIST_NEXT((elm), field);		\
527*dba28784SAugustin Cavalier } while (0)
528*dba28784SAugustin Cavalier 
529*dba28784SAugustin Cavalier #define	LIST_INSERT_HEAD(head, elm, field) do {				\
530*dba28784SAugustin Cavalier 	QMD_LIST_CHECK_HEAD((head), field);				\
531*dba28784SAugustin Cavalier 	if ((LIST_NEXT((elm), field) = LIST_FIRST((head))) != NULL)	\
532*dba28784SAugustin Cavalier 		LIST_FIRST((head))->field.le_prev = &LIST_NEXT((elm), field);\
533*dba28784SAugustin Cavalier 	LIST_FIRST((head)) = (elm);					\
534*dba28784SAugustin Cavalier 	(elm)->field.le_prev = &LIST_FIRST((head));			\
535*dba28784SAugustin Cavalier } while (0)
536*dba28784SAugustin Cavalier 
537*dba28784SAugustin Cavalier #define	LIST_NEXT(elm, field)	((elm)->field.le_next)
538*dba28784SAugustin Cavalier 
539*dba28784SAugustin Cavalier #define	LIST_PREV(elm, head, type, field)			\
540*dba28784SAugustin Cavalier 	((elm)->field.le_prev == &LIST_FIRST((head)) ? NULL :	\
541*dba28784SAugustin Cavalier 	    __containerof((elm)->field.le_prev,			\
542*dba28784SAugustin Cavalier 	    QUEUE_TYPEOF(type), field.le_next))
543*dba28784SAugustin Cavalier 
544*dba28784SAugustin Cavalier #define	LIST_REMOVE(elm, field) do {					\
545*dba28784SAugustin Cavalier 	QMD_SAVELINK(oldnext, (elm)->field.le_next);			\
546*dba28784SAugustin Cavalier 	QMD_SAVELINK(oldprev, (elm)->field.le_prev);			\
547*dba28784SAugustin Cavalier 	QMD_LIST_CHECK_NEXT(elm, field);				\
548*dba28784SAugustin Cavalier 	QMD_LIST_CHECK_PREV(elm, field);				\
549*dba28784SAugustin Cavalier 	if (LIST_NEXT((elm), field) != NULL)				\
550*dba28784SAugustin Cavalier 		LIST_NEXT((elm), field)->field.le_prev = 		\
551*dba28784SAugustin Cavalier 		    (elm)->field.le_prev;				\
552*dba28784SAugustin Cavalier 	*(elm)->field.le_prev = LIST_NEXT((elm), field);		\
553*dba28784SAugustin Cavalier 	TRASHIT(*oldnext);						\
554*dba28784SAugustin Cavalier 	TRASHIT(*oldprev);						\
555*dba28784SAugustin Cavalier } while (0)
556*dba28784SAugustin Cavalier 
557*dba28784SAugustin Cavalier #define LIST_SWAP(head1, head2, type, field) do {			\
558*dba28784SAugustin Cavalier 	QUEUE_TYPEOF(type) *swap_tmp = LIST_FIRST(head1);		\
559*dba28784SAugustin Cavalier 	LIST_FIRST((head1)) = LIST_FIRST((head2));			\
560*dba28784SAugustin Cavalier 	LIST_FIRST((head2)) = swap_tmp;					\
561*dba28784SAugustin Cavalier 	if ((swap_tmp = LIST_FIRST((head1))) != NULL)			\
562*dba28784SAugustin Cavalier 		swap_tmp->field.le_prev = &LIST_FIRST((head1));		\
563*dba28784SAugustin Cavalier 	if ((swap_tmp = LIST_FIRST((head2))) != NULL)			\
564*dba28784SAugustin Cavalier 		swap_tmp->field.le_prev = &LIST_FIRST((head2));		\
565*dba28784SAugustin Cavalier } while (0)
566*dba28784SAugustin Cavalier 
567*dba28784SAugustin Cavalier /*
568*dba28784SAugustin Cavalier  * Tail queue declarations.
569*dba28784SAugustin Cavalier  */
570*dba28784SAugustin Cavalier #define	TAILQ_HEAD(name, type)						\
571*dba28784SAugustin Cavalier struct name {								\
572*dba28784SAugustin Cavalier 	struct type *tqh_first;	/* first element */			\
573*dba28784SAugustin Cavalier 	struct type **tqh_last;	/* addr of last next element */		\
574*dba28784SAugustin Cavalier 	TRACEBUF							\
575*dba28784SAugustin Cavalier }
576*dba28784SAugustin Cavalier 
577*dba28784SAugustin Cavalier #define	TAILQ_CLASS_HEAD(name, type)					\
578*dba28784SAugustin Cavalier struct name {								\
579*dba28784SAugustin Cavalier 	class type *tqh_first;	/* first element */			\
580*dba28784SAugustin Cavalier 	class type **tqh_last;	/* addr of last next element */		\
581*dba28784SAugustin Cavalier 	TRACEBUF							\
582*dba28784SAugustin Cavalier }
583*dba28784SAugustin Cavalier 
584*dba28784SAugustin Cavalier #define	TAILQ_HEAD_INITIALIZER(head)					\
585*dba28784SAugustin Cavalier 	{ NULL, &(head).tqh_first, TRACEBUF_INITIALIZER }
586*dba28784SAugustin Cavalier 
587*dba28784SAugustin Cavalier #define	TAILQ_ENTRY(type)						\
588*dba28784SAugustin Cavalier struct {								\
589*dba28784SAugustin Cavalier 	struct type *tqe_next;	/* next element */			\
590*dba28784SAugustin Cavalier 	struct type **tqe_prev;	/* address of previous next element */	\
591*dba28784SAugustin Cavalier 	TRACEBUF							\
592*dba28784SAugustin Cavalier }
593*dba28784SAugustin Cavalier 
594*dba28784SAugustin Cavalier #define	TAILQ_CLASS_ENTRY(type)						\
595*dba28784SAugustin Cavalier struct {								\
596*dba28784SAugustin Cavalier 	class type *tqe_next;	/* next element */			\
597*dba28784SAugustin Cavalier 	class type **tqe_prev;	/* address of previous next element */	\
598*dba28784SAugustin Cavalier 	TRACEBUF							\
599*dba28784SAugustin Cavalier }
600*dba28784SAugustin Cavalier 
601*dba28784SAugustin Cavalier /*
602*dba28784SAugustin Cavalier  * Tail queue functions.
603*dba28784SAugustin Cavalier  */
604*dba28784SAugustin Cavalier #if (defined(_KERNEL) && defined(INVARIANTS))
605*dba28784SAugustin Cavalier #define	QMD_TAILQ_CHECK_HEAD(head, field) do {				\
606*dba28784SAugustin Cavalier 	if (!TAILQ_EMPTY(head) &&					\
607*dba28784SAugustin Cavalier 	    TAILQ_FIRST((head))->field.tqe_prev !=			\
608*dba28784SAugustin Cavalier 	     &TAILQ_FIRST((head)))					\
609*dba28784SAugustin Cavalier 		panic("Bad tailq head %p first->prev != head", (head));	\
610*dba28784SAugustin Cavalier } while (0)
611*dba28784SAugustin Cavalier 
612*dba28784SAugustin Cavalier #define	QMD_TAILQ_CHECK_TAIL(head, field) do {				\
613*dba28784SAugustin Cavalier 	if (*(head)->tqh_last != NULL)					\
614*dba28784SAugustin Cavalier 	    	panic("Bad tailq NEXT(%p->tqh_last) != NULL", (head)); 	\
615*dba28784SAugustin Cavalier } while (0)
616*dba28784SAugustin Cavalier 
617*dba28784SAugustin Cavalier #define	QMD_TAILQ_CHECK_NEXT(elm, field) do {				\
618*dba28784SAugustin Cavalier 	if (TAILQ_NEXT((elm), field) != NULL &&				\
619*dba28784SAugustin Cavalier 	    TAILQ_NEXT((elm), field)->field.tqe_prev !=			\
620*dba28784SAugustin Cavalier 	     &((elm)->field.tqe_next))					\
621*dba28784SAugustin Cavalier 		panic("Bad link elm %p next->prev != elm", (elm));	\
622*dba28784SAugustin Cavalier } while (0)
623*dba28784SAugustin Cavalier 
624*dba28784SAugustin Cavalier #define	QMD_TAILQ_CHECK_PREV(elm, field) do {				\
625*dba28784SAugustin Cavalier 	if (*(elm)->field.tqe_prev != (elm))				\
626*dba28784SAugustin Cavalier 		panic("Bad link elm %p prev->next != elm", (elm));	\
627*dba28784SAugustin Cavalier } while (0)
628*dba28784SAugustin Cavalier #else
629*dba28784SAugustin Cavalier #define	QMD_TAILQ_CHECK_HEAD(head, field)
630*dba28784SAugustin Cavalier #define	QMD_TAILQ_CHECK_TAIL(head, headname)
631*dba28784SAugustin Cavalier #define	QMD_TAILQ_CHECK_NEXT(elm, field)
632*dba28784SAugustin Cavalier #define	QMD_TAILQ_CHECK_PREV(elm, field)
633*dba28784SAugustin Cavalier #endif /* (_KERNEL && INVARIANTS) */
634*dba28784SAugustin Cavalier 
635*dba28784SAugustin Cavalier #define	TAILQ_CONCAT(head1, head2, field) do {				\
636*dba28784SAugustin Cavalier 	if (!TAILQ_EMPTY(head2)) {					\
637*dba28784SAugustin Cavalier 		*(head1)->tqh_last = (head2)->tqh_first;		\
638*dba28784SAugustin Cavalier 		(head2)->tqh_first->field.tqe_prev = (head1)->tqh_last;	\
639*dba28784SAugustin Cavalier 		(head1)->tqh_last = (head2)->tqh_last;			\
640*dba28784SAugustin Cavalier 		TAILQ_INIT((head2));					\
641*dba28784SAugustin Cavalier 		QMD_TRACE_HEAD(head1);					\
642*dba28784SAugustin Cavalier 		QMD_TRACE_HEAD(head2);					\
643*dba28784SAugustin Cavalier 	}								\
644*dba28784SAugustin Cavalier } while (0)
645*dba28784SAugustin Cavalier 
646*dba28784SAugustin Cavalier #define	TAILQ_EMPTY(head)	((head)->tqh_first == NULL)
647*dba28784SAugustin Cavalier 
648*dba28784SAugustin Cavalier #define	TAILQ_FIRST(head)	((head)->tqh_first)
649*dba28784SAugustin Cavalier 
650*dba28784SAugustin Cavalier #define	TAILQ_FOREACH(var, head, field)					\
651*dba28784SAugustin Cavalier 	for ((var) = TAILQ_FIRST((head));				\
652*dba28784SAugustin Cavalier 	    (var);							\
653*dba28784SAugustin Cavalier 	    (var) = TAILQ_NEXT((var), field))
654*dba28784SAugustin Cavalier 
655*dba28784SAugustin Cavalier #define	TAILQ_FOREACH_FROM(var, head, field)				\
656*dba28784SAugustin Cavalier 	for ((var) = ((var) ? (var) : TAILQ_FIRST((head)));		\
657*dba28784SAugustin Cavalier 	    (var);							\
658*dba28784SAugustin Cavalier 	    (var) = TAILQ_NEXT((var), field))
659*dba28784SAugustin Cavalier 
660*dba28784SAugustin Cavalier #define	TAILQ_FOREACH_SAFE(var, head, field, tvar)			\
661*dba28784SAugustin Cavalier 	for ((var) = TAILQ_FIRST((head));				\
662*dba28784SAugustin Cavalier 	    (var) && ((tvar) = TAILQ_NEXT((var), field), 1);		\
663*dba28784SAugustin Cavalier 	    (var) = (tvar))
664*dba28784SAugustin Cavalier 
665*dba28784SAugustin Cavalier #define	TAILQ_FOREACH_FROM_SAFE(var, head, field, tvar)			\
666*dba28784SAugustin Cavalier 	for ((var) = ((var) ? (var) : TAILQ_FIRST((head)));		\
667*dba28784SAugustin Cavalier 	    (var) && ((tvar) = TAILQ_NEXT((var), field), 1);		\
668*dba28784SAugustin Cavalier 	    (var) = (tvar))
669*dba28784SAugustin Cavalier 
670*dba28784SAugustin Cavalier #define	TAILQ_FOREACH_REVERSE(var, head, headname, field)		\
671*dba28784SAugustin Cavalier 	for ((var) = TAILQ_LAST((head), headname);			\
672*dba28784SAugustin Cavalier 	    (var);							\
673*dba28784SAugustin Cavalier 	    (var) = TAILQ_PREV((var), headname, field))
674*dba28784SAugustin Cavalier 
675*dba28784SAugustin Cavalier #define	TAILQ_FOREACH_REVERSE_FROM(var, head, headname, field)		\
676*dba28784SAugustin Cavalier 	for ((var) = ((var) ? (var) : TAILQ_LAST((head), headname));	\
677*dba28784SAugustin Cavalier 	    (var);							\
678*dba28784SAugustin Cavalier 	    (var) = TAILQ_PREV((var), headname, field))
679*dba28784SAugustin Cavalier 
680*dba28784SAugustin Cavalier #define	TAILQ_FOREACH_REVERSE_SAFE(var, head, headname, field, tvar)	\
681*dba28784SAugustin Cavalier 	for ((var) = TAILQ_LAST((head), headname);			\
682*dba28784SAugustin Cavalier 	    (var) && ((tvar) = TAILQ_PREV((var), headname, field), 1);	\
683*dba28784SAugustin Cavalier 	    (var) = (tvar))
684*dba28784SAugustin Cavalier 
685*dba28784SAugustin Cavalier #define	TAILQ_FOREACH_REVERSE_FROM_SAFE(var, head, headname, field, tvar) \
686*dba28784SAugustin Cavalier 	for ((var) = ((var) ? (var) : TAILQ_LAST((head), headname));	\
687*dba28784SAugustin Cavalier 	    (var) && ((tvar) = TAILQ_PREV((var), headname, field), 1);	\
688*dba28784SAugustin Cavalier 	    (var) = (tvar))
689*dba28784SAugustin Cavalier 
690*dba28784SAugustin Cavalier #define	TAILQ_INIT(head) do {						\
691*dba28784SAugustin Cavalier 	TAILQ_FIRST((head)) = NULL;					\
692*dba28784SAugustin Cavalier 	(head)->tqh_last = &TAILQ_FIRST((head));			\
693*dba28784SAugustin Cavalier 	QMD_TRACE_HEAD(head);						\
694*dba28784SAugustin Cavalier } while (0)
695*dba28784SAugustin Cavalier 
696*dba28784SAugustin Cavalier #define	TAILQ_INSERT_AFTER(head, listelm, elm, field) do {		\
697*dba28784SAugustin Cavalier 	QMD_TAILQ_CHECK_NEXT(listelm, field);				\
698*dba28784SAugustin Cavalier 	if ((TAILQ_NEXT((elm), field) = TAILQ_NEXT((listelm), field)) != NULL)\
699*dba28784SAugustin Cavalier 		TAILQ_NEXT((elm), field)->field.tqe_prev = 		\
700*dba28784SAugustin Cavalier 		    &TAILQ_NEXT((elm), field);				\
701*dba28784SAugustin Cavalier 	else {								\
702*dba28784SAugustin Cavalier 		(head)->tqh_last = &TAILQ_NEXT((elm), field);		\
703*dba28784SAugustin Cavalier 		QMD_TRACE_HEAD(head);					\
704*dba28784SAugustin Cavalier 	}								\
705*dba28784SAugustin Cavalier 	TAILQ_NEXT((listelm), field) = (elm);				\
706*dba28784SAugustin Cavalier 	(elm)->field.tqe_prev = &TAILQ_NEXT((listelm), field);		\
707*dba28784SAugustin Cavalier 	QMD_TRACE_ELEM(&(elm)->field);					\
708*dba28784SAugustin Cavalier 	QMD_TRACE_ELEM(&(listelm)->field);				\
709*dba28784SAugustin Cavalier } while (0)
710*dba28784SAugustin Cavalier 
711*dba28784SAugustin Cavalier #define	TAILQ_INSERT_BEFORE(listelm, elm, field) do {			\
712*dba28784SAugustin Cavalier 	QMD_TAILQ_CHECK_PREV(listelm, field);				\
713*dba28784SAugustin Cavalier 	(elm)->field.tqe_prev = (listelm)->field.tqe_prev;		\
714*dba28784SAugustin Cavalier 	TAILQ_NEXT((elm), field) = (listelm);				\
715*dba28784SAugustin Cavalier 	*(listelm)->field.tqe_prev = (elm);				\
716*dba28784SAugustin Cavalier 	(listelm)->field.tqe_prev = &TAILQ_NEXT((elm), field);		\
717*dba28784SAugustin Cavalier 	QMD_TRACE_ELEM(&(elm)->field);					\
718*dba28784SAugustin Cavalier 	QMD_TRACE_ELEM(&(listelm)->field);				\
719*dba28784SAugustin Cavalier } while (0)
720*dba28784SAugustin Cavalier 
721*dba28784SAugustin Cavalier #define	TAILQ_INSERT_HEAD(head, elm, field) do {			\
722*dba28784SAugustin Cavalier 	QMD_TAILQ_CHECK_HEAD(head, field);				\
723*dba28784SAugustin Cavalier 	if ((TAILQ_NEXT((elm), field) = TAILQ_FIRST((head))) != NULL)	\
724*dba28784SAugustin Cavalier 		TAILQ_FIRST((head))->field.tqe_prev =			\
725*dba28784SAugustin Cavalier 		    &TAILQ_NEXT((elm), field);				\
726*dba28784SAugustin Cavalier 	else								\
727*dba28784SAugustin Cavalier 		(head)->tqh_last = &TAILQ_NEXT((elm), field);		\
728*dba28784SAugustin Cavalier 	TAILQ_FIRST((head)) = (elm);					\
729*dba28784SAugustin Cavalier 	(elm)->field.tqe_prev = &TAILQ_FIRST((head));			\
730*dba28784SAugustin Cavalier 	QMD_TRACE_HEAD(head);						\
731*dba28784SAugustin Cavalier 	QMD_TRACE_ELEM(&(elm)->field);					\
732*dba28784SAugustin Cavalier } while (0)
733*dba28784SAugustin Cavalier 
734*dba28784SAugustin Cavalier #define	TAILQ_INSERT_TAIL(head, elm, field) do {			\
735*dba28784SAugustin Cavalier 	QMD_TAILQ_CHECK_TAIL(head, field);				\
736*dba28784SAugustin Cavalier 	TAILQ_NEXT((elm), field) = NULL;				\
737*dba28784SAugustin Cavalier 	(elm)->field.tqe_prev = (head)->tqh_last;			\
738*dba28784SAugustin Cavalier 	*(head)->tqh_last = (elm);					\
739*dba28784SAugustin Cavalier 	(head)->tqh_last = &TAILQ_NEXT((elm), field);			\
740*dba28784SAugustin Cavalier 	QMD_TRACE_HEAD(head);						\
741*dba28784SAugustin Cavalier 	QMD_TRACE_ELEM(&(elm)->field);					\
742*dba28784SAugustin Cavalier } while (0)
743*dba28784SAugustin Cavalier 
744*dba28784SAugustin Cavalier #define	TAILQ_LAST(head, headname)					\
745*dba28784SAugustin Cavalier 	(*(((struct headname *)((head)->tqh_last))->tqh_last))
746*dba28784SAugustin Cavalier 
747*dba28784SAugustin Cavalier #define	TAILQ_NEXT(elm, field) ((elm)->field.tqe_next)
748*dba28784SAugustin Cavalier 
749*dba28784SAugustin Cavalier #define	TAILQ_PREV(elm, headname, field)				\
750*dba28784SAugustin Cavalier 	(*(((struct headname *)((elm)->field.tqe_prev))->tqh_last))
751*dba28784SAugustin Cavalier 
752*dba28784SAugustin Cavalier #define	TAILQ_REMOVE(head, elm, field) do {				\
753*dba28784SAugustin Cavalier 	QMD_SAVELINK(oldnext, (elm)->field.tqe_next);			\
754*dba28784SAugustin Cavalier 	QMD_SAVELINK(oldprev, (elm)->field.tqe_prev);			\
755*dba28784SAugustin Cavalier 	QMD_TAILQ_CHECK_NEXT(elm, field);				\
756*dba28784SAugustin Cavalier 	QMD_TAILQ_CHECK_PREV(elm, field);				\
757*dba28784SAugustin Cavalier 	if ((TAILQ_NEXT((elm), field)) != NULL)				\
758*dba28784SAugustin Cavalier 		TAILQ_NEXT((elm), field)->field.tqe_prev = 		\
759*dba28784SAugustin Cavalier 		    (elm)->field.tqe_prev;				\
760*dba28784SAugustin Cavalier 	else {								\
761*dba28784SAugustin Cavalier 		(head)->tqh_last = (elm)->field.tqe_prev;		\
762*dba28784SAugustin Cavalier 		QMD_TRACE_HEAD(head);					\
763*dba28784SAugustin Cavalier 	}								\
764*dba28784SAugustin Cavalier 	*(elm)->field.tqe_prev = TAILQ_NEXT((elm), field);		\
765*dba28784SAugustin Cavalier 	TRASHIT(*oldnext);						\
766*dba28784SAugustin Cavalier 	TRASHIT(*oldprev);						\
767*dba28784SAugustin Cavalier 	QMD_TRACE_ELEM(&(elm)->field);					\
768*dba28784SAugustin Cavalier } while (0)
769*dba28784SAugustin Cavalier 
770*dba28784SAugustin Cavalier #define TAILQ_SWAP(head1, head2, type, field) do {			\
771*dba28784SAugustin Cavalier 	QUEUE_TYPEOF(type) *swap_first = (head1)->tqh_first;		\
772*dba28784SAugustin Cavalier 	QUEUE_TYPEOF(type) **swap_last = (head1)->tqh_last;		\
773*dba28784SAugustin Cavalier 	(head1)->tqh_first = (head2)->tqh_first;			\
774*dba28784SAugustin Cavalier 	(head1)->tqh_last = (head2)->tqh_last;				\
775*dba28784SAugustin Cavalier 	(head2)->tqh_first = swap_first;				\
776*dba28784SAugustin Cavalier 	(head2)->tqh_last = swap_last;					\
777*dba28784SAugustin Cavalier 	if ((swap_first = (head1)->tqh_first) != NULL)			\
778*dba28784SAugustin Cavalier 		swap_first->field.tqe_prev = &(head1)->tqh_first;	\
779*dba28784SAugustin Cavalier 	else								\
780*dba28784SAugustin Cavalier 		(head1)->tqh_last = &(head1)->tqh_first;		\
781*dba28784SAugustin Cavalier 	if ((swap_first = (head2)->tqh_first) != NULL)			\
782*dba28784SAugustin Cavalier 		swap_first->field.tqe_prev = &(head2)->tqh_first;	\
783*dba28784SAugustin Cavalier 	else								\
784*dba28784SAugustin Cavalier 		(head2)->tqh_last = &(head2)->tqh_first;		\
785*dba28784SAugustin Cavalier } while (0)
786*dba28784SAugustin Cavalier 
787*dba28784SAugustin Cavalier #endif /* !_SYS_QUEUE_H_ */
788