1c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner/*
2c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner * Copyright (c) 1991, 1993
3c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner *	The Regents of the University of California.  All rights reserved.
4c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner *
5c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner * Redistribution and use in source and binary forms, with or without
6c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner * modification, are permitted provided that the following conditions
7c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner * are met:
8c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner * 1. Redistributions of source code must retain the above copyright
9c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner *    notice, this list of conditions and the following disclaimer.
10c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner * 2. Redistributions in binary form must reproduce the above copyright
11c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner *    notice, this list of conditions and the following disclaimer in the
12c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner *    documentation and/or other materials provided with the distribution.
13c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner * 3. Neither the name of the University nor the names of its contributors
14c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner *    may be used to endorse or promote products derived from this software
15c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner *    without specific prior written permission.
16c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner *
17c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner * SUCH DAMAGE.
28c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner *
29c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner *	@(#)queue.h	8.5 (Berkeley) 8/20/94
30c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner */
31c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner
32c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner#ifndef	_SYS_QUEUE_H_
33c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner#define	_SYS_QUEUE_H_
34c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner
35c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner/*
36c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner * This file defines five types of data structures: singly-linked lists,
37c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner * lists, simple queues, tail queues, and circular queues.
38c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner *
39c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner * A singly-linked list is headed by a single forward pointer. The
40c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner * elements are singly linked for minimum space and pointer manipulation
41c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner * overhead at the expense of O(n) removal for arbitrary elements. New
42c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner * elements can be added to the list after an existing element or at the
43c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner * head of the list.  Elements being removed from the head of the list
44c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner * should use the explicit macro for this purpose for optimum
45c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner * efficiency. A singly-linked list may only be traversed in the forward
46c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner * direction.  Singly-linked lists are ideal for applications with large
47c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner * datasets and few or no removals or for implementing a LIFO queue.
48c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner *
49c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner * A list is headed by a single forward pointer (or an array of forward
50c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner * pointers for a hash table header). The elements are doubly linked
51c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner * so that an arbitrary element can be removed without a need to
52c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner * traverse the list. New elements can be added to the list before
53c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner * or after an existing element or at the head of the list. A list
54c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner * may only be traversed in the forward direction.
55c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner *
56c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner * A simple queue is headed by a pair of pointers, one the head of the
57c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner * list and the other to the tail of the list. The elements are singly
58c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner * linked to save space, so elements can only be removed from the
59c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner * head of the list. New elements can be added to the list after
60c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner * an existing element, at the head of the list, or at the end of the
61c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner * list. A simple queue may only be traversed in the forward direction.
62c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner *
63c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner * A tail queue is headed by a pair of pointers, one to the head of the
64c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner * list and the other to the tail of the list. The elements are doubly
65c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner * linked so that an arbitrary element can be removed without a need to
66c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner * traverse the list. New elements can be added to the list before or
67c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner * after an existing element, at the head of the list, or at the end of
68c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner * the list. A tail queue may be traversed in either direction.
69c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner *
70c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner * A circle queue is headed by a pair of pointers, one to the head of the
71c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner * list and the other to the tail of the list. The elements are doubly
72c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner * linked so that an arbitrary element can be removed without a need to
73c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner * traverse the list. New elements can be added to the list before or after
74c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner * an existing element, at the head of the list, or at the end of the list.
75c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner * A circle queue may be traversed in either direction, but has a more
76c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner * complex end of list detection.
77c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner *
78c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner * For details on the use of these macros, see the queue(3) manual page.
79c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner */
80c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner
81c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner/*
82c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner * List definitions.
83c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner */
84c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner#define	LIST_HEAD(name, type)						\
85c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turnerstruct name {								\
86c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	struct type *lh_first;	/* first element */			\
87c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner}
88c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner
89c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner#define	LIST_HEAD_INITIALIZER(head)					\
90c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	{ NULL }
91c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner
92c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner#define	LIST_ENTRY(type)						\
93c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turnerstruct {								\
94c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	struct type *le_next;	/* next element */			\
95c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	struct type **le_prev;	/* address of previous next element */	\
96c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner}
97c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner
98c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner/*
99c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner * List functions.
100c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner */
101c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner#define	LIST_INIT(head) do {						\
102c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	(head)->lh_first = NULL;					\
103c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner} while (/*CONSTCOND*/0)
104c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner
105c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner#define	LIST_INSERT_AFTER(listelm, elm, field) do {			\
106c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	if (((elm)->field.le_next = (listelm)->field.le_next) != NULL)	\
107c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner		(listelm)->field.le_next->field.le_prev =		\
108c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner		    &(elm)->field.le_next;				\
109c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	(listelm)->field.le_next = (elm);				\
110c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	(elm)->field.le_prev = &(listelm)->field.le_next;		\
111c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner} while (/*CONSTCOND*/0)
112c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner
113c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner#define	LIST_INSERT_BEFORE(listelm, elm, field) do {			\
114c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	(elm)->field.le_prev = (listelm)->field.le_prev;		\
115c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	(elm)->field.le_next = (listelm);				\
116c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	*(listelm)->field.le_prev = (elm);				\
117c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	(listelm)->field.le_prev = &(elm)->field.le_next;		\
118c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner} while (/*CONSTCOND*/0)
119c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner
120c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner#define	LIST_INSERT_HEAD(head, elm, field) do {				\
121c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	if (((elm)->field.le_next = (head)->lh_first) != NULL)		\
122c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner		(head)->lh_first->field.le_prev = &(elm)->field.le_next;\
123c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	(head)->lh_first = (elm);					\
124c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	(elm)->field.le_prev = &(head)->lh_first;			\
125c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner} while (/*CONSTCOND*/0)
126c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner
127c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner#define	LIST_REMOVE(elm, field) do {					\
128c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	if ((elm)->field.le_next != NULL)				\
129c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner		(elm)->field.le_next->field.le_prev = 			\
130c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner		    (elm)->field.le_prev;				\
131c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	*(elm)->field.le_prev = (elm)->field.le_next;			\
132c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner} while (/*CONSTCOND*/0)
133c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner
134c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner#define	LIST_FOREACH(var, head, field)					\
135c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	for ((var) = ((head)->lh_first);				\
136c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner		(var);							\
137c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner		(var) = ((var)->field.le_next))
138c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner
139c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner/*
140c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner * List access methods.
141c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner */
142c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner#define	LIST_EMPTY(head)		((head)->lh_first == NULL)
143c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner#define	LIST_FIRST(head)		((head)->lh_first)
144c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner#define	LIST_NEXT(elm, field)		((elm)->field.le_next)
145c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner
146c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner
147c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner/*
148c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner * Singly-linked List definitions.
149c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner */
150c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner#define	SLIST_HEAD(name, type)						\
151c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turnerstruct name {								\
152c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	struct type *slh_first;	/* first element */			\
153c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner}
154c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner
155c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner#define	SLIST_HEAD_INITIALIZER(head)					\
156c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	{ NULL }
157c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner
158c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner#define	SLIST_ENTRY(type)						\
159c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turnerstruct {								\
160c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	struct type *sle_next;	/* next element */			\
161c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner}
162c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner
163c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner/*
164c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner * Singly-linked List functions.
165c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner */
166c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner#define	SLIST_INIT(head) do {						\
167c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	(head)->slh_first = NULL;					\
168c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner} while (/*CONSTCOND*/0)
169c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner
170c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner#define	SLIST_INSERT_AFTER(slistelm, elm, field) do {			\
171c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	(elm)->field.sle_next = (slistelm)->field.sle_next;		\
172c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	(slistelm)->field.sle_next = (elm);				\
173c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner} while (/*CONSTCOND*/0)
174c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner
175c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner#define	SLIST_INSERT_HEAD(head, elm, field) do {			\
176c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	(elm)->field.sle_next = (head)->slh_first;			\
177c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	(head)->slh_first = (elm);					\
178c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner} while (/*CONSTCOND*/0)
179c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner
180c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner#define	SLIST_REMOVE_HEAD(head, field) do {				\
181c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	(head)->slh_first = (head)->slh_first->field.sle_next;		\
182c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner} while (/*CONSTCOND*/0)
183c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner
184c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner#define	SLIST_REMOVE(head, elm, type, field) do {			\
185c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	if ((head)->slh_first == (elm)) {				\
186c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner		SLIST_REMOVE_HEAD((head), field);			\
187c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	}								\
188c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	else {								\
189c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner		struct type *curelm = (head)->slh_first;		\
190c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner		while(curelm->field.sle_next != (elm))			\
191c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner			curelm = curelm->field.sle_next;		\
192c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner		curelm->field.sle_next =				\
193c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner		    curelm->field.sle_next->field.sle_next;		\
194c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	}								\
195c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner} while (/*CONSTCOND*/0)
196c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner
197c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner#define	SLIST_FOREACH(var, head, field)					\
198c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	for((var) = (head)->slh_first; (var); (var) = (var)->field.sle_next)
199c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner
200c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner/*
201c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner * Singly-linked List access methods.
202c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner */
203c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner#define	SLIST_EMPTY(head)	((head)->slh_first == NULL)
204c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner#define	SLIST_FIRST(head)	((head)->slh_first)
205c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner#define	SLIST_NEXT(elm, field)	((elm)->field.sle_next)
206c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner
207c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner
208c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner/*
209c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner * Singly-linked Tail queue declarations.
210c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner */
211c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner#define	STAILQ_HEAD(name, type)					\
212c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turnerstruct name {								\
213c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	struct type *stqh_first;	/* first element */			\
214c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	struct type **stqh_last;	/* addr of last next element */		\
215c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner}
216c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner
217c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner#define	STAILQ_HEAD_INITIALIZER(head)					\
218c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	{ NULL, &(head).stqh_first }
219c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner
220c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner#define	STAILQ_ENTRY(type)						\
221c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turnerstruct {								\
222c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	struct type *stqe_next;	/* next element */			\
223c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner}
224c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner
225c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner/*
226c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner * Singly-linked Tail queue functions.
227c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner */
228c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner#define	STAILQ_INIT(head) do {						\
229c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	(head)->stqh_first = NULL;					\
230c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	(head)->stqh_last = &(head)->stqh_first;				\
231c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner} while (/*CONSTCOND*/0)
232c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner
233c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner#define	STAILQ_INSERT_HEAD(head, elm, field) do {			\
234c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	if (((elm)->field.stqe_next = (head)->stqh_first) == NULL)	\
235c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner		(head)->stqh_last = &(elm)->field.stqe_next;		\
236c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	(head)->stqh_first = (elm);					\
237c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner} while (/*CONSTCOND*/0)
238c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner
239c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner#define	STAILQ_INSERT_TAIL(head, elm, field) do {			\
240c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	(elm)->field.stqe_next = NULL;					\
241c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	*(head)->stqh_last = (elm);					\
242c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	(head)->stqh_last = &(elm)->field.stqe_next;			\
243c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner} while (/*CONSTCOND*/0)
244c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner
245c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner#define	STAILQ_INSERT_AFTER(head, listelm, elm, field) do {		\
246c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	if (((elm)->field.stqe_next = (listelm)->field.stqe_next) == NULL)\
247c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner		(head)->stqh_last = &(elm)->field.stqe_next;		\
248c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	(listelm)->field.stqe_next = (elm);				\
249c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner} while (/*CONSTCOND*/0)
250c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner
251c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner#define	STAILQ_REMOVE_HEAD(head, field) do {				\
252c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	if (((head)->stqh_first = (head)->stqh_first->field.stqe_next) == NULL) \
253c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner		(head)->stqh_last = &(head)->stqh_first;			\
254c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner} while (/*CONSTCOND*/0)
255c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner
256c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner#define	STAILQ_REMOVE(head, elm, type, field) do {			\
257c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	if ((head)->stqh_first == (elm)) {				\
258c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner		STAILQ_REMOVE_HEAD((head), field);			\
259c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	} else {							\
260c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner		struct type *curelm = (head)->stqh_first;		\
261c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner		while (curelm->field.stqe_next != (elm))			\
262c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner			curelm = curelm->field.stqe_next;		\
263c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner		if ((curelm->field.stqe_next =				\
264c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner			curelm->field.stqe_next->field.stqe_next) == NULL) \
265c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner			    (head)->stqh_last = &(curelm)->field.stqe_next; \
266c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	}								\
267c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner} while (/*CONSTCOND*/0)
268c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner
269c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner#define	STAILQ_FOREACH(var, head, field)				\
270c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	for ((var) = ((head)->stqh_first);				\
271c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner		(var);							\
272c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner		(var) = ((var)->field.stqe_next))
273c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner
274c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner/*
275c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner * Singly-linked Tail queue access methods.
276c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner */
277c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner#define	STAILQ_EMPTY(head)	((head)->stqh_first == NULL)
278c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner#define	STAILQ_FIRST(head)	((head)->stqh_first)
279c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner#define	STAILQ_NEXT(elm, field)	((elm)->field.stqe_next)
280c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner
281c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner
282c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner/*
283c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner * Simple queue definitions.
284c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner */
285c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner#define	SIMPLEQ_HEAD(name, type)					\
286c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turnerstruct name {								\
287c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	struct type *sqh_first;	/* first element */			\
288c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	struct type **sqh_last;	/* addr of last next element */		\
289c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner}
290c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner
291c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner#define	SIMPLEQ_HEAD_INITIALIZER(head)					\
292c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	{ NULL, &(head).sqh_first }
293c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner
294c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner#define	SIMPLEQ_ENTRY(type)						\
295c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turnerstruct {								\
296c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	struct type *sqe_next;	/* next element */			\
297c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner}
298c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner
299c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner/*
300c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner * Simple queue functions.
301c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner */
302c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner#define	SIMPLEQ_INIT(head) do {						\
303c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	(head)->sqh_first = NULL;					\
304c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	(head)->sqh_last = &(head)->sqh_first;				\
305c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner} while (/*CONSTCOND*/0)
306c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner
307c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner#define	SIMPLEQ_INSERT_HEAD(head, elm, field) do {			\
308c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	if (((elm)->field.sqe_next = (head)->sqh_first) == NULL)	\
309c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner		(head)->sqh_last = &(elm)->field.sqe_next;		\
310c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	(head)->sqh_first = (elm);					\
311c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner} while (/*CONSTCOND*/0)
312c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner
313c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner#define	SIMPLEQ_INSERT_TAIL(head, elm, field) do {			\
314c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	(elm)->field.sqe_next = NULL;					\
315c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	*(head)->sqh_last = (elm);					\
316c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	(head)->sqh_last = &(elm)->field.sqe_next;			\
317c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner} while (/*CONSTCOND*/0)
318c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner
319c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner#define	SIMPLEQ_INSERT_AFTER(head, listelm, elm, field) do {		\
320c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	if (((elm)->field.sqe_next = (listelm)->field.sqe_next) == NULL)\
321c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner		(head)->sqh_last = &(elm)->field.sqe_next;		\
322c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	(listelm)->field.sqe_next = (elm);				\
323c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner} while (/*CONSTCOND*/0)
324c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner
325c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner#define	SIMPLEQ_REMOVE_HEAD(head, field) do {				\
326c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	if (((head)->sqh_first = (head)->sqh_first->field.sqe_next) == NULL) \
327c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner		(head)->sqh_last = &(head)->sqh_first;			\
328c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner} while (/*CONSTCOND*/0)
329c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner
330c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner#define	SIMPLEQ_REMOVE(head, elm, type, field) do {			\
331c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	if ((head)->sqh_first == (elm)) {				\
332c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner		SIMPLEQ_REMOVE_HEAD((head), field);			\
333c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	} else {							\
334c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner		struct type *curelm = (head)->sqh_first;		\
335c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner		while (curelm->field.sqe_next != (elm))			\
336c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner			curelm = curelm->field.sqe_next;		\
337c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner		if ((curelm->field.sqe_next =				\
338c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner			curelm->field.sqe_next->field.sqe_next) == NULL) \
339c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner			    (head)->sqh_last = &(curelm)->field.sqe_next; \
340c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	}								\
341c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner} while (/*CONSTCOND*/0)
342c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner
343c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner#define	SIMPLEQ_FOREACH(var, head, field)				\
344c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	for ((var) = ((head)->sqh_first);				\
345c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner		(var);							\
346c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner		(var) = ((var)->field.sqe_next))
347c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner
348c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner/*
349c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner * Simple queue access methods.
350c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner */
351c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner#define	SIMPLEQ_EMPTY(head)		((head)->sqh_first == NULL)
352c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner#define	SIMPLEQ_FIRST(head)		((head)->sqh_first)
353c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner#define	SIMPLEQ_NEXT(elm, field)	((elm)->field.sqe_next)
354c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner
355c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner
356c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner/*
357c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner * Tail queue definitions.
358c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner */
359c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner#define	_TAILQ_HEAD(name, type, qual)					\
360c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turnerstruct name {								\
361c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	qual type *tqh_first;		/* first element */		\
362c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	qual type *qual *tqh_last;	/* addr of last next element */	\
363c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner}
364c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner#define TAILQ_HEAD(name, type)	_TAILQ_HEAD(name, struct type,)
365c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner
366c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner#define	TAILQ_HEAD_INITIALIZER(head)					\
367c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	{ NULL, &(head).tqh_first }
368c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner
369c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner#define	_TAILQ_ENTRY(type, qual)					\
370c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turnerstruct {								\
371c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	qual type *tqe_next;		/* next element */		\
372c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	qual type *qual *tqe_prev;	/* address of previous next element */\
373c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner}
374c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner#define TAILQ_ENTRY(type)	_TAILQ_ENTRY(struct type,)
375c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner
376c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner/*
377c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner * Tail queue functions.
378c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner */
379c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner#define	TAILQ_INIT(head) do {						\
380c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	(head)->tqh_first = NULL;					\
381c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	(head)->tqh_last = &(head)->tqh_first;				\
382c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner} while (/*CONSTCOND*/0)
383c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner
384c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner#define	TAILQ_INSERT_HEAD(head, elm, field) do {			\
385c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	if (((elm)->field.tqe_next = (head)->tqh_first) != NULL)	\
386c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner		(head)->tqh_first->field.tqe_prev =			\
387c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner		    &(elm)->field.tqe_next;				\
388c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	else								\
389c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner		(head)->tqh_last = &(elm)->field.tqe_next;		\
390c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	(head)->tqh_first = (elm);					\
391c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	(elm)->field.tqe_prev = &(head)->tqh_first;			\
392c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner} while (/*CONSTCOND*/0)
393c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner
394c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner#define	TAILQ_INSERT_TAIL(head, elm, field) do {			\
395c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	(elm)->field.tqe_next = NULL;					\
396c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	(elm)->field.tqe_prev = (head)->tqh_last;			\
397c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	*(head)->tqh_last = (elm);					\
398c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	(head)->tqh_last = &(elm)->field.tqe_next;			\
399c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner} while (/*CONSTCOND*/0)
400c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner
401c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner#define	TAILQ_INSERT_AFTER(head, listelm, elm, field) do {		\
402c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	if (((elm)->field.tqe_next = (listelm)->field.tqe_next) != NULL)\
403c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner		(elm)->field.tqe_next->field.tqe_prev = 		\
404c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner		    &(elm)->field.tqe_next;				\
405c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	else								\
406c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner		(head)->tqh_last = &(elm)->field.tqe_next;		\
407c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	(listelm)->field.tqe_next = (elm);				\
408c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	(elm)->field.tqe_prev = &(listelm)->field.tqe_next;		\
409c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner} while (/*CONSTCOND*/0)
410c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner
411c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner#define	TAILQ_INSERT_BEFORE(listelm, elm, field) do {			\
412c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	(elm)->field.tqe_prev = (listelm)->field.tqe_prev;		\
413c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	(elm)->field.tqe_next = (listelm);				\
414c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	*(listelm)->field.tqe_prev = (elm);				\
415c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	(listelm)->field.tqe_prev = &(elm)->field.tqe_next;		\
416c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner} while (/*CONSTCOND*/0)
417c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner
418c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner#define	TAILQ_REMOVE(head, elm, field) do {				\
419c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	if (((elm)->field.tqe_next) != NULL)				\
420c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner		(elm)->field.tqe_next->field.tqe_prev = 		\
421c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner		    (elm)->field.tqe_prev;				\
422c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	else								\
423c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner		(head)->tqh_last = (elm)->field.tqe_prev;		\
424c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	*(elm)->field.tqe_prev = (elm)->field.tqe_next;			\
425c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner} while (/*CONSTCOND*/0)
426c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner
427c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner#define	TAILQ_FOREACH(var, head, field)					\
428c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	for ((var) = ((head)->tqh_first);				\
429c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner		(var);							\
430c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner		(var) = ((var)->field.tqe_next))
431c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner
432c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner#define	TAILQ_FOREACH_REVERSE(var, head, headname, field)		\
433c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	for ((var) = (*(((struct headname *)((head)->tqh_last))->tqh_last));	\
434c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner		(var);							\
435c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner		(var) = (*(((struct headname *)((var)->field.tqe_prev))->tqh_last)))
436c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner
437c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner/*
438c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner * Tail queue access methods.
439c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner */
440c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner#define	TAILQ_EMPTY(head)		((head)->tqh_first == NULL)
441c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner#define	TAILQ_FIRST(head)		((head)->tqh_first)
442c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner#define	TAILQ_NEXT(elm, field)		((elm)->field.tqe_next)
443c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner
444c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner#define	TAILQ_LAST(head, headname) \
445c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	(*(((struct headname *)((head)->tqh_last))->tqh_last))
446c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner#define	TAILQ_PREV(elm, headname, field) \
447c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	(*(((struct headname *)((elm)->field.tqe_prev))->tqh_last))
448c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner
449c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner
450c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner/*
451c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner * Circular queue definitions.
452c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner */
453c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner#define	CIRCLEQ_HEAD(name, type)					\
454c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turnerstruct name {								\
455c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	struct type *cqh_first;		/* first element */		\
456c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	struct type *cqh_last;		/* last element */		\
457c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner}
458c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner
459c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner#define	CIRCLEQ_HEAD_INITIALIZER(head)					\
460c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	{ (void *)&head, (void *)&head }
461c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner
462c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner#define	CIRCLEQ_ENTRY(type)						\
463c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turnerstruct {								\
464c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	struct type *cqe_next;		/* next element */		\
465c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	struct type *cqe_prev;		/* previous element */		\
466c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner}
467c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner
468c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner/*
469c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner * Circular queue functions.
470c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner */
471c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner#define	CIRCLEQ_INIT(head) do {						\
472c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	(head)->cqh_first = (void *)(head);				\
473c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	(head)->cqh_last = (void *)(head);				\
474c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner} while (/*CONSTCOND*/0)
475c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner
476c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner#define	CIRCLEQ_INSERT_AFTER(head, listelm, elm, field) do {		\
477c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	(elm)->field.cqe_next = (listelm)->field.cqe_next;		\
478c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	(elm)->field.cqe_prev = (listelm);				\
479c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	if ((listelm)->field.cqe_next == (void *)(head))		\
480c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner		(head)->cqh_last = (elm);				\
481c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	else								\
482c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner		(listelm)->field.cqe_next->field.cqe_prev = (elm);	\
483c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	(listelm)->field.cqe_next = (elm);				\
484c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner} while (/*CONSTCOND*/0)
485c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner
486c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner#define	CIRCLEQ_INSERT_BEFORE(head, listelm, elm, field) do {		\
487c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	(elm)->field.cqe_next = (listelm);				\
488c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	(elm)->field.cqe_prev = (listelm)->field.cqe_prev;		\
489c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	if ((listelm)->field.cqe_prev == (void *)(head))		\
490c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner		(head)->cqh_first = (elm);				\
491c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	else								\
492c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner		(listelm)->field.cqe_prev->field.cqe_next = (elm);	\
493c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	(listelm)->field.cqe_prev = (elm);				\
494c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner} while (/*CONSTCOND*/0)
495c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner
496c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner#define	CIRCLEQ_INSERT_HEAD(head, elm, field) do {			\
497c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	(elm)->field.cqe_next = (head)->cqh_first;			\
498c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	(elm)->field.cqe_prev = (void *)(head);				\
499c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	if ((head)->cqh_last == (void *)(head))				\
500c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner		(head)->cqh_last = (elm);				\
501c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	else								\
502c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner		(head)->cqh_first->field.cqe_prev = (elm);		\
503c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	(head)->cqh_first = (elm);					\
504c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner} while (/*CONSTCOND*/0)
505c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner
506c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner#define	CIRCLEQ_INSERT_TAIL(head, elm, field) do {			\
507c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	(elm)->field.cqe_next = (void *)(head);				\
508c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	(elm)->field.cqe_prev = (head)->cqh_last;			\
509c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	if ((head)->cqh_first == (void *)(head))			\
510c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner		(head)->cqh_first = (elm);				\
511c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	else								\
512c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner		(head)->cqh_last->field.cqe_next = (elm);		\
513c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	(head)->cqh_last = (elm);					\
514c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner} while (/*CONSTCOND*/0)
515c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner
516c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner#define	CIRCLEQ_REMOVE(head, elm, field) do {				\
517c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	if ((elm)->field.cqe_next == (void *)(head))			\
518c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner		(head)->cqh_last = (elm)->field.cqe_prev;		\
519c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	else								\
520c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner		(elm)->field.cqe_next->field.cqe_prev =			\
521c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner		    (elm)->field.cqe_prev;				\
522c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	if ((elm)->field.cqe_prev == (void *)(head))			\
523c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner		(head)->cqh_first = (elm)->field.cqe_next;		\
524c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	else								\
525c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner		(elm)->field.cqe_prev->field.cqe_next =			\
526c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner		    (elm)->field.cqe_next;				\
527c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner} while (/*CONSTCOND*/0)
528c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner
529c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner#define	CIRCLEQ_FOREACH(var, head, field)				\
530c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	for ((var) = ((head)->cqh_first);				\
531c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner		(var) != (const void *)(head);				\
532c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner		(var) = ((var)->field.cqe_next))
533c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner
534c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner#define	CIRCLEQ_FOREACH_REVERSE(var, head, field)			\
535c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	for ((var) = ((head)->cqh_last);				\
536c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner		(var) != (const void *)(head);				\
537c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner		(var) = ((var)->field.cqe_prev))
538c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner
539c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner/*
540c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner * Circular queue access methods.
541c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner */
542c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner#define	CIRCLEQ_EMPTY(head)		((head)->cqh_first == (void *)(head))
543c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner#define	CIRCLEQ_FIRST(head)		((head)->cqh_first)
544c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner#define	CIRCLEQ_LAST(head)		((head)->cqh_last)
545c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner#define	CIRCLEQ_NEXT(elm, field)	((elm)->field.cqe_next)
546c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner#define	CIRCLEQ_PREV(elm, field)	((elm)->field.cqe_prev)
547c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner
548c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner#define CIRCLEQ_LOOP_NEXT(head, elm, field)				\
549c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	(((elm)->field.cqe_next == (void *)(head))			\
550c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	    ? ((head)->cqh_first)					\
551c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	    : (elm->field.cqe_next))
552c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner#define CIRCLEQ_LOOP_PREV(head, elm, field)				\
553c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	(((elm)->field.cqe_prev == (void *)(head))			\
554c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	    ? ((head)->cqh_last)					\
555c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner	    : (elm->field.cqe_prev))
556c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner
557c817c5210e4207908b83faaf08a2c5b95251f871David 'Digit' Turner#endif	/* sys/queue.h */
558