xref: /optee_os/lib/libutils/isoc/include/sys/queue.h (revision b01047730e77127c23a36591643eeb8bb0487d68)
1*b0104773SPascal Brand /*	$NetBSD: queue.h,v 1.49.6.1 2008/11/20 03:22:38 snj Exp $	*/
2*b0104773SPascal Brand 
3*b0104773SPascal Brand /*
4*b0104773SPascal Brand  * Copyright (c) 1991, 1993
5*b0104773SPascal Brand  *	The Regents of the University of California.  All rights reserved.
6*b0104773SPascal Brand  *
7*b0104773SPascal Brand  * Redistribution and use in source and binary forms, with or without
8*b0104773SPascal Brand  * modification, are permitted provided that the following conditions
9*b0104773SPascal Brand  * are met:
10*b0104773SPascal Brand  * 1. Redistributions of source code must retain the above copyright
11*b0104773SPascal Brand  *    notice, this list of conditions and the following disclaimer.
12*b0104773SPascal Brand  * 2. Redistributions in binary form must reproduce the above copyright
13*b0104773SPascal Brand  *    notice, this list of conditions and the following disclaimer in the
14*b0104773SPascal Brand  *    documentation and/or other materials provided with the distribution.
15*b0104773SPascal Brand  * 3. Neither the name of the University nor the names of its contributors
16*b0104773SPascal Brand  *    may be used to endorse or promote products derived from this software
17*b0104773SPascal Brand  *    without specific prior written permission.
18*b0104773SPascal Brand  *
19*b0104773SPascal Brand  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20*b0104773SPascal Brand  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21*b0104773SPascal Brand  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22*b0104773SPascal Brand  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23*b0104773SPascal Brand  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24*b0104773SPascal Brand  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25*b0104773SPascal Brand  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26*b0104773SPascal Brand  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27*b0104773SPascal Brand  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28*b0104773SPascal Brand  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29*b0104773SPascal Brand  * SUCH DAMAGE.
30*b0104773SPascal Brand  *
31*b0104773SPascal Brand  *	@(#)queue.h	8.5 (Berkeley) 8/20/94
32*b0104773SPascal Brand  */
33*b0104773SPascal Brand 
34*b0104773SPascal Brand #ifndef	_SYS_QUEUE_H_
35*b0104773SPascal Brand #define	_SYS_QUEUE_H_
36*b0104773SPascal Brand 
37*b0104773SPascal Brand /*#include <sys/null.h> */
38*b0104773SPascal Brand 
39*b0104773SPascal Brand /*
40*b0104773SPascal Brand  * This file defines five types of data structures: singly-linked lists,
41*b0104773SPascal Brand  * lists, simple queues, tail queues, and circular queues.
42*b0104773SPascal Brand  *
43*b0104773SPascal Brand  * A singly-linked list is headed by a single forward pointer. The
44*b0104773SPascal Brand  * elements are singly linked for minimum space and pointer manipulation
45*b0104773SPascal Brand  * overhead at the expense of O(n) removal for arbitrary elements. New
46*b0104773SPascal Brand  * elements can be added to the list after an existing element or at the
47*b0104773SPascal Brand  * head of the list.  Elements being removed from the head of the list
48*b0104773SPascal Brand  * should use the explicit macro for this purpose for optimum
49*b0104773SPascal Brand  * efficiency. A singly-linked list may only be traversed in the forward
50*b0104773SPascal Brand  * direction.  Singly-linked lists are ideal for applications with large
51*b0104773SPascal Brand  * datasets and few or no removals or for implementing a LIFO queue.
52*b0104773SPascal Brand  *
53*b0104773SPascal Brand  * A list is headed by a single forward pointer (or an array of forward
54*b0104773SPascal Brand  * pointers for a hash table header). The elements are doubly linked
55*b0104773SPascal Brand  * so that an arbitrary element can be removed without a need to
56*b0104773SPascal Brand  * traverse the list. New elements can be added to the list before
57*b0104773SPascal Brand  * or after an existing element or at the head of the list. A list
58*b0104773SPascal Brand  * may only be traversed in the forward direction.
59*b0104773SPascal Brand  *
60*b0104773SPascal Brand  * A simple queue is headed by a pair of pointers, one the head of the
61*b0104773SPascal Brand  * list and the other to the tail of the list. The elements are singly
62*b0104773SPascal Brand  * linked to save space, so elements can only be removed from the
63*b0104773SPascal Brand  * head of the list. New elements can be added to the list after
64*b0104773SPascal Brand  * an existing element, at the head of the list, or at the end of the
65*b0104773SPascal Brand  * list. A simple queue may only be traversed in the forward direction.
66*b0104773SPascal Brand  *
67*b0104773SPascal Brand  * A tail queue is headed by a pair of pointers, one to the head of the
68*b0104773SPascal Brand  * list and the other to the tail of the list. The elements are doubly
69*b0104773SPascal Brand  * linked so that an arbitrary element can be removed without a need to
70*b0104773SPascal Brand  * traverse the list. New elements can be added to the list before or
71*b0104773SPascal Brand  * after an existing element, at the head of the list, or at the end of
72*b0104773SPascal Brand  * the list. A tail queue may be traversed in either direction.
73*b0104773SPascal Brand  *
74*b0104773SPascal Brand  * A circle queue is headed by a pair of pointers, one to the head of the
75*b0104773SPascal Brand  * list and the other to the tail of the list. The elements are doubly
76*b0104773SPascal Brand  * linked so that an arbitrary element can be removed without a need to
77*b0104773SPascal Brand  * traverse the list. New elements can be added to the list before or after
78*b0104773SPascal Brand  * an existing element, at the head of the list, or at the end of the list.
79*b0104773SPascal Brand  * A circle queue may be traversed in either direction, but has a more
80*b0104773SPascal Brand  * complex end of list detection.
81*b0104773SPascal Brand  *
82*b0104773SPascal Brand  * For details on the use of these macros, see the queue(3) manual page.
83*b0104773SPascal Brand  */
84*b0104773SPascal Brand 
85*b0104773SPascal Brand /*
86*b0104773SPascal Brand  * List definitions.
87*b0104773SPascal Brand  */
88*b0104773SPascal Brand #define	LIST_HEAD(name, type)						\
89*b0104773SPascal Brand struct name {								\
90*b0104773SPascal Brand 	struct type *lh_first;	/* first element */			\
91*b0104773SPascal Brand }
92*b0104773SPascal Brand 
93*b0104773SPascal Brand #define	LIST_HEAD_INITIALIZER(head)					\
94*b0104773SPascal Brand 	{ NULL }
95*b0104773SPascal Brand 
96*b0104773SPascal Brand #define	LIST_ENTRY(type)						\
97*b0104773SPascal Brand struct {								\
98*b0104773SPascal Brand 	struct type *le_next;	/* next element */			\
99*b0104773SPascal Brand 	struct type **le_prev;	/* address of previous next element */	\
100*b0104773SPascal Brand }
101*b0104773SPascal Brand 
102*b0104773SPascal Brand /*
103*b0104773SPascal Brand  * List functions.
104*b0104773SPascal Brand  */
105*b0104773SPascal Brand #if defined(_KERNEL) && defined(QUEUEDEBUG)
106*b0104773SPascal Brand #define	QUEUEDEBUG_LIST_INSERT_HEAD(head, elm, field)			\
107*b0104773SPascal Brand 	if ((head)->lh_first &&						\
108*b0104773SPascal Brand 	    (head)->lh_first->field.le_prev != &(head)->lh_first)	\
109*b0104773SPascal Brand 		panic("LIST_INSERT_HEAD %p %s:%d", (head), __FILE__, __LINE__);
110*b0104773SPascal Brand #define	QUEUEDEBUG_LIST_OP(elm, field)					\
111*b0104773SPascal Brand 	if ((elm)->field.le_next &&					\
112*b0104773SPascal Brand 	    (elm)->field.le_next->field.le_prev !=			\
113*b0104773SPascal Brand 	    &(elm)->field.le_next)					\
114*b0104773SPascal Brand 		panic("LIST_* forw %p %s:%d", (elm), __FILE__, __LINE__);\
115*b0104773SPascal Brand 	if (*(elm)->field.le_prev != (elm))				\
116*b0104773SPascal Brand 		panic("LIST_* back %p %s:%d", (elm), __FILE__, __LINE__);
117*b0104773SPascal Brand #define	QUEUEDEBUG_LIST_POSTREMOVE(elm, field)				\
118*b0104773SPascal Brand 	(elm)->field.le_next = (void *)1L;				\
119*b0104773SPascal Brand 	(elm)->field.le_prev = (void *)1L;
120*b0104773SPascal Brand #else
121*b0104773SPascal Brand #define	QUEUEDEBUG_LIST_INSERT_HEAD(head, elm, field)
122*b0104773SPascal Brand #define	QUEUEDEBUG_LIST_OP(elm, field)
123*b0104773SPascal Brand #define	QUEUEDEBUG_LIST_POSTREMOVE(elm, field)
124*b0104773SPascal Brand #endif
125*b0104773SPascal Brand 
126*b0104773SPascal Brand #define	LIST_INIT(head) do {						\
127*b0104773SPascal Brand 	(head)->lh_first = NULL;					\
128*b0104773SPascal Brand } while (/* CONSTCOND */0)
129*b0104773SPascal Brand 
130*b0104773SPascal Brand #define	LIST_INSERT_AFTER(listelm, elm, field) do {			\
131*b0104773SPascal Brand 	QUEUEDEBUG_LIST_OP((listelm), field)				\
132*b0104773SPascal Brand 	if (((elm)->field.le_next = (listelm)->field.le_next) != NULL)	\
133*b0104773SPascal Brand 		(listelm)->field.le_next->field.le_prev =		\
134*b0104773SPascal Brand 		    &(elm)->field.le_next;				\
135*b0104773SPascal Brand 	(listelm)->field.le_next = (elm);				\
136*b0104773SPascal Brand 	(elm)->field.le_prev = &(listelm)->field.le_next;		\
137*b0104773SPascal Brand } while (/* CONSTCOND */0)
138*b0104773SPascal Brand 
139*b0104773SPascal Brand #define	LIST_INSERT_BEFORE(listelm, elm, field) do {			\
140*b0104773SPascal Brand 	QUEUEDEBUG_LIST_OP((listelm), field)				\
141*b0104773SPascal Brand 	(elm)->field.le_prev = (listelm)->field.le_prev;		\
142*b0104773SPascal Brand 	(elm)->field.le_next = (listelm);				\
143*b0104773SPascal Brand 	*(listelm)->field.le_prev = (elm);				\
144*b0104773SPascal Brand 	(listelm)->field.le_prev = &(elm)->field.le_next;		\
145*b0104773SPascal Brand } while (/* CONSTCOND */0)
146*b0104773SPascal Brand 
147*b0104773SPascal Brand #define	LIST_INSERT_HEAD(head, elm, field) do {				\
148*b0104773SPascal Brand 	QUEUEDEBUG_LIST_INSERT_HEAD((head), (elm), field)		\
149*b0104773SPascal Brand 	if (((elm)->field.le_next = (head)->lh_first) != NULL)		\
150*b0104773SPascal Brand 		(head)->lh_first->field.le_prev = &(elm)->field.le_next;\
151*b0104773SPascal Brand 	(head)->lh_first = (elm);					\
152*b0104773SPascal Brand 	(elm)->field.le_prev = &(head)->lh_first;			\
153*b0104773SPascal Brand } while (/* CONSTCOND */0)
154*b0104773SPascal Brand 
155*b0104773SPascal Brand #define	LIST_REMOVE(elm, field) do {					\
156*b0104773SPascal Brand 	QUEUEDEBUG_LIST_OP((elm), field)				\
157*b0104773SPascal Brand 	if ((elm)->field.le_next != NULL)				\
158*b0104773SPascal Brand 		(elm)->field.le_next->field.le_prev =			\
159*b0104773SPascal Brand 		    (elm)->field.le_prev;				\
160*b0104773SPascal Brand 	*(elm)->field.le_prev = (elm)->field.le_next;			\
161*b0104773SPascal Brand 	QUEUEDEBUG_LIST_POSTREMOVE((elm), field)			\
162*b0104773SPascal Brand } while (/* CONSTCOND */0)
163*b0104773SPascal Brand 
164*b0104773SPascal Brand #define	LIST_FOREACH(var, head, field)					\
165*b0104773SPascal Brand 	for ((var) = ((head)->lh_first);				\
166*b0104773SPascal Brand 		(var);							\
167*b0104773SPascal Brand 		(var) = ((var)->field.le_next))
168*b0104773SPascal Brand 
169*b0104773SPascal Brand /*
170*b0104773SPascal Brand  * List access methods.
171*b0104773SPascal Brand  */
172*b0104773SPascal Brand #define	LIST_EMPTY(head)		((head)->lh_first == NULL)
173*b0104773SPascal Brand #define	LIST_FIRST(head)		((head)->lh_first)
174*b0104773SPascal Brand #define	LIST_NEXT(elm, field)		((elm)->field.le_next)
175*b0104773SPascal Brand 
176*b0104773SPascal Brand /*
177*b0104773SPascal Brand  * Singly-linked List definitions.
178*b0104773SPascal Brand  */
179*b0104773SPascal Brand #define	SLIST_HEAD(name, type)						\
180*b0104773SPascal Brand struct name {								\
181*b0104773SPascal Brand 	struct type *slh_first;	/* first element */			\
182*b0104773SPascal Brand }
183*b0104773SPascal Brand 
184*b0104773SPascal Brand #define	SLIST_HEAD_INITIALIZER(head)					\
185*b0104773SPascal Brand 	{ NULL }
186*b0104773SPascal Brand 
187*b0104773SPascal Brand #define	SLIST_ENTRY(type)						\
188*b0104773SPascal Brand struct {								\
189*b0104773SPascal Brand 	struct type *sle_next;	/* next element */			\
190*b0104773SPascal Brand }
191*b0104773SPascal Brand 
192*b0104773SPascal Brand /*
193*b0104773SPascal Brand  * Singly-linked List functions.
194*b0104773SPascal Brand  */
195*b0104773SPascal Brand #define	SLIST_INIT(head) do {						\
196*b0104773SPascal Brand 	(head)->slh_first = NULL;					\
197*b0104773SPascal Brand } while (/* CONSTCOND */0)
198*b0104773SPascal Brand 
199*b0104773SPascal Brand #define	SLIST_INSERT_AFTER(slistelm, elm, field) do {			\
200*b0104773SPascal Brand 	(elm)->field.sle_next = (slistelm)->field.sle_next;		\
201*b0104773SPascal Brand 	(slistelm)->field.sle_next = (elm);				\
202*b0104773SPascal Brand } while (/* CONSTCOND */0)
203*b0104773SPascal Brand 
204*b0104773SPascal Brand #define	SLIST_INSERT_HEAD(head, elm, field) do {			\
205*b0104773SPascal Brand 	(elm)->field.sle_next = (head)->slh_first;			\
206*b0104773SPascal Brand 	(head)->slh_first = (elm);					\
207*b0104773SPascal Brand } while (/* CONSTCOND */0)
208*b0104773SPascal Brand 
209*b0104773SPascal Brand #define	SLIST_REMOVE_HEAD(head, field) do {				\
210*b0104773SPascal Brand 	(head)->slh_first = (head)->slh_first->field.sle_next;		\
211*b0104773SPascal Brand } while (/* CONSTCOND */0)
212*b0104773SPascal Brand 
213*b0104773SPascal Brand #define	SLIST_REMOVE(head, elm, type, field) do {			\
214*b0104773SPascal Brand 	if ((head)->slh_first == (elm)) {				\
215*b0104773SPascal Brand 		SLIST_REMOVE_HEAD((head), field);			\
216*b0104773SPascal Brand 	}								\
217*b0104773SPascal Brand 	else {								\
218*b0104773SPascal Brand 		struct type *curelm = (head)->slh_first;		\
219*b0104773SPascal Brand 		while(curelm->field.sle_next != (elm))			\
220*b0104773SPascal Brand 			curelm = curelm->field.sle_next;		\
221*b0104773SPascal Brand 		curelm->field.sle_next =				\
222*b0104773SPascal Brand 		    curelm->field.sle_next->field.sle_next;		\
223*b0104773SPascal Brand 	}								\
224*b0104773SPascal Brand } while (/* CONSTCOND */0)
225*b0104773SPascal Brand 
226*b0104773SPascal Brand #define	SLIST_REMOVE_AFTER(slistelm, field) do {			\
227*b0104773SPascal Brand 	(slistelm)->field.sle_next =					\
228*b0104773SPascal Brand 	    SLIST_NEXT(SLIST_NEXT((slistelm), field), field);		\
229*b0104773SPascal Brand } while (/* CONSTCOND */0)
230*b0104773SPascal Brand 
231*b0104773SPascal Brand #define	SLIST_FOREACH(var, head, field)					\
232*b0104773SPascal Brand 	for((var) = (head)->slh_first; (var); (var) = (var)->field.sle_next)
233*b0104773SPascal Brand 
234*b0104773SPascal Brand /*
235*b0104773SPascal Brand  * Singly-linked List access methods.
236*b0104773SPascal Brand  */
237*b0104773SPascal Brand #define	SLIST_EMPTY(head)	((head)->slh_first == NULL)
238*b0104773SPascal Brand #define	SLIST_FIRST(head)	((head)->slh_first)
239*b0104773SPascal Brand #define	SLIST_NEXT(elm, field)	((elm)->field.sle_next)
240*b0104773SPascal Brand 
241*b0104773SPascal Brand /*
242*b0104773SPascal Brand  * Singly-linked Tail queue declarations.
243*b0104773SPascal Brand  */
244*b0104773SPascal Brand #define	STAILQ_HEAD(name, type)					\
245*b0104773SPascal Brand struct name {								\
246*b0104773SPascal Brand 	struct type *stqh_first;	/* first element */			\
247*b0104773SPascal Brand 	struct type **stqh_last;	/* addr of last next element */		\
248*b0104773SPascal Brand }
249*b0104773SPascal Brand 
250*b0104773SPascal Brand #define	STAILQ_HEAD_INITIALIZER(head)					\
251*b0104773SPascal Brand 	{ NULL, &(head).stqh_first }
252*b0104773SPascal Brand 
253*b0104773SPascal Brand #define	STAILQ_ENTRY(type)						\
254*b0104773SPascal Brand struct {								\
255*b0104773SPascal Brand 	struct type *stqe_next;	/* next element */			\
256*b0104773SPascal Brand }
257*b0104773SPascal Brand 
258*b0104773SPascal Brand /*
259*b0104773SPascal Brand  * Singly-linked Tail queue functions.
260*b0104773SPascal Brand  */
261*b0104773SPascal Brand #define	STAILQ_INIT(head) do {						\
262*b0104773SPascal Brand 	(head)->stqh_first = NULL;					\
263*b0104773SPascal Brand 	(head)->stqh_last = &(head)->stqh_first;				\
264*b0104773SPascal Brand } while (/* CONSTCOND */0)
265*b0104773SPascal Brand 
266*b0104773SPascal Brand #define	STAILQ_INSERT_HEAD(head, elm, field) do {			\
267*b0104773SPascal Brand 	if (((elm)->field.stqe_next = (head)->stqh_first) == NULL)	\
268*b0104773SPascal Brand 		(head)->stqh_last = &(elm)->field.stqe_next;		\
269*b0104773SPascal Brand 	(head)->stqh_first = (elm);					\
270*b0104773SPascal Brand } while (/* CONSTCOND */0)
271*b0104773SPascal Brand 
272*b0104773SPascal Brand #define	STAILQ_INSERT_TAIL(head, elm, field) do {			\
273*b0104773SPascal Brand 	(elm)->field.stqe_next = NULL;					\
274*b0104773SPascal Brand 	*(head)->stqh_last = (elm);					\
275*b0104773SPascal Brand 	(head)->stqh_last = &(elm)->field.stqe_next;			\
276*b0104773SPascal Brand } while (/* CONSTCOND */0)
277*b0104773SPascal Brand 
278*b0104773SPascal Brand #define	STAILQ_INSERT_AFTER(head, listelm, elm, field) do {		\
279*b0104773SPascal Brand 	if (((elm)->field.stqe_next = (listelm)->field.stqe_next) == NULL)\
280*b0104773SPascal Brand 		(head)->stqh_last = &(elm)->field.stqe_next;		\
281*b0104773SPascal Brand 	(listelm)->field.stqe_next = (elm);				\
282*b0104773SPascal Brand } while (/* CONSTCOND */0)
283*b0104773SPascal Brand 
284*b0104773SPascal Brand #define	STAILQ_REMOVE_HEAD(head, field) do {				\
285*b0104773SPascal Brand 	if (((head)->stqh_first = (head)->stqh_first->field.stqe_next) == NULL) \
286*b0104773SPascal Brand 		(head)->stqh_last = &(head)->stqh_first;			\
287*b0104773SPascal Brand } while (/* CONSTCOND */0)
288*b0104773SPascal Brand 
289*b0104773SPascal Brand #define	STAILQ_REMOVE(head, elm, type, field) do {			\
290*b0104773SPascal Brand 	if ((head)->stqh_first == (elm)) {				\
291*b0104773SPascal Brand 		STAILQ_REMOVE_HEAD((head), field);			\
292*b0104773SPascal Brand 	} else {							\
293*b0104773SPascal Brand 		struct type *curelm = (head)->stqh_first;		\
294*b0104773SPascal Brand 		while (curelm->field.stqe_next != (elm))			\
295*b0104773SPascal Brand 			curelm = curelm->field.stqe_next;		\
296*b0104773SPascal Brand 		if ((curelm->field.stqe_next =				\
297*b0104773SPascal Brand 			curelm->field.stqe_next->field.stqe_next) == NULL) \
298*b0104773SPascal Brand 			    (head)->stqh_last = &(curelm)->field.stqe_next; \
299*b0104773SPascal Brand 	}								\
300*b0104773SPascal Brand } while (/* CONSTCOND */0)
301*b0104773SPascal Brand 
302*b0104773SPascal Brand #define	STAILQ_FOREACH(var, head, field)				\
303*b0104773SPascal Brand 	for ((var) = ((head)->stqh_first);				\
304*b0104773SPascal Brand 		(var);							\
305*b0104773SPascal Brand 		(var) = ((var)->field.stqe_next))
306*b0104773SPascal Brand 
307*b0104773SPascal Brand #define	STAILQ_CONCAT(head1, head2) do {				\
308*b0104773SPascal Brand 	if (!STAILQ_EMPTY((head2))) {					\
309*b0104773SPascal Brand 		*(head1)->stqh_last = (head2)->stqh_first;		\
310*b0104773SPascal Brand 		(head1)->stqh_last = (head2)->stqh_last;		\
311*b0104773SPascal Brand 		STAILQ_INIT((head2));					\
312*b0104773SPascal Brand 	}								\
313*b0104773SPascal Brand } while (/* CONSTCOND */0)
314*b0104773SPascal Brand 
315*b0104773SPascal Brand /*
316*b0104773SPascal Brand  * Singly-linked Tail queue access methods.
317*b0104773SPascal Brand  */
318*b0104773SPascal Brand #define	STAILQ_EMPTY(head)	((head)->stqh_first == NULL)
319*b0104773SPascal Brand #define	STAILQ_FIRST(head)	((head)->stqh_first)
320*b0104773SPascal Brand #define	STAILQ_NEXT(elm, field)	((elm)->field.stqe_next)
321*b0104773SPascal Brand 
322*b0104773SPascal Brand /*
323*b0104773SPascal Brand  * Simple queue definitions.
324*b0104773SPascal Brand  */
325*b0104773SPascal Brand #define	SIMPLEQ_HEAD(name, type)					\
326*b0104773SPascal Brand struct name {								\
327*b0104773SPascal Brand 	struct type *sqh_first;	/* first element */			\
328*b0104773SPascal Brand 	struct type **sqh_last;	/* addr of last next element */		\
329*b0104773SPascal Brand }
330*b0104773SPascal Brand 
331*b0104773SPascal Brand #define	SIMPLEQ_HEAD_INITIALIZER(head)					\
332*b0104773SPascal Brand 	{ NULL, &(head).sqh_first }
333*b0104773SPascal Brand 
334*b0104773SPascal Brand #define	SIMPLEQ_ENTRY(type)						\
335*b0104773SPascal Brand struct {								\
336*b0104773SPascal Brand 	struct type *sqe_next;	/* next element */			\
337*b0104773SPascal Brand }
338*b0104773SPascal Brand 
339*b0104773SPascal Brand /*
340*b0104773SPascal Brand  * Simple queue functions.
341*b0104773SPascal Brand  */
342*b0104773SPascal Brand #define	SIMPLEQ_INIT(head) do {						\
343*b0104773SPascal Brand 	(head)->sqh_first = NULL;					\
344*b0104773SPascal Brand 	(head)->sqh_last = &(head)->sqh_first;				\
345*b0104773SPascal Brand } while (/* CONSTCOND */0)
346*b0104773SPascal Brand 
347*b0104773SPascal Brand #define	SIMPLEQ_INSERT_HEAD(head, elm, field) do {			\
348*b0104773SPascal Brand 	if (((elm)->field.sqe_next = (head)->sqh_first) == NULL)	\
349*b0104773SPascal Brand 		(head)->sqh_last = &(elm)->field.sqe_next;		\
350*b0104773SPascal Brand 	(head)->sqh_first = (elm);					\
351*b0104773SPascal Brand } while (/* CONSTCOND */0)
352*b0104773SPascal Brand 
353*b0104773SPascal Brand #define	SIMPLEQ_INSERT_TAIL(head, elm, field) do {			\
354*b0104773SPascal Brand 	(elm)->field.sqe_next = NULL;					\
355*b0104773SPascal Brand 	*(head)->sqh_last = (elm);					\
356*b0104773SPascal Brand 	(head)->sqh_last = &(elm)->field.sqe_next;			\
357*b0104773SPascal Brand } while (/* CONSTCOND */0)
358*b0104773SPascal Brand 
359*b0104773SPascal Brand #define	SIMPLEQ_INSERT_AFTER(head, listelm, elm, field) do {		\
360*b0104773SPascal Brand 	if (((elm)->field.sqe_next = (listelm)->field.sqe_next) == NULL)\
361*b0104773SPascal Brand 		(head)->sqh_last = &(elm)->field.sqe_next;		\
362*b0104773SPascal Brand 	(listelm)->field.sqe_next = (elm);				\
363*b0104773SPascal Brand } while (/* CONSTCOND */0)
364*b0104773SPascal Brand 
365*b0104773SPascal Brand #define	SIMPLEQ_REMOVE_HEAD(head, field) do {				\
366*b0104773SPascal Brand 	if (((head)->sqh_first = (head)->sqh_first->field.sqe_next) == NULL) \
367*b0104773SPascal Brand 		(head)->sqh_last = &(head)->sqh_first;			\
368*b0104773SPascal Brand } while (/* CONSTCOND */0)
369*b0104773SPascal Brand 
370*b0104773SPascal Brand #define	SIMPLEQ_REMOVE(head, elm, type, field) do {			\
371*b0104773SPascal Brand 	if ((head)->sqh_first == (elm)) {				\
372*b0104773SPascal Brand 		SIMPLEQ_REMOVE_HEAD((head), field);			\
373*b0104773SPascal Brand 	} else {							\
374*b0104773SPascal Brand 		struct type *curelm = (head)->sqh_first;		\
375*b0104773SPascal Brand 		while (curelm->field.sqe_next != (elm))			\
376*b0104773SPascal Brand 			curelm = curelm->field.sqe_next;		\
377*b0104773SPascal Brand 		if ((curelm->field.sqe_next =				\
378*b0104773SPascal Brand 			curelm->field.sqe_next->field.sqe_next) == NULL) \
379*b0104773SPascal Brand 			    (head)->sqh_last = &(curelm)->field.sqe_next; \
380*b0104773SPascal Brand 	}								\
381*b0104773SPascal Brand } while (/* CONSTCOND */0)
382*b0104773SPascal Brand 
383*b0104773SPascal Brand #define	SIMPLEQ_FOREACH(var, head, field)				\
384*b0104773SPascal Brand 	for ((var) = ((head)->sqh_first);				\
385*b0104773SPascal Brand 		(var);							\
386*b0104773SPascal Brand 		(var) = ((var)->field.sqe_next))
387*b0104773SPascal Brand 
388*b0104773SPascal Brand /*
389*b0104773SPascal Brand  * Simple queue access methods.
390*b0104773SPascal Brand  */
391*b0104773SPascal Brand #define	SIMPLEQ_EMPTY(head)		((head)->sqh_first == NULL)
392*b0104773SPascal Brand #define	SIMPLEQ_FIRST(head)		((head)->sqh_first)
393*b0104773SPascal Brand #define	SIMPLEQ_NEXT(elm, field)	((elm)->field.sqe_next)
394*b0104773SPascal Brand 
395*b0104773SPascal Brand /*
396*b0104773SPascal Brand  * Tail queue definitions.
397*b0104773SPascal Brand  */
398*b0104773SPascal Brand #define	_TAILQ_HEAD(name, type, qual)					\
399*b0104773SPascal Brand struct name {								\
400*b0104773SPascal Brand 	qual type *tqh_first;		/* first element */		\
401*b0104773SPascal Brand 	qual type *qual *tqh_last;	/* addr of last next element */	\
402*b0104773SPascal Brand }
403*b0104773SPascal Brand #define TAILQ_HEAD(name, type)	_TAILQ_HEAD(name, struct type,)
404*b0104773SPascal Brand 
405*b0104773SPascal Brand #define	TAILQ_HEAD_INITIALIZER(head)					\
406*b0104773SPascal Brand 	{ NULL, &(head).tqh_first }
407*b0104773SPascal Brand 
408*b0104773SPascal Brand #define	_TAILQ_ENTRY(type, qual)					\
409*b0104773SPascal Brand struct {								\
410*b0104773SPascal Brand 	qual type *tqe_next;		/* next element */		\
411*b0104773SPascal Brand 	qual type *qual *tqe_prev;	/* address of previous next element */\
412*b0104773SPascal Brand }
413*b0104773SPascal Brand #define TAILQ_ENTRY(type)	_TAILQ_ENTRY(struct type,)
414*b0104773SPascal Brand 
415*b0104773SPascal Brand /*
416*b0104773SPascal Brand  * Tail queue functions.
417*b0104773SPascal Brand  */
418*b0104773SPascal Brand #if defined(_KERNEL) && defined(QUEUEDEBUG)
419*b0104773SPascal Brand #define	QUEUEDEBUG_TAILQ_INSERT_HEAD(head, elm, field)			\
420*b0104773SPascal Brand 	if ((head)->tqh_first &&					\
421*b0104773SPascal Brand 	    (head)->tqh_first->field.tqe_prev != &(head)->tqh_first)	\
422*b0104773SPascal Brand 		panic("TAILQ_INSERT_HEAD %p %s:%d", (head), __FILE__, __LINE__);
423*b0104773SPascal Brand #define	QUEUEDEBUG_TAILQ_INSERT_TAIL(head, elm, field)			\
424*b0104773SPascal Brand 	if (*(head)->tqh_last != NULL)					\
425*b0104773SPascal Brand 		panic("TAILQ_INSERT_TAIL %p %s:%d", (head), __FILE__, __LINE__);
426*b0104773SPascal Brand #define	QUEUEDEBUG_TAILQ_OP(elm, field)					\
427*b0104773SPascal Brand 	if ((elm)->field.tqe_next &&					\
428*b0104773SPascal Brand 	    (elm)->field.tqe_next->field.tqe_prev !=			\
429*b0104773SPascal Brand 	    &(elm)->field.tqe_next)					\
430*b0104773SPascal Brand 		panic("TAILQ_* forw %p %s:%d", (elm), __FILE__, __LINE__);\
431*b0104773SPascal Brand 	if (*(elm)->field.tqe_prev != (elm))				\
432*b0104773SPascal Brand 		panic("TAILQ_* back %p %s:%d", (elm), __FILE__, __LINE__);
433*b0104773SPascal Brand #define	QUEUEDEBUG_TAILQ_PREREMOVE(head, elm, field)			\
434*b0104773SPascal Brand 	if ((elm)->field.tqe_next == NULL &&				\
435*b0104773SPascal Brand 	    (head)->tqh_last != &(elm)->field.tqe_next)			\
436*b0104773SPascal Brand 		panic("TAILQ_PREREMOVE head %p elm %p %s:%d",		\
437*b0104773SPascal Brand 		      (head), (elm), __FILE__, __LINE__);
438*b0104773SPascal Brand #define	QUEUEDEBUG_TAILQ_POSTREMOVE(elm, field)				\
439*b0104773SPascal Brand 	(elm)->field.tqe_next = (void *)1L;				\
440*b0104773SPascal Brand 	(elm)->field.tqe_prev = (void *)1L;
441*b0104773SPascal Brand #else
442*b0104773SPascal Brand #define	QUEUEDEBUG_TAILQ_INSERT_HEAD(head, elm, field)
443*b0104773SPascal Brand #define	QUEUEDEBUG_TAILQ_INSERT_TAIL(head, elm, field)
444*b0104773SPascal Brand #define	QUEUEDEBUG_TAILQ_OP(elm, field)
445*b0104773SPascal Brand #define	QUEUEDEBUG_TAILQ_PREREMOVE(head, elm, field)
446*b0104773SPascal Brand #define	QUEUEDEBUG_TAILQ_POSTREMOVE(elm, field)
447*b0104773SPascal Brand #endif
448*b0104773SPascal Brand 
449*b0104773SPascal Brand #define	TAILQ_INIT(head) do {						\
450*b0104773SPascal Brand 	(head)->tqh_first = NULL;					\
451*b0104773SPascal Brand 	(head)->tqh_last = &(head)->tqh_first;				\
452*b0104773SPascal Brand } while (/* CONSTCOND */0)
453*b0104773SPascal Brand 
454*b0104773SPascal Brand #define	TAILQ_INSERT_HEAD(head, elm, field) do {			\
455*b0104773SPascal Brand 	QUEUEDEBUG_TAILQ_INSERT_HEAD((head), (elm), field)		\
456*b0104773SPascal Brand 	if (((elm)->field.tqe_next = (head)->tqh_first) != NULL)	\
457*b0104773SPascal Brand 		(head)->tqh_first->field.tqe_prev =			\
458*b0104773SPascal Brand 		    &(elm)->field.tqe_next;				\
459*b0104773SPascal Brand 	else								\
460*b0104773SPascal Brand 		(head)->tqh_last = &(elm)->field.tqe_next;		\
461*b0104773SPascal Brand 	(head)->tqh_first = (elm);					\
462*b0104773SPascal Brand 	(elm)->field.tqe_prev = &(head)->tqh_first;			\
463*b0104773SPascal Brand } while (/* CONSTCOND */0)
464*b0104773SPascal Brand 
465*b0104773SPascal Brand #define	TAILQ_INSERT_TAIL(head, elm, field) do {			\
466*b0104773SPascal Brand 	QUEUEDEBUG_TAILQ_INSERT_TAIL((head), (elm), field)		\
467*b0104773SPascal Brand 	(elm)->field.tqe_next = NULL;					\
468*b0104773SPascal Brand 	(elm)->field.tqe_prev = (head)->tqh_last;			\
469*b0104773SPascal Brand 	*(head)->tqh_last = (elm);					\
470*b0104773SPascal Brand 	(head)->tqh_last = &(elm)->field.tqe_next;			\
471*b0104773SPascal Brand } while (/* CONSTCOND */0)
472*b0104773SPascal Brand 
473*b0104773SPascal Brand #define	TAILQ_INSERT_AFTER(head, listelm, elm, field) do {		\
474*b0104773SPascal Brand 	QUEUEDEBUG_TAILQ_OP((listelm), field)				\
475*b0104773SPascal Brand 	if (((elm)->field.tqe_next = (listelm)->field.tqe_next) != NULL)\
476*b0104773SPascal Brand 		(elm)->field.tqe_next->field.tqe_prev =			\
477*b0104773SPascal Brand 		    &(elm)->field.tqe_next;				\
478*b0104773SPascal Brand 	else								\
479*b0104773SPascal Brand 		(head)->tqh_last = &(elm)->field.tqe_next;		\
480*b0104773SPascal Brand 	(listelm)->field.tqe_next = (elm);				\
481*b0104773SPascal Brand 	(elm)->field.tqe_prev = &(listelm)->field.tqe_next;		\
482*b0104773SPascal Brand } while (/* CONSTCOND */0)
483*b0104773SPascal Brand 
484*b0104773SPascal Brand #define	TAILQ_INSERT_BEFORE(listelm, elm, field) do {			\
485*b0104773SPascal Brand 	QUEUEDEBUG_TAILQ_OP((listelm), field)				\
486*b0104773SPascal Brand 	(elm)->field.tqe_prev = (listelm)->field.tqe_prev;		\
487*b0104773SPascal Brand 	(elm)->field.tqe_next = (listelm);				\
488*b0104773SPascal Brand 	*(listelm)->field.tqe_prev = (elm);				\
489*b0104773SPascal Brand 	(listelm)->field.tqe_prev = &(elm)->field.tqe_next;		\
490*b0104773SPascal Brand } while (/* CONSTCOND */0)
491*b0104773SPascal Brand 
492*b0104773SPascal Brand #define	TAILQ_REMOVE(head, elm, field) do {				\
493*b0104773SPascal Brand 	QUEUEDEBUG_TAILQ_PREREMOVE((head), (elm), field)		\
494*b0104773SPascal Brand 	QUEUEDEBUG_TAILQ_OP((elm), field)				\
495*b0104773SPascal Brand 	if (((elm)->field.tqe_next) != NULL)				\
496*b0104773SPascal Brand 		(elm)->field.tqe_next->field.tqe_prev =			\
497*b0104773SPascal Brand 		    (elm)->field.tqe_prev;				\
498*b0104773SPascal Brand 	else								\
499*b0104773SPascal Brand 		(head)->tqh_last = (elm)->field.tqe_prev;		\
500*b0104773SPascal Brand 	*(elm)->field.tqe_prev = (elm)->field.tqe_next;			\
501*b0104773SPascal Brand 	QUEUEDEBUG_TAILQ_POSTREMOVE((elm), field);			\
502*b0104773SPascal Brand } while (/* CONSTCOND */0)
503*b0104773SPascal Brand 
504*b0104773SPascal Brand #define	TAILQ_FOREACH(var, head, field)					\
505*b0104773SPascal Brand 	for ((var) = ((head)->tqh_first);				\
506*b0104773SPascal Brand 		(var);							\
507*b0104773SPascal Brand 		(var) = ((var)->field.tqe_next))
508*b0104773SPascal Brand 
509*b0104773SPascal Brand #define	TAILQ_FOREACH_SAFE(var, head, field, next)			\
510*b0104773SPascal Brand 	for ((var) = ((head)->tqh_first);				\
511*b0104773SPascal Brand 		(var) != NULL && ((next) = TAILQ_NEXT(var, field), 1);	\
512*b0104773SPascal Brand 		(var) = (next))
513*b0104773SPascal Brand 
514*b0104773SPascal Brand #define	TAILQ_FOREACH_REVERSE(var, head, headname, field)		\
515*b0104773SPascal Brand 	for ((var) = (*(((struct headname *)((head)->tqh_last))->tqh_last));	\
516*b0104773SPascal Brand 		(var);							\
517*b0104773SPascal Brand 		(var) = (*(((struct headname *)((var)->field.tqe_prev))->tqh_last)))
518*b0104773SPascal Brand 
519*b0104773SPascal Brand #define	TAILQ_FOREACH_REVERSE_SAFE(var, head, headname, field, prev)	\
520*b0104773SPascal Brand 	for ((var) = TAILQ_LAST((head), headname);			\
521*b0104773SPascal Brand 		(var) && ((prev) = TAILQ_PREV((var), headname, field), 1);\
522*b0104773SPascal Brand 		(var) = (prev))
523*b0104773SPascal Brand 
524*b0104773SPascal Brand #define	TAILQ_CONCAT(head1, head2, field) do {				\
525*b0104773SPascal Brand 	if (!TAILQ_EMPTY(head2)) {					\
526*b0104773SPascal Brand 		*(head1)->tqh_last = (head2)->tqh_first;		\
527*b0104773SPascal Brand 		(head2)->tqh_first->field.tqe_prev = (head1)->tqh_last;	\
528*b0104773SPascal Brand 		(head1)->tqh_last = (head2)->tqh_last;			\
529*b0104773SPascal Brand 		TAILQ_INIT((head2));					\
530*b0104773SPascal Brand 	}								\
531*b0104773SPascal Brand } while (/* CONSTCOND */0)
532*b0104773SPascal Brand 
533*b0104773SPascal Brand /*
534*b0104773SPascal Brand  * Tail queue access methods.
535*b0104773SPascal Brand  */
536*b0104773SPascal Brand #define	TAILQ_EMPTY(head)		((head)->tqh_first == NULL)
537*b0104773SPascal Brand #define	TAILQ_FIRST(head)		((head)->tqh_first)
538*b0104773SPascal Brand #define	TAILQ_NEXT(elm, field)		((elm)->field.tqe_next)
539*b0104773SPascal Brand 
540*b0104773SPascal Brand #define	TAILQ_LAST(head, headname) \
541*b0104773SPascal Brand 	(*(((struct headname *)((head)->tqh_last))->tqh_last))
542*b0104773SPascal Brand #define	TAILQ_PREV(elm, headname, field) \
543*b0104773SPascal Brand 	(*(((struct headname *)((elm)->field.tqe_prev))->tqh_last))
544*b0104773SPascal Brand 
545*b0104773SPascal Brand /*
546*b0104773SPascal Brand  * Circular queue definitions.
547*b0104773SPascal Brand  */
548*b0104773SPascal Brand #if defined(_KERNEL) && defined(QUEUEDEBUG)
549*b0104773SPascal Brand #define QUEUEDEBUG_CIRCLEQ_HEAD(head, field)				\
550*b0104773SPascal Brand 	if ((head)->cqh_first != (void *)(head) &&			\
551*b0104773SPascal Brand 	    (head)->cqh_first->field.cqe_prev != (void *)(head))	\
552*b0104773SPascal Brand 		panic("CIRCLEQ head forw %p %s:%d", (head),		\
553*b0104773SPascal Brand 		      __FILE__, __LINE__);				\
554*b0104773SPascal Brand 	if ((head)->cqh_last != (void *)(head) &&			\
555*b0104773SPascal Brand 	    (head)->cqh_last->field.cqe_next != (void *)(head))		\
556*b0104773SPascal Brand 		panic("CIRCLEQ head back %p %s:%d", (head),		\
557*b0104773SPascal Brand 		      __FILE__, __LINE__);
558*b0104773SPascal Brand #define QUEUEDEBUG_CIRCLEQ_ELM(head, elm, field)			\
559*b0104773SPascal Brand 	if ((elm)->field.cqe_next == (void *)(head)) {			\
560*b0104773SPascal Brand 		if ((head)->cqh_last != (elm))				\
561*b0104773SPascal Brand 			panic("CIRCLEQ elm last %p %s:%d", (elm),	\
562*b0104773SPascal Brand 			      __FILE__, __LINE__);			\
563*b0104773SPascal Brand 	} else {							\
564*b0104773SPascal Brand 		if ((elm)->field.cqe_next->field.cqe_prev != (elm))	\
565*b0104773SPascal Brand 			panic("CIRCLEQ elm forw %p %s:%d", (elm),	\
566*b0104773SPascal Brand 			      __FILE__, __LINE__);			\
567*b0104773SPascal Brand 	}								\
568*b0104773SPascal Brand 	if ((elm)->field.cqe_prev == (void *)(head)) {			\
569*b0104773SPascal Brand 		if ((head)->cqh_first != (elm))				\
570*b0104773SPascal Brand 			panic("CIRCLEQ elm first %p %s:%d", (elm),	\
571*b0104773SPascal Brand 			      __FILE__, __LINE__);			\
572*b0104773SPascal Brand 	} else {							\
573*b0104773SPascal Brand 		if ((elm)->field.cqe_prev->field.cqe_next != (elm))	\
574*b0104773SPascal Brand 			panic("CIRCLEQ elm prev %p %s:%d", (elm),	\
575*b0104773SPascal Brand 			      __FILE__, __LINE__);			\
576*b0104773SPascal Brand 	}
577*b0104773SPascal Brand #define QUEUEDEBUG_CIRCLEQ_POSTREMOVE(elm, field)			\
578*b0104773SPascal Brand 	(elm)->field.cqe_next = (void *)1L;				\
579*b0104773SPascal Brand 	(elm)->field.cqe_prev = (void *)1L;
580*b0104773SPascal Brand #else
581*b0104773SPascal Brand #define QUEUEDEBUG_CIRCLEQ_HEAD(head, field)
582*b0104773SPascal Brand #define QUEUEDEBUG_CIRCLEQ_ELM(head, elm, field)
583*b0104773SPascal Brand #define QUEUEDEBUG_CIRCLEQ_POSTREMOVE(elm, field)
584*b0104773SPascal Brand #endif
585*b0104773SPascal Brand 
586*b0104773SPascal Brand #define	CIRCLEQ_HEAD(name, type)					\
587*b0104773SPascal Brand struct name {								\
588*b0104773SPascal Brand 	struct type *cqh_first;		/* first element */		\
589*b0104773SPascal Brand 	struct type *cqh_last;		/* last element */		\
590*b0104773SPascal Brand }
591*b0104773SPascal Brand 
592*b0104773SPascal Brand #define	CIRCLEQ_HEAD_INITIALIZER(head)					\
593*b0104773SPascal Brand 	{ (void *)&head, (void *)&head }
594*b0104773SPascal Brand 
595*b0104773SPascal Brand #define	CIRCLEQ_ENTRY(type)						\
596*b0104773SPascal Brand struct {								\
597*b0104773SPascal Brand 	struct type *cqe_next;		/* next element */		\
598*b0104773SPascal Brand 	struct type *cqe_prev;		/* previous element */		\
599*b0104773SPascal Brand }
600*b0104773SPascal Brand 
601*b0104773SPascal Brand /*
602*b0104773SPascal Brand  * Circular queue functions.
603*b0104773SPascal Brand  */
604*b0104773SPascal Brand #define	CIRCLEQ_INIT(head) do {						\
605*b0104773SPascal Brand 	(head)->cqh_first = (void *)(head);				\
606*b0104773SPascal Brand 	(head)->cqh_last = (void *)(head);				\
607*b0104773SPascal Brand } while (/* CONSTCOND */0)
608*b0104773SPascal Brand 
609*b0104773SPascal Brand #define	CIRCLEQ_INSERT_AFTER(head, listelm, elm, field) do {		\
610*b0104773SPascal Brand 	QUEUEDEBUG_CIRCLEQ_HEAD((head), field)				\
611*b0104773SPascal Brand 	QUEUEDEBUG_CIRCLEQ_ELM((head), (listelm), field)		\
612*b0104773SPascal Brand 	(elm)->field.cqe_next = (listelm)->field.cqe_next;		\
613*b0104773SPascal Brand 	(elm)->field.cqe_prev = (listelm);				\
614*b0104773SPascal Brand 	if ((listelm)->field.cqe_next == (void *)(head))		\
615*b0104773SPascal Brand 		(head)->cqh_last = (elm);				\
616*b0104773SPascal Brand 	else								\
617*b0104773SPascal Brand 		(listelm)->field.cqe_next->field.cqe_prev = (elm);	\
618*b0104773SPascal Brand 	(listelm)->field.cqe_next = (elm);				\
619*b0104773SPascal Brand } while (/* CONSTCOND */0)
620*b0104773SPascal Brand 
621*b0104773SPascal Brand #define	CIRCLEQ_INSERT_BEFORE(head, listelm, elm, field) do {		\
622*b0104773SPascal Brand 	QUEUEDEBUG_CIRCLEQ_HEAD((head), field)				\
623*b0104773SPascal Brand 	QUEUEDEBUG_CIRCLEQ_ELM((head), (listelm), field)		\
624*b0104773SPascal Brand 	(elm)->field.cqe_next = (listelm);				\
625*b0104773SPascal Brand 	(elm)->field.cqe_prev = (listelm)->field.cqe_prev;		\
626*b0104773SPascal Brand 	if ((listelm)->field.cqe_prev == (void *)(head))		\
627*b0104773SPascal Brand 		(head)->cqh_first = (elm);				\
628*b0104773SPascal Brand 	else								\
629*b0104773SPascal Brand 		(listelm)->field.cqe_prev->field.cqe_next = (elm);	\
630*b0104773SPascal Brand 	(listelm)->field.cqe_prev = (elm);				\
631*b0104773SPascal Brand } while (/* CONSTCOND */0)
632*b0104773SPascal Brand 
633*b0104773SPascal Brand #define	CIRCLEQ_INSERT_HEAD(head, elm, field) do {			\
634*b0104773SPascal Brand 	QUEUEDEBUG_CIRCLEQ_HEAD((head), field)				\
635*b0104773SPascal Brand 	(elm)->field.cqe_next = (head)->cqh_first;			\
636*b0104773SPascal Brand 	(elm)->field.cqe_prev = (void *)(head);				\
637*b0104773SPascal Brand 	if ((head)->cqh_last == (void *)(head))				\
638*b0104773SPascal Brand 		(head)->cqh_last = (elm);				\
639*b0104773SPascal Brand 	else								\
640*b0104773SPascal Brand 		(head)->cqh_first->field.cqe_prev = (elm);		\
641*b0104773SPascal Brand 	(head)->cqh_first = (elm);					\
642*b0104773SPascal Brand } while (/* CONSTCOND */0)
643*b0104773SPascal Brand 
644*b0104773SPascal Brand #define	CIRCLEQ_INSERT_TAIL(head, elm, field) do {			\
645*b0104773SPascal Brand 	QUEUEDEBUG_CIRCLEQ_HEAD((head), field)				\
646*b0104773SPascal Brand 	(elm)->field.cqe_next = (void *)(head);				\
647*b0104773SPascal Brand 	(elm)->field.cqe_prev = (head)->cqh_last;			\
648*b0104773SPascal Brand 	if ((head)->cqh_first == (void *)(head))			\
649*b0104773SPascal Brand 		(head)->cqh_first = (elm);				\
650*b0104773SPascal Brand 	else								\
651*b0104773SPascal Brand 		(head)->cqh_last->field.cqe_next = (elm);		\
652*b0104773SPascal Brand 	(head)->cqh_last = (elm);					\
653*b0104773SPascal Brand } while (/* CONSTCOND */0)
654*b0104773SPascal Brand 
655*b0104773SPascal Brand #define	CIRCLEQ_REMOVE(head, elm, field) do {				\
656*b0104773SPascal Brand 	QUEUEDEBUG_CIRCLEQ_HEAD((head), field)				\
657*b0104773SPascal Brand 	QUEUEDEBUG_CIRCLEQ_ELM((head), (elm), field)			\
658*b0104773SPascal Brand 	if ((elm)->field.cqe_next == (void *)(head))			\
659*b0104773SPascal Brand 		(head)->cqh_last = (elm)->field.cqe_prev;		\
660*b0104773SPascal Brand 	else								\
661*b0104773SPascal Brand 		(elm)->field.cqe_next->field.cqe_prev =			\
662*b0104773SPascal Brand 		    (elm)->field.cqe_prev;				\
663*b0104773SPascal Brand 	if ((elm)->field.cqe_prev == (void *)(head))			\
664*b0104773SPascal Brand 		(head)->cqh_first = (elm)->field.cqe_next;		\
665*b0104773SPascal Brand 	else								\
666*b0104773SPascal Brand 		(elm)->field.cqe_prev->field.cqe_next =			\
667*b0104773SPascal Brand 		    (elm)->field.cqe_next;				\
668*b0104773SPascal Brand 	QUEUEDEBUG_CIRCLEQ_POSTREMOVE((elm), field)			\
669*b0104773SPascal Brand } while (/* CONSTCOND */0)
670*b0104773SPascal Brand 
671*b0104773SPascal Brand #define	CIRCLEQ_FOREACH(var, head, field)				\
672*b0104773SPascal Brand 	for ((var) = ((head)->cqh_first);				\
673*b0104773SPascal Brand 		(var) != (const void *)(head);				\
674*b0104773SPascal Brand 		(var) = ((var)->field.cqe_next))
675*b0104773SPascal Brand 
676*b0104773SPascal Brand #define	CIRCLEQ_FOREACH_REVERSE(var, head, field)			\
677*b0104773SPascal Brand 	for ((var) = ((head)->cqh_last);				\
678*b0104773SPascal Brand 		(var) != (const void *)(head);				\
679*b0104773SPascal Brand 		(var) = ((var)->field.cqe_prev))
680*b0104773SPascal Brand 
681*b0104773SPascal Brand /*
682*b0104773SPascal Brand  * Circular queue access methods.
683*b0104773SPascal Brand  */
684*b0104773SPascal Brand #define	CIRCLEQ_EMPTY(head)		((head)->cqh_first == (void *)(head))
685*b0104773SPascal Brand #define	CIRCLEQ_FIRST(head)		((head)->cqh_first)
686*b0104773SPascal Brand #define	CIRCLEQ_LAST(head)		((head)->cqh_last)
687*b0104773SPascal Brand #define	CIRCLEQ_NEXT(elm, field)	((elm)->field.cqe_next)
688*b0104773SPascal Brand #define	CIRCLEQ_PREV(elm, field)	((elm)->field.cqe_prev)
689*b0104773SPascal Brand 
690*b0104773SPascal Brand #define CIRCLEQ_LOOP_NEXT(head, elm, field)				\
691*b0104773SPascal Brand 	(((elm)->field.cqe_next == (void *)(head))			\
692*b0104773SPascal Brand 	    ? ((head)->cqh_first)					\
693*b0104773SPascal Brand 	    : (elm->field.cqe_next))
694*b0104773SPascal Brand #define CIRCLEQ_LOOP_PREV(head, elm, field)				\
695*b0104773SPascal Brand 	(((elm)->field.cqe_prev == (void *)(head))			\
696*b0104773SPascal Brand 	    ? ((head)->cqh_last)					\
697*b0104773SPascal Brand 	    : (elm->field.cqe_prev))
698*b0104773SPascal Brand 
699*b0104773SPascal Brand #endif /* !_SYS_QUEUE_H_ */
700