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