1*2b15cb3dSCy Schubert /*	$OpenBSD: queue.h,v 1.16 2000/09/07 19:47:59 art Exp $	*/
2*2b15cb3dSCy Schubert /*	$NetBSD: queue.h,v 1.11 1996/05/16 05:17:14 mycroft Exp $	*/
3*2b15cb3dSCy Schubert 
4*2b15cb3dSCy Schubert /*
5*2b15cb3dSCy Schubert  * Copyright (c) 1991, 1993
6*2b15cb3dSCy Schubert  *	The Regents of the University of California.  All rights reserved.
7*2b15cb3dSCy Schubert  *
8*2b15cb3dSCy Schubert  * Redistribution and use in source and binary forms, with or without
9*2b15cb3dSCy Schubert  * modification, are permitted provided that the following conditions
10*2b15cb3dSCy Schubert  * are met:
11*2b15cb3dSCy Schubert  * 1. Redistributions of source code must retain the above copyright
12*2b15cb3dSCy Schubert  *    notice, this list of conditions and the following disclaimer.
13*2b15cb3dSCy Schubert  * 2. Redistributions in binary form must reproduce the above copyright
14*2b15cb3dSCy Schubert  *    notice, this list of conditions and the following disclaimer in the
15*2b15cb3dSCy Schubert  *    documentation and/or other materials provided with the distribution.
16*2b15cb3dSCy Schubert  * 3. Neither the name of the University nor the names of its contributors
17*2b15cb3dSCy Schubert  *    may be used to endorse or promote products derived from this software
18*2b15cb3dSCy Schubert  *    without specific prior written permission.
19*2b15cb3dSCy Schubert  *
20*2b15cb3dSCy Schubert  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21*2b15cb3dSCy Schubert  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22*2b15cb3dSCy Schubert  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23*2b15cb3dSCy Schubert  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24*2b15cb3dSCy Schubert  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25*2b15cb3dSCy Schubert  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26*2b15cb3dSCy Schubert  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27*2b15cb3dSCy Schubert  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28*2b15cb3dSCy Schubert  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29*2b15cb3dSCy Schubert  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30*2b15cb3dSCy Schubert  * SUCH DAMAGE.
31*2b15cb3dSCy Schubert  *
32*2b15cb3dSCy Schubert  *	@(#)queue.h	8.5 (Berkeley) 8/20/94
33*2b15cb3dSCy Schubert  */
34*2b15cb3dSCy Schubert 
35*2b15cb3dSCy Schubert #ifndef	SYS_QUEUE_H__
36*2b15cb3dSCy Schubert #define	SYS_QUEUE_H__
37*2b15cb3dSCy Schubert 
38*2b15cb3dSCy Schubert /*
39*2b15cb3dSCy Schubert  * This file defines five types of data structures: singly-linked lists,
40*2b15cb3dSCy Schubert  * lists, simple queues, tail queues, and circular queues.
41*2b15cb3dSCy Schubert  *
42*2b15cb3dSCy Schubert  *
43*2b15cb3dSCy Schubert  * A singly-linked list is headed by a single forward pointer. The elements
44*2b15cb3dSCy Schubert  * are singly linked for minimum space and pointer manipulation overhead at
45*2b15cb3dSCy Schubert  * the expense of O(n) removal for arbitrary elements. New elements can be
46*2b15cb3dSCy Schubert  * added to the list after an existing element or at the head of the list.
47*2b15cb3dSCy Schubert  * Elements being removed from the head of the list should use the explicit
48*2b15cb3dSCy Schubert  * macro for this purpose for optimum efficiency. A singly-linked list may
49*2b15cb3dSCy Schubert  * only be traversed in the forward direction.  Singly-linked lists are ideal
50*2b15cb3dSCy Schubert  * for applications with large datasets and few or no removals or for
51*2b15cb3dSCy Schubert  * implementing a LIFO queue.
52*2b15cb3dSCy Schubert  *
53*2b15cb3dSCy Schubert  * A list is headed by a single forward pointer (or an array of forward
54*2b15cb3dSCy Schubert  * pointers for a hash table header). The elements are doubly linked
55*2b15cb3dSCy Schubert  * so that an arbitrary element can be removed without a need to
56*2b15cb3dSCy Schubert  * traverse the list. New elements can be added to the list before
57*2b15cb3dSCy Schubert  * or after an existing element or at the head of the list. A list
58*2b15cb3dSCy Schubert  * may only be traversed in the forward direction.
59*2b15cb3dSCy Schubert  *
60*2b15cb3dSCy Schubert  * A simple queue is headed by a pair of pointers, one the head of the
61*2b15cb3dSCy Schubert  * list and the other to the tail of the list. The elements are singly
62*2b15cb3dSCy Schubert  * linked to save space, so elements can only be removed from the
63*2b15cb3dSCy Schubert  * head of the list. New elements can be added to the list before or after
64*2b15cb3dSCy Schubert  * an existing element, at the head of the list, or at the end of the
65*2b15cb3dSCy Schubert  * list. A simple queue may only be traversed in the forward direction.
66*2b15cb3dSCy Schubert  *
67*2b15cb3dSCy Schubert  * A tail queue is headed by a pair of pointers, one to the head of the
68*2b15cb3dSCy Schubert  * list and the other to the tail of the list. The elements are doubly
69*2b15cb3dSCy Schubert  * linked so that an arbitrary element can be removed without a need to
70*2b15cb3dSCy Schubert  * traverse the list. New elements can be added to the list before or
71*2b15cb3dSCy Schubert  * after an existing element, at the head of the list, or at the end of
72*2b15cb3dSCy Schubert  * the list. A tail queue may be traversed in either direction.
73*2b15cb3dSCy Schubert  *
74*2b15cb3dSCy Schubert  * A circle queue is headed by a pair of pointers, one to the head of the
75*2b15cb3dSCy Schubert  * list and the other to the tail of the list. The elements are doubly
76*2b15cb3dSCy Schubert  * linked so that an arbitrary element can be removed without a need to
77*2b15cb3dSCy Schubert  * traverse the list. New elements can be added to the list before or after
78*2b15cb3dSCy Schubert  * an existing element, at the head of the list, or at the end of the list.
79*2b15cb3dSCy Schubert  * A circle queue may be traversed in either direction, but has a more
80*2b15cb3dSCy Schubert  * complex end of list detection.
81*2b15cb3dSCy Schubert  *
82*2b15cb3dSCy Schubert  * For details on the use of these macros, see the queue(3) manual page.
83*2b15cb3dSCy Schubert  */
84*2b15cb3dSCy Schubert 
85*2b15cb3dSCy Schubert /*
86*2b15cb3dSCy Schubert  * Singly-linked List definitions.
87*2b15cb3dSCy Schubert  */
88*2b15cb3dSCy Schubert #define SLIST_HEAD(name, type)						\
89*2b15cb3dSCy Schubert struct name {								\
90*2b15cb3dSCy Schubert 	struct type *slh_first;	/* first element */			\
91*2b15cb3dSCy Schubert }
92*2b15cb3dSCy Schubert 
93*2b15cb3dSCy Schubert #define	SLIST_HEAD_INITIALIZER(head)					\
94*2b15cb3dSCy Schubert 	{ NULL }
95*2b15cb3dSCy Schubert 
96*2b15cb3dSCy Schubert #ifndef _WIN32
97*2b15cb3dSCy Schubert #define SLIST_ENTRY(type)						\
98*2b15cb3dSCy Schubert struct {								\
99*2b15cb3dSCy Schubert 	struct type *sle_next;	/* next element */			\
100*2b15cb3dSCy Schubert }
101*2b15cb3dSCy Schubert #endif
102*2b15cb3dSCy Schubert 
103*2b15cb3dSCy Schubert /*
104*2b15cb3dSCy Schubert  * Singly-linked List access methods.
105*2b15cb3dSCy Schubert  */
106*2b15cb3dSCy Schubert #define	SLIST_FIRST(head)	((head)->slh_first)
107*2b15cb3dSCy Schubert #define	SLIST_END(head)		NULL
108*2b15cb3dSCy Schubert #define	SLIST_EMPTY(head)	(SLIST_FIRST(head) == SLIST_END(head))
109*2b15cb3dSCy Schubert #define	SLIST_NEXT(elm, field)	((elm)->field.sle_next)
110*2b15cb3dSCy Schubert 
111*2b15cb3dSCy Schubert #define	SLIST_FOREACH(var, head, field)					\
112*2b15cb3dSCy Schubert 	for((var) = SLIST_FIRST(head);					\
113*2b15cb3dSCy Schubert 	    (var) != SLIST_END(head);					\
114*2b15cb3dSCy Schubert 	    (var) = SLIST_NEXT(var, field))
115*2b15cb3dSCy Schubert 
116*2b15cb3dSCy Schubert /*
117*2b15cb3dSCy Schubert  * Singly-linked List functions.
118*2b15cb3dSCy Schubert  */
119*2b15cb3dSCy Schubert #define	SLIST_INIT(head) {						\
120*2b15cb3dSCy Schubert 	SLIST_FIRST(head) = SLIST_END(head);				\
121*2b15cb3dSCy Schubert }
122*2b15cb3dSCy Schubert 
123*2b15cb3dSCy Schubert #define	SLIST_INSERT_AFTER(slistelm, elm, field) do {			\
124*2b15cb3dSCy Schubert 	(elm)->field.sle_next = (slistelm)->field.sle_next;		\
125*2b15cb3dSCy Schubert 	(slistelm)->field.sle_next = (elm);				\
126*2b15cb3dSCy Schubert } while (0)
127*2b15cb3dSCy Schubert 
128*2b15cb3dSCy Schubert #define	SLIST_INSERT_HEAD(head, elm, field) do {			\
129*2b15cb3dSCy Schubert 	(elm)->field.sle_next = (head)->slh_first;			\
130*2b15cb3dSCy Schubert 	(head)->slh_first = (elm);					\
131*2b15cb3dSCy Schubert } while (0)
132*2b15cb3dSCy Schubert 
133*2b15cb3dSCy Schubert #define	SLIST_REMOVE_HEAD(head, field) do {				\
134*2b15cb3dSCy Schubert 	(head)->slh_first = (head)->slh_first->field.sle_next;		\
135*2b15cb3dSCy Schubert } while (0)
136*2b15cb3dSCy Schubert 
137*2b15cb3dSCy Schubert /*
138*2b15cb3dSCy Schubert  * List definitions.
139*2b15cb3dSCy Schubert  */
140*2b15cb3dSCy Schubert #define LIST_HEAD(name, type)						\
141*2b15cb3dSCy Schubert struct name {								\
142*2b15cb3dSCy Schubert 	struct type *lh_first;	/* first element */			\
143*2b15cb3dSCy Schubert }
144*2b15cb3dSCy Schubert 
145*2b15cb3dSCy Schubert #define LIST_HEAD_INITIALIZER(head)					\
146*2b15cb3dSCy Schubert 	{ NULL }
147*2b15cb3dSCy Schubert 
148*2b15cb3dSCy Schubert #define LIST_ENTRY(type)						\
149*2b15cb3dSCy Schubert struct {								\
150*2b15cb3dSCy Schubert 	struct type *le_next;	/* next element */			\
151*2b15cb3dSCy Schubert 	struct type **le_prev;	/* address of previous next element */	\
152*2b15cb3dSCy Schubert }
153*2b15cb3dSCy Schubert 
154*2b15cb3dSCy Schubert /*
155*2b15cb3dSCy Schubert  * List access methods
156*2b15cb3dSCy Schubert  */
157*2b15cb3dSCy Schubert #define	LIST_FIRST(head)		((head)->lh_first)
158*2b15cb3dSCy Schubert #define	LIST_END(head)			NULL
159*2b15cb3dSCy Schubert #define	LIST_EMPTY(head)		(LIST_FIRST(head) == LIST_END(head))
160*2b15cb3dSCy Schubert #define	LIST_NEXT(elm, field)		((elm)->field.le_next)
161*2b15cb3dSCy Schubert 
162*2b15cb3dSCy Schubert #define LIST_FOREACH(var, head, field)					\
163*2b15cb3dSCy Schubert 	for((var) = LIST_FIRST(head);					\
164*2b15cb3dSCy Schubert 	    (var)!= LIST_END(head);					\
165*2b15cb3dSCy Schubert 	    (var) = LIST_NEXT(var, field))
166*2b15cb3dSCy Schubert 
167*2b15cb3dSCy Schubert /*
168*2b15cb3dSCy Schubert  * List functions.
169*2b15cb3dSCy Schubert  */
170*2b15cb3dSCy Schubert #define	LIST_INIT(head) do {						\
171*2b15cb3dSCy Schubert 	LIST_FIRST(head) = LIST_END(head);				\
172*2b15cb3dSCy Schubert } while (0)
173*2b15cb3dSCy Schubert 
174*2b15cb3dSCy Schubert #define LIST_INSERT_AFTER(listelm, elm, field) do {			\
175*2b15cb3dSCy Schubert 	if (((elm)->field.le_next = (listelm)->field.le_next) != NULL)	\
176*2b15cb3dSCy Schubert 		(listelm)->field.le_next->field.le_prev =		\
177*2b15cb3dSCy Schubert 		    &(elm)->field.le_next;				\
178*2b15cb3dSCy Schubert 	(listelm)->field.le_next = (elm);				\
179*2b15cb3dSCy Schubert 	(elm)->field.le_prev = &(listelm)->field.le_next;		\
180*2b15cb3dSCy Schubert } while (0)
181*2b15cb3dSCy Schubert 
182*2b15cb3dSCy Schubert #define	LIST_INSERT_BEFORE(listelm, elm, field) do {			\
183*2b15cb3dSCy Schubert 	(elm)->field.le_prev = (listelm)->field.le_prev;		\
184*2b15cb3dSCy Schubert 	(elm)->field.le_next = (listelm);				\
185*2b15cb3dSCy Schubert 	*(listelm)->field.le_prev = (elm);				\
186*2b15cb3dSCy Schubert 	(listelm)->field.le_prev = &(elm)->field.le_next;		\
187*2b15cb3dSCy Schubert } while (0)
188*2b15cb3dSCy Schubert 
189*2b15cb3dSCy Schubert #define LIST_INSERT_HEAD(head, elm, field) do {				\
190*2b15cb3dSCy Schubert 	if (((elm)->field.le_next = (head)->lh_first) != NULL)		\
191*2b15cb3dSCy Schubert 		(head)->lh_first->field.le_prev = &(elm)->field.le_next;\
192*2b15cb3dSCy Schubert 	(head)->lh_first = (elm);					\
193*2b15cb3dSCy Schubert 	(elm)->field.le_prev = &(head)->lh_first;			\
194*2b15cb3dSCy Schubert } while (0)
195*2b15cb3dSCy Schubert 
196*2b15cb3dSCy Schubert #define LIST_REMOVE(elm, field) do {					\
197*2b15cb3dSCy Schubert 	if ((elm)->field.le_next != NULL)				\
198*2b15cb3dSCy Schubert 		(elm)->field.le_next->field.le_prev =			\
199*2b15cb3dSCy Schubert 		    (elm)->field.le_prev;				\
200*2b15cb3dSCy Schubert 	*(elm)->field.le_prev = (elm)->field.le_next;			\
201*2b15cb3dSCy Schubert } while (0)
202*2b15cb3dSCy Schubert 
203*2b15cb3dSCy Schubert #define LIST_REPLACE(elm, elm2, field) do {				\
204*2b15cb3dSCy Schubert 	if (((elm2)->field.le_next = (elm)->field.le_next) != NULL)	\
205*2b15cb3dSCy Schubert 		(elm2)->field.le_next->field.le_prev =			\
206*2b15cb3dSCy Schubert 		    &(elm2)->field.le_next;				\
207*2b15cb3dSCy Schubert 	(elm2)->field.le_prev = (elm)->field.le_prev;			\
208*2b15cb3dSCy Schubert 	*(elm2)->field.le_prev = (elm2);				\
209*2b15cb3dSCy Schubert } while (0)
210*2b15cb3dSCy Schubert 
211*2b15cb3dSCy Schubert /*
212*2b15cb3dSCy Schubert  * Simple queue definitions.
213*2b15cb3dSCy Schubert  */
214*2b15cb3dSCy Schubert #define SIMPLEQ_HEAD(name, type)					\
215*2b15cb3dSCy Schubert struct name {								\
216*2b15cb3dSCy Schubert 	struct type *sqh_first;	/* first element */			\
217*2b15cb3dSCy Schubert 	struct type **sqh_last;	/* addr of last next element */		\
218*2b15cb3dSCy Schubert }
219*2b15cb3dSCy Schubert 
220*2b15cb3dSCy Schubert #define SIMPLEQ_HEAD_INITIALIZER(head)					\
221*2b15cb3dSCy Schubert 	{ NULL, &(head).sqh_first }
222*2b15cb3dSCy Schubert 
223*2b15cb3dSCy Schubert #define SIMPLEQ_ENTRY(type)						\
224*2b15cb3dSCy Schubert struct {								\
225*2b15cb3dSCy Schubert 	struct type *sqe_next;	/* next element */			\
226*2b15cb3dSCy Schubert }
227*2b15cb3dSCy Schubert 
228*2b15cb3dSCy Schubert /*
229*2b15cb3dSCy Schubert  * Simple queue access methods.
230*2b15cb3dSCy Schubert  */
231*2b15cb3dSCy Schubert #define	SIMPLEQ_FIRST(head)	    ((head)->sqh_first)
232*2b15cb3dSCy Schubert #define	SIMPLEQ_END(head)	    NULL
233*2b15cb3dSCy Schubert #define	SIMPLEQ_EMPTY(head)	    (SIMPLEQ_FIRST(head) == SIMPLEQ_END(head))
234*2b15cb3dSCy Schubert #define	SIMPLEQ_NEXT(elm, field)    ((elm)->field.sqe_next)
235*2b15cb3dSCy Schubert 
236*2b15cb3dSCy Schubert #define SIMPLEQ_FOREACH(var, head, field)				\
237*2b15cb3dSCy Schubert 	for((var) = SIMPLEQ_FIRST(head);				\
238*2b15cb3dSCy Schubert 	    (var) != SIMPLEQ_END(head);					\
239*2b15cb3dSCy Schubert 	    (var) = SIMPLEQ_NEXT(var, field))
240*2b15cb3dSCy Schubert 
241*2b15cb3dSCy Schubert /*
242*2b15cb3dSCy Schubert  * Simple queue functions.
243*2b15cb3dSCy Schubert  */
244*2b15cb3dSCy Schubert #define	SIMPLEQ_INIT(head) do {						\
245*2b15cb3dSCy Schubert 	(head)->sqh_first = NULL;					\
246*2b15cb3dSCy Schubert 	(head)->sqh_last = &(head)->sqh_first;				\
247*2b15cb3dSCy Schubert } while (0)
248*2b15cb3dSCy Schubert 
249*2b15cb3dSCy Schubert #define SIMPLEQ_INSERT_HEAD(head, elm, field) do {			\
250*2b15cb3dSCy Schubert 	if (((elm)->field.sqe_next = (head)->sqh_first) == NULL)	\
251*2b15cb3dSCy Schubert 		(head)->sqh_last = &(elm)->field.sqe_next;		\
252*2b15cb3dSCy Schubert 	(head)->sqh_first = (elm);					\
253*2b15cb3dSCy Schubert } while (0)
254*2b15cb3dSCy Schubert 
255*2b15cb3dSCy Schubert #define SIMPLEQ_INSERT_TAIL(head, elm, field) do {			\
256*2b15cb3dSCy Schubert 	(elm)->field.sqe_next = NULL;					\
257*2b15cb3dSCy Schubert 	*(head)->sqh_last = (elm);					\
258*2b15cb3dSCy Schubert 	(head)->sqh_last = &(elm)->field.sqe_next;			\
259*2b15cb3dSCy Schubert } while (0)
260*2b15cb3dSCy Schubert 
261*2b15cb3dSCy Schubert #define SIMPLEQ_INSERT_AFTER(head, listelm, elm, field) do {		\
262*2b15cb3dSCy Schubert 	if (((elm)->field.sqe_next = (listelm)->field.sqe_next) == NULL)\
263*2b15cb3dSCy Schubert 		(head)->sqh_last = &(elm)->field.sqe_next;		\
264*2b15cb3dSCy Schubert 	(listelm)->field.sqe_next = (elm);				\
265*2b15cb3dSCy Schubert } while (0)
266*2b15cb3dSCy Schubert 
267*2b15cb3dSCy Schubert #define SIMPLEQ_REMOVE_HEAD(head, elm, field) do {			\
268*2b15cb3dSCy Schubert 	if (((head)->sqh_first = (elm)->field.sqe_next) == NULL)	\
269*2b15cb3dSCy Schubert 		(head)->sqh_last = &(head)->sqh_first;			\
270*2b15cb3dSCy Schubert } while (0)
271*2b15cb3dSCy Schubert 
272*2b15cb3dSCy Schubert /*
273*2b15cb3dSCy Schubert  * Tail queue definitions.
274*2b15cb3dSCy Schubert  */
275*2b15cb3dSCy Schubert #define TAILQ_HEAD(name, type)						\
276*2b15cb3dSCy Schubert struct name {								\
277*2b15cb3dSCy Schubert 	struct type *tqh_first;	/* first element */			\
278*2b15cb3dSCy Schubert 	struct type **tqh_last;	/* addr of last next element */		\
279*2b15cb3dSCy Schubert }
280*2b15cb3dSCy Schubert 
281*2b15cb3dSCy Schubert #define TAILQ_HEAD_INITIALIZER(head)					\
282*2b15cb3dSCy Schubert 	{ NULL, &(head).tqh_first }
283*2b15cb3dSCy Schubert 
284*2b15cb3dSCy Schubert #define TAILQ_ENTRY(type)						\
285*2b15cb3dSCy Schubert struct {								\
286*2b15cb3dSCy Schubert 	struct type *tqe_next;	/* next element */			\
287*2b15cb3dSCy Schubert 	struct type **tqe_prev;	/* address of previous next element */	\
288*2b15cb3dSCy Schubert }
289*2b15cb3dSCy Schubert 
290*2b15cb3dSCy Schubert /*
291*2b15cb3dSCy Schubert  * tail queue access methods
292*2b15cb3dSCy Schubert  */
293*2b15cb3dSCy Schubert #define	TAILQ_FIRST(head)		((head)->tqh_first)
294*2b15cb3dSCy Schubert #define	TAILQ_END(head)			NULL
295*2b15cb3dSCy Schubert #define	TAILQ_NEXT(elm, field)		((elm)->field.tqe_next)
296*2b15cb3dSCy Schubert #define TAILQ_LAST(head, headname)					\
297*2b15cb3dSCy Schubert 	(*(((struct headname *)((head)->tqh_last))->tqh_last))
298*2b15cb3dSCy Schubert /* XXX */
299*2b15cb3dSCy Schubert #define TAILQ_PREV(elm, headname, field)				\
300*2b15cb3dSCy Schubert 	(*(((struct headname *)((elm)->field.tqe_prev))->tqh_last))
301*2b15cb3dSCy Schubert #define	TAILQ_EMPTY(head)						\
302*2b15cb3dSCy Schubert 	(TAILQ_FIRST(head) == TAILQ_END(head))
303*2b15cb3dSCy Schubert 
304*2b15cb3dSCy Schubert #define TAILQ_FOREACH(var, head, field)					\
305*2b15cb3dSCy Schubert 	for((var) = TAILQ_FIRST(head);					\
306*2b15cb3dSCy Schubert 	    (var) != TAILQ_END(head);					\
307*2b15cb3dSCy Schubert 	    (var) = TAILQ_NEXT(var, field))
308*2b15cb3dSCy Schubert 
309*2b15cb3dSCy Schubert #define TAILQ_FOREACH_REVERSE(var, head, headname, field)		\
310*2b15cb3dSCy Schubert 	for((var) = TAILQ_LAST(head, headname);				\
311*2b15cb3dSCy Schubert 	    (var) != TAILQ_END(head);					\
312*2b15cb3dSCy Schubert 	    (var) = TAILQ_PREV(var, headname, field))
313*2b15cb3dSCy Schubert 
314*2b15cb3dSCy Schubert /*
315*2b15cb3dSCy Schubert  * Tail queue functions.
316*2b15cb3dSCy Schubert  */
317*2b15cb3dSCy Schubert #define	TAILQ_INIT(head) do {						\
318*2b15cb3dSCy Schubert 	(head)->tqh_first = NULL;					\
319*2b15cb3dSCy Schubert 	(head)->tqh_last = &(head)->tqh_first;				\
320*2b15cb3dSCy Schubert } while (0)
321*2b15cb3dSCy Schubert 
322*2b15cb3dSCy Schubert #define TAILQ_INSERT_HEAD(head, elm, field) do {			\
323*2b15cb3dSCy Schubert 	if (((elm)->field.tqe_next = (head)->tqh_first) != NULL)	\
324*2b15cb3dSCy Schubert 		(head)->tqh_first->field.tqe_prev =			\
325*2b15cb3dSCy Schubert 		    &(elm)->field.tqe_next;				\
326*2b15cb3dSCy Schubert 	else								\
327*2b15cb3dSCy Schubert 		(head)->tqh_last = &(elm)->field.tqe_next;		\
328*2b15cb3dSCy Schubert 	(head)->tqh_first = (elm);					\
329*2b15cb3dSCy Schubert 	(elm)->field.tqe_prev = &(head)->tqh_first;			\
330*2b15cb3dSCy Schubert } while (0)
331*2b15cb3dSCy Schubert 
332*2b15cb3dSCy Schubert #define TAILQ_INSERT_TAIL(head, elm, field) do {			\
333*2b15cb3dSCy Schubert 	(elm)->field.tqe_next = NULL;					\
334*2b15cb3dSCy Schubert 	(elm)->field.tqe_prev = (head)->tqh_last;			\
335*2b15cb3dSCy Schubert 	*(head)->tqh_last = (elm);					\
336*2b15cb3dSCy Schubert 	(head)->tqh_last = &(elm)->field.tqe_next;			\
337*2b15cb3dSCy Schubert } while (0)
338*2b15cb3dSCy Schubert 
339*2b15cb3dSCy Schubert #define TAILQ_INSERT_AFTER(head, listelm, elm, field) do {		\
340*2b15cb3dSCy Schubert 	if (((elm)->field.tqe_next = (listelm)->field.tqe_next) != NULL)\
341*2b15cb3dSCy Schubert 		(elm)->field.tqe_next->field.tqe_prev =			\
342*2b15cb3dSCy Schubert 		    &(elm)->field.tqe_next;				\
343*2b15cb3dSCy Schubert 	else								\
344*2b15cb3dSCy Schubert 		(head)->tqh_last = &(elm)->field.tqe_next;		\
345*2b15cb3dSCy Schubert 	(listelm)->field.tqe_next = (elm);				\
346*2b15cb3dSCy Schubert 	(elm)->field.tqe_prev = &(listelm)->field.tqe_next;		\
347*2b15cb3dSCy Schubert } while (0)
348*2b15cb3dSCy Schubert 
349*2b15cb3dSCy Schubert #define	TAILQ_INSERT_BEFORE(listelm, elm, field) do {			\
350*2b15cb3dSCy Schubert 	(elm)->field.tqe_prev = (listelm)->field.tqe_prev;		\
351*2b15cb3dSCy Schubert 	(elm)->field.tqe_next = (listelm);				\
352*2b15cb3dSCy Schubert 	*(listelm)->field.tqe_prev = (elm);				\
353*2b15cb3dSCy Schubert 	(listelm)->field.tqe_prev = &(elm)->field.tqe_next;		\
354*2b15cb3dSCy Schubert } while (0)
355*2b15cb3dSCy Schubert 
356*2b15cb3dSCy Schubert #define TAILQ_REMOVE(head, elm, field) do {				\
357*2b15cb3dSCy Schubert 	if (((elm)->field.tqe_next) != NULL)				\
358*2b15cb3dSCy Schubert 		(elm)->field.tqe_next->field.tqe_prev =			\
359*2b15cb3dSCy Schubert 		    (elm)->field.tqe_prev;				\
360*2b15cb3dSCy Schubert 	else								\
361*2b15cb3dSCy Schubert 		(head)->tqh_last = (elm)->field.tqe_prev;		\
362*2b15cb3dSCy Schubert 	*(elm)->field.tqe_prev = (elm)->field.tqe_next;			\
363*2b15cb3dSCy Schubert } while (0)
364*2b15cb3dSCy Schubert 
365*2b15cb3dSCy Schubert #define TAILQ_REPLACE(head, elm, elm2, field) do {			\
366*2b15cb3dSCy Schubert 	if (((elm2)->field.tqe_next = (elm)->field.tqe_next) != NULL)	\
367*2b15cb3dSCy Schubert 		(elm2)->field.tqe_next->field.tqe_prev =		\
368*2b15cb3dSCy Schubert 		    &(elm2)->field.tqe_next;				\
369*2b15cb3dSCy Schubert 	else								\
370*2b15cb3dSCy Schubert 		(head)->tqh_last = &(elm2)->field.tqe_next;		\
371*2b15cb3dSCy Schubert 	(elm2)->field.tqe_prev = (elm)->field.tqe_prev;			\
372*2b15cb3dSCy Schubert 	*(elm2)->field.tqe_prev = (elm2);				\
373*2b15cb3dSCy Schubert } while (0)
374*2b15cb3dSCy Schubert 
375*2b15cb3dSCy Schubert /*
376*2b15cb3dSCy Schubert  * Circular queue definitions.
377*2b15cb3dSCy Schubert  */
378*2b15cb3dSCy Schubert #define CIRCLEQ_HEAD(name, type)					\
379*2b15cb3dSCy Schubert struct name {								\
380*2b15cb3dSCy Schubert 	struct type *cqh_first;		/* first element */		\
381*2b15cb3dSCy Schubert 	struct type *cqh_last;		/* last element */		\
382*2b15cb3dSCy Schubert }
383*2b15cb3dSCy Schubert 
384*2b15cb3dSCy Schubert #define CIRCLEQ_HEAD_INITIALIZER(head)					\
385*2b15cb3dSCy Schubert 	{ CIRCLEQ_END(&head), CIRCLEQ_END(&head) }
386*2b15cb3dSCy Schubert 
387*2b15cb3dSCy Schubert #define CIRCLEQ_ENTRY(type)						\
388*2b15cb3dSCy Schubert struct {								\
389*2b15cb3dSCy Schubert 	struct type *cqe_next;		/* next element */		\
390*2b15cb3dSCy Schubert 	struct type *cqe_prev;		/* previous element */		\
391*2b15cb3dSCy Schubert }
392*2b15cb3dSCy Schubert 
393*2b15cb3dSCy Schubert /*
394*2b15cb3dSCy Schubert  * Circular queue access methods
395*2b15cb3dSCy Schubert  */
396*2b15cb3dSCy Schubert #define	CIRCLEQ_FIRST(head)		((head)->cqh_first)
397*2b15cb3dSCy Schubert #define	CIRCLEQ_LAST(head)		((head)->cqh_last)
398*2b15cb3dSCy Schubert #define	CIRCLEQ_END(head)		((void *)(head))
399*2b15cb3dSCy Schubert #define	CIRCLEQ_NEXT(elm, field)	((elm)->field.cqe_next)
400*2b15cb3dSCy Schubert #define	CIRCLEQ_PREV(elm, field)	((elm)->field.cqe_prev)
401*2b15cb3dSCy Schubert #define	CIRCLEQ_EMPTY(head)						\
402*2b15cb3dSCy Schubert 	(CIRCLEQ_FIRST(head) == CIRCLEQ_END(head))
403*2b15cb3dSCy Schubert 
404*2b15cb3dSCy Schubert #define CIRCLEQ_FOREACH(var, head, field)				\
405*2b15cb3dSCy Schubert 	for((var) = CIRCLEQ_FIRST(head);				\
406*2b15cb3dSCy Schubert 	    (var) != CIRCLEQ_END(head);					\
407*2b15cb3dSCy Schubert 	    (var) = CIRCLEQ_NEXT(var, field))
408*2b15cb3dSCy Schubert 
409*2b15cb3dSCy Schubert #define CIRCLEQ_FOREACH_REVERSE(var, head, field)			\
410*2b15cb3dSCy Schubert 	for((var) = CIRCLEQ_LAST(head);					\
411*2b15cb3dSCy Schubert 	    (var) != CIRCLEQ_END(head);					\
412*2b15cb3dSCy Schubert 	    (var) = CIRCLEQ_PREV(var, field))
413*2b15cb3dSCy Schubert 
414*2b15cb3dSCy Schubert /*
415*2b15cb3dSCy Schubert  * Circular queue functions.
416*2b15cb3dSCy Schubert  */
417*2b15cb3dSCy Schubert #define	CIRCLEQ_INIT(head) do {						\
418*2b15cb3dSCy Schubert 	(head)->cqh_first = CIRCLEQ_END(head);				\
419*2b15cb3dSCy Schubert 	(head)->cqh_last = CIRCLEQ_END(head);				\
420*2b15cb3dSCy Schubert } while (0)
421*2b15cb3dSCy Schubert 
422*2b15cb3dSCy Schubert #define CIRCLEQ_INSERT_AFTER(head, listelm, elm, field) do {		\
423*2b15cb3dSCy Schubert 	(elm)->field.cqe_next = (listelm)->field.cqe_next;		\
424*2b15cb3dSCy Schubert 	(elm)->field.cqe_prev = (listelm);				\
425*2b15cb3dSCy Schubert 	if ((listelm)->field.cqe_next == CIRCLEQ_END(head))		\
426*2b15cb3dSCy Schubert 		(head)->cqh_last = (elm);				\
427*2b15cb3dSCy Schubert 	else								\
428*2b15cb3dSCy Schubert 		(listelm)->field.cqe_next->field.cqe_prev = (elm);	\
429*2b15cb3dSCy Schubert 	(listelm)->field.cqe_next = (elm);				\
430*2b15cb3dSCy Schubert } while (0)
431*2b15cb3dSCy Schubert 
432*2b15cb3dSCy Schubert #define CIRCLEQ_INSERT_BEFORE(head, listelm, elm, field) do {		\
433*2b15cb3dSCy Schubert 	(elm)->field.cqe_next = (listelm);				\
434*2b15cb3dSCy Schubert 	(elm)->field.cqe_prev = (listelm)->field.cqe_prev;		\
435*2b15cb3dSCy Schubert 	if ((listelm)->field.cqe_prev == CIRCLEQ_END(head))		\
436*2b15cb3dSCy Schubert 		(head)->cqh_first = (elm);				\
437*2b15cb3dSCy Schubert 	else								\
438*2b15cb3dSCy Schubert 		(listelm)->field.cqe_prev->field.cqe_next = (elm);	\
439*2b15cb3dSCy Schubert 	(listelm)->field.cqe_prev = (elm);				\
440*2b15cb3dSCy Schubert } while (0)
441*2b15cb3dSCy Schubert 
442*2b15cb3dSCy Schubert #define CIRCLEQ_INSERT_HEAD(head, elm, field) do {			\
443*2b15cb3dSCy Schubert 	(elm)->field.cqe_next = (head)->cqh_first;			\
444*2b15cb3dSCy Schubert 	(elm)->field.cqe_prev = CIRCLEQ_END(head);			\
445*2b15cb3dSCy Schubert 	if ((head)->cqh_last == CIRCLEQ_END(head))			\
446*2b15cb3dSCy Schubert 		(head)->cqh_last = (elm);				\
447*2b15cb3dSCy Schubert 	else								\
448*2b15cb3dSCy Schubert 		(head)->cqh_first->field.cqe_prev = (elm);		\
449*2b15cb3dSCy Schubert 	(head)->cqh_first = (elm);					\
450*2b15cb3dSCy Schubert } while (0)
451*2b15cb3dSCy Schubert 
452*2b15cb3dSCy Schubert #define CIRCLEQ_INSERT_TAIL(head, elm, field) do {			\
453*2b15cb3dSCy Schubert 	(elm)->field.cqe_next = CIRCLEQ_END(head);			\
454*2b15cb3dSCy Schubert 	(elm)->field.cqe_prev = (head)->cqh_last;			\
455*2b15cb3dSCy Schubert 	if ((head)->cqh_first == CIRCLEQ_END(head))			\
456*2b15cb3dSCy Schubert 		(head)->cqh_first = (elm);				\
457*2b15cb3dSCy Schubert 	else								\
458*2b15cb3dSCy Schubert 		(head)->cqh_last->field.cqe_next = (elm);		\
459*2b15cb3dSCy Schubert 	(head)->cqh_last = (elm);					\
460*2b15cb3dSCy Schubert } while (0)
461*2b15cb3dSCy Schubert 
462*2b15cb3dSCy Schubert #define	CIRCLEQ_REMOVE(head, elm, field) do {				\
463*2b15cb3dSCy Schubert 	if ((elm)->field.cqe_next == CIRCLEQ_END(head))			\
464*2b15cb3dSCy Schubert 		(head)->cqh_last = (elm)->field.cqe_prev;		\
465*2b15cb3dSCy Schubert 	else								\
466*2b15cb3dSCy Schubert 		(elm)->field.cqe_next->field.cqe_prev =			\
467*2b15cb3dSCy Schubert 		    (elm)->field.cqe_prev;				\
468*2b15cb3dSCy Schubert 	if ((elm)->field.cqe_prev == CIRCLEQ_END(head))			\
469*2b15cb3dSCy Schubert 		(head)->cqh_first = (elm)->field.cqe_next;		\
470*2b15cb3dSCy Schubert 	else								\
471*2b15cb3dSCy Schubert 		(elm)->field.cqe_prev->field.cqe_next =			\
472*2b15cb3dSCy Schubert 		    (elm)->field.cqe_next;				\
473*2b15cb3dSCy Schubert } while (0)
474*2b15cb3dSCy Schubert 
475*2b15cb3dSCy Schubert #define CIRCLEQ_REPLACE(head, elm, elm2, field) do {			\
476*2b15cb3dSCy Schubert 	if (((elm2)->field.cqe_next = (elm)->field.cqe_next) ==		\
477*2b15cb3dSCy Schubert 	    CIRCLEQ_END(head))						\
478*2b15cb3dSCy Schubert 		(head).cqh_last = (elm2);				\
479*2b15cb3dSCy Schubert 	else								\
480*2b15cb3dSCy Schubert 		(elm2)->field.cqe_next->field.cqe_prev = (elm2);	\
481*2b15cb3dSCy Schubert 	if (((elm2)->field.cqe_prev = (elm)->field.cqe_prev) ==		\
482*2b15cb3dSCy Schubert 	    CIRCLEQ_END(head))						\
483*2b15cb3dSCy Schubert 		(head).cqh_first = (elm2);				\
484*2b15cb3dSCy Schubert 	else								\
485*2b15cb3dSCy Schubert 		(elm2)->field.cqe_prev->field.cqe_next = (elm2);	\
486*2b15cb3dSCy Schubert } while (0)
487*2b15cb3dSCy Schubert 
488*2b15cb3dSCy Schubert #endif	/* !SYS_QUEUE_H__ */
489