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