1 /* $NetBSD: queue.h,v 1.52 2009/04/20 09:56:08 mschuett Exp $ */
4 * Qemu version: Copy from netbsd, removed debug code, removed some of
5 * the implementations. Left in lists, simple queues, tail queues and
10 * Copyright (c) 1991, 1993
11 * The Regents of the University of California. All rights reserved.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
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.
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
37 * @(#)queue.h 8.5 (Berkeley) 8/20/94
40 #ifndef QEMU_SYS_QUEUE_H_
41 #define QEMU_SYS_QUEUE_H_
44 * This file defines four types of data structures:
45 * lists, simple queues, tail queues, and circular queues.
47 * A list is headed by a single forward pointer (or an array of forward
48 * pointers for a hash table header). The elements are doubly linked
49 * so that an arbitrary element can be removed without a need to
50 * traverse the list. New elements can be added to the list before
51 * or after an existing element or at the head of the list. A list
52 * may only be traversed in the forward direction.
54 * A simple queue is headed by a pair of pointers, one the head of the
55 * list and the other to the tail of the list. The elements are singly
56 * linked to save space, so elements can only be removed from the
57 * head of the list. New elements can be added to the list after
58 * an existing element, at the head of the list, or at the end of the
59 * list. A simple queue may only be traversed in the forward direction.
61 * A tail queue is headed by a pair of pointers, one to the head of the
62 * list and the other to the tail of the list. The elements are doubly
63 * linked so that an arbitrary element can be removed without a need to
64 * traverse the list. New elements can be added to the list before or
65 * after an existing element, at the head of the list, or at the end of
66 * the list. A tail queue may be traversed in either direction.
68 * A circle queue is headed by a pair of pointers, one to the head of the
69 * list and the other to the tail of the list. The elements are doubly
70 * linked so that an arbitrary element can be removed without a need to
71 * traverse the list. New elements can be added to the list before or after
72 * an existing element, at the head of the list, or at the end of the list.
73 * A circle queue may be traversed in either direction, but has a more
74 * complex end of list detection.
76 * For details on the use of these macros, see the queue(3) manual page.
79 #include "qemu-barrier.h" /* for smp_wmb() */
84 #define QLIST_HEAD(name, type) \
86 struct type *lh_first; /* first element */ \
89 #define QLIST_HEAD_INITIALIZER(head) \
92 #define QLIST_ENTRY(type) \
94 struct type *le_next; /* next element */ \
95 struct type **le_prev; /* address of previous next element */ \
101 #define QLIST_INIT(head) do { \
102 (head)->lh_first = NULL; \
103 } while (/*CONSTCOND*/0)
105 #define QLIST_INSERT_AFTER(listelm, elm, field) do { \
106 if (((elm)->field.le_next = (listelm)->field.le_next) != NULL) \
107 (listelm)->field.le_next->field.le_prev = \
108 &(elm)->field.le_next; \
109 (listelm)->field.le_next = (elm); \
110 (elm)->field.le_prev = &(listelm)->field.le_next; \
111 } while (/*CONSTCOND*/0)
113 #define QLIST_INSERT_BEFORE(listelm, elm, field) do { \
114 (elm)->field.le_prev = (listelm)->field.le_prev; \
115 (elm)->field.le_next = (listelm); \
116 *(listelm)->field.le_prev = (elm); \
117 (listelm)->field.le_prev = &(elm)->field.le_next; \
118 } while (/*CONSTCOND*/0)
120 #define QLIST_INSERT_HEAD(head, elm, field) do { \
121 if (((elm)->field.le_next = (head)->lh_first) != NULL) \
122 (head)->lh_first->field.le_prev = &(elm)->field.le_next;\
123 (head)->lh_first = (elm); \
124 (elm)->field.le_prev = &(head)->lh_first; \
125 } while (/*CONSTCOND*/0)
127 #define QLIST_INSERT_HEAD_RCU(head, elm, field) do { \
128 (elm)->field.le_prev = &(head)->lh_first; \
129 (elm)->field.le_next = (head)->lh_first; \
130 smp_wmb(); /* fill elm before linking it */ \
131 if ((head)->lh_first != NULL) { \
132 (head)->lh_first->field.le_prev = &(elm)->field.le_next; \
134 (head)->lh_first = (elm); \
136 } while (/* CONSTCOND*/0)
138 #define QLIST_REMOVE(elm, field) do { \
139 if ((elm)->field.le_next != NULL) \
140 (elm)->field.le_next->field.le_prev = \
141 (elm)->field.le_prev; \
142 *(elm)->field.le_prev = (elm)->field.le_next; \
143 } while (/*CONSTCOND*/0)
145 #define QLIST_FOREACH(var, head, field) \
146 for ((var) = ((head)->lh_first); \
148 (var) = ((var)->field.le_next))
150 #define QLIST_FOREACH_SAFE(var, head, field, next_var) \
151 for ((var) = ((head)->lh_first); \
152 (var) && ((next_var) = ((var)->field.le_next), 1); \
156 * List access methods.
158 #define QLIST_EMPTY(head) ((head)->lh_first == NULL)
159 #define QLIST_FIRST(head) ((head)->lh_first)
160 #define QLIST_NEXT(elm, field) ((elm)->field.le_next)
164 * Simple queue definitions.
166 #define QSIMPLEQ_HEAD(name, type) \
168 struct type *sqh_first; /* first element */ \
169 struct type **sqh_last; /* addr of last next element */ \
172 #define QSIMPLEQ_HEAD_INITIALIZER(head) \
173 { NULL, &(head).sqh_first }
175 #define QSIMPLEQ_ENTRY(type) \
177 struct type *sqe_next; /* next element */ \
181 * Simple queue functions.
183 #define QSIMPLEQ_INIT(head) do { \
184 (head)->sqh_first = NULL; \
185 (head)->sqh_last = &(head)->sqh_first; \
186 } while (/*CONSTCOND*/0)
188 #define QSIMPLEQ_INSERT_HEAD(head, elm, field) do { \
189 if (((elm)->field.sqe_next = (head)->sqh_first) == NULL) \
190 (head)->sqh_last = &(elm)->field.sqe_next; \
191 (head)->sqh_first = (elm); \
192 } while (/*CONSTCOND*/0)
194 #define QSIMPLEQ_INSERT_TAIL(head, elm, field) do { \
195 (elm)->field.sqe_next = NULL; \
196 *(head)->sqh_last = (elm); \
197 (head)->sqh_last = &(elm)->field.sqe_next; \
198 } while (/*CONSTCOND*/0)
200 #define QSIMPLEQ_INSERT_AFTER(head, listelm, elm, field) do { \
201 if (((elm)->field.sqe_next = (listelm)->field.sqe_next) == NULL) \
202 (head)->sqh_last = &(elm)->field.sqe_next; \
203 (listelm)->field.sqe_next = (elm); \
204 } while (/*CONSTCOND*/0)
206 #define QSIMPLEQ_REMOVE_HEAD(head, field) do { \
207 if (((head)->sqh_first = (head)->sqh_first->field.sqe_next) == NULL)\
208 (head)->sqh_last = &(head)->sqh_first; \
209 } while (/*CONSTCOND*/0)
211 #define QSIMPLEQ_REMOVE(head, elm, type, field) do { \
212 if ((head)->sqh_first == (elm)) { \
213 QSIMPLEQ_REMOVE_HEAD((head), field); \
215 struct type *curelm = (head)->sqh_first; \
216 while (curelm->field.sqe_next != (elm)) \
217 curelm = curelm->field.sqe_next; \
218 if ((curelm->field.sqe_next = \
219 curelm->field.sqe_next->field.sqe_next) == NULL) \
220 (head)->sqh_last = &(curelm)->field.sqe_next; \
222 } while (/*CONSTCOND*/0)
224 #define QSIMPLEQ_FOREACH(var, head, field) \
225 for ((var) = ((head)->sqh_first); \
227 (var) = ((var)->field.sqe_next))
229 #define QSIMPLEQ_FOREACH_SAFE(var, head, field, next) \
230 for ((var) = ((head)->sqh_first); \
231 (var) && ((next = ((var)->field.sqe_next)), 1); \
234 #define QSIMPLEQ_CONCAT(head1, head2) do { \
235 if (!QSIMPLEQ_EMPTY((head2))) { \
236 *(head1)->sqh_last = (head2)->sqh_first; \
237 (head1)->sqh_last = (head2)->sqh_last; \
238 QSIMPLEQ_INIT((head2)); \
240 } while (/*CONSTCOND*/0)
242 #define QSIMPLEQ_LAST(head, type, field) \
243 (QSIMPLEQ_EMPTY((head)) ? \
245 ((struct type *)(void *) \
246 ((char *)((head)->sqh_last) - offsetof(struct type, field))))
249 * Simple queue access methods.
251 #define QSIMPLEQ_EMPTY(head) ((head)->sqh_first == NULL)
252 #define QSIMPLEQ_FIRST(head) ((head)->sqh_first)
253 #define QSIMPLEQ_NEXT(elm, field) ((elm)->field.sqe_next)
257 * Tail queue definitions.
259 #define Q_TAILQ_HEAD(name, type, qual) \
261 qual type *tqh_first; /* first element */ \
262 qual type *qual *tqh_last; /* addr of last next element */ \
264 #define QTAILQ_HEAD(name, type) Q_TAILQ_HEAD(name, struct type,)
266 #define QTAILQ_HEAD_INITIALIZER(head) \
267 { NULL, &(head).tqh_first }
269 #define Q_TAILQ_ENTRY(type, qual) \
271 qual type *tqe_next; /* next element */ \
272 qual type *qual *tqe_prev; /* address of previous next element */\
274 #define QTAILQ_ENTRY(type) Q_TAILQ_ENTRY(struct type,)
277 * Tail queue functions.
279 #define QTAILQ_INIT(head) do { \
280 (head)->tqh_first = NULL; \
281 (head)->tqh_last = &(head)->tqh_first; \
282 } while (/*CONSTCOND*/0)
284 #define QTAILQ_INSERT_HEAD(head, elm, field) do { \
285 if (((elm)->field.tqe_next = (head)->tqh_first) != NULL) \
286 (head)->tqh_first->field.tqe_prev = \
287 &(elm)->field.tqe_next; \
289 (head)->tqh_last = &(elm)->field.tqe_next; \
290 (head)->tqh_first = (elm); \
291 (elm)->field.tqe_prev = &(head)->tqh_first; \
292 } while (/*CONSTCOND*/0)
294 #define QTAILQ_INSERT_TAIL(head, elm, field) do { \
295 (elm)->field.tqe_next = NULL; \
296 (elm)->field.tqe_prev = (head)->tqh_last; \
297 *(head)->tqh_last = (elm); \
298 (head)->tqh_last = &(elm)->field.tqe_next; \
299 } while (/*CONSTCOND*/0)
301 #define QTAILQ_INSERT_AFTER(head, listelm, elm, field) do { \
302 if (((elm)->field.tqe_next = (listelm)->field.tqe_next) != NULL)\
303 (elm)->field.tqe_next->field.tqe_prev = \
304 &(elm)->field.tqe_next; \
306 (head)->tqh_last = &(elm)->field.tqe_next; \
307 (listelm)->field.tqe_next = (elm); \
308 (elm)->field.tqe_prev = &(listelm)->field.tqe_next; \
309 } while (/*CONSTCOND*/0)
311 #define QTAILQ_INSERT_BEFORE(listelm, elm, field) do { \
312 (elm)->field.tqe_prev = (listelm)->field.tqe_prev; \
313 (elm)->field.tqe_next = (listelm); \
314 *(listelm)->field.tqe_prev = (elm); \
315 (listelm)->field.tqe_prev = &(elm)->field.tqe_next; \
316 } while (/*CONSTCOND*/0)
318 #define QTAILQ_REMOVE(head, elm, field) do { \
319 if (((elm)->field.tqe_next) != NULL) \
320 (elm)->field.tqe_next->field.tqe_prev = \
321 (elm)->field.tqe_prev; \
323 (head)->tqh_last = (elm)->field.tqe_prev; \
324 *(elm)->field.tqe_prev = (elm)->field.tqe_next; \
325 } while (/*CONSTCOND*/0)
327 #define QTAILQ_FOREACH(var, head, field) \
328 for ((var) = ((head)->tqh_first); \
330 (var) = ((var)->field.tqe_next))
332 #define QTAILQ_FOREACH_SAFE(var, head, field, next_var) \
333 for ((var) = ((head)->tqh_first); \
334 (var) && ((next_var) = ((var)->field.tqe_next), 1); \
337 #define QTAILQ_FOREACH_REVERSE(var, head, headname, field) \
338 for ((var) = (*(((struct headname *)((head)->tqh_last))->tqh_last)); \
340 (var) = (*(((struct headname *)((var)->field.tqe_prev))->tqh_last)))
343 * Tail queue access methods.
345 #define QTAILQ_EMPTY(head) ((head)->tqh_first == NULL)
346 #define QTAILQ_FIRST(head) ((head)->tqh_first)
347 #define QTAILQ_NEXT(elm, field) ((elm)->field.tqe_next)
349 #define QTAILQ_LAST(head, headname) \
350 (*(((struct headname *)((head)->tqh_last))->tqh_last))
351 #define QTAILQ_PREV(elm, headname, field) \
352 (*(((struct headname *)((elm)->field.tqe_prev))->tqh_last))
356 * Circular queue definitions.
358 #define QCIRCLEQ_HEAD(name, type) \
360 struct type *cqh_first; /* first element */ \
361 struct type *cqh_last; /* last element */ \
364 #define QCIRCLEQ_HEAD_INITIALIZER(head) \
365 { (void *)&head, (void *)&head }
367 #define QCIRCLEQ_ENTRY(type) \
369 struct type *cqe_next; /* next element */ \
370 struct type *cqe_prev; /* previous element */ \
374 * Circular queue functions.
376 #define QCIRCLEQ_INIT(head) do { \
377 (head)->cqh_first = (void *)(head); \
378 (head)->cqh_last = (void *)(head); \
379 } while (/*CONSTCOND*/0)
381 #define QCIRCLEQ_INSERT_AFTER(head, listelm, elm, field) do { \
382 (elm)->field.cqe_next = (listelm)->field.cqe_next; \
383 (elm)->field.cqe_prev = (listelm); \
384 if ((listelm)->field.cqe_next == (void *)(head)) \
385 (head)->cqh_last = (elm); \
387 (listelm)->field.cqe_next->field.cqe_prev = (elm); \
388 (listelm)->field.cqe_next = (elm); \
389 } while (/*CONSTCOND*/0)
391 #define QCIRCLEQ_INSERT_BEFORE(head, listelm, elm, field) do { \
392 (elm)->field.cqe_next = (listelm); \
393 (elm)->field.cqe_prev = (listelm)->field.cqe_prev; \
394 if ((listelm)->field.cqe_prev == (void *)(head)) \
395 (head)->cqh_first = (elm); \
397 (listelm)->field.cqe_prev->field.cqe_next = (elm); \
398 (listelm)->field.cqe_prev = (elm); \
399 } while (/*CONSTCOND*/0)
401 #define QCIRCLEQ_INSERT_HEAD(head, elm, field) do { \
402 (elm)->field.cqe_next = (head)->cqh_first; \
403 (elm)->field.cqe_prev = (void *)(head); \
404 if ((head)->cqh_last == (void *)(head)) \
405 (head)->cqh_last = (elm); \
407 (head)->cqh_first->field.cqe_prev = (elm); \
408 (head)->cqh_first = (elm); \
409 } while (/*CONSTCOND*/0)
411 #define QCIRCLEQ_INSERT_TAIL(head, elm, field) do { \
412 (elm)->field.cqe_next = (void *)(head); \
413 (elm)->field.cqe_prev = (head)->cqh_last; \
414 if ((head)->cqh_first == (void *)(head)) \
415 (head)->cqh_first = (elm); \
417 (head)->cqh_last->field.cqe_next = (elm); \
418 (head)->cqh_last = (elm); \
419 } while (/*CONSTCOND*/0)
421 #define QCIRCLEQ_REMOVE(head, elm, field) do { \
422 if ((elm)->field.cqe_next == (void *)(head)) \
423 (head)->cqh_last = (elm)->field.cqe_prev; \
425 (elm)->field.cqe_next->field.cqe_prev = \
426 (elm)->field.cqe_prev; \
427 if ((elm)->field.cqe_prev == (void *)(head)) \
428 (head)->cqh_first = (elm)->field.cqe_next; \
430 (elm)->field.cqe_prev->field.cqe_next = \
431 (elm)->field.cqe_next; \
432 } while (/*CONSTCOND*/0)
434 #define QCIRCLEQ_FOREACH(var, head, field) \
435 for ((var) = ((head)->cqh_first); \
436 (var) != (const void *)(head); \
437 (var) = ((var)->field.cqe_next))
439 #define QCIRCLEQ_FOREACH_REVERSE(var, head, field) \
440 for ((var) = ((head)->cqh_last); \
441 (var) != (const void *)(head); \
442 (var) = ((var)->field.cqe_prev))
445 * Circular queue access methods.
447 #define QCIRCLEQ_EMPTY(head) ((head)->cqh_first == (void *)(head))
448 #define QCIRCLEQ_FIRST(head) ((head)->cqh_first)
449 #define QCIRCLEQ_LAST(head) ((head)->cqh_last)
450 #define QCIRCLEQ_NEXT(elm, field) ((elm)->field.cqe_next)
451 #define QCIRCLEQ_PREV(elm, field) ((elm)->field.cqe_prev)
453 #define QCIRCLEQ_LOOP_NEXT(head, elm, field) \
454 (((elm)->field.cqe_next == (void *)(head)) \
455 ? ((head)->cqh_first) \
456 : (elm->field.cqe_next))
457 #define QCIRCLEQ_LOOP_PREV(head, elm, field) \
458 (((elm)->field.cqe_prev == (void *)(head)) \
459 ? ((head)->cqh_last) \
460 : (elm->field.cqe_prev))
462 #endif /* !QEMU_SYS_QUEUE_H_ */