Initialize
[sdk/emulator/qemu.git] / qemu-queue.h
1 /*      $NetBSD: queue.h,v 1.52 2009/04/20 09:56:08 mschuett Exp $ */
2
3 /*
4  * Qemu version: Copy from netbsd, removed debug code, removed some of
5  * the implementations.  Left in lists, simple queues, tail queues and
6  * circular queues.
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
40 #ifndef QEMU_SYS_QUEUE_H_
41 #define QEMU_SYS_QUEUE_H_
42
43 /*
44  * This file defines four types of data structures:
45  * lists, simple queues, tail queues, and circular queues.
46  *
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.
53  *
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.
60  *
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.
67  *
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.
75  *
76  * For details on the use of these macros, see the queue(3) manual page.
77  */
78
79 /*
80  * List definitions.
81  */
82 #define QLIST_HEAD(name, type)                                          \
83 struct name {                                                           \
84         struct type *lh_first;  /* first element */                     \
85 }
86
87 #define QLIST_HEAD_INITIALIZER(head)                                    \
88         { NULL }
89
90 #define QLIST_ENTRY(type)                                               \
91 struct {                                                                \
92         struct type *le_next;   /* next element */                      \
93         struct type **le_prev;  /* address of previous next element */  \
94 }
95
96 /*
97  * List functions.
98  */
99 #define QLIST_INIT(head) do {                                           \
100         (head)->lh_first = NULL;                                        \
101 } while (/*CONSTCOND*/0)
102
103 #define QLIST_INSERT_AFTER(listelm, elm, field) do {                    \
104         if (((elm)->field.le_next = (listelm)->field.le_next) != NULL)  \
105                 (listelm)->field.le_next->field.le_prev =               \
106                     &(elm)->field.le_next;                              \
107         (listelm)->field.le_next = (elm);                               \
108         (elm)->field.le_prev = &(listelm)->field.le_next;               \
109 } while (/*CONSTCOND*/0)
110
111 #define QLIST_INSERT_BEFORE(listelm, elm, field) do {                   \
112         (elm)->field.le_prev = (listelm)->field.le_prev;                \
113         (elm)->field.le_next = (listelm);                               \
114         *(listelm)->field.le_prev = (elm);                              \
115         (listelm)->field.le_prev = &(elm)->field.le_next;               \
116 } while (/*CONSTCOND*/0)
117
118 #define QLIST_INSERT_HEAD(head, elm, field) do {                        \
119         if (((elm)->field.le_next = (head)->lh_first) != NULL)          \
120                 (head)->lh_first->field.le_prev = &(elm)->field.le_next;\
121         (head)->lh_first = (elm);                                       \
122         (elm)->field.le_prev = &(head)->lh_first;                       \
123 } while (/*CONSTCOND*/0)
124
125 #define QLIST_REMOVE(elm, field) do {                                   \
126         if ((elm)->field.le_next != NULL)                               \
127                 (elm)->field.le_next->field.le_prev =                   \
128                     (elm)->field.le_prev;                               \
129         *(elm)->field.le_prev = (elm)->field.le_next;                   \
130 } while (/*CONSTCOND*/0)
131
132 #define QLIST_FOREACH(var, head, field)                                 \
133         for ((var) = ((head)->lh_first);                                \
134                 (var);                                                  \
135                 (var) = ((var)->field.le_next))
136
137 #define QLIST_FOREACH_SAFE(var, head, field, next_var)                  \
138         for ((var) = ((head)->lh_first);                                \
139                 (var) && ((next_var) = ((var)->field.le_next), 1);      \
140                 (var) = (next_var))
141
142 /*
143  * List access methods.
144  */
145 #define QLIST_EMPTY(head)                ((head)->lh_first == NULL)
146 #define QLIST_FIRST(head)                ((head)->lh_first)
147 #define QLIST_NEXT(elm, field)           ((elm)->field.le_next)
148
149
150 /*
151  * Simple queue definitions.
152  */
153 #define QSIMPLEQ_HEAD(name, type)                                       \
154 struct name {                                                           \
155     struct type *sqh_first;    /* first element */                      \
156     struct type **sqh_last;    /* addr of last next element */          \
157 }
158
159 #define QSIMPLEQ_HEAD_INITIALIZER(head)                                 \
160     { NULL, &(head).sqh_first }
161
162 #define QSIMPLEQ_ENTRY(type)                                            \
163 struct {                                                                \
164     struct type *sqe_next;    /* next element */                        \
165 }
166
167 /*
168  * Simple queue functions.
169  */
170 #define QSIMPLEQ_INIT(head) do {                                        \
171     (head)->sqh_first = NULL;                                           \
172     (head)->sqh_last = &(head)->sqh_first;                              \
173 } while (/*CONSTCOND*/0)
174
175 #define QSIMPLEQ_INSERT_HEAD(head, elm, field) do {                     \
176     if (((elm)->field.sqe_next = (head)->sqh_first) == NULL)            \
177         (head)->sqh_last = &(elm)->field.sqe_next;                      \
178     (head)->sqh_first = (elm);                                          \
179 } while (/*CONSTCOND*/0)
180
181 #define QSIMPLEQ_INSERT_TAIL(head, elm, field) do {                     \
182     (elm)->field.sqe_next = NULL;                                       \
183     *(head)->sqh_last = (elm);                                          \
184     (head)->sqh_last = &(elm)->field.sqe_next;                          \
185 } while (/*CONSTCOND*/0)
186
187 #define QSIMPLEQ_INSERT_AFTER(head, listelm, elm, field) do {           \
188     if (((elm)->field.sqe_next = (listelm)->field.sqe_next) == NULL)    \
189         (head)->sqh_last = &(elm)->field.sqe_next;                      \
190     (listelm)->field.sqe_next = (elm);                                  \
191 } while (/*CONSTCOND*/0)
192
193 #define QSIMPLEQ_REMOVE_HEAD(head, field) do {                          \
194     if (((head)->sqh_first = (head)->sqh_first->field.sqe_next) == NULL)\
195         (head)->sqh_last = &(head)->sqh_first;                          \
196 } while (/*CONSTCOND*/0)
197
198 #define QSIMPLEQ_REMOVE(head, elm, type, field) do {                    \
199     if ((head)->sqh_first == (elm)) {                                   \
200         QSIMPLEQ_REMOVE_HEAD((head), field);                            \
201     } else {                                                            \
202         struct type *curelm = (head)->sqh_first;                        \
203         while (curelm->field.sqe_next != (elm))                         \
204             curelm = curelm->field.sqe_next;                            \
205         if ((curelm->field.sqe_next =                                   \
206             curelm->field.sqe_next->field.sqe_next) == NULL)            \
207                 (head)->sqh_last = &(curelm)->field.sqe_next;           \
208     }                                                                   \
209 } while (/*CONSTCOND*/0)
210
211 #define QSIMPLEQ_FOREACH(var, head, field)                              \
212     for ((var) = ((head)->sqh_first);                                   \
213         (var);                                                          \
214         (var) = ((var)->field.sqe_next))
215
216 #define QSIMPLEQ_FOREACH_SAFE(var, head, field, next)                   \
217     for ((var) = ((head)->sqh_first);                                   \
218         (var) && ((next = ((var)->field.sqe_next)), 1);                 \
219         (var) = (next))
220
221 #define QSIMPLEQ_CONCAT(head1, head2) do {                              \
222     if (!QSIMPLEQ_EMPTY((head2))) {                                     \
223         *(head1)->sqh_last = (head2)->sqh_first;                        \
224         (head1)->sqh_last = (head2)->sqh_last;                          \
225         QSIMPLEQ_INIT((head2));                                         \
226     }                                                                   \
227 } while (/*CONSTCOND*/0)
228
229 #define QSIMPLEQ_LAST(head, type, field)                                \
230     (QSIMPLEQ_EMPTY((head)) ?                                           \
231         NULL :                                                          \
232             ((struct type *)(void *)                                    \
233         ((char *)((head)->sqh_last) - offsetof(struct type, field))))
234
235 /*
236  * Simple queue access methods.
237  */
238 #define QSIMPLEQ_EMPTY(head)        ((head)->sqh_first == NULL)
239 #define QSIMPLEQ_FIRST(head)        ((head)->sqh_first)
240 #define QSIMPLEQ_NEXT(elm, field)   ((elm)->field.sqe_next)
241
242
243 /*
244  * Tail queue definitions.
245  */
246 #define Q_TAILQ_HEAD(name, type, qual)                                  \
247 struct name {                                                           \
248         qual type *tqh_first;           /* first element */             \
249         qual type *qual *tqh_last;      /* addr of last next element */ \
250 }
251 #define QTAILQ_HEAD(name, type)  Q_TAILQ_HEAD(name, struct type,)
252
253 #define QTAILQ_HEAD_INITIALIZER(head)                                   \
254         { NULL, &(head).tqh_first }
255
256 #define Q_TAILQ_ENTRY(type, qual)                                       \
257 struct {                                                                \
258         qual type *tqe_next;            /* next element */              \
259         qual type *qual *tqe_prev;      /* address of previous next element */\
260 }
261 #define QTAILQ_ENTRY(type)       Q_TAILQ_ENTRY(struct type,)
262
263 /*
264  * Tail queue functions.
265  */
266 #define QTAILQ_INIT(head) do {                                          \
267         (head)->tqh_first = NULL;                                       \
268         (head)->tqh_last = &(head)->tqh_first;                          \
269 } while (/*CONSTCOND*/0)
270
271 #define QTAILQ_INSERT_HEAD(head, elm, field) do {                       \
272         if (((elm)->field.tqe_next = (head)->tqh_first) != NULL)        \
273                 (head)->tqh_first->field.tqe_prev =                     \
274                     &(elm)->field.tqe_next;                             \
275         else                                                            \
276                 (head)->tqh_last = &(elm)->field.tqe_next;              \
277         (head)->tqh_first = (elm);                                      \
278         (elm)->field.tqe_prev = &(head)->tqh_first;                     \
279 } while (/*CONSTCOND*/0)
280
281 #define QTAILQ_INSERT_TAIL(head, elm, field) do {                       \
282         (elm)->field.tqe_next = NULL;                                   \
283         (elm)->field.tqe_prev = (head)->tqh_last;                       \
284         *(head)->tqh_last = (elm);                                      \
285         (head)->tqh_last = &(elm)->field.tqe_next;                      \
286 } while (/*CONSTCOND*/0)
287
288 #define QTAILQ_INSERT_AFTER(head, listelm, elm, field) do {             \
289         if (((elm)->field.tqe_next = (listelm)->field.tqe_next) != NULL)\
290                 (elm)->field.tqe_next->field.tqe_prev =                 \
291                     &(elm)->field.tqe_next;                             \
292         else                                                            \
293                 (head)->tqh_last = &(elm)->field.tqe_next;              \
294         (listelm)->field.tqe_next = (elm);                              \
295         (elm)->field.tqe_prev = &(listelm)->field.tqe_next;             \
296 } while (/*CONSTCOND*/0)
297
298 #define QTAILQ_INSERT_BEFORE(listelm, elm, field) do {                  \
299         (elm)->field.tqe_prev = (listelm)->field.tqe_prev;              \
300         (elm)->field.tqe_next = (listelm);                              \
301         *(listelm)->field.tqe_prev = (elm);                             \
302         (listelm)->field.tqe_prev = &(elm)->field.tqe_next;             \
303 } while (/*CONSTCOND*/0)
304
305 #define QTAILQ_REMOVE(head, elm, field) do {                            \
306         if (((elm)->field.tqe_next) != NULL)                            \
307                 (elm)->field.tqe_next->field.tqe_prev =                 \
308                     (elm)->field.tqe_prev;                              \
309         else                                                            \
310                 (head)->tqh_last = (elm)->field.tqe_prev;               \
311         *(elm)->field.tqe_prev = (elm)->field.tqe_next;                 \
312 } while (/*CONSTCOND*/0)
313
314 #define QTAILQ_FOREACH(var, head, field)                                \
315         for ((var) = ((head)->tqh_first);                               \
316                 (var);                                                  \
317                 (var) = ((var)->field.tqe_next))
318
319 #define QTAILQ_FOREACH_SAFE(var, head, field, next_var)                 \
320         for ((var) = ((head)->tqh_first);                               \
321                 (var) && ((next_var) = ((var)->field.tqe_next), 1);     \
322                 (var) = (next_var))
323
324 #define QTAILQ_FOREACH_REVERSE(var, head, headname, field)              \
325         for ((var) = (*(((struct headname *)((head)->tqh_last))->tqh_last));    \
326                 (var);                                                  \
327                 (var) = (*(((struct headname *)((var)->field.tqe_prev))->tqh_last)))
328
329 /*
330  * Tail queue access methods.
331  */
332 #define QTAILQ_EMPTY(head)               ((head)->tqh_first == NULL)
333 #define QTAILQ_FIRST(head)               ((head)->tqh_first)
334 #define QTAILQ_NEXT(elm, field)          ((elm)->field.tqe_next)
335
336 #define QTAILQ_LAST(head, headname) \
337         (*(((struct headname *)((head)->tqh_last))->tqh_last))
338 #define QTAILQ_PREV(elm, headname, field) \
339         (*(((struct headname *)((elm)->field.tqe_prev))->tqh_last))
340
341
342 /*
343  * Circular queue definitions.
344  */
345 #define QCIRCLEQ_HEAD(name, type)                                       \
346 struct name {                                                           \
347         struct type *cqh_first;         /* first element */             \
348         struct type *cqh_last;          /* last element */              \
349 }
350
351 #define QCIRCLEQ_HEAD_INITIALIZER(head)                                 \
352         { (void *)&head, (void *)&head }
353
354 #define QCIRCLEQ_ENTRY(type)                                            \
355 struct {                                                                \
356         struct type *cqe_next;          /* next element */              \
357         struct type *cqe_prev;          /* previous element */          \
358 }
359
360 /*
361  * Circular queue functions.
362  */
363 #define QCIRCLEQ_INIT(head) do {                                        \
364         (head)->cqh_first = (void *)(head);                             \
365         (head)->cqh_last = (void *)(head);                              \
366 } while (/*CONSTCOND*/0)
367
368 #define QCIRCLEQ_INSERT_AFTER(head, listelm, elm, field) do {           \
369         (elm)->field.cqe_next = (listelm)->field.cqe_next;              \
370         (elm)->field.cqe_prev = (listelm);                              \
371         if ((listelm)->field.cqe_next == (void *)(head))                \
372                 (head)->cqh_last = (elm);                               \
373         else                                                            \
374                 (listelm)->field.cqe_next->field.cqe_prev = (elm);      \
375         (listelm)->field.cqe_next = (elm);                              \
376 } while (/*CONSTCOND*/0)
377
378 #define QCIRCLEQ_INSERT_BEFORE(head, listelm, elm, field) do {          \
379         (elm)->field.cqe_next = (listelm);                              \
380         (elm)->field.cqe_prev = (listelm)->field.cqe_prev;              \
381         if ((listelm)->field.cqe_prev == (void *)(head))                \
382                 (head)->cqh_first = (elm);                              \
383         else                                                            \
384                 (listelm)->field.cqe_prev->field.cqe_next = (elm);      \
385         (listelm)->field.cqe_prev = (elm);                              \
386 } while (/*CONSTCOND*/0)
387
388 #define QCIRCLEQ_INSERT_HEAD(head, elm, field) do {                     \
389         (elm)->field.cqe_next = (head)->cqh_first;                      \
390         (elm)->field.cqe_prev = (void *)(head);                         \
391         if ((head)->cqh_last == (void *)(head))                         \
392                 (head)->cqh_last = (elm);                               \
393         else                                                            \
394                 (head)->cqh_first->field.cqe_prev = (elm);              \
395         (head)->cqh_first = (elm);                                      \
396 } while (/*CONSTCOND*/0)
397
398 #define QCIRCLEQ_INSERT_TAIL(head, elm, field) do {                     \
399         (elm)->field.cqe_next = (void *)(head);                         \
400         (elm)->field.cqe_prev = (head)->cqh_last;                       \
401         if ((head)->cqh_first == (void *)(head))                        \
402                 (head)->cqh_first = (elm);                              \
403         else                                                            \
404                 (head)->cqh_last->field.cqe_next = (elm);               \
405         (head)->cqh_last = (elm);                                       \
406 } while (/*CONSTCOND*/0)
407
408 #define QCIRCLEQ_REMOVE(head, elm, field) do {                          \
409         if ((elm)->field.cqe_next == (void *)(head))                    \
410                 (head)->cqh_last = (elm)->field.cqe_prev;               \
411         else                                                            \
412                 (elm)->field.cqe_next->field.cqe_prev =                 \
413                     (elm)->field.cqe_prev;                              \
414         if ((elm)->field.cqe_prev == (void *)(head))                    \
415                 (head)->cqh_first = (elm)->field.cqe_next;              \
416         else                                                            \
417                 (elm)->field.cqe_prev->field.cqe_next =                 \
418                     (elm)->field.cqe_next;                              \
419 } while (/*CONSTCOND*/0)
420
421 #define QCIRCLEQ_FOREACH(var, head, field)                              \
422         for ((var) = ((head)->cqh_first);                               \
423                 (var) != (const void *)(head);                          \
424                 (var) = ((var)->field.cqe_next))
425
426 #define QCIRCLEQ_FOREACH_REVERSE(var, head, field)                      \
427         for ((var) = ((head)->cqh_last);                                \
428                 (var) != (const void *)(head);                          \
429                 (var) = ((var)->field.cqe_prev))
430
431 /*
432  * Circular queue access methods.
433  */
434 #define QCIRCLEQ_EMPTY(head)             ((head)->cqh_first == (void *)(head))
435 #define QCIRCLEQ_FIRST(head)             ((head)->cqh_first)
436 #define QCIRCLEQ_LAST(head)              ((head)->cqh_last)
437 #define QCIRCLEQ_NEXT(elm, field)        ((elm)->field.cqe_next)
438 #define QCIRCLEQ_PREV(elm, field)        ((elm)->field.cqe_prev)
439
440 #define QCIRCLEQ_LOOP_NEXT(head, elm, field)                            \
441         (((elm)->field.cqe_next == (void *)(head))                      \
442             ? ((head)->cqh_first)                                       \
443             : (elm->field.cqe_next))
444 #define QCIRCLEQ_LOOP_PREV(head, elm, field)                            \
445         (((elm)->field.cqe_prev == (void *)(head))                      \
446             ? ((head)->cqh_last)                                        \
447             : (elm->field.cqe_prev))
448
449 #endif  /* !QEMU_SYS_QUEUE_H_ */