xref: /wlan-driver/qca-wifi-host-cmn/utils/sys/queue.h (revision 5113495b16420b49004c444715d2daae2066e7dc)
1*5113495bSYour Name /*
2*5113495bSYour Name * Copyright (c) 1991, 1993
3*5113495bSYour Name *    The Regents of the University of California.  All rights reserved.
4*5113495bSYour Name *
5*5113495bSYour Name * Redistribution and use in source and binary forms, with or without
6*5113495bSYour Name * modification, are permitted provided that the following conditions
7*5113495bSYour Name * are met:
8*5113495bSYour Name * 1. Redistributions of source code must retain the above copyright
9*5113495bSYour Name *    notice, this list of conditions and the following disclaimer.
10*5113495bSYour Name * 2. Redistributions in binary form must reproduce the above copyright
11*5113495bSYour Name *    notice, this list of conditions and the following disclaimer in the
12*5113495bSYour Name *    documentation and/or other materials provided with the distribution.
13*5113495bSYour Name * 4. Neither the name of the University nor the names of its contributors
14*5113495bSYour Name *    may be used to endorse or promote products derived from this software
15*5113495bSYour Name *    without specific prior written permission.
16*5113495bSYour Name *
17*5113495bSYour Name * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18*5113495bSYour Name * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19*5113495bSYour Name * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20*5113495bSYour Name * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21*5113495bSYour Name * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22*5113495bSYour Name * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23*5113495bSYour Name * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24*5113495bSYour Name * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25*5113495bSYour Name * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26*5113495bSYour Name * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27*5113495bSYour Name * SUCH DAMAGE.
28*5113495bSYour Name *
29*5113495bSYour Name *    @(#)queue.h    8.5 (Berkeley) 8/20/94
30*5113495bSYour Name * $FreeBSD: src/sys/sys/queue.h,v 1.58 2004/04/07 04:19:49 imp Exp $
31*5113495bSYour Name */
32*5113495bSYour Name 
33*5113495bSYour Name #ifndef _QUEUE_H_
34*5113495bSYour Name #define _QUEUE_H_
35*5113495bSYour Name 
36*5113495bSYour Name /*
37*5113495bSYour Name  * This file defines four types of data structures: singly-linked lists,
38*5113495bSYour Name  * singly-linked tail queues, lists and tail queues.
39*5113495bSYour Name  *
40*5113495bSYour Name  * A singly-linked list is headed by a single forward pointer. The elements
41*5113495bSYour Name  * are singly linked for minimum space and pointer manipulation overhead at
42*5113495bSYour Name  * the expense of O(n) removal for arbitrary elements. New elements can be
43*5113495bSYour Name  * added to the list after an existing element or at the head of the list.
44*5113495bSYour Name  * Elements being removed from the head of the list should use the explicit
45*5113495bSYour Name  * macro for this purpose for optimum efficiency. A singly-linked list may
46*5113495bSYour Name  * only be traversed in the forward direction.  Singly-linked lists are ideal
47*5113495bSYour Name  * for applications with large datasets and few or no removals or for
48*5113495bSYour Name  * implementing a LIFO queue.
49*5113495bSYour Name  *
50*5113495bSYour Name  * A singly-linked tail queue is headed by a pair of pointers, one to the
51*5113495bSYour Name  * head of the list and the other to the tail of the list. The elements are
52*5113495bSYour Name  * singly linked for minimum space and pointer manipulation overhead at the
53*5113495bSYour Name  * expense of O(n) removal for arbitrary elements. New elements can be added
54*5113495bSYour Name  * to the list after an existing element, at the head of the list, or at the
55*5113495bSYour Name  * end of the list. Elements being removed from the head of the tail queue
56*5113495bSYour Name  * should use the explicit macro for this purpose for optimum efficiency.
57*5113495bSYour Name  * A singly-linked tail queue may only be traversed in the forward direction.
58*5113495bSYour Name  * Singly-linked tail queues are ideal for applications with large datasets
59*5113495bSYour Name  * and few or no removals or for implementing a FIFO queue.
60*5113495bSYour Name  *
61*5113495bSYour Name  * A list is headed by a single forward pointer (or an array of forward
62*5113495bSYour Name  * pointers for a hash table header). The elements are doubly linked
63*5113495bSYour Name  * so that an arbitrary element can be removed without a need to
64*5113495bSYour Name  * traverse the list. New elements can be added to the list before
65*5113495bSYour Name  * or after an existing element or at the head of the list. A list
66*5113495bSYour Name  * may only be traversed in the forward direction.
67*5113495bSYour Name  *
68*5113495bSYour Name  * A tail queue is headed by a pair of pointers, one to the head of the
69*5113495bSYour Name  * list and the other to the tail of the list. The elements are doubly
70*5113495bSYour Name  * linked so that an arbitrary element can be removed without a need to
71*5113495bSYour Name  * traverse the list. New elements can be added to the list before or
72*5113495bSYour Name  * after an existing element, at the head of the list, or at the end of
73*5113495bSYour Name  * the list. A tail queue may be traversed in either direction.
74*5113495bSYour Name  *
75*5113495bSYour Name  * For details on the use of these macros, see the queue(3) manual page.
76*5113495bSYour Name  *
77*5113495bSYour Name  *
78*5113495bSYour Name  *                SLIST    LIST    STAILQ    TAILQ
79*5113495bSYour Name  * _HEAD            +    +    +    +
80*5113495bSYour Name  * _HEAD_INITIALIZER        +    +    +    +
81*5113495bSYour Name  * _ENTRY            +    +    +    +
82*5113495bSYour Name  * _INIT            +    +    +    +
83*5113495bSYour Name  * _EMPTY            +    +    +    +
84*5113495bSYour Name  * _FIRST            +    +    +    +
85*5113495bSYour Name  * _NEXT            +    +    +    +
86*5113495bSYour Name  * _PREV            -    -    -    +
87*5113495bSYour Name  * _LAST            -    -    +    +
88*5113495bSYour Name  * _FOREACH            +    +    +    +
89*5113495bSYour Name  * _FOREACH_SAFE        +    +    +    +
90*5113495bSYour Name  * _FOREACH_REVERSE        -    -    -    +
91*5113495bSYour Name  * _FOREACH_REVERSE_SAFE    -    -    -    +
92*5113495bSYour Name  * _INSERT_HEAD            +    +    +    +
93*5113495bSYour Name  * _INSERT_BEFORE        -    +    -    +
94*5113495bSYour Name  * _INSERT_AFTER        +    +    +    +
95*5113495bSYour Name  * _INSERT_TAIL            -    -    +    +
96*5113495bSYour Name  * _CONCAT            -    -    +    +
97*5113495bSYour Name  * _REMOVE_HEAD            +    -    +    -
98*5113495bSYour Name  * _REMOVE            +    +    +    +
99*5113495bSYour Name  *
100*5113495bSYour Name  */
101*5113495bSYour Name #define    QUEUE_MACRO_DEBUG 0
102*5113495bSYour Name #if QUEUE_MACRO_DEBUG
103*5113495bSYour Name /*
104*5113495bSYour Name  * Store the last 2 places the queue element or head was altered
105*5113495bSYour Name  */
106*5113495bSYour Name struct qm_trace {
107*5113495bSYour Name 	char *lastfile;
108*5113495bSYour Name 	int lastline;
109*5113495bSYour Name 	char *prevfile;
110*5113495bSYour Name 	int prevline;
111*5113495bSYour Name };
112*5113495bSYour Name 
113*5113495bSYour Name #define    TRACEBUF    struct qm_trace trace;
114*5113495bSYour Name #define    TRASHIT(x)    do {(x) = (void *)NULL; } while (0)
115*5113495bSYour Name 
116*5113495bSYour Name #define    QMD_TRACE_HEAD(head) do {			\
117*5113495bSYour Name 		(head)->trace.prevline = (head)->trace.lastline;	\
118*5113495bSYour Name 		(head)->trace.prevfile = (head)->trace.lastfile;	\
119*5113495bSYour Name 		(head)->trace.lastline = __LINE__;		  \
120*5113495bSYour Name 		(head)->trace.lastfile = __FILE__;		  \
121*5113495bSYour Name } while (0)
122*5113495bSYour Name 
123*5113495bSYour Name #define    QMD_TRACE_ELEM(elem) do {			\
124*5113495bSYour Name 		(elem)->trace.prevline = (elem)->trace.lastline;	\
125*5113495bSYour Name 		(elem)->trace.prevfile = (elem)->trace.lastfile;	\
126*5113495bSYour Name 		(elem)->trace.lastline = __LINE__;		  \
127*5113495bSYour Name 		(elem)->trace.lastfile = __FILE__;		  \
128*5113495bSYour Name } while (0)
129*5113495bSYour Name 
130*5113495bSYour Name #else
131*5113495bSYour Name #define    QMD_TRACE_ELEM(elem)
132*5113495bSYour Name #define    QMD_TRACE_HEAD(head)
133*5113495bSYour Name #define    TRACEBUF
134*5113495bSYour Name #define TRASHIT(x) do {(x) = (void *)0; } while (0)
135*5113495bSYour Name #endif /* QUEUE_MACRO_DEBUG */
136*5113495bSYour Name 
137*5113495bSYour Name #ifdef ATHR_RNWF
138*5113495bSYour Name /*
139*5113495bSYour Name  * NDIS contains a defn for SLIST_ENTRY and SINGLE_LIST_ENTRY
140*5113495bSYour Name  */
141*5113495bSYour Name #endif
142*5113495bSYour Name 
143*5113495bSYour Name /*
144*5113495bSYour Name  * Singly-linked List declarations.
145*5113495bSYour Name  */
146*5113495bSYour Name #define    SLIST_HEAD(name, type)			 \
147*5113495bSYour Name 	struct name {				     \
148*5113495bSYour Name 		struct type *slh_first; /* first element */	       \
149*5113495bSYour Name 	}
150*5113495bSYour Name 
151*5113495bSYour Name #define    SLIST_HEAD_INITIALIZER(head)			   \
152*5113495bSYour Name 	{ NULL }
153*5113495bSYour Name 
154*5113495bSYour Name #define    SING_LIST_ENTRY(type)			\
155*5113495bSYour Name 	struct {				\
156*5113495bSYour Name 		struct type *sle_next; /* next element */	     \
157*5113495bSYour Name 	}
158*5113495bSYour Name 
159*5113495bSYour Name /*
160*5113495bSYour Name  * Singly-linked List functions.
161*5113495bSYour Name  */
162*5113495bSYour Name #define    SLIST_EMPTY(head)    ((head)->slh_first == NULL)
163*5113495bSYour Name 
164*5113495bSYour Name #define    SLIST_FIRST(head)    ((head)->slh_first)
165*5113495bSYour Name 
166*5113495bSYour Name #define    SLIST_FOREACH(var, head, field)		      \
167*5113495bSYour Name 	for ((var) = SLIST_FIRST((head));		 \
168*5113495bSYour Name 	     (var);			       \
169*5113495bSYour Name 	     (var) = SLIST_NEXT((var), field))
170*5113495bSYour Name 
171*5113495bSYour Name #define    SLIST_FOREACH_SAFE(var, head, field, tvar)		 \
172*5113495bSYour Name 	for ((var) = SLIST_FIRST((head));		 \
173*5113495bSYour Name 	     (var) && ((tvar) = SLIST_NEXT((var), field), 1);	     \
174*5113495bSYour Name 	     (var) = (tvar))
175*5113495bSYour Name 
176*5113495bSYour Name #define    SLIST_FOREACH_PREVPTR(var, varp, head, field)	    \
177*5113495bSYour Name 	for ((varp) = &SLIST_FIRST((head));		   \
178*5113495bSYour Name 	     ((var) = *(varp)) != NULL;			   \
179*5113495bSYour Name 	     (varp) = &SLIST_NEXT((var), field))
180*5113495bSYour Name 
181*5113495bSYour Name #define    SLIST_INIT(head) do {			\
182*5113495bSYour Name 		SLIST_FIRST((head)) = NULL;		       \
183*5113495bSYour Name } while (0)
184*5113495bSYour Name 
185*5113495bSYour Name #define    SLIST_INSERT_AFTER(slistelm, elm, field) do {	    \
186*5113495bSYour Name 		SLIST_NEXT((elm), field) = SLIST_NEXT((slistelm), field);    \
187*5113495bSYour Name 		SLIST_NEXT((slistelm), field) = (elm);		      \
188*5113495bSYour Name } while (0)
189*5113495bSYour Name 
190*5113495bSYour Name #define    SLIST_INSERT_HEAD(head, elm, field) do {	       \
191*5113495bSYour Name 		SLIST_NEXT((elm), field) = SLIST_FIRST((head));		   \
192*5113495bSYour Name 		SLIST_FIRST((head)) = (elm);			\
193*5113495bSYour Name } while (0)
194*5113495bSYour Name 
195*5113495bSYour Name #define    SLIST_NEXT(elm, field)    ((elm)->field.sle_next)
196*5113495bSYour Name 
197*5113495bSYour Name #define    SLIST_REMOVE(head, elm, type, field) do {		\
198*5113495bSYour Name 		if (SLIST_FIRST((head)) == (elm)) {		   \
199*5113495bSYour Name 			SLIST_REMOVE_HEAD((head), field);	     \
200*5113495bSYour Name 		}				 \
201*5113495bSYour Name 		else {				      \
202*5113495bSYour Name 			struct type *curelm = SLIST_FIRST((head));	  \
203*5113495bSYour Name 			while (SLIST_NEXT(curelm, field) != (elm))	  \
204*5113495bSYour Name 				curelm = SLIST_NEXT(curelm, field);	   \
205*5113495bSYour Name 			SLIST_NEXT(curelm, field) =		   \
206*5113495bSYour Name 				SLIST_NEXT(SLIST_NEXT(curelm, field), field);\
207*5113495bSYour Name 		}				 \
208*5113495bSYour Name } while (0)
209*5113495bSYour Name 
210*5113495bSYour Name #define    SLIST_REMOVE_HEAD(head, field) do {		      \
211*5113495bSYour Name 		SLIST_FIRST((head)) = SLIST_NEXT(SLIST_FIRST((head)),	\
212*5113495bSYour Name 						 field);	 \
213*5113495bSYour Name } while (0)
214*5113495bSYour Name 
215*5113495bSYour Name /*
216*5113495bSYour Name  * Singly-linked Tail queue declarations.
217*5113495bSYour Name  */
218*5113495bSYour Name #define    STAILQ_HEAD(name, type)			  \
219*5113495bSYour Name 	struct name {				     \
220*5113495bSYour Name 		struct type *stqh_first;		\
221*5113495bSYour Name 		struct type **stqh_last;	 \
222*5113495bSYour Name 	}
223*5113495bSYour Name 
224*5113495bSYour Name #define    STAILQ_HEAD_INITIALIZER(head)		    \
225*5113495bSYour Name 	{ NULL, &(head).stqh_first }
226*5113495bSYour Name 
227*5113495bSYour Name #define    STAILQ_ENTRY(type)			     \
228*5113495bSYour Name 	struct {				\
229*5113495bSYour Name 		struct type *stqe_next; /* next element */	      \
230*5113495bSYour Name 	}
231*5113495bSYour Name 
232*5113495bSYour Name /*
233*5113495bSYour Name  * Singly-linked Tail queue functions.
234*5113495bSYour Name  */
235*5113495bSYour Name #define    STAILQ_CONCAT(head1, head2) do {		   \
236*5113495bSYour Name 		if (!STAILQ_EMPTY((head2))) {			 \
237*5113495bSYour Name 			*(head1)->stqh_last = (head2)->stqh_first;	  \
238*5113495bSYour Name 			(head1)->stqh_last = (head2)->stqh_last;	\
239*5113495bSYour Name 			STAILQ_INIT((head2));			 \
240*5113495bSYour Name 		}				 \
241*5113495bSYour Name } while (0)
242*5113495bSYour Name 
243*5113495bSYour Name #define    STAILQ_EMPTY(head)    ((head)->stqh_first == NULL)
244*5113495bSYour Name 
245*5113495bSYour Name #define    STAILQ_FIRST(head)    ((head)->stqh_first)
246*5113495bSYour Name 
247*5113495bSYour Name #define    STAILQ_FOREACH(var, head, field)		   \
248*5113495bSYour Name 	for ((var) = STAILQ_FIRST((head));		 \
249*5113495bSYour Name 	    (var);			      \
250*5113495bSYour Name 	    (var) = STAILQ_NEXT((var), field))
251*5113495bSYour Name 
252*5113495bSYour Name #define    STAILQ_FOREACH_SAFE(var, head, field, tvar)		  \
253*5113495bSYour Name 	for ((var) = STAILQ_FIRST((head));		  \
254*5113495bSYour Name 	     (var) && ((tvar) = STAILQ_NEXT((var), field), 1);	      \
255*5113495bSYour Name 	     (var) = (tvar))
256*5113495bSYour Name 
257*5113495bSYour Name #define    STAILQ_INIT(head) do {			 \
258*5113495bSYour Name 		STAILQ_FIRST((head)) = NULL;			\
259*5113495bSYour Name 		(head)->stqh_last = &STAILQ_FIRST((head));	      \
260*5113495bSYour Name } while (0)
261*5113495bSYour Name 
262*5113495bSYour Name #define    STAILQ_INSERT_AFTER(head, tqelm, elm, field) do {	    \
263*5113495bSYour Name 		if ((STAILQ_NEXT((elm), field) = STAILQ_NEXT((tqelm),	\
264*5113495bSYour Name 							     field)) == NULL) \
265*5113495bSYour Name 			(head)->stqh_last = &STAILQ_NEXT((elm), field);	       \
266*5113495bSYour Name 		STAILQ_NEXT((tqelm), field) = (elm);		    \
267*5113495bSYour Name } while (0)
268*5113495bSYour Name 
269*5113495bSYour Name #define    STAILQ_INSERT_HEAD(head, elm, field) do {		\
270*5113495bSYour Name 		if ((STAILQ_NEXT((elm), field) = STAILQ_FIRST((head))) == \
271*5113495bSYour Name 		    NULL)	\
272*5113495bSYour Name 			(head)->stqh_last = &STAILQ_NEXT((elm), field);	       \
273*5113495bSYour Name 		STAILQ_FIRST((head)) = (elm);			 \
274*5113495bSYour Name } while (0)
275*5113495bSYour Name 
276*5113495bSYour Name #define    STAILQ_INSERT_TAIL(head, elm, field) do {		\
277*5113495bSYour Name 		STAILQ_NEXT((elm), field) = NULL;		 \
278*5113495bSYour Name 		*(head)->stqh_last = (elm);		       \
279*5113495bSYour Name 		(head)->stqh_last = &STAILQ_NEXT((elm), field);		   \
280*5113495bSYour Name } while (0)
281*5113495bSYour Name 
282*5113495bSYour Name #define    STAILQ_LAST(head, type, field)		     \
283*5113495bSYour Name 	(STAILQ_EMPTY((head)) ?			       \
284*5113495bSYour Name 	 NULL :				   \
285*5113495bSYour Name 	 ((struct type *)		     \
286*5113495bSYour Name 	  ((char *)((head)->stqh_last) - __offsetof(struct type, field))))
287*5113495bSYour Name 
288*5113495bSYour Name #define    STAILQ_NEXT(elm, field)    ((elm)->field.stqe_next)
289*5113495bSYour Name 
290*5113495bSYour Name #define    STAILQ_REMOVE(head, elm, type, field) do {		 \
291*5113495bSYour Name 		if (STAILQ_FIRST((head)) == (elm)) {		    \
292*5113495bSYour Name 			STAILQ_REMOVE_HEAD((head), field);	      \
293*5113495bSYour Name 		}				 \
294*5113495bSYour Name 		else {				      \
295*5113495bSYour Name 			struct type *curelm = STAILQ_FIRST((head));	   \
296*5113495bSYour Name 			while (STAILQ_NEXT(curelm, field) != (elm))	   \
297*5113495bSYour Name 				curelm = STAILQ_NEXT(curelm, field);	    \
298*5113495bSYour Name 			if ((STAILQ_NEXT(curelm, field) =	     \
299*5113495bSYour Name 				     STAILQ_NEXT(STAILQ_NEXT(curelm, field), \
300*5113495bSYour Name 						 field)) == NULL) \
301*5113495bSYour Name 				(head)->stqh_last = &STAILQ_NEXT((curelm),\
302*5113495bSYour Name 								 field); \
303*5113495bSYour Name 		}				 \
304*5113495bSYour Name } while (0)
305*5113495bSYour Name 
306*5113495bSYour Name #define    STAILQ_REMOVE_AFTER(head, elm, field) do {		 \
307*5113495bSYour Name 		if (STAILQ_NEXT(elm, field)) {	      \
308*5113495bSYour Name 			if ((STAILQ_NEXT(elm, field) =		  \
309*5113495bSYour Name 				     STAILQ_NEXT(STAILQ_NEXT(elm, field), \
310*5113495bSYour Name 							     field)) == NULL) \
311*5113495bSYour Name 				(head)->stqh_last =		\
312*5113495bSYour Name 					&STAILQ_NEXT((elm), field);	\
313*5113495bSYour Name 		}				 \
314*5113495bSYour Name } while (0)
315*5113495bSYour Name 
316*5113495bSYour Name #define    STAILQ_REMOVE_HEAD(head, field) do {		       \
317*5113495bSYour Name 		if ((STAILQ_FIRST((head)) =		       \
318*5113495bSYour Name 			     STAILQ_NEXT(STAILQ_FIRST((head)), field)) == \
319*5113495bSYour Name 									NULL)\
320*5113495bSYour Name 			(head)->stqh_last = &STAILQ_FIRST((head));	  \
321*5113495bSYour Name } while (0)
322*5113495bSYour Name 
323*5113495bSYour Name #define    STAILQ_REMOVE_HEAD_UNTIL(head, elm, field) do {	      \
324*5113495bSYour Name 		if ((STAILQ_FIRST((head)) =		\
325*5113495bSYour Name 				STAILQ_NEXT((elm), field)) == NULL)	   \
326*5113495bSYour Name 			(head)->stqh_last = &STAILQ_FIRST((head));	  \
327*5113495bSYour Name } while (0)
328*5113495bSYour Name 
329*5113495bSYour Name /*
330*5113495bSYour Name  * List declarations.
331*5113495bSYour Name  */
332*5113495bSYour Name #define    ATH_LIST_HEAD(name, type)			\
333*5113495bSYour Name 	struct name {				     \
334*5113495bSYour Name 		struct type *lh_first;	      \
335*5113495bSYour Name 	}
336*5113495bSYour Name 
337*5113495bSYour Name #ifndef LIST_HEAD
338*5113495bSYour Name #define LIST_HEAD ATH_LIST_HEAD
339*5113495bSYour Name #endif
340*5113495bSYour Name 
341*5113495bSYour Name #define    LIST_HEAD_INITIALIZER(head)			  \
342*5113495bSYour Name 	{ NULL }
343*5113495bSYour Name 
344*5113495bSYour Name #define    LIST_ENTRY(type)			   \
345*5113495bSYour Name 	struct {				\
346*5113495bSYour Name 		struct type *le_next;		\
347*5113495bSYour Name 		struct type **le_prev;		\
348*5113495bSYour Name 	}
349*5113495bSYour Name 
350*5113495bSYour Name /*
351*5113495bSYour Name  * List functions.
352*5113495bSYour Name  */
353*5113495bSYour Name 
354*5113495bSYour Name #define    LIST_EMPTY(head)    ((head)->lh_first == NULL)
355*5113495bSYour Name 
356*5113495bSYour Name #define    LIST_FIRST(head)    ((head)->lh_first)
357*5113495bSYour Name 
358*5113495bSYour Name #define    LIST_FOREACH(var, head, field)		     \
359*5113495bSYour Name 	for ((var) = LIST_FIRST((head));		\
360*5113495bSYour Name 	     (var);			       \
361*5113495bSYour Name 	     (var) = LIST_NEXT((var), field))
362*5113495bSYour Name 
363*5113495bSYour Name #define    LIST_FOREACH_SAFE(var, head, field, tvar)		\
364*5113495bSYour Name 	for ((var) = LIST_FIRST((head));		\
365*5113495bSYour Name 	     (var) && ((tvar) = LIST_NEXT((var), field), 1);	    \
366*5113495bSYour Name 	     (var) = (tvar))
367*5113495bSYour Name 
368*5113495bSYour Name #define    LIST_INIT(head) do {			       \
369*5113495bSYour Name 		LIST_FIRST((head)) = NULL;		      \
370*5113495bSYour Name } while (0)
371*5113495bSYour Name 
372*5113495bSYour Name #define    LIST_INSERT_AFTER(listelm, elm, field) do {		  \
373*5113495bSYour Name 		if ((LIST_NEXT((elm), field) =		\
374*5113495bSYour Name 					LIST_NEXT((listelm), field)) != NULL) \
375*5113495bSYour Name 			LIST_NEXT((listelm), field)->field.le_prev =	    \
376*5113495bSYour Name 				&LIST_NEXT((elm), field);		 \
377*5113495bSYour Name 		LIST_NEXT((listelm), field) = (elm);		    \
378*5113495bSYour Name 		(elm)->field.le_prev = &LIST_NEXT((listelm), field);	    \
379*5113495bSYour Name } while (0)
380*5113495bSYour Name 
381*5113495bSYour Name #define    LIST_INSERT_BEFORE(listelm, elm, field) do {		   \
382*5113495bSYour Name 		(elm)->field.le_prev = (listelm)->field.le_prev;	\
383*5113495bSYour Name 		LIST_NEXT((elm), field) = (listelm);		    \
384*5113495bSYour Name 		*(listelm)->field.le_prev = (elm);		  \
385*5113495bSYour Name 		(listelm)->field.le_prev = &LIST_NEXT((elm), field);	    \
386*5113495bSYour Name } while (0)
387*5113495bSYour Name 
388*5113495bSYour Name #define    LIST_INSERT_HEAD(head, elm, field) do {		  \
389*5113495bSYour Name 		if ((LIST_NEXT((elm), field) = LIST_FIRST((head))) != NULL)    \
390*5113495bSYour Name 			LIST_FIRST((head))->field.le_prev =		\
391*5113495bSYour Name 						&LIST_NEXT((elm), field); \
392*5113495bSYour Name 		LIST_FIRST((head)) = (elm);		       \
393*5113495bSYour Name 		(elm)->field.le_prev = &LIST_FIRST((head));	       \
394*5113495bSYour Name } while (0)
395*5113495bSYour Name 
396*5113495bSYour Name #define    LIST_NEXT(elm, field)    ((elm)->field.le_next)
397*5113495bSYour Name 
398*5113495bSYour Name #define    LIST_REMOVE(elm, field) do {			   \
399*5113495bSYour Name 		if (LIST_NEXT((elm), field) != NULL)		    \
400*5113495bSYour Name 			LIST_NEXT((elm), field)->field.le_prev =	 \
401*5113495bSYour Name 				(elm)->field.le_prev;		     \
402*5113495bSYour Name 		*(elm)->field.le_prev = LIST_NEXT((elm), field);	\
403*5113495bSYour Name } while (0)
404*5113495bSYour Name 
405*5113495bSYour Name /*
406*5113495bSYour Name  * Tail queue declarations.
407*5113495bSYour Name  */
408*5113495bSYour Name #ifndef TRACE_TX_LEAK
409*5113495bSYour Name #define TRACE_TX_LEAK 0
410*5113495bSYour Name #endif
411*5113495bSYour Name 
412*5113495bSYour Name #if TRACE_TX_LEAK
413*5113495bSYour Name #define  HEADNAME        char   headname[64];
414*5113495bSYour Name #define  COPY_HEADNAME(head)  OS_MEMCPY((head)->headname, #head, sizeof(#head))
415*5113495bSYour Name #else
416*5113495bSYour Name #define  HEADNAME
417*5113495bSYour Name #define  COPY_HEADNAME(head)
418*5113495bSYour Name #endif
419*5113495bSYour Name 
420*5113495bSYour Name #define    TAILQ_HEAD(name, type)			 \
421*5113495bSYour Name 	struct name {				     \
422*5113495bSYour Name 		struct type *tqh_first;	       \
423*5113495bSYour Name 		struct type **tqh_last; 	\
424*5113495bSYour Name 		HEADNAME			\
425*5113495bSYour Name 			TRACEBUF			    \
426*5113495bSYour Name 	}
427*5113495bSYour Name 
428*5113495bSYour Name #define    TAILQ_HEAD_INITIALIZER(head)			   \
429*5113495bSYour Name 	{ NULL, &(head).tqh_first }
430*5113495bSYour Name 
431*5113495bSYour Name #define    TAILQ_ENTRY(type)			    \
432*5113495bSYour Name 	struct {				\
433*5113495bSYour Name 		struct type *tqe_next;		\
434*5113495bSYour Name 		struct type **tqe_prev;		\
435*5113495bSYour Name 		TRACEBUF			    \
436*5113495bSYour Name 	}
437*5113495bSYour Name 
438*5113495bSYour Name /*
439*5113495bSYour Name  * Tail queue functions.
440*5113495bSYour Name  */
441*5113495bSYour Name 
442*5113495bSYour Name #define    TAILQ_EMPTY(head)    ((head)->tqh_first == NULL)
443*5113495bSYour Name 
444*5113495bSYour Name #define    TAILQ_FIRST(head)    ((head)->tqh_first)
445*5113495bSYour Name 
446*5113495bSYour Name #define    TAILQ_FOREACH(var, head, field)		      \
447*5113495bSYour Name 	for ((var) = TAILQ_FIRST((head));		 \
448*5113495bSYour Name 	     (var);			       \
449*5113495bSYour Name 	     (var) = TAILQ_NEXT((var), field))
450*5113495bSYour Name 
451*5113495bSYour Name #define    TAILQ_FOREACH_SAFE(var, head, field, tvar)		 \
452*5113495bSYour Name 	for ((var) = TAILQ_FIRST((head));		 \
453*5113495bSYour Name 	     (var) && ((tvar) = TAILQ_NEXT((var), field), 1);	     \
454*5113495bSYour Name 	     (var) = (tvar))
455*5113495bSYour Name 
456*5113495bSYour Name #define    TAILQ_FOREACH_REVERSE(var, head, headname, field)	    \
457*5113495bSYour Name 	for ((var) = TAILQ_LAST((head), headname);	      \
458*5113495bSYour Name 	     (var);			       \
459*5113495bSYour Name 	     (var) = TAILQ_PREV((var), headname, field))
460*5113495bSYour Name 
461*5113495bSYour Name #define    TAILQ_FOREACH_REVERSE_SAFE(var, head, headname, field, tvar)	   \
462*5113495bSYour Name 	for ((var) = TAILQ_LAST((head), headname);	      \
463*5113495bSYour Name 	     (var) && ((tvar) = TAILQ_PREV((var), headname, field), 1);	   \
464*5113495bSYour Name 	     (var) = (tvar))
465*5113495bSYour Name 
466*5113495bSYour Name #define    TAILQ_INIT(head) do {			\
467*5113495bSYour Name 		TAILQ_FIRST((head)) = NULL;		       \
468*5113495bSYour Name 		(head)->tqh_last = &TAILQ_FIRST((head));	    \
469*5113495bSYour Name 		COPY_HEADNAME(head);			     \
470*5113495bSYour Name 		QMD_TRACE_HEAD(head);			     \
471*5113495bSYour Name } while (0)
472*5113495bSYour Name 
473*5113495bSYour Name #define    TAILQ_INSERT_AFTER(head, listelm, elm, field) do {	     \
474*5113495bSYour Name 		if ((TAILQ_NEXT((elm), field) = TAILQ_NEXT((listelm), \
475*5113495bSYour Name 		     field)) != NULL) \
476*5113495bSYour Name 			TAILQ_NEXT((elm), field)->field.tqe_prev =	   \
477*5113495bSYour Name 				&TAILQ_NEXT((elm), field);		  \
478*5113495bSYour Name 		else {				      \
479*5113495bSYour Name 			(head)->tqh_last = &TAILQ_NEXT((elm), field);	     \
480*5113495bSYour Name 			QMD_TRACE_HEAD(head);			 \
481*5113495bSYour Name 		}				 \
482*5113495bSYour Name 		TAILQ_NEXT((listelm), field) = (elm);		     \
483*5113495bSYour Name 		(elm)->field.tqe_prev = &TAILQ_NEXT((listelm), field);	      \
484*5113495bSYour Name 		QMD_TRACE_ELEM(&(elm)->field);			  \
485*5113495bSYour Name 		QMD_TRACE_ELEM(&listelm->field);		\
486*5113495bSYour Name } while (0)
487*5113495bSYour Name 
488*5113495bSYour Name #define    TAILQ_INSERT_BEFORE(listelm, elm, field) do {	    \
489*5113495bSYour Name 		(elm)->field.tqe_prev = (listelm)->field.tqe_prev;	  \
490*5113495bSYour Name 		TAILQ_NEXT((elm), field) = (listelm);		     \
491*5113495bSYour Name 		*(listelm)->field.tqe_prev = (elm);		   \
492*5113495bSYour Name 		(listelm)->field.tqe_prev = &TAILQ_NEXT((elm), field);	      \
493*5113495bSYour Name 		QMD_TRACE_ELEM(&(elm)->field);			  \
494*5113495bSYour Name 		QMD_TRACE_ELEM(&listelm->field);		\
495*5113495bSYour Name } while (0)
496*5113495bSYour Name 
497*5113495bSYour Name #define    TAILQ_INSERT_HEAD(head, elm, field) do {	       \
498*5113495bSYour Name 		if ((TAILQ_NEXT((elm), field) = TAILQ_FIRST((head))) != NULL)\
499*5113495bSYour Name 			TAILQ_FIRST((head))->field.tqe_prev =		 \
500*5113495bSYour Name 				&TAILQ_NEXT((elm), field);		  \
501*5113495bSYour Name 		else				    \
502*5113495bSYour Name 			(head)->tqh_last = &TAILQ_NEXT((elm), field);	     \
503*5113495bSYour Name 		TAILQ_FIRST((head)) = (elm);			\
504*5113495bSYour Name 		(elm)->field.tqe_prev = &TAILQ_FIRST((head));		 \
505*5113495bSYour Name 		QMD_TRACE_HEAD(head);			     \
506*5113495bSYour Name 		QMD_TRACE_ELEM(&(elm)->field);			  \
507*5113495bSYour Name } while (0)
508*5113495bSYour Name 
509*5113495bSYour Name #define    TAILQ_INSERT_TAIL(head, elm, field) do {	       \
510*5113495bSYour Name 		TAILQ_NEXT((elm), field) = NULL;		\
511*5113495bSYour Name 		(elm)->field.tqe_prev = (head)->tqh_last;	     \
512*5113495bSYour Name 		*(head)->tqh_last = (elm);		      \
513*5113495bSYour Name 		(head)->tqh_last = &TAILQ_NEXT((elm), field);		 \
514*5113495bSYour Name 		QMD_TRACE_HEAD(head);			     \
515*5113495bSYour Name 		QMD_TRACE_ELEM(&(elm)->field);			  \
516*5113495bSYour Name } while (0)
517*5113495bSYour Name 
518*5113495bSYour Name #define    TAILQ_LAST(head, headname)			 \
519*5113495bSYour Name 	(*(((struct headname *)((head)->tqh_last))->tqh_last))
520*5113495bSYour Name 
521*5113495bSYour Name #define    TAILQ_NEXT(elm, field) ((elm)->field.tqe_next)
522*5113495bSYour Name 
523*5113495bSYour Name #define    TAILQ_PREV(elm, headname, field)		   \
524*5113495bSYour Name 	(*(((struct headname *)((elm)->field.tqe_prev))->tqh_last))
525*5113495bSYour Name 
526*5113495bSYour Name #define    TAILQ_REMOVE(head, elm, field) do {		      \
527*5113495bSYour Name 		if ((TAILQ_NEXT((elm), field)) != NULL)		       \
528*5113495bSYour Name 			TAILQ_NEXT((elm), field)->field.tqe_prev =	   \
529*5113495bSYour Name 				(elm)->field.tqe_prev;		      \
530*5113495bSYour Name 		else {				      \
531*5113495bSYour Name 			(head)->tqh_last = (elm)->field.tqe_prev;	 \
532*5113495bSYour Name 			QMD_TRACE_HEAD(head);			 \
533*5113495bSYour Name 		}				 \
534*5113495bSYour Name 		*(elm)->field.tqe_prev = TAILQ_NEXT((elm), field);	  \
535*5113495bSYour Name 		TRASHIT((elm)->field.tqe_next);			   \
536*5113495bSYour Name 		TRASHIT((elm)->field.tqe_prev);			   \
537*5113495bSYour Name 		QMD_TRACE_ELEM(&(elm)->field);			  \
538*5113495bSYour Name } while (0)
539*5113495bSYour Name 
540*5113495bSYour Name #define TAILQ_CONCAT(head1, head2, field)  do {			 \
541*5113495bSYour Name 		if (!TAILQ_EMPTY(head2)) {			\
542*5113495bSYour Name 			*(head1)->tqh_last = (head2)->tqh_first;	\
543*5113495bSYour Name 			(head2)->tqh_first->field.tqe_prev = (head1)->tqh_last;\
544*5113495bSYour Name 			(head1)->tqh_last  = (head2)->tqh_last;		\
545*5113495bSYour Name 			TAILQ_INIT((head2));				\
546*5113495bSYour Name 		}						\
547*5113495bSYour Name } while (0)
548*5113495bSYour Name 
549*5113495bSYour Name #ifdef _KERNEL
550*5113495bSYour Name 
551*5113495bSYour Name /*
552*5113495bSYour Name  * XXX insque() and remque() are an old way of handling certain queues.
553*5113495bSYour Name  * They bogusly assumes that all queue heads look alike.
554*5113495bSYour Name  */
555*5113495bSYour Name 
556*5113495bSYour Name struct quehead {
557*5113495bSYour Name 	struct quehead *qh_link;
558*5113495bSYour Name 	struct quehead *qh_rlink;
559*5113495bSYour Name };
560*5113495bSYour Name 
561*5113495bSYour Name #if defined(__GNUC__) || defined(__INTEL_COMPILER)
562*5113495bSYour Name 
insque(void * a,void * b)563*5113495bSYour Name static inline void insque(void *a, void *b)
564*5113495bSYour Name {
565*5113495bSYour Name 	struct quehead *element = (struct quehead *)a,
566*5113495bSYour Name 	*head = (struct quehead *)b;
567*5113495bSYour Name 
568*5113495bSYour Name 	element->qh_link = head->qh_link;
569*5113495bSYour Name 	element->qh_rlink = head;
570*5113495bSYour Name 	head->qh_link = element;
571*5113495bSYour Name 	element->qh_link->qh_rlink = element;
572*5113495bSYour Name }
573*5113495bSYour Name 
remque(void * a)574*5113495bSYour Name static inline void remque(void *a)
575*5113495bSYour Name {
576*5113495bSYour Name 	struct quehead *element = (struct quehead *)a;
577*5113495bSYour Name 
578*5113495bSYour Name 	element->qh_link->qh_rlink = element->qh_rlink;
579*5113495bSYour Name 	element->qh_rlink->qh_link = element->qh_link;
580*5113495bSYour Name 	element->qh_rlink = 0;
581*5113495bSYour Name }
582*5113495bSYour Name 
583*5113495bSYour Name #else                           /* !(__GNUC__ || __INTEL_COMPILER) */
584*5113495bSYour Name 
585*5113495bSYour Name void insque(void *a, void *b);
586*5113495bSYour Name void remque(void *a);
587*5113495bSYour Name 
588*5113495bSYour Name #endif /* __GNUC__ || __INTEL_COMPILER */
589*5113495bSYour Name 
590*5113495bSYour Name #endif /* _KERNEL */
591*5113495bSYour Name 
592*5113495bSYour Name #endif /* _QUEUE_H_ */
593