]>
Commit | Line | Data |
---|---|---|
c616bbe1 | 1 | /* $NetBSD: queue.h,v 1.52 2009/04/20 09:56:08 mschuett Exp $ */ |
fc56ef08 BS |
2 | |
3 | /* | |
4 | * Qemu version: Copy from netbsd, removed debug code, removed some of | |
6095aa88 PB |
5 | * the implementations. Left in singly-linked lists, lists, simple |
6 | * queues, tail queues and circular 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 | ||
72cf2d4f BS |
40 | #ifndef QEMU_SYS_QUEUE_H_ |
41 | #define QEMU_SYS_QUEUE_H_ | |
fc56ef08 BS |
42 | |
43 | /* | |
6095aa88 | 44 | * This file defines five types of data structures: singly-linked lists, |
c616bbe1 | 45 | * lists, simple queues, tail queues, and circular 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 | * | |
78 | * A circle queue is headed by a pair of pointers, one to the head of the | |
79 | * list and the other to the tail of the list. The elements are doubly | |
80 | * linked so that an arbitrary element can be removed without a need to | |
81 | * traverse the list. New elements can be added to the list before or after | |
82 | * an existing element, at the head of the list, or at the end of the list. | |
83 | * A circle queue may be traversed in either direction, but has a more | |
84 | * complex end of list detection. | |
85 | * | |
86 | * For details on the use of these macros, see the queue(3) manual page. | |
87 | */ | |
88 | ||
5f7d05ec HPB |
89 | #include "qemu-barrier.h" /* for smp_wmb() */ |
90 | ||
fc56ef08 BS |
91 | /* |
92 | * List definitions. | |
93 | */ | |
72cf2d4f | 94 | #define QLIST_HEAD(name, type) \ |
fc56ef08 BS |
95 | struct name { \ |
96 | struct type *lh_first; /* first element */ \ | |
97 | } | |
98 | ||
72cf2d4f | 99 | #define QLIST_HEAD_INITIALIZER(head) \ |
fc56ef08 BS |
100 | { NULL } |
101 | ||
72cf2d4f | 102 | #define QLIST_ENTRY(type) \ |
fc56ef08 BS |
103 | struct { \ |
104 | struct type *le_next; /* next element */ \ | |
105 | struct type **le_prev; /* address of previous next element */ \ | |
106 | } | |
107 | ||
108 | /* | |
109 | * List functions. | |
110 | */ | |
72cf2d4f | 111 | #define QLIST_INIT(head) do { \ |
fc56ef08 BS |
112 | (head)->lh_first = NULL; \ |
113 | } while (/*CONSTCOND*/0) | |
114 | ||
72cf2d4f | 115 | #define QLIST_INSERT_AFTER(listelm, elm, field) do { \ |
fc56ef08 BS |
116 | if (((elm)->field.le_next = (listelm)->field.le_next) != NULL) \ |
117 | (listelm)->field.le_next->field.le_prev = \ | |
118 | &(elm)->field.le_next; \ | |
119 | (listelm)->field.le_next = (elm); \ | |
120 | (elm)->field.le_prev = &(listelm)->field.le_next; \ | |
121 | } while (/*CONSTCOND*/0) | |
122 | ||
72cf2d4f | 123 | #define QLIST_INSERT_BEFORE(listelm, elm, field) do { \ |
fc56ef08 BS |
124 | (elm)->field.le_prev = (listelm)->field.le_prev; \ |
125 | (elm)->field.le_next = (listelm); \ | |
126 | *(listelm)->field.le_prev = (elm); \ | |
127 | (listelm)->field.le_prev = &(elm)->field.le_next; \ | |
128 | } while (/*CONSTCOND*/0) | |
129 | ||
72cf2d4f | 130 | #define QLIST_INSERT_HEAD(head, elm, field) do { \ |
fc56ef08 BS |
131 | if (((elm)->field.le_next = (head)->lh_first) != NULL) \ |
132 | (head)->lh_first->field.le_prev = &(elm)->field.le_next;\ | |
133 | (head)->lh_first = (elm); \ | |
134 | (elm)->field.le_prev = &(head)->lh_first; \ | |
135 | } while (/*CONSTCOND*/0) | |
136 | ||
5f7d05ec HPB |
137 | #define QLIST_INSERT_HEAD_RCU(head, elm, field) do { \ |
138 | (elm)->field.le_prev = &(head)->lh_first; \ | |
139 | (elm)->field.le_next = (head)->lh_first; \ | |
140 | smp_wmb(); /* fill elm before linking it */ \ | |
141 | if ((head)->lh_first != NULL) { \ | |
142 | (head)->lh_first->field.le_prev = &(elm)->field.le_next; \ | |
143 | } \ | |
144 | (head)->lh_first = (elm); \ | |
145 | smp_wmb(); \ | |
146 | } while (/* CONSTCOND*/0) | |
147 | ||
72cf2d4f | 148 | #define QLIST_REMOVE(elm, field) do { \ |
fc56ef08 BS |
149 | if ((elm)->field.le_next != NULL) \ |
150 | (elm)->field.le_next->field.le_prev = \ | |
151 | (elm)->field.le_prev; \ | |
152 | *(elm)->field.le_prev = (elm)->field.le_next; \ | |
153 | } while (/*CONSTCOND*/0) | |
154 | ||
72cf2d4f | 155 | #define QLIST_FOREACH(var, head, field) \ |
fc56ef08 BS |
156 | for ((var) = ((head)->lh_first); \ |
157 | (var); \ | |
158 | (var) = ((var)->field.le_next)) | |
159 | ||
72cf2d4f | 160 | #define QLIST_FOREACH_SAFE(var, head, field, next_var) \ |
bb150dc8 JQ |
161 | for ((var) = ((head)->lh_first); \ |
162 | (var) && ((next_var) = ((var)->field.le_next), 1); \ | |
163 | (var) = (next_var)) | |
164 | ||
fc56ef08 BS |
165 | /* |
166 | * List access methods. | |
167 | */ | |
72cf2d4f BS |
168 | #define QLIST_EMPTY(head) ((head)->lh_first == NULL) |
169 | #define QLIST_FIRST(head) ((head)->lh_first) | |
170 | #define QLIST_NEXT(elm, field) ((elm)->field.le_next) | |
fc56ef08 BS |
171 | |
172 | ||
6095aa88 PB |
173 | /* |
174 | * Singly-linked List definitions. | |
175 | */ | |
176 | #define QSLIST_HEAD(name, type) \ | |
177 | struct name { \ | |
178 | struct type *slh_first; /* first element */ \ | |
179 | } | |
180 | ||
181 | #define QSLIST_HEAD_INITIALIZER(head) \ | |
182 | { NULL } | |
183 | ||
184 | #define QSLIST_ENTRY(type) \ | |
185 | struct { \ | |
186 | struct type *sle_next; /* next element */ \ | |
187 | } | |
188 | ||
189 | /* | |
190 | * Singly-linked List functions. | |
191 | */ | |
192 | #define QSLIST_INIT(head) do { \ | |
193 | (head)->slh_first = NULL; \ | |
194 | } while (/*CONSTCOND*/0) | |
195 | ||
196 | #define QSLIST_INSERT_AFTER(slistelm, elm, field) do { \ | |
197 | (elm)->field.sle_next = (slistelm)->field.sle_next; \ | |
198 | (slistelm)->field.sle_next = (elm); \ | |
199 | } while (/*CONSTCOND*/0) | |
200 | ||
201 | #define QSLIST_INSERT_HEAD(head, elm, field) do { \ | |
202 | (elm)->field.sle_next = (head)->slh_first; \ | |
203 | (head)->slh_first = (elm); \ | |
204 | } while (/*CONSTCOND*/0) | |
205 | ||
206 | #define QSLIST_REMOVE_HEAD(head, field) do { \ | |
207 | (head)->slh_first = (head)->slh_first->field.sle_next; \ | |
208 | } while (/*CONSTCOND*/0) | |
209 | ||
210 | #define QSLIST_REMOVE_AFTER(slistelm, field) do { \ | |
211 | (slistelm)->field.sle_next = \ | |
212 | QSLIST_NEXT(QSLIST_NEXT((slistelm), field), field); \ | |
213 | } while (/*CONSTCOND*/0) | |
214 | ||
215 | #define QSLIST_FOREACH(var, head, field) \ | |
216 | for((var) = (head)->slh_first; (var); (var) = (var)->field.sle_next) | |
217 | ||
218 | #define QSLIST_FOREACH_SAFE(var, head, field, tvar) \ | |
219 | for ((var) = QSLIST_FIRST((head)); \ | |
220 | (var) && ((tvar) = QSLIST_NEXT((var), field), 1); \ | |
221 | (var) = (tvar)) | |
222 | ||
223 | /* | |
224 | * Singly-linked List access methods. | |
225 | */ | |
226 | #define QSLIST_EMPTY(head) ((head)->slh_first == NULL) | |
227 | #define QSLIST_FIRST(head) ((head)->slh_first) | |
228 | #define QSLIST_NEXT(elm, field) ((elm)->field.sle_next) | |
229 | ||
230 | ||
c616bbe1 PR |
231 | /* |
232 | * Simple queue definitions. | |
233 | */ | |
234 | #define QSIMPLEQ_HEAD(name, type) \ | |
235 | struct name { \ | |
236 | struct type *sqh_first; /* first element */ \ | |
237 | struct type **sqh_last; /* addr of last next element */ \ | |
238 | } | |
239 | ||
240 | #define QSIMPLEQ_HEAD_INITIALIZER(head) \ | |
241 | { NULL, &(head).sqh_first } | |
242 | ||
243 | #define QSIMPLEQ_ENTRY(type) \ | |
244 | struct { \ | |
245 | struct type *sqe_next; /* next element */ \ | |
246 | } | |
247 | ||
248 | /* | |
249 | * Simple queue functions. | |
250 | */ | |
251 | #define QSIMPLEQ_INIT(head) do { \ | |
252 | (head)->sqh_first = NULL; \ | |
253 | (head)->sqh_last = &(head)->sqh_first; \ | |
254 | } while (/*CONSTCOND*/0) | |
255 | ||
256 | #define QSIMPLEQ_INSERT_HEAD(head, elm, field) do { \ | |
257 | if (((elm)->field.sqe_next = (head)->sqh_first) == NULL) \ | |
258 | (head)->sqh_last = &(elm)->field.sqe_next; \ | |
259 | (head)->sqh_first = (elm); \ | |
260 | } while (/*CONSTCOND*/0) | |
261 | ||
262 | #define QSIMPLEQ_INSERT_TAIL(head, elm, field) do { \ | |
263 | (elm)->field.sqe_next = NULL; \ | |
264 | *(head)->sqh_last = (elm); \ | |
265 | (head)->sqh_last = &(elm)->field.sqe_next; \ | |
266 | } while (/*CONSTCOND*/0) | |
267 | ||
268 | #define QSIMPLEQ_INSERT_AFTER(head, listelm, elm, field) do { \ | |
269 | if (((elm)->field.sqe_next = (listelm)->field.sqe_next) == NULL) \ | |
270 | (head)->sqh_last = &(elm)->field.sqe_next; \ | |
271 | (listelm)->field.sqe_next = (elm); \ | |
272 | } while (/*CONSTCOND*/0) | |
273 | ||
274 | #define QSIMPLEQ_REMOVE_HEAD(head, field) do { \ | |
275 | if (((head)->sqh_first = (head)->sqh_first->field.sqe_next) == NULL)\ | |
276 | (head)->sqh_last = &(head)->sqh_first; \ | |
277 | } while (/*CONSTCOND*/0) | |
278 | ||
279 | #define QSIMPLEQ_REMOVE(head, elm, type, field) do { \ | |
280 | if ((head)->sqh_first == (elm)) { \ | |
281 | QSIMPLEQ_REMOVE_HEAD((head), field); \ | |
282 | } else { \ | |
283 | struct type *curelm = (head)->sqh_first; \ | |
284 | while (curelm->field.sqe_next != (elm)) \ | |
285 | curelm = curelm->field.sqe_next; \ | |
286 | if ((curelm->field.sqe_next = \ | |
287 | curelm->field.sqe_next->field.sqe_next) == NULL) \ | |
288 | (head)->sqh_last = &(curelm)->field.sqe_next; \ | |
289 | } \ | |
290 | } while (/*CONSTCOND*/0) | |
291 | ||
292 | #define QSIMPLEQ_FOREACH(var, head, field) \ | |
293 | for ((var) = ((head)->sqh_first); \ | |
294 | (var); \ | |
295 | (var) = ((var)->field.sqe_next)) | |
296 | ||
297 | #define QSIMPLEQ_FOREACH_SAFE(var, head, field, next) \ | |
298 | for ((var) = ((head)->sqh_first); \ | |
299 | (var) && ((next = ((var)->field.sqe_next)), 1); \ | |
300 | (var) = (next)) | |
301 | ||
302 | #define QSIMPLEQ_CONCAT(head1, head2) do { \ | |
303 | if (!QSIMPLEQ_EMPTY((head2))) { \ | |
304 | *(head1)->sqh_last = (head2)->sqh_first; \ | |
305 | (head1)->sqh_last = (head2)->sqh_last; \ | |
306 | QSIMPLEQ_INIT((head2)); \ | |
307 | } \ | |
308 | } while (/*CONSTCOND*/0) | |
309 | ||
310 | #define QSIMPLEQ_LAST(head, type, field) \ | |
311 | (QSIMPLEQ_EMPTY((head)) ? \ | |
312 | NULL : \ | |
313 | ((struct type *)(void *) \ | |
314 | ((char *)((head)->sqh_last) - offsetof(struct type, field)))) | |
315 | ||
316 | /* | |
317 | * Simple queue access methods. | |
318 | */ | |
319 | #define QSIMPLEQ_EMPTY(head) ((head)->sqh_first == NULL) | |
320 | #define QSIMPLEQ_FIRST(head) ((head)->sqh_first) | |
321 | #define QSIMPLEQ_NEXT(elm, field) ((elm)->field.sqe_next) | |
322 | ||
323 | ||
fc56ef08 BS |
324 | /* |
325 | * Tail queue definitions. | |
326 | */ | |
72cf2d4f | 327 | #define Q_TAILQ_HEAD(name, type, qual) \ |
fc56ef08 BS |
328 | struct name { \ |
329 | qual type *tqh_first; /* first element */ \ | |
330 | qual type *qual *tqh_last; /* addr of last next element */ \ | |
331 | } | |
72cf2d4f | 332 | #define QTAILQ_HEAD(name, type) Q_TAILQ_HEAD(name, struct type,) |
fc56ef08 | 333 | |
72cf2d4f | 334 | #define QTAILQ_HEAD_INITIALIZER(head) \ |
fc56ef08 BS |
335 | { NULL, &(head).tqh_first } |
336 | ||
72cf2d4f | 337 | #define Q_TAILQ_ENTRY(type, qual) \ |
fc56ef08 BS |
338 | struct { \ |
339 | qual type *tqe_next; /* next element */ \ | |
340 | qual type *qual *tqe_prev; /* address of previous next element */\ | |
341 | } | |
72cf2d4f | 342 | #define QTAILQ_ENTRY(type) Q_TAILQ_ENTRY(struct type,) |
fc56ef08 BS |
343 | |
344 | /* | |
345 | * Tail queue functions. | |
346 | */ | |
72cf2d4f | 347 | #define QTAILQ_INIT(head) do { \ |
fc56ef08 BS |
348 | (head)->tqh_first = NULL; \ |
349 | (head)->tqh_last = &(head)->tqh_first; \ | |
350 | } while (/*CONSTCOND*/0) | |
351 | ||
72cf2d4f | 352 | #define QTAILQ_INSERT_HEAD(head, elm, field) do { \ |
fc56ef08 BS |
353 | if (((elm)->field.tqe_next = (head)->tqh_first) != NULL) \ |
354 | (head)->tqh_first->field.tqe_prev = \ | |
355 | &(elm)->field.tqe_next; \ | |
356 | else \ | |
357 | (head)->tqh_last = &(elm)->field.tqe_next; \ | |
358 | (head)->tqh_first = (elm); \ | |
359 | (elm)->field.tqe_prev = &(head)->tqh_first; \ | |
360 | } while (/*CONSTCOND*/0) | |
361 | ||
72cf2d4f | 362 | #define QTAILQ_INSERT_TAIL(head, elm, field) do { \ |
fc56ef08 BS |
363 | (elm)->field.tqe_next = NULL; \ |
364 | (elm)->field.tqe_prev = (head)->tqh_last; \ | |
365 | *(head)->tqh_last = (elm); \ | |
366 | (head)->tqh_last = &(elm)->field.tqe_next; \ | |
367 | } while (/*CONSTCOND*/0) | |
368 | ||
72cf2d4f | 369 | #define QTAILQ_INSERT_AFTER(head, listelm, elm, field) do { \ |
fc56ef08 BS |
370 | if (((elm)->field.tqe_next = (listelm)->field.tqe_next) != NULL)\ |
371 | (elm)->field.tqe_next->field.tqe_prev = \ | |
372 | &(elm)->field.tqe_next; \ | |
373 | else \ | |
374 | (head)->tqh_last = &(elm)->field.tqe_next; \ | |
375 | (listelm)->field.tqe_next = (elm); \ | |
376 | (elm)->field.tqe_prev = &(listelm)->field.tqe_next; \ | |
377 | } while (/*CONSTCOND*/0) | |
378 | ||
72cf2d4f | 379 | #define QTAILQ_INSERT_BEFORE(listelm, elm, field) do { \ |
fc56ef08 BS |
380 | (elm)->field.tqe_prev = (listelm)->field.tqe_prev; \ |
381 | (elm)->field.tqe_next = (listelm); \ | |
382 | *(listelm)->field.tqe_prev = (elm); \ | |
383 | (listelm)->field.tqe_prev = &(elm)->field.tqe_next; \ | |
384 | } while (/*CONSTCOND*/0) | |
385 | ||
72cf2d4f | 386 | #define QTAILQ_REMOVE(head, elm, field) do { \ |
fc56ef08 BS |
387 | if (((elm)->field.tqe_next) != NULL) \ |
388 | (elm)->field.tqe_next->field.tqe_prev = \ | |
389 | (elm)->field.tqe_prev; \ | |
390 | else \ | |
391 | (head)->tqh_last = (elm)->field.tqe_prev; \ | |
392 | *(elm)->field.tqe_prev = (elm)->field.tqe_next; \ | |
393 | } while (/*CONSTCOND*/0) | |
394 | ||
72cf2d4f | 395 | #define QTAILQ_FOREACH(var, head, field) \ |
fc56ef08 BS |
396 | for ((var) = ((head)->tqh_first); \ |
397 | (var); \ | |
398 | (var) = ((var)->field.tqe_next)) | |
399 | ||
72cf2d4f | 400 | #define QTAILQ_FOREACH_SAFE(var, head, field, next_var) \ |
fc56ef08 BS |
401 | for ((var) = ((head)->tqh_first); \ |
402 | (var) && ((next_var) = ((var)->field.tqe_next), 1); \ | |
403 | (var) = (next_var)) | |
404 | ||
72cf2d4f | 405 | #define QTAILQ_FOREACH_REVERSE(var, head, headname, field) \ |
fc56ef08 BS |
406 | for ((var) = (*(((struct headname *)((head)->tqh_last))->tqh_last)); \ |
407 | (var); \ | |
408 | (var) = (*(((struct headname *)((var)->field.tqe_prev))->tqh_last))) | |
409 | ||
410 | /* | |
411 | * Tail queue access methods. | |
412 | */ | |
72cf2d4f BS |
413 | #define QTAILQ_EMPTY(head) ((head)->tqh_first == NULL) |
414 | #define QTAILQ_FIRST(head) ((head)->tqh_first) | |
415 | #define QTAILQ_NEXT(elm, field) ((elm)->field.tqe_next) | |
fc56ef08 | 416 | |
72cf2d4f | 417 | #define QTAILQ_LAST(head, headname) \ |
fc56ef08 | 418 | (*(((struct headname *)((head)->tqh_last))->tqh_last)) |
72cf2d4f | 419 | #define QTAILQ_PREV(elm, headname, field) \ |
fc56ef08 BS |
420 | (*(((struct headname *)((elm)->field.tqe_prev))->tqh_last)) |
421 | ||
422 | ||
423 | /* | |
424 | * Circular queue definitions. | |
425 | */ | |
72cf2d4f | 426 | #define QCIRCLEQ_HEAD(name, type) \ |
fc56ef08 BS |
427 | struct name { \ |
428 | struct type *cqh_first; /* first element */ \ | |
429 | struct type *cqh_last; /* last element */ \ | |
430 | } | |
431 | ||
72cf2d4f | 432 | #define QCIRCLEQ_HEAD_INITIALIZER(head) \ |
fc56ef08 BS |
433 | { (void *)&head, (void *)&head } |
434 | ||
72cf2d4f | 435 | #define QCIRCLEQ_ENTRY(type) \ |
fc56ef08 BS |
436 | struct { \ |
437 | struct type *cqe_next; /* next element */ \ | |
438 | struct type *cqe_prev; /* previous element */ \ | |
439 | } | |
440 | ||
441 | /* | |
442 | * Circular queue functions. | |
443 | */ | |
72cf2d4f | 444 | #define QCIRCLEQ_INIT(head) do { \ |
fc56ef08 BS |
445 | (head)->cqh_first = (void *)(head); \ |
446 | (head)->cqh_last = (void *)(head); \ | |
447 | } while (/*CONSTCOND*/0) | |
448 | ||
72cf2d4f | 449 | #define QCIRCLEQ_INSERT_AFTER(head, listelm, elm, field) do { \ |
fc56ef08 BS |
450 | (elm)->field.cqe_next = (listelm)->field.cqe_next; \ |
451 | (elm)->field.cqe_prev = (listelm); \ | |
452 | if ((listelm)->field.cqe_next == (void *)(head)) \ | |
453 | (head)->cqh_last = (elm); \ | |
454 | else \ | |
455 | (listelm)->field.cqe_next->field.cqe_prev = (elm); \ | |
456 | (listelm)->field.cqe_next = (elm); \ | |
457 | } while (/*CONSTCOND*/0) | |
458 | ||
72cf2d4f | 459 | #define QCIRCLEQ_INSERT_BEFORE(head, listelm, elm, field) do { \ |
fc56ef08 BS |
460 | (elm)->field.cqe_next = (listelm); \ |
461 | (elm)->field.cqe_prev = (listelm)->field.cqe_prev; \ | |
462 | if ((listelm)->field.cqe_prev == (void *)(head)) \ | |
463 | (head)->cqh_first = (elm); \ | |
464 | else \ | |
465 | (listelm)->field.cqe_prev->field.cqe_next = (elm); \ | |
466 | (listelm)->field.cqe_prev = (elm); \ | |
467 | } while (/*CONSTCOND*/0) | |
468 | ||
72cf2d4f | 469 | #define QCIRCLEQ_INSERT_HEAD(head, elm, field) do { \ |
fc56ef08 BS |
470 | (elm)->field.cqe_next = (head)->cqh_first; \ |
471 | (elm)->field.cqe_prev = (void *)(head); \ | |
472 | if ((head)->cqh_last == (void *)(head)) \ | |
473 | (head)->cqh_last = (elm); \ | |
474 | else \ | |
475 | (head)->cqh_first->field.cqe_prev = (elm); \ | |
476 | (head)->cqh_first = (elm); \ | |
477 | } while (/*CONSTCOND*/0) | |
478 | ||
72cf2d4f | 479 | #define QCIRCLEQ_INSERT_TAIL(head, elm, field) do { \ |
fc56ef08 BS |
480 | (elm)->field.cqe_next = (void *)(head); \ |
481 | (elm)->field.cqe_prev = (head)->cqh_last; \ | |
482 | if ((head)->cqh_first == (void *)(head)) \ | |
483 | (head)->cqh_first = (elm); \ | |
484 | else \ | |
485 | (head)->cqh_last->field.cqe_next = (elm); \ | |
486 | (head)->cqh_last = (elm); \ | |
487 | } while (/*CONSTCOND*/0) | |
488 | ||
72cf2d4f | 489 | #define QCIRCLEQ_REMOVE(head, elm, field) do { \ |
fc56ef08 BS |
490 | if ((elm)->field.cqe_next == (void *)(head)) \ |
491 | (head)->cqh_last = (elm)->field.cqe_prev; \ | |
492 | else \ | |
493 | (elm)->field.cqe_next->field.cqe_prev = \ | |
494 | (elm)->field.cqe_prev; \ | |
495 | if ((elm)->field.cqe_prev == (void *)(head)) \ | |
496 | (head)->cqh_first = (elm)->field.cqe_next; \ | |
497 | else \ | |
498 | (elm)->field.cqe_prev->field.cqe_next = \ | |
499 | (elm)->field.cqe_next; \ | |
500 | } while (/*CONSTCOND*/0) | |
501 | ||
72cf2d4f | 502 | #define QCIRCLEQ_FOREACH(var, head, field) \ |
fc56ef08 BS |
503 | for ((var) = ((head)->cqh_first); \ |
504 | (var) != (const void *)(head); \ | |
505 | (var) = ((var)->field.cqe_next)) | |
506 | ||
72cf2d4f | 507 | #define QCIRCLEQ_FOREACH_REVERSE(var, head, field) \ |
fc56ef08 BS |
508 | for ((var) = ((head)->cqh_last); \ |
509 | (var) != (const void *)(head); \ | |
510 | (var) = ((var)->field.cqe_prev)) | |
511 | ||
512 | /* | |
513 | * Circular queue access methods. | |
514 | */ | |
72cf2d4f BS |
515 | #define QCIRCLEQ_EMPTY(head) ((head)->cqh_first == (void *)(head)) |
516 | #define QCIRCLEQ_FIRST(head) ((head)->cqh_first) | |
517 | #define QCIRCLEQ_LAST(head) ((head)->cqh_last) | |
518 | #define QCIRCLEQ_NEXT(elm, field) ((elm)->field.cqe_next) | |
519 | #define QCIRCLEQ_PREV(elm, field) ((elm)->field.cqe_prev) | |
fc56ef08 | 520 | |
72cf2d4f | 521 | #define QCIRCLEQ_LOOP_NEXT(head, elm, field) \ |
fc56ef08 BS |
522 | (((elm)->field.cqe_next == (void *)(head)) \ |
523 | ? ((head)->cqh_first) \ | |
524 | : (elm->field.cqe_next)) | |
72cf2d4f | 525 | #define QCIRCLEQ_LOOP_PREV(head, elm, field) \ |
fc56ef08 BS |
526 | (((elm)->field.cqe_prev == (void *)(head)) \ |
527 | ? ((head)->cqh_last) \ | |
528 | : (elm->field.cqe_prev)) | |
529 | ||
72cf2d4f | 530 | #endif /* !QEMU_SYS_QUEUE_H_ */ |