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