xref: /optee_os/lib/libutils/isoc/include/sys/queue.h (revision 1bb929836182ecb96d2d9d268daa807c67596396)
1*1bb92983SJerome 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 
177b0104773SPascal Brand /*
178b0104773SPascal Brand  * Singly-linked List definitions.
179b0104773SPascal Brand  */
180b0104773SPascal Brand #define	SLIST_HEAD(name, type)						\
181b0104773SPascal Brand struct name {								\
182b0104773SPascal Brand 	struct type *slh_first;	/* first element */			\
183b0104773SPascal Brand }
184b0104773SPascal Brand 
185b0104773SPascal Brand #define	SLIST_HEAD_INITIALIZER(head)					\
186b0104773SPascal Brand 	{ NULL }
187b0104773SPascal Brand 
188b0104773SPascal Brand #define	SLIST_ENTRY(type)						\
189b0104773SPascal Brand struct {								\
190b0104773SPascal Brand 	struct type *sle_next;	/* next element */			\
191b0104773SPascal Brand }
192b0104773SPascal Brand 
193b0104773SPascal Brand /*
194b0104773SPascal Brand  * Singly-linked List functions.
195b0104773SPascal Brand  */
196b0104773SPascal Brand #define	SLIST_INIT(head) do {						\
197b0104773SPascal Brand 	(head)->slh_first = NULL;					\
198b0104773SPascal Brand } while (/* CONSTCOND */0)
199b0104773SPascal Brand 
200b0104773SPascal Brand #define	SLIST_INSERT_AFTER(slistelm, elm, field) do {			\
201b0104773SPascal Brand 	(elm)->field.sle_next = (slistelm)->field.sle_next;		\
202b0104773SPascal Brand 	(slistelm)->field.sle_next = (elm);				\
203b0104773SPascal Brand } while (/* CONSTCOND */0)
204b0104773SPascal Brand 
205b0104773SPascal Brand #define	SLIST_INSERT_HEAD(head, elm, field) do {			\
206b0104773SPascal Brand 	(elm)->field.sle_next = (head)->slh_first;			\
207b0104773SPascal Brand 	(head)->slh_first = (elm);					\
208b0104773SPascal Brand } while (/* CONSTCOND */0)
209b0104773SPascal Brand 
210b0104773SPascal Brand #define	SLIST_REMOVE_HEAD(head, field) do {				\
211b0104773SPascal Brand 	(head)->slh_first = (head)->slh_first->field.sle_next;		\
212b0104773SPascal Brand } while (/* CONSTCOND */0)
213b0104773SPascal Brand 
214b0104773SPascal Brand #define	SLIST_REMOVE(head, elm, type, field) do {			\
215b0104773SPascal Brand 	if ((head)->slh_first == (elm)) {				\
216b0104773SPascal Brand 		SLIST_REMOVE_HEAD((head), field);			\
217b0104773SPascal Brand 	}								\
218b0104773SPascal Brand 	else {								\
219b0104773SPascal Brand 		struct type *curelm = (head)->slh_first;		\
220b0104773SPascal Brand 		while(curelm->field.sle_next != (elm))			\
221b0104773SPascal Brand 			curelm = curelm->field.sle_next;		\
222b0104773SPascal Brand 		curelm->field.sle_next =				\
223b0104773SPascal Brand 		    curelm->field.sle_next->field.sle_next;		\
224b0104773SPascal Brand 	}								\
225b0104773SPascal Brand } while (/* CONSTCOND */0)
226b0104773SPascal Brand 
227b0104773SPascal Brand #define	SLIST_REMOVE_AFTER(slistelm, field) do {			\
228b0104773SPascal Brand 	(slistelm)->field.sle_next =					\
229b0104773SPascal Brand 	    SLIST_NEXT(SLIST_NEXT((slistelm), field), field);		\
230b0104773SPascal Brand } while (/* CONSTCOND */0)
231b0104773SPascal Brand 
232b0104773SPascal Brand #define	SLIST_FOREACH(var, head, field)					\
233b0104773SPascal Brand 	for((var) = (head)->slh_first; (var); (var) = (var)->field.sle_next)
234b0104773SPascal Brand 
235b0104773SPascal Brand /*
236b0104773SPascal Brand  * Singly-linked List access methods.
237b0104773SPascal Brand  */
238b0104773SPascal Brand #define	SLIST_EMPTY(head)	((head)->slh_first == NULL)
239b0104773SPascal Brand #define	SLIST_FIRST(head)	((head)->slh_first)
240b0104773SPascal Brand #define	SLIST_NEXT(elm, field)	((elm)->field.sle_next)
241b0104773SPascal Brand 
242b0104773SPascal Brand /*
243b0104773SPascal Brand  * Singly-linked Tail queue declarations.
244b0104773SPascal Brand  */
245b0104773SPascal Brand #define	STAILQ_HEAD(name, type)					\
246b0104773SPascal Brand struct name {								\
247b0104773SPascal Brand 	struct type *stqh_first;	/* first element */			\
248b0104773SPascal Brand 	struct type **stqh_last;	/* addr of last next element */		\
249b0104773SPascal Brand }
250b0104773SPascal Brand 
251b0104773SPascal Brand #define	STAILQ_HEAD_INITIALIZER(head)					\
252b0104773SPascal Brand 	{ NULL, &(head).stqh_first }
253b0104773SPascal Brand 
254b0104773SPascal Brand #define	STAILQ_ENTRY(type)						\
255b0104773SPascal Brand struct {								\
256b0104773SPascal Brand 	struct type *stqe_next;	/* next element */			\
257b0104773SPascal Brand }
258b0104773SPascal Brand 
259b0104773SPascal Brand /*
260b0104773SPascal Brand  * Singly-linked Tail queue functions.
261b0104773SPascal Brand  */
262b0104773SPascal Brand #define	STAILQ_INIT(head) do {						\
263b0104773SPascal Brand 	(head)->stqh_first = NULL;					\
264b0104773SPascal Brand 	(head)->stqh_last = &(head)->stqh_first;				\
265b0104773SPascal Brand } while (/* CONSTCOND */0)
266b0104773SPascal Brand 
267b0104773SPascal Brand #define	STAILQ_INSERT_HEAD(head, elm, field) do {			\
268b0104773SPascal Brand 	if (((elm)->field.stqe_next = (head)->stqh_first) == NULL)	\
269b0104773SPascal Brand 		(head)->stqh_last = &(elm)->field.stqe_next;		\
270b0104773SPascal Brand 	(head)->stqh_first = (elm);					\
271b0104773SPascal Brand } while (/* CONSTCOND */0)
272b0104773SPascal Brand 
273b0104773SPascal Brand #define	STAILQ_INSERT_TAIL(head, elm, field) do {			\
274b0104773SPascal Brand 	(elm)->field.stqe_next = NULL;					\
275b0104773SPascal Brand 	*(head)->stqh_last = (elm);					\
276b0104773SPascal Brand 	(head)->stqh_last = &(elm)->field.stqe_next;			\
277b0104773SPascal Brand } while (/* CONSTCOND */0)
278b0104773SPascal Brand 
279b0104773SPascal Brand #define	STAILQ_INSERT_AFTER(head, listelm, elm, field) do {		\
280b0104773SPascal Brand 	if (((elm)->field.stqe_next = (listelm)->field.stqe_next) == NULL)\
281b0104773SPascal Brand 		(head)->stqh_last = &(elm)->field.stqe_next;		\
282b0104773SPascal Brand 	(listelm)->field.stqe_next = (elm);				\
283b0104773SPascal Brand } while (/* CONSTCOND */0)
284b0104773SPascal Brand 
285b0104773SPascal Brand #define	STAILQ_REMOVE_HEAD(head, field) do {				\
286b0104773SPascal Brand 	if (((head)->stqh_first = (head)->stqh_first->field.stqe_next) == NULL) \
287b0104773SPascal Brand 		(head)->stqh_last = &(head)->stqh_first;			\
288b0104773SPascal Brand } while (/* CONSTCOND */0)
289b0104773SPascal Brand 
290b0104773SPascal Brand #define	STAILQ_REMOVE(head, elm, type, field) do {			\
291b0104773SPascal Brand 	if ((head)->stqh_first == (elm)) {				\
292b0104773SPascal Brand 		STAILQ_REMOVE_HEAD((head), field);			\
293b0104773SPascal Brand 	} else {							\
294b0104773SPascal Brand 		struct type *curelm = (head)->stqh_first;		\
295b0104773SPascal Brand 		while (curelm->field.stqe_next != (elm))			\
296b0104773SPascal Brand 			curelm = curelm->field.stqe_next;		\
297b0104773SPascal Brand 		if ((curelm->field.stqe_next =				\
298b0104773SPascal Brand 			curelm->field.stqe_next->field.stqe_next) == NULL) \
299b0104773SPascal Brand 			    (head)->stqh_last = &(curelm)->field.stqe_next; \
300b0104773SPascal Brand 	}								\
301b0104773SPascal Brand } while (/* CONSTCOND */0)
302b0104773SPascal Brand 
303b0104773SPascal Brand #define	STAILQ_FOREACH(var, head, field)				\
304b0104773SPascal Brand 	for ((var) = ((head)->stqh_first);				\
305b0104773SPascal Brand 		(var);							\
306b0104773SPascal Brand 		(var) = ((var)->field.stqe_next))
307b0104773SPascal Brand 
308b0104773SPascal Brand #define	STAILQ_CONCAT(head1, head2) do {				\
309b0104773SPascal Brand 	if (!STAILQ_EMPTY((head2))) {					\
310b0104773SPascal Brand 		*(head1)->stqh_last = (head2)->stqh_first;		\
311b0104773SPascal Brand 		(head1)->stqh_last = (head2)->stqh_last;		\
312b0104773SPascal Brand 		STAILQ_INIT((head2));					\
313b0104773SPascal Brand 	}								\
314b0104773SPascal Brand } while (/* CONSTCOND */0)
315b0104773SPascal Brand 
316b0104773SPascal Brand /*
317b0104773SPascal Brand  * Singly-linked Tail queue access methods.
318b0104773SPascal Brand  */
319b0104773SPascal Brand #define	STAILQ_EMPTY(head)	((head)->stqh_first == NULL)
320b0104773SPascal Brand #define	STAILQ_FIRST(head)	((head)->stqh_first)
321b0104773SPascal Brand #define	STAILQ_NEXT(elm, field)	((elm)->field.stqe_next)
322b0104773SPascal Brand 
323b0104773SPascal Brand /*
324b0104773SPascal Brand  * Simple queue definitions.
325b0104773SPascal Brand  */
326b0104773SPascal Brand #define	SIMPLEQ_HEAD(name, type)					\
327b0104773SPascal Brand struct name {								\
328b0104773SPascal Brand 	struct type *sqh_first;	/* first element */			\
329b0104773SPascal Brand 	struct type **sqh_last;	/* addr of last next element */		\
330b0104773SPascal Brand }
331b0104773SPascal Brand 
332b0104773SPascal Brand #define	SIMPLEQ_HEAD_INITIALIZER(head)					\
333b0104773SPascal Brand 	{ NULL, &(head).sqh_first }
334b0104773SPascal Brand 
335b0104773SPascal Brand #define	SIMPLEQ_ENTRY(type)						\
336b0104773SPascal Brand struct {								\
337b0104773SPascal Brand 	struct type *sqe_next;	/* next element */			\
338b0104773SPascal Brand }
339b0104773SPascal Brand 
340b0104773SPascal Brand /*
341b0104773SPascal Brand  * Simple queue functions.
342b0104773SPascal Brand  */
343b0104773SPascal Brand #define	SIMPLEQ_INIT(head) do {						\
344b0104773SPascal Brand 	(head)->sqh_first = NULL;					\
345b0104773SPascal Brand 	(head)->sqh_last = &(head)->sqh_first;				\
346b0104773SPascal Brand } while (/* CONSTCOND */0)
347b0104773SPascal Brand 
348b0104773SPascal Brand #define	SIMPLEQ_INSERT_HEAD(head, elm, field) do {			\
349b0104773SPascal Brand 	if (((elm)->field.sqe_next = (head)->sqh_first) == NULL)	\
350b0104773SPascal Brand 		(head)->sqh_last = &(elm)->field.sqe_next;		\
351b0104773SPascal Brand 	(head)->sqh_first = (elm);					\
352b0104773SPascal Brand } while (/* CONSTCOND */0)
353b0104773SPascal Brand 
354b0104773SPascal Brand #define	SIMPLEQ_INSERT_TAIL(head, elm, field) do {			\
355b0104773SPascal Brand 	(elm)->field.sqe_next = NULL;					\
356b0104773SPascal Brand 	*(head)->sqh_last = (elm);					\
357b0104773SPascal Brand 	(head)->sqh_last = &(elm)->field.sqe_next;			\
358b0104773SPascal Brand } while (/* CONSTCOND */0)
359b0104773SPascal Brand 
360b0104773SPascal Brand #define	SIMPLEQ_INSERT_AFTER(head, listelm, elm, field) do {		\
361b0104773SPascal Brand 	if (((elm)->field.sqe_next = (listelm)->field.sqe_next) == NULL)\
362b0104773SPascal Brand 		(head)->sqh_last = &(elm)->field.sqe_next;		\
363b0104773SPascal Brand 	(listelm)->field.sqe_next = (elm);				\
364b0104773SPascal Brand } while (/* CONSTCOND */0)
365b0104773SPascal Brand 
366b0104773SPascal Brand #define	SIMPLEQ_REMOVE_HEAD(head, field) do {				\
367b0104773SPascal Brand 	if (((head)->sqh_first = (head)->sqh_first->field.sqe_next) == NULL) \
368b0104773SPascal Brand 		(head)->sqh_last = &(head)->sqh_first;			\
369b0104773SPascal Brand } while (/* CONSTCOND */0)
370b0104773SPascal Brand 
371b0104773SPascal Brand #define	SIMPLEQ_REMOVE(head, elm, type, field) do {			\
372b0104773SPascal Brand 	if ((head)->sqh_first == (elm)) {				\
373b0104773SPascal Brand 		SIMPLEQ_REMOVE_HEAD((head), field);			\
374b0104773SPascal Brand 	} else {							\
375b0104773SPascal Brand 		struct type *curelm = (head)->sqh_first;		\
376b0104773SPascal Brand 		while (curelm->field.sqe_next != (elm))			\
377b0104773SPascal Brand 			curelm = curelm->field.sqe_next;		\
378b0104773SPascal Brand 		if ((curelm->field.sqe_next =				\
379b0104773SPascal Brand 			curelm->field.sqe_next->field.sqe_next) == NULL) \
380b0104773SPascal Brand 			    (head)->sqh_last = &(curelm)->field.sqe_next; \
381b0104773SPascal Brand 	}								\
382b0104773SPascal Brand } while (/* CONSTCOND */0)
383b0104773SPascal Brand 
384b0104773SPascal Brand #define	SIMPLEQ_FOREACH(var, head, field)				\
385b0104773SPascal Brand 	for ((var) = ((head)->sqh_first);				\
386b0104773SPascal Brand 		(var);							\
387b0104773SPascal Brand 		(var) = ((var)->field.sqe_next))
388b0104773SPascal Brand 
389b0104773SPascal Brand /*
390b0104773SPascal Brand  * Simple queue access methods.
391b0104773SPascal Brand  */
392b0104773SPascal Brand #define	SIMPLEQ_EMPTY(head)		((head)->sqh_first == NULL)
393b0104773SPascal Brand #define	SIMPLEQ_FIRST(head)		((head)->sqh_first)
394b0104773SPascal Brand #define	SIMPLEQ_NEXT(elm, field)	((elm)->field.sqe_next)
395b0104773SPascal Brand 
396b0104773SPascal Brand /*
397b0104773SPascal Brand  * Tail queue definitions.
398b0104773SPascal Brand  */
399b0104773SPascal Brand #define	_TAILQ_HEAD(name, type, qual)					\
400b0104773SPascal Brand struct name {								\
401b0104773SPascal Brand 	qual type *tqh_first;		/* first element */		\
402b0104773SPascal Brand 	qual type *qual *tqh_last;	/* addr of last next element */	\
403b0104773SPascal Brand }
404b0104773SPascal Brand #define TAILQ_HEAD(name, type)	_TAILQ_HEAD(name, struct type,)
405b0104773SPascal Brand 
406b0104773SPascal Brand #define	TAILQ_HEAD_INITIALIZER(head)					\
407b0104773SPascal Brand 	{ NULL, &(head).tqh_first }
408b0104773SPascal Brand 
409b0104773SPascal Brand #define	_TAILQ_ENTRY(type, qual)					\
410b0104773SPascal Brand struct {								\
411b0104773SPascal Brand 	qual type *tqe_next;		/* next element */		\
412b0104773SPascal Brand 	qual type *qual *tqe_prev;	/* address of previous next element */\
413b0104773SPascal Brand }
414b0104773SPascal Brand #define TAILQ_ENTRY(type)	_TAILQ_ENTRY(struct type,)
415b0104773SPascal Brand 
416b0104773SPascal Brand /*
417b0104773SPascal Brand  * Tail queue functions.
418b0104773SPascal Brand  */
419b0104773SPascal Brand #if defined(_KERNEL) && defined(QUEUEDEBUG)
420b0104773SPascal Brand #define	QUEUEDEBUG_TAILQ_INSERT_HEAD(head, elm, field)			\
421b0104773SPascal Brand 	if ((head)->tqh_first &&					\
422b0104773SPascal Brand 	    (head)->tqh_first->field.tqe_prev != &(head)->tqh_first)	\
423b0104773SPascal Brand 		panic("TAILQ_INSERT_HEAD %p %s:%d", (head), __FILE__, __LINE__);
424b0104773SPascal Brand #define	QUEUEDEBUG_TAILQ_INSERT_TAIL(head, elm, field)			\
425b0104773SPascal Brand 	if (*(head)->tqh_last != NULL)					\
426b0104773SPascal Brand 		panic("TAILQ_INSERT_TAIL %p %s:%d", (head), __FILE__, __LINE__);
427b0104773SPascal Brand #define	QUEUEDEBUG_TAILQ_OP(elm, field)					\
428b0104773SPascal Brand 	if ((elm)->field.tqe_next &&					\
429b0104773SPascal Brand 	    (elm)->field.tqe_next->field.tqe_prev !=			\
430b0104773SPascal Brand 	    &(elm)->field.tqe_next)					\
431b0104773SPascal Brand 		panic("TAILQ_* forw %p %s:%d", (elm), __FILE__, __LINE__);\
432b0104773SPascal Brand 	if (*(elm)->field.tqe_prev != (elm))				\
433b0104773SPascal Brand 		panic("TAILQ_* back %p %s:%d", (elm), __FILE__, __LINE__);
434b0104773SPascal Brand #define	QUEUEDEBUG_TAILQ_PREREMOVE(head, elm, field)			\
435b0104773SPascal Brand 	if ((elm)->field.tqe_next == NULL &&				\
436b0104773SPascal Brand 	    (head)->tqh_last != &(elm)->field.tqe_next)			\
437b0104773SPascal Brand 		panic("TAILQ_PREREMOVE head %p elm %p %s:%d",		\
438b0104773SPascal Brand 		      (head), (elm), __FILE__, __LINE__);
439b0104773SPascal Brand #define	QUEUEDEBUG_TAILQ_POSTREMOVE(elm, field)				\
440b0104773SPascal Brand 	(elm)->field.tqe_next = (void *)1L;				\
441b0104773SPascal Brand 	(elm)->field.tqe_prev = (void *)1L;
442b0104773SPascal Brand #else
443b0104773SPascal Brand #define	QUEUEDEBUG_TAILQ_INSERT_HEAD(head, elm, field)
444b0104773SPascal Brand #define	QUEUEDEBUG_TAILQ_INSERT_TAIL(head, elm, field)
445b0104773SPascal Brand #define	QUEUEDEBUG_TAILQ_OP(elm, field)
446b0104773SPascal Brand #define	QUEUEDEBUG_TAILQ_PREREMOVE(head, elm, field)
447b0104773SPascal Brand #define	QUEUEDEBUG_TAILQ_POSTREMOVE(elm, field)
448b0104773SPascal Brand #endif
449b0104773SPascal Brand 
450b0104773SPascal Brand #define	TAILQ_INIT(head) do {						\
451b0104773SPascal Brand 	(head)->tqh_first = NULL;					\
452b0104773SPascal Brand 	(head)->tqh_last = &(head)->tqh_first;				\
453b0104773SPascal Brand } while (/* CONSTCOND */0)
454b0104773SPascal Brand 
455b0104773SPascal Brand #define	TAILQ_INSERT_HEAD(head, elm, field) do {			\
456b0104773SPascal Brand 	QUEUEDEBUG_TAILQ_INSERT_HEAD((head), (elm), field)		\
457b0104773SPascal Brand 	if (((elm)->field.tqe_next = (head)->tqh_first) != NULL)	\
458b0104773SPascal Brand 		(head)->tqh_first->field.tqe_prev =			\
459b0104773SPascal Brand 		    &(elm)->field.tqe_next;				\
460b0104773SPascal Brand 	else								\
461b0104773SPascal Brand 		(head)->tqh_last = &(elm)->field.tqe_next;		\
462b0104773SPascal Brand 	(head)->tqh_first = (elm);					\
463b0104773SPascal Brand 	(elm)->field.tqe_prev = &(head)->tqh_first;			\
464b0104773SPascal Brand } while (/* CONSTCOND */0)
465b0104773SPascal Brand 
466b0104773SPascal Brand #define	TAILQ_INSERT_TAIL(head, elm, field) do {			\
467b0104773SPascal Brand 	QUEUEDEBUG_TAILQ_INSERT_TAIL((head), (elm), field)		\
468b0104773SPascal Brand 	(elm)->field.tqe_next = NULL;					\
469b0104773SPascal Brand 	(elm)->field.tqe_prev = (head)->tqh_last;			\
470b0104773SPascal Brand 	*(head)->tqh_last = (elm);					\
471b0104773SPascal Brand 	(head)->tqh_last = &(elm)->field.tqe_next;			\
472b0104773SPascal Brand } while (/* CONSTCOND */0)
473b0104773SPascal Brand 
474b0104773SPascal Brand #define	TAILQ_INSERT_AFTER(head, listelm, elm, field) do {		\
475b0104773SPascal Brand 	QUEUEDEBUG_TAILQ_OP((listelm), field)				\
476b0104773SPascal Brand 	if (((elm)->field.tqe_next = (listelm)->field.tqe_next) != NULL)\
477b0104773SPascal Brand 		(elm)->field.tqe_next->field.tqe_prev =			\
478b0104773SPascal Brand 		    &(elm)->field.tqe_next;				\
479b0104773SPascal Brand 	else								\
480b0104773SPascal Brand 		(head)->tqh_last = &(elm)->field.tqe_next;		\
481b0104773SPascal Brand 	(listelm)->field.tqe_next = (elm);				\
482b0104773SPascal Brand 	(elm)->field.tqe_prev = &(listelm)->field.tqe_next;		\
483b0104773SPascal Brand } while (/* CONSTCOND */0)
484b0104773SPascal Brand 
485b0104773SPascal Brand #define	TAILQ_INSERT_BEFORE(listelm, elm, field) do {			\
486b0104773SPascal Brand 	QUEUEDEBUG_TAILQ_OP((listelm), field)				\
487b0104773SPascal Brand 	(elm)->field.tqe_prev = (listelm)->field.tqe_prev;		\
488b0104773SPascal Brand 	(elm)->field.tqe_next = (listelm);				\
489b0104773SPascal Brand 	*(listelm)->field.tqe_prev = (elm);				\
490b0104773SPascal Brand 	(listelm)->field.tqe_prev = &(elm)->field.tqe_next;		\
491b0104773SPascal Brand } while (/* CONSTCOND */0)
492b0104773SPascal Brand 
493b0104773SPascal Brand #define	TAILQ_REMOVE(head, elm, field) do {				\
494b0104773SPascal Brand 	QUEUEDEBUG_TAILQ_PREREMOVE((head), (elm), field)		\
495b0104773SPascal Brand 	QUEUEDEBUG_TAILQ_OP((elm), field)				\
496b0104773SPascal Brand 	if (((elm)->field.tqe_next) != NULL)				\
497b0104773SPascal Brand 		(elm)->field.tqe_next->field.tqe_prev =			\
498b0104773SPascal Brand 		    (elm)->field.tqe_prev;				\
499b0104773SPascal Brand 	else								\
500b0104773SPascal Brand 		(head)->tqh_last = (elm)->field.tqe_prev;		\
501b0104773SPascal Brand 	*(elm)->field.tqe_prev = (elm)->field.tqe_next;			\
502b0104773SPascal Brand 	QUEUEDEBUG_TAILQ_POSTREMOVE((elm), field);			\
503b0104773SPascal Brand } while (/* CONSTCOND */0)
504b0104773SPascal Brand 
505b0104773SPascal Brand #define	TAILQ_FOREACH(var, head, field)					\
506b0104773SPascal Brand 	for ((var) = ((head)->tqh_first);				\
507b0104773SPascal Brand 		(var);							\
508b0104773SPascal Brand 		(var) = ((var)->field.tqe_next))
509b0104773SPascal Brand 
510b0104773SPascal Brand #define	TAILQ_FOREACH_SAFE(var, head, field, next)			\
511b0104773SPascal Brand 	for ((var) = ((head)->tqh_first);				\
512b0104773SPascal Brand 		(var) != NULL && ((next) = TAILQ_NEXT(var, field), 1);	\
513b0104773SPascal Brand 		(var) = (next))
514b0104773SPascal Brand 
515b0104773SPascal Brand #define	TAILQ_FOREACH_REVERSE(var, head, headname, field)		\
516b0104773SPascal Brand 	for ((var) = (*(((struct headname *)((head)->tqh_last))->tqh_last));	\
517b0104773SPascal Brand 		(var);							\
518b0104773SPascal Brand 		(var) = (*(((struct headname *)((var)->field.tqe_prev))->tqh_last)))
519b0104773SPascal Brand 
520b0104773SPascal Brand #define	TAILQ_FOREACH_REVERSE_SAFE(var, head, headname, field, prev)	\
521b0104773SPascal Brand 	for ((var) = TAILQ_LAST((head), headname);			\
522b0104773SPascal Brand 		(var) && ((prev) = TAILQ_PREV((var), headname, field), 1);\
523b0104773SPascal Brand 		(var) = (prev))
524b0104773SPascal Brand 
525b0104773SPascal Brand #define	TAILQ_CONCAT(head1, head2, field) do {				\
526b0104773SPascal Brand 	if (!TAILQ_EMPTY(head2)) {					\
527b0104773SPascal Brand 		*(head1)->tqh_last = (head2)->tqh_first;		\
528b0104773SPascal Brand 		(head2)->tqh_first->field.tqe_prev = (head1)->tqh_last;	\
529b0104773SPascal Brand 		(head1)->tqh_last = (head2)->tqh_last;			\
530b0104773SPascal Brand 		TAILQ_INIT((head2));					\
531b0104773SPascal Brand 	}								\
532b0104773SPascal Brand } while (/* CONSTCOND */0)
533b0104773SPascal Brand 
534b0104773SPascal Brand /*
535b0104773SPascal Brand  * Tail queue access methods.
536b0104773SPascal Brand  */
537b0104773SPascal Brand #define	TAILQ_EMPTY(head)		((head)->tqh_first == NULL)
538b0104773SPascal Brand #define	TAILQ_FIRST(head)		((head)->tqh_first)
539b0104773SPascal Brand #define	TAILQ_NEXT(elm, field)		((elm)->field.tqe_next)
540b0104773SPascal Brand 
541b0104773SPascal Brand #define	TAILQ_LAST(head, headname) \
542b0104773SPascal Brand 	(*(((struct headname *)((head)->tqh_last))->tqh_last))
543b0104773SPascal Brand #define	TAILQ_PREV(elm, headname, field) \
544b0104773SPascal Brand 	(*(((struct headname *)((elm)->field.tqe_prev))->tqh_last))
545b0104773SPascal Brand 
546b0104773SPascal Brand /*
547b0104773SPascal Brand  * Circular queue definitions.
548b0104773SPascal Brand  */
549b0104773SPascal Brand #if defined(_KERNEL) && defined(QUEUEDEBUG)
550b0104773SPascal Brand #define QUEUEDEBUG_CIRCLEQ_HEAD(head, field)				\
551b0104773SPascal Brand 	if ((head)->cqh_first != (void *)(head) &&			\
552b0104773SPascal Brand 	    (head)->cqh_first->field.cqe_prev != (void *)(head))	\
553b0104773SPascal Brand 		panic("CIRCLEQ head forw %p %s:%d", (head),		\
554b0104773SPascal Brand 		      __FILE__, __LINE__);				\
555b0104773SPascal Brand 	if ((head)->cqh_last != (void *)(head) &&			\
556b0104773SPascal Brand 	    (head)->cqh_last->field.cqe_next != (void *)(head))		\
557b0104773SPascal Brand 		panic("CIRCLEQ head back %p %s:%d", (head),		\
558b0104773SPascal Brand 		      __FILE__, __LINE__);
559b0104773SPascal Brand #define QUEUEDEBUG_CIRCLEQ_ELM(head, elm, field)			\
560b0104773SPascal Brand 	if ((elm)->field.cqe_next == (void *)(head)) {			\
561b0104773SPascal Brand 		if ((head)->cqh_last != (elm))				\
562b0104773SPascal Brand 			panic("CIRCLEQ elm last %p %s:%d", (elm),	\
563b0104773SPascal Brand 			      __FILE__, __LINE__);			\
564b0104773SPascal Brand 	} else {							\
565b0104773SPascal Brand 		if ((elm)->field.cqe_next->field.cqe_prev != (elm))	\
566b0104773SPascal Brand 			panic("CIRCLEQ elm forw %p %s:%d", (elm),	\
567b0104773SPascal Brand 			      __FILE__, __LINE__);			\
568b0104773SPascal Brand 	}								\
569b0104773SPascal Brand 	if ((elm)->field.cqe_prev == (void *)(head)) {			\
570b0104773SPascal Brand 		if ((head)->cqh_first != (elm))				\
571b0104773SPascal Brand 			panic("CIRCLEQ elm first %p %s:%d", (elm),	\
572b0104773SPascal Brand 			      __FILE__, __LINE__);			\
573b0104773SPascal Brand 	} else {							\
574b0104773SPascal Brand 		if ((elm)->field.cqe_prev->field.cqe_next != (elm))	\
575b0104773SPascal Brand 			panic("CIRCLEQ elm prev %p %s:%d", (elm),	\
576b0104773SPascal Brand 			      __FILE__, __LINE__);			\
577b0104773SPascal Brand 	}
578b0104773SPascal Brand #define QUEUEDEBUG_CIRCLEQ_POSTREMOVE(elm, field)			\
579b0104773SPascal Brand 	(elm)->field.cqe_next = (void *)1L;				\
580b0104773SPascal Brand 	(elm)->field.cqe_prev = (void *)1L;
581b0104773SPascal Brand #else
582b0104773SPascal Brand #define QUEUEDEBUG_CIRCLEQ_HEAD(head, field)
583b0104773SPascal Brand #define QUEUEDEBUG_CIRCLEQ_ELM(head, elm, field)
584b0104773SPascal Brand #define QUEUEDEBUG_CIRCLEQ_POSTREMOVE(elm, field)
585b0104773SPascal Brand #endif
586b0104773SPascal Brand 
587b0104773SPascal Brand #define	CIRCLEQ_HEAD(name, type)					\
588b0104773SPascal Brand struct name {								\
589b0104773SPascal Brand 	struct type *cqh_first;		/* first element */		\
590b0104773SPascal Brand 	struct type *cqh_last;		/* last element */		\
591b0104773SPascal Brand }
592b0104773SPascal Brand 
593b0104773SPascal Brand #define	CIRCLEQ_HEAD_INITIALIZER(head)					\
594b0104773SPascal Brand 	{ (void *)&head, (void *)&head }
595b0104773SPascal Brand 
596b0104773SPascal Brand #define	CIRCLEQ_ENTRY(type)						\
597b0104773SPascal Brand struct {								\
598b0104773SPascal Brand 	struct type *cqe_next;		/* next element */		\
599b0104773SPascal Brand 	struct type *cqe_prev;		/* previous element */		\
600b0104773SPascal Brand }
601b0104773SPascal Brand 
602b0104773SPascal Brand /*
603b0104773SPascal Brand  * Circular queue functions.
604b0104773SPascal Brand  */
605b0104773SPascal Brand #define	CIRCLEQ_INIT(head) do {						\
606b0104773SPascal Brand 	(head)->cqh_first = (void *)(head);				\
607b0104773SPascal Brand 	(head)->cqh_last = (void *)(head);				\
608b0104773SPascal Brand } while (/* CONSTCOND */0)
609b0104773SPascal Brand 
610b0104773SPascal Brand #define	CIRCLEQ_INSERT_AFTER(head, listelm, elm, field) do {		\
611b0104773SPascal Brand 	QUEUEDEBUG_CIRCLEQ_HEAD((head), field)				\
612b0104773SPascal Brand 	QUEUEDEBUG_CIRCLEQ_ELM((head), (listelm), field)		\
613b0104773SPascal Brand 	(elm)->field.cqe_next = (listelm)->field.cqe_next;		\
614b0104773SPascal Brand 	(elm)->field.cqe_prev = (listelm);				\
615b0104773SPascal Brand 	if ((listelm)->field.cqe_next == (void *)(head))		\
616b0104773SPascal Brand 		(head)->cqh_last = (elm);				\
617b0104773SPascal Brand 	else								\
618b0104773SPascal Brand 		(listelm)->field.cqe_next->field.cqe_prev = (elm);	\
619b0104773SPascal Brand 	(listelm)->field.cqe_next = (elm);				\
620b0104773SPascal Brand } while (/* CONSTCOND */0)
621b0104773SPascal Brand 
622b0104773SPascal Brand #define	CIRCLEQ_INSERT_BEFORE(head, listelm, elm, field) do {		\
623b0104773SPascal Brand 	QUEUEDEBUG_CIRCLEQ_HEAD((head), field)				\
624b0104773SPascal Brand 	QUEUEDEBUG_CIRCLEQ_ELM((head), (listelm), field)		\
625b0104773SPascal Brand 	(elm)->field.cqe_next = (listelm);				\
626b0104773SPascal Brand 	(elm)->field.cqe_prev = (listelm)->field.cqe_prev;		\
627b0104773SPascal Brand 	if ((listelm)->field.cqe_prev == (void *)(head))		\
628b0104773SPascal Brand 		(head)->cqh_first = (elm);				\
629b0104773SPascal Brand 	else								\
630b0104773SPascal Brand 		(listelm)->field.cqe_prev->field.cqe_next = (elm);	\
631b0104773SPascal Brand 	(listelm)->field.cqe_prev = (elm);				\
632b0104773SPascal Brand } while (/* CONSTCOND */0)
633b0104773SPascal Brand 
634b0104773SPascal Brand #define	CIRCLEQ_INSERT_HEAD(head, elm, field) do {			\
635b0104773SPascal Brand 	QUEUEDEBUG_CIRCLEQ_HEAD((head), field)				\
636b0104773SPascal Brand 	(elm)->field.cqe_next = (head)->cqh_first;			\
637b0104773SPascal Brand 	(elm)->field.cqe_prev = (void *)(head);				\
638b0104773SPascal Brand 	if ((head)->cqh_last == (void *)(head))				\
639b0104773SPascal Brand 		(head)->cqh_last = (elm);				\
640b0104773SPascal Brand 	else								\
641b0104773SPascal Brand 		(head)->cqh_first->field.cqe_prev = (elm);		\
642b0104773SPascal Brand 	(head)->cqh_first = (elm);					\
643b0104773SPascal Brand } while (/* CONSTCOND */0)
644b0104773SPascal Brand 
645b0104773SPascal Brand #define	CIRCLEQ_INSERT_TAIL(head, elm, field) do {			\
646b0104773SPascal Brand 	QUEUEDEBUG_CIRCLEQ_HEAD((head), field)				\
647b0104773SPascal Brand 	(elm)->field.cqe_next = (void *)(head);				\
648b0104773SPascal Brand 	(elm)->field.cqe_prev = (head)->cqh_last;			\
649b0104773SPascal Brand 	if ((head)->cqh_first == (void *)(head))			\
650b0104773SPascal Brand 		(head)->cqh_first = (elm);				\
651b0104773SPascal Brand 	else								\
652b0104773SPascal Brand 		(head)->cqh_last->field.cqe_next = (elm);		\
653b0104773SPascal Brand 	(head)->cqh_last = (elm);					\
654b0104773SPascal Brand } while (/* CONSTCOND */0)
655b0104773SPascal Brand 
656b0104773SPascal Brand #define	CIRCLEQ_REMOVE(head, elm, field) do {				\
657b0104773SPascal Brand 	QUEUEDEBUG_CIRCLEQ_HEAD((head), field)				\
658b0104773SPascal Brand 	QUEUEDEBUG_CIRCLEQ_ELM((head), (elm), field)			\
659b0104773SPascal Brand 	if ((elm)->field.cqe_next == (void *)(head))			\
660b0104773SPascal Brand 		(head)->cqh_last = (elm)->field.cqe_prev;		\
661b0104773SPascal Brand 	else								\
662b0104773SPascal Brand 		(elm)->field.cqe_next->field.cqe_prev =			\
663b0104773SPascal Brand 		    (elm)->field.cqe_prev;				\
664b0104773SPascal Brand 	if ((elm)->field.cqe_prev == (void *)(head))			\
665b0104773SPascal Brand 		(head)->cqh_first = (elm)->field.cqe_next;		\
666b0104773SPascal Brand 	else								\
667b0104773SPascal Brand 		(elm)->field.cqe_prev->field.cqe_next =			\
668b0104773SPascal Brand 		    (elm)->field.cqe_next;				\
669b0104773SPascal Brand 	QUEUEDEBUG_CIRCLEQ_POSTREMOVE((elm), field)			\
670b0104773SPascal Brand } while (/* CONSTCOND */0)
671b0104773SPascal Brand 
672b0104773SPascal Brand #define	CIRCLEQ_FOREACH(var, head, field)				\
673b0104773SPascal Brand 	for ((var) = ((head)->cqh_first);				\
674b0104773SPascal Brand 		(var) != (const void *)(head);				\
675b0104773SPascal Brand 		(var) = ((var)->field.cqe_next))
676b0104773SPascal Brand 
677b0104773SPascal Brand #define	CIRCLEQ_FOREACH_REVERSE(var, head, field)			\
678b0104773SPascal Brand 	for ((var) = ((head)->cqh_last);				\
679b0104773SPascal Brand 		(var) != (const void *)(head);				\
680b0104773SPascal Brand 		(var) = ((var)->field.cqe_prev))
681b0104773SPascal Brand 
682b0104773SPascal Brand /*
683b0104773SPascal Brand  * Circular queue access methods.
684b0104773SPascal Brand  */
685b0104773SPascal Brand #define	CIRCLEQ_EMPTY(head)		((head)->cqh_first == (void *)(head))
686b0104773SPascal Brand #define	CIRCLEQ_FIRST(head)		((head)->cqh_first)
687b0104773SPascal Brand #define	CIRCLEQ_LAST(head)		((head)->cqh_last)
688b0104773SPascal Brand #define	CIRCLEQ_NEXT(elm, field)	((elm)->field.cqe_next)
689b0104773SPascal Brand #define	CIRCLEQ_PREV(elm, field)	((elm)->field.cqe_prev)
690b0104773SPascal Brand 
691b0104773SPascal Brand #define CIRCLEQ_LOOP_NEXT(head, elm, field)				\
692b0104773SPascal Brand 	(((elm)->field.cqe_next == (void *)(head))			\
693b0104773SPascal Brand 	    ? ((head)->cqh_first)					\
694b0104773SPascal Brand 	    : (elm->field.cqe_next))
695b0104773SPascal Brand #define CIRCLEQ_LOOP_PREV(head, elm, field)				\
696b0104773SPascal Brand 	(((elm)->field.cqe_prev == (void *)(head))			\
697b0104773SPascal Brand 	    ? ((head)->cqh_last)					\
698b0104773SPascal Brand 	    : (elm->field.cqe_prev))
699b0104773SPascal Brand 
700b0104773SPascal Brand #endif /* !_SYS_QUEUE_H_ */
701