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