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