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