]>
Commit | Line | Data |
---|---|---|
c616bbe1 | 1 | /* $NetBSD: queue.h,v 1.52 2009/04/20 09:56:08 mschuett Exp $ */ |
fc56ef08 BS |
2 | |
3 | /* | |
5cbdb3a3 | 4 | * QEMU version: Copy from netbsd, removed debug code, removed some of |
6095aa88 | 5 | * the implementations. Left in singly-linked lists, lists, simple |
cf904cfa | 6 | * queues, and tail queues. |
fc56ef08 BS |
7 | */ |
8 | ||
9 | /* | |
10 | * Copyright (c) 1991, 1993 | |
11 | * The Regents of the University of California. All rights reserved. | |
12 | * | |
13 | * Redistribution and use in source and binary forms, with or without | |
14 | * modification, are permitted provided that the following conditions | |
15 | * are met: | |
16 | * 1. Redistributions of source code must retain the above copyright | |
17 | * notice, this list of conditions and the following disclaimer. | |
18 | * 2. Redistributions in binary form must reproduce the above copyright | |
19 | * notice, this list of conditions and the following disclaimer in the | |
20 | * documentation and/or other materials provided with the distribution. | |
21 | * 3. Neither the name of the University nor the names of its contributors | |
22 | * may be used to endorse or promote products derived from this software | |
23 | * without specific prior written permission. | |
24 | * | |
25 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
26 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
27 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
28 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
29 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
30 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
31 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
32 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
33 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
34 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
35 | * SUCH DAMAGE. | |
36 | * | |
37 | * @(#)queue.h 8.5 (Berkeley) 8/20/94 | |
38 | */ | |
39 | ||
2a6a4076 MA |
40 | #ifndef QEMU_SYS_QUEUE_H |
41 | #define QEMU_SYS_QUEUE_H | |
fc56ef08 BS |
42 | |
43 | /* | |
cf904cfa PB |
44 | * This file defines four types of data structures: singly-linked lists, |
45 | * lists, simple queues, and tail queues. | |
fc56ef08 | 46 | * |
6095aa88 PB |
47 | * A singly-linked list is headed by a single forward pointer. The |
48 | * elements are singly linked for minimum space and pointer manipulation | |
49 | * overhead at the expense of O(n) removal for arbitrary elements. New | |
50 | * elements can be added to the list after an existing element or at the | |
51 | * head of the list. Elements being removed from the head of the list | |
52 | * should use the explicit macro for this purpose for optimum | |
53 | * efficiency. A singly-linked list may only be traversed in the forward | |
54 | * direction. Singly-linked lists are ideal for applications with large | |
55 | * datasets and few or no removals or for implementing a LIFO queue. | |
56 | * | |
fc56ef08 BS |
57 | * A list is headed by a single forward pointer (or an array of forward |
58 | * pointers for a hash table header). The elements are doubly linked | |
59 | * so that an arbitrary element can be removed without a need to | |
60 | * traverse the list. New elements can be added to the list before | |
61 | * or after an existing element or at the head of the list. A list | |
62 | * may only be traversed in the forward direction. | |
63 | * | |
c616bbe1 PR |
64 | * A simple queue is headed by a pair of pointers, one the head of the |
65 | * list and the other to the tail of the list. The elements are singly | |
66 | * linked to save space, so elements can only be removed from the | |
67 | * head of the list. New elements can be added to the list after | |
68 | * an existing element, at the head of the list, or at the end of the | |
69 | * list. A simple queue may only be traversed in the forward direction. | |
70 | * | |
fc56ef08 BS |
71 | * A tail queue is headed by a pair of pointers, one to the head of the |
72 | * list and the other to the tail of the list. The elements are doubly | |
73 | * linked so that an arbitrary element can be removed without a need to | |
74 | * traverse the list. New elements can be added to the list before or | |
75 | * after an existing element, at the head of the list, or at the end of | |
76 | * the list. A tail queue may be traversed in either direction. | |
77 | * | |
fc56ef08 BS |
78 | * For details on the use of these macros, see the queue(3) manual page. |
79 | */ | |
80 | ||
1de7afc9 | 81 | #include "qemu/atomic.h" /* for smp_wmb() */ |
5f7d05ec | 82 | |
fc56ef08 BS |
83 | /* |
84 | * List definitions. | |
85 | */ | |
72cf2d4f | 86 | #define QLIST_HEAD(name, type) \ |
fc56ef08 BS |
87 | struct name { \ |
88 | struct type *lh_first; /* first element */ \ | |
89 | } | |
90 | ||
72cf2d4f | 91 | #define QLIST_HEAD_INITIALIZER(head) \ |
fc56ef08 BS |
92 | { NULL } |
93 | ||
72cf2d4f | 94 | #define QLIST_ENTRY(type) \ |
fc56ef08 BS |
95 | struct { \ |
96 | struct type *le_next; /* next element */ \ | |
97 | struct type **le_prev; /* address of previous next element */ \ | |
98 | } | |
99 | ||
100 | /* | |
101 | * List functions. | |
102 | */ | |
72cf2d4f | 103 | #define QLIST_INIT(head) do { \ |
fc56ef08 BS |
104 | (head)->lh_first = NULL; \ |
105 | } while (/*CONSTCOND*/0) | |
7911747b PB |
106 | |
107 | #define QLIST_SWAP(dstlist, srclist, field) do { \ | |
108 | void *tmplist; \ | |
109 | tmplist = (srclist)->lh_first; \ | |
110 | (srclist)->lh_first = (dstlist)->lh_first; \ | |
111 | if ((srclist)->lh_first != NULL) { \ | |
112 | (srclist)->lh_first->field.le_prev = &(srclist)->lh_first; \ | |
113 | } \ | |
114 | (dstlist)->lh_first = tmplist; \ | |
115 | if ((dstlist)->lh_first != NULL) { \ | |
116 | (dstlist)->lh_first->field.le_prev = &(dstlist)->lh_first; \ | |
117 | } \ | |
118 | } while (/*CONSTCOND*/0) | |
fc56ef08 | 119 | |
72cf2d4f | 120 | #define QLIST_INSERT_AFTER(listelm, elm, field) do { \ |
fc56ef08 BS |
121 | if (((elm)->field.le_next = (listelm)->field.le_next) != NULL) \ |
122 | (listelm)->field.le_next->field.le_prev = \ | |
123 | &(elm)->field.le_next; \ | |
124 | (listelm)->field.le_next = (elm); \ | |
125 | (elm)->field.le_prev = &(listelm)->field.le_next; \ | |
126 | } while (/*CONSTCOND*/0) | |
127 | ||
72cf2d4f | 128 | #define QLIST_INSERT_BEFORE(listelm, elm, field) do { \ |
fc56ef08 BS |
129 | (elm)->field.le_prev = (listelm)->field.le_prev; \ |
130 | (elm)->field.le_next = (listelm); \ | |
131 | *(listelm)->field.le_prev = (elm); \ | |
132 | (listelm)->field.le_prev = &(elm)->field.le_next; \ | |
133 | } while (/*CONSTCOND*/0) | |
134 | ||
72cf2d4f | 135 | #define QLIST_INSERT_HEAD(head, elm, field) do { \ |
fc56ef08 BS |
136 | if (((elm)->field.le_next = (head)->lh_first) != NULL) \ |
137 | (head)->lh_first->field.le_prev = &(elm)->field.le_next;\ | |
138 | (head)->lh_first = (elm); \ | |
139 | (elm)->field.le_prev = &(head)->lh_first; \ | |
140 | } while (/*CONSTCOND*/0) | |
141 | ||
72cf2d4f | 142 | #define QLIST_REMOVE(elm, field) do { \ |
fc56ef08 BS |
143 | if ((elm)->field.le_next != NULL) \ |
144 | (elm)->field.le_next->field.le_prev = \ | |
145 | (elm)->field.le_prev; \ | |
146 | *(elm)->field.le_prev = (elm)->field.le_next; \ | |
147 | } while (/*CONSTCOND*/0) | |
148 | ||
72cf2d4f | 149 | #define QLIST_FOREACH(var, head, field) \ |
fc56ef08 BS |
150 | for ((var) = ((head)->lh_first); \ |
151 | (var); \ | |
152 | (var) = ((var)->field.le_next)) | |
153 | ||
72cf2d4f | 154 | #define QLIST_FOREACH_SAFE(var, head, field, next_var) \ |
bb150dc8 JQ |
155 | for ((var) = ((head)->lh_first); \ |
156 | (var) && ((next_var) = ((var)->field.le_next), 1); \ | |
157 | (var) = (next_var)) | |
158 | ||
fc56ef08 BS |
159 | /* |
160 | * List access methods. | |
161 | */ | |
72cf2d4f BS |
162 | #define QLIST_EMPTY(head) ((head)->lh_first == NULL) |
163 | #define QLIST_FIRST(head) ((head)->lh_first) | |
164 | #define QLIST_NEXT(elm, field) ((elm)->field.le_next) | |
fc56ef08 BS |
165 | |
166 | ||
6095aa88 PB |
167 | /* |
168 | * Singly-linked List definitions. | |
169 | */ | |
170 | #define QSLIST_HEAD(name, type) \ | |
171 | struct name { \ | |
172 | struct type *slh_first; /* first element */ \ | |
173 | } | |
174 | ||
175 | #define QSLIST_HEAD_INITIALIZER(head) \ | |
176 | { NULL } | |
177 | ||
178 | #define QSLIST_ENTRY(type) \ | |
179 | struct { \ | |
180 | struct type *sle_next; /* next element */ \ | |
181 | } | |
182 | ||
183 | /* | |
184 | * Singly-linked List functions. | |
185 | */ | |
186 | #define QSLIST_INIT(head) do { \ | |
187 | (head)->slh_first = NULL; \ | |
188 | } while (/*CONSTCOND*/0) | |
189 | ||
190 | #define QSLIST_INSERT_AFTER(slistelm, elm, field) do { \ | |
191 | (elm)->field.sle_next = (slistelm)->field.sle_next; \ | |
192 | (slistelm)->field.sle_next = (elm); \ | |
193 | } while (/*CONSTCOND*/0) | |
194 | ||
195 | #define QSLIST_INSERT_HEAD(head, elm, field) do { \ | |
c740ad92 PB |
196 | (elm)->field.sle_next = (head)->slh_first; \ |
197 | (head)->slh_first = (elm); \ | |
198 | } while (/*CONSTCOND*/0) | |
199 | ||
2120465f PB |
200 | #define QSLIST_INSERT_HEAD_ATOMIC(head, elm, field) do { \ |
201 | typeof(elm) save_sle_next; \ | |
202 | do { \ | |
203 | save_sle_next = (elm)->field.sle_next = (head)->slh_first; \ | |
204 | } while (atomic_cmpxchg(&(head)->slh_first, save_sle_next, (elm)) != \ | |
205 | save_sle_next); \ | |
c740ad92 PB |
206 | } while (/*CONSTCOND*/0) |
207 | ||
208 | #define QSLIST_MOVE_ATOMIC(dest, src) do { \ | |
209 | (dest)->slh_first = atomic_xchg(&(src)->slh_first, NULL); \ | |
6095aa88 PB |
210 | } while (/*CONSTCOND*/0) |
211 | ||
212 | #define QSLIST_REMOVE_HEAD(head, field) do { \ | |
213 | (head)->slh_first = (head)->slh_first->field.sle_next; \ | |
214 | } while (/*CONSTCOND*/0) | |
215 | ||
216 | #define QSLIST_REMOVE_AFTER(slistelm, field) do { \ | |
217 | (slistelm)->field.sle_next = \ | |
218 | QSLIST_NEXT(QSLIST_NEXT((slistelm), field), field); \ | |
219 | } while (/*CONSTCOND*/0) | |
220 | ||
221 | #define QSLIST_FOREACH(var, head, field) \ | |
222 | for((var) = (head)->slh_first; (var); (var) = (var)->field.sle_next) | |
223 | ||
224 | #define QSLIST_FOREACH_SAFE(var, head, field, tvar) \ | |
225 | for ((var) = QSLIST_FIRST((head)); \ | |
226 | (var) && ((tvar) = QSLIST_NEXT((var), field), 1); \ | |
227 | (var) = (tvar)) | |
228 | ||
229 | /* | |
230 | * Singly-linked List access methods. | |
231 | */ | |
232 | #define QSLIST_EMPTY(head) ((head)->slh_first == NULL) | |
233 | #define QSLIST_FIRST(head) ((head)->slh_first) | |
234 | #define QSLIST_NEXT(elm, field) ((elm)->field.sle_next) | |
235 | ||
236 | ||
c616bbe1 PR |
237 | /* |
238 | * Simple queue definitions. | |
239 | */ | |
240 | #define QSIMPLEQ_HEAD(name, type) \ | |
241 | struct name { \ | |
242 | struct type *sqh_first; /* first element */ \ | |
243 | struct type **sqh_last; /* addr of last next element */ \ | |
244 | } | |
245 | ||
246 | #define QSIMPLEQ_HEAD_INITIALIZER(head) \ | |
247 | { NULL, &(head).sqh_first } | |
248 | ||
249 | #define QSIMPLEQ_ENTRY(type) \ | |
250 | struct { \ | |
251 | struct type *sqe_next; /* next element */ \ | |
252 | } | |
253 | ||
254 | /* | |
255 | * Simple queue functions. | |
256 | */ | |
257 | #define QSIMPLEQ_INIT(head) do { \ | |
258 | (head)->sqh_first = NULL; \ | |
259 | (head)->sqh_last = &(head)->sqh_first; \ | |
260 | } while (/*CONSTCOND*/0) | |
261 | ||
262 | #define QSIMPLEQ_INSERT_HEAD(head, elm, field) do { \ | |
263 | if (((elm)->field.sqe_next = (head)->sqh_first) == NULL) \ | |
264 | (head)->sqh_last = &(elm)->field.sqe_next; \ | |
265 | (head)->sqh_first = (elm); \ | |
266 | } while (/*CONSTCOND*/0) | |
267 | ||
268 | #define QSIMPLEQ_INSERT_TAIL(head, elm, field) do { \ | |
269 | (elm)->field.sqe_next = NULL; \ | |
270 | *(head)->sqh_last = (elm); \ | |
271 | (head)->sqh_last = &(elm)->field.sqe_next; \ | |
272 | } while (/*CONSTCOND*/0) | |
273 | ||
274 | #define QSIMPLEQ_INSERT_AFTER(head, listelm, elm, field) do { \ | |
275 | if (((elm)->field.sqe_next = (listelm)->field.sqe_next) == NULL) \ | |
276 | (head)->sqh_last = &(elm)->field.sqe_next; \ | |
277 | (listelm)->field.sqe_next = (elm); \ | |
278 | } while (/*CONSTCOND*/0) | |
279 | ||
280 | #define QSIMPLEQ_REMOVE_HEAD(head, field) do { \ | |
281 | if (((head)->sqh_first = (head)->sqh_first->field.sqe_next) == NULL)\ | |
282 | (head)->sqh_last = &(head)->sqh_first; \ | |
283 | } while (/*CONSTCOND*/0) | |
284 | ||
82595da8 PB |
285 | #define QSIMPLEQ_SPLIT_AFTER(head, elm, field, removed) do { \ |
286 | QSIMPLEQ_INIT(removed); \ | |
287 | if (((removed)->sqh_first = (head)->sqh_first) != NULL) { \ | |
288 | if (((head)->sqh_first = (elm)->field.sqe_next) == NULL) { \ | |
289 | (head)->sqh_last = &(head)->sqh_first; \ | |
290 | } \ | |
291 | (removed)->sqh_last = &(elm)->field.sqe_next; \ | |
292 | (elm)->field.sqe_next = NULL; \ | |
293 | } \ | |
294 | } while (/*CONSTCOND*/0) | |
295 | ||
c616bbe1 PR |
296 | #define QSIMPLEQ_REMOVE(head, elm, type, field) do { \ |
297 | if ((head)->sqh_first == (elm)) { \ | |
298 | QSIMPLEQ_REMOVE_HEAD((head), field); \ | |
299 | } else { \ | |
300 | struct type *curelm = (head)->sqh_first; \ | |
301 | while (curelm->field.sqe_next != (elm)) \ | |
302 | curelm = curelm->field.sqe_next; \ | |
303 | if ((curelm->field.sqe_next = \ | |
304 | curelm->field.sqe_next->field.sqe_next) == NULL) \ | |
305 | (head)->sqh_last = &(curelm)->field.sqe_next; \ | |
306 | } \ | |
307 | } while (/*CONSTCOND*/0) | |
308 | ||
309 | #define QSIMPLEQ_FOREACH(var, head, field) \ | |
310 | for ((var) = ((head)->sqh_first); \ | |
311 | (var); \ | |
312 | (var) = ((var)->field.sqe_next)) | |
313 | ||
314 | #define QSIMPLEQ_FOREACH_SAFE(var, head, field, next) \ | |
315 | for ((var) = ((head)->sqh_first); \ | |
316 | (var) && ((next = ((var)->field.sqe_next)), 1); \ | |
317 | (var) = (next)) | |
318 | ||
319 | #define QSIMPLEQ_CONCAT(head1, head2) do { \ | |
320 | if (!QSIMPLEQ_EMPTY((head2))) { \ | |
321 | *(head1)->sqh_last = (head2)->sqh_first; \ | |
322 | (head1)->sqh_last = (head2)->sqh_last; \ | |
323 | QSIMPLEQ_INIT((head2)); \ | |
324 | } \ | |
325 | } while (/*CONSTCOND*/0) | |
326 | ||
67a74148 SH |
327 | #define QSIMPLEQ_PREPEND(head1, head2) do { \ |
328 | if (!QSIMPLEQ_EMPTY((head2))) { \ | |
329 | *(head2)->sqh_last = (head1)->sqh_first; \ | |
330 | (head1)->sqh_first = (head2)->sqh_first; \ | |
331 | QSIMPLEQ_INIT((head2)); \ | |
332 | } \ | |
333 | } while (/*CONSTCOND*/0) | |
334 | ||
c616bbe1 PR |
335 | #define QSIMPLEQ_LAST(head, type, field) \ |
336 | (QSIMPLEQ_EMPTY((head)) ? \ | |
337 | NULL : \ | |
338 | ((struct type *)(void *) \ | |
339 | ((char *)((head)->sqh_last) - offsetof(struct type, field)))) | |
340 | ||
341 | /* | |
342 | * Simple queue access methods. | |
343 | */ | |
ae526e32 | 344 | #define QSIMPLEQ_EMPTY_ATOMIC(head) (atomic_read(&((head)->sqh_first)) == NULL) |
c616bbe1 PR |
345 | #define QSIMPLEQ_EMPTY(head) ((head)->sqh_first == NULL) |
346 | #define QSIMPLEQ_FIRST(head) ((head)->sqh_first) | |
347 | #define QSIMPLEQ_NEXT(elm, field) ((elm)->field.sqe_next) | |
348 | ||
7274f01b PB |
349 | typedef struct QTailQLink { |
350 | void *tql_next; | |
351 | struct QTailQLink *tql_prev; | |
352 | } QTailQLink; | |
c616bbe1 | 353 | |
fc56ef08 | 354 | /* |
7274f01b PB |
355 | * Tail queue definitions. The union acts as a poor man template, as if |
356 | * it were QTailQLink<type>. | |
fc56ef08 | 357 | */ |
f95bb39c | 358 | #define QTAILQ_HEAD(name, type) \ |
7274f01b PB |
359 | union name { \ |
360 | struct type *tqh_first; /* first element */ \ | |
361 | QTailQLink tqh_circ; /* link for circular backwards list */ \ | |
fc56ef08 | 362 | } |
fc56ef08 | 363 | |
72cf2d4f | 364 | #define QTAILQ_HEAD_INITIALIZER(head) \ |
7274f01b | 365 | { .tqh_circ = { NULL, &(head).tqh_circ } } |
fc56ef08 | 366 | |
f95bb39c | 367 | #define QTAILQ_ENTRY(type) \ |
7274f01b PB |
368 | union { \ |
369 | struct type *tqe_next; /* next element */ \ | |
370 | QTailQLink tqe_circ; /* link for circular backwards list */ \ | |
fc56ef08 | 371 | } |
fc56ef08 BS |
372 | |
373 | /* | |
374 | * Tail queue functions. | |
375 | */ | |
72cf2d4f | 376 | #define QTAILQ_INIT(head) do { \ |
fc56ef08 | 377 | (head)->tqh_first = NULL; \ |
7274f01b | 378 | (head)->tqh_circ.tql_prev = &(head)->tqh_circ; \ |
fc56ef08 BS |
379 | } while (/*CONSTCOND*/0) |
380 | ||
72cf2d4f | 381 | #define QTAILQ_INSERT_HEAD(head, elm, field) do { \ |
fc56ef08 | 382 | if (((elm)->field.tqe_next = (head)->tqh_first) != NULL) \ |
7274f01b PB |
383 | (head)->tqh_first->field.tqe_circ.tql_prev = \ |
384 | &(elm)->field.tqe_circ; \ | |
fc56ef08 | 385 | else \ |
7274f01b | 386 | (head)->tqh_circ.tql_prev = &(elm)->field.tqe_circ; \ |
fc56ef08 | 387 | (head)->tqh_first = (elm); \ |
7274f01b | 388 | (elm)->field.tqe_circ.tql_prev = &(head)->tqh_circ; \ |
fc56ef08 BS |
389 | } while (/*CONSTCOND*/0) |
390 | ||
72cf2d4f | 391 | #define QTAILQ_INSERT_TAIL(head, elm, field) do { \ |
fc56ef08 | 392 | (elm)->field.tqe_next = NULL; \ |
7274f01b PB |
393 | (elm)->field.tqe_circ.tql_prev = (head)->tqh_circ.tql_prev; \ |
394 | (head)->tqh_circ.tql_prev->tql_next = (elm); \ | |
395 | (head)->tqh_circ.tql_prev = &(elm)->field.tqe_circ; \ | |
fc56ef08 BS |
396 | } while (/*CONSTCOND*/0) |
397 | ||
72cf2d4f | 398 | #define QTAILQ_INSERT_AFTER(head, listelm, elm, field) do { \ |
fc56ef08 | 399 | if (((elm)->field.tqe_next = (listelm)->field.tqe_next) != NULL)\ |
7274f01b PB |
400 | (elm)->field.tqe_next->field.tqe_circ.tql_prev = \ |
401 | &(elm)->field.tqe_circ; \ | |
fc56ef08 | 402 | else \ |
7274f01b | 403 | (head)->tqh_circ.tql_prev = &(elm)->field.tqe_circ; \ |
fc56ef08 | 404 | (listelm)->field.tqe_next = (elm); \ |
7274f01b | 405 | (elm)->field.tqe_circ.tql_prev = &(listelm)->field.tqe_circ; \ |
fc56ef08 BS |
406 | } while (/*CONSTCOND*/0) |
407 | ||
7274f01b PB |
408 | #define QTAILQ_INSERT_BEFORE(listelm, elm, field) do { \ |
409 | (elm)->field.tqe_circ.tql_prev = (listelm)->field.tqe_circ.tql_prev; \ | |
410 | (elm)->field.tqe_next = (listelm); \ | |
411 | (listelm)->field.tqe_circ.tql_prev->tql_next = (elm); \ | |
412 | (listelm)->field.tqe_circ.tql_prev = &(elm)->field.tqe_circ; \ | |
fc56ef08 BS |
413 | } while (/*CONSTCOND*/0) |
414 | ||
72cf2d4f | 415 | #define QTAILQ_REMOVE(head, elm, field) do { \ |
fc56ef08 | 416 | if (((elm)->field.tqe_next) != NULL) \ |
7274f01b PB |
417 | (elm)->field.tqe_next->field.tqe_circ.tql_prev = \ |
418 | (elm)->field.tqe_circ.tql_prev; \ | |
fc56ef08 | 419 | else \ |
7274f01b PB |
420 | (head)->tqh_circ.tql_prev = (elm)->field.tqe_circ.tql_prev; \ |
421 | (elm)->field.tqe_circ.tql_prev->tql_next = (elm)->field.tqe_next; \ | |
422 | (elm)->field.tqe_circ.tql_prev = NULL; \ | |
fc56ef08 BS |
423 | } while (/*CONSTCOND*/0) |
424 | ||
72cf2d4f | 425 | #define QTAILQ_FOREACH(var, head, field) \ |
fc56ef08 BS |
426 | for ((var) = ((head)->tqh_first); \ |
427 | (var); \ | |
428 | (var) = ((var)->field.tqe_next)) | |
429 | ||
72cf2d4f | 430 | #define QTAILQ_FOREACH_SAFE(var, head, field, next_var) \ |
fc56ef08 BS |
431 | for ((var) = ((head)->tqh_first); \ |
432 | (var) && ((next_var) = ((var)->field.tqe_next), 1); \ | |
433 | (var) = (next_var)) | |
434 | ||
eae3eb3e PB |
435 | #define QTAILQ_FOREACH_REVERSE(var, head, field) \ |
436 | for ((var) = QTAILQ_LAST(head); \ | |
fc56ef08 | 437 | (var); \ |
eae3eb3e | 438 | (var) = QTAILQ_PREV(var, field)) |
fc56ef08 | 439 | |
eae3eb3e PB |
440 | #define QTAILQ_FOREACH_REVERSE_SAFE(var, head, field, prev_var) \ |
441 | for ((var) = QTAILQ_LAST(head); \ | |
5ed76a4c | 442 | (var) && ((prev_var) = QTAILQ_PREV(var, field), 1); \ |
15fa08f8 RH |
443 | (var) = (prev_var)) |
444 | ||
fc56ef08 BS |
445 | /* |
446 | * Tail queue access methods. | |
447 | */ | |
72cf2d4f BS |
448 | #define QTAILQ_EMPTY(head) ((head)->tqh_first == NULL) |
449 | #define QTAILQ_FIRST(head) ((head)->tqh_first) | |
450 | #define QTAILQ_NEXT(elm, field) ((elm)->field.tqe_next) | |
7274f01b | 451 | #define QTAILQ_IN_USE(elm, field) ((elm)->field.tqe_circ.tql_prev != NULL) |
fc56ef08 | 452 | |
7274f01b PB |
453 | #define QTAILQ_LINK_PREV(link) \ |
454 | ((link).tql_prev->tql_prev->tql_next) | |
eae3eb3e | 455 | #define QTAILQ_LAST(head) \ |
7274f01b | 456 | ((typeof((head)->tqh_first)) QTAILQ_LINK_PREV((head)->tqh_circ)) |
eae3eb3e | 457 | #define QTAILQ_PREV(elm, field) \ |
7274f01b | 458 | ((typeof((elm)->field.tqe_next)) QTAILQ_LINK_PREV((elm)->field.tqe_circ)) |
fc56ef08 | 459 | |
94869d5c | 460 | #define field_at_offset(base, offset, type) \ |
7274f01b | 461 | ((type *) (((char *) (base)) + (offset))) |
94869d5c JD |
462 | |
463 | /* | |
7274f01b PB |
464 | * Raw access of elements of a tail queue head. Offsets are all zero |
465 | * because it's a union. | |
94869d5c JD |
466 | */ |
467 | #define QTAILQ_RAW_FIRST(head) \ | |
7274f01b PB |
468 | field_at_offset(head, 0, void *) |
469 | #define QTAILQ_RAW_TQH_CIRC(head) \ | |
470 | field_at_offset(head, 0, QTailQLink) | |
94869d5c JD |
471 | |
472 | /* | |
473 | * Raw access of elements of a tail entry | |
474 | */ | |
475 | #define QTAILQ_RAW_NEXT(elm, entry) \ | |
7274f01b PB |
476 | field_at_offset(elm, entry, void *) |
477 | #define QTAILQ_RAW_TQE_CIRC(elm, entry) \ | |
478 | field_at_offset(elm, entry, QTailQLink) | |
94869d5c | 479 | /* |
7274f01b | 480 | * Tail queue traversal using pointer arithmetic. |
94869d5c JD |
481 | */ |
482 | #define QTAILQ_RAW_FOREACH(elm, head, entry) \ | |
7274f01b | 483 | for ((elm) = *QTAILQ_RAW_FIRST(head); \ |
94869d5c | 484 | (elm); \ |
7274f01b | 485 | (elm) = *QTAILQ_RAW_NEXT(elm, entry)) |
94869d5c JD |
486 | /* |
487 | * Tail queue insertion using pointer arithmetic. | |
488 | */ | |
7274f01b PB |
489 | #define QTAILQ_RAW_INSERT_TAIL(head, elm, entry) do { \ |
490 | *QTAILQ_RAW_NEXT(elm, entry) = NULL; \ | |
491 | QTAILQ_RAW_TQE_CIRC(elm, entry)->tql_prev = QTAILQ_RAW_TQH_CIRC(head)->tql_prev; \ | |
492 | QTAILQ_RAW_TQH_CIRC(head)->tql_prev->tql_next = (elm); \ | |
493 | QTAILQ_RAW_TQH_CIRC(head)->tql_prev = QTAILQ_RAW_TQE_CIRC(elm, entry); \ | |
94869d5c JD |
494 | } while (/*CONSTCOND*/0) |
495 | ||
2a6a4076 | 496 | #endif /* QEMU_SYS_QUEUE_H */ |