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