Blame


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