Statistics
| Branch: | Revision:

root / qemu-queue.h @ a74cdab4

History | View | Annotate | Download (22 kB)

1 c616bbe1 Pierre Riteau
/*      $NetBSD: queue.h,v 1.52 2009/04/20 09:56:08 mschuett Exp $ */
2 fc56ef08 blueswir1
3 fc56ef08 blueswir1
/*
4 fc56ef08 blueswir1
 * Qemu version: Copy from netbsd, removed debug code, removed some of
5 c616bbe1 Pierre Riteau
 * the implementations.  Left in lists, simple queues, tail queues and
6 c616bbe1 Pierre Riteau
 * circular queues.
7 fc56ef08 blueswir1
 */
8 fc56ef08 blueswir1
9 fc56ef08 blueswir1
/*
10 fc56ef08 blueswir1
 * Copyright (c) 1991, 1993
11 fc56ef08 blueswir1
 *      The Regents of the University of California.  All rights reserved.
12 fc56ef08 blueswir1
 *
13 fc56ef08 blueswir1
 * Redistribution and use in source and binary forms, with or without
14 fc56ef08 blueswir1
 * modification, are permitted provided that the following conditions
15 fc56ef08 blueswir1
 * are met:
16 fc56ef08 blueswir1
 * 1. Redistributions of source code must retain the above copyright
17 fc56ef08 blueswir1
 *    notice, this list of conditions and the following disclaimer.
18 fc56ef08 blueswir1
 * 2. Redistributions in binary form must reproduce the above copyright
19 fc56ef08 blueswir1
 *    notice, this list of conditions and the following disclaimer in the
20 fc56ef08 blueswir1
 *    documentation and/or other materials provided with the distribution.
21 fc56ef08 blueswir1
 * 3. Neither the name of the University nor the names of its contributors
22 fc56ef08 blueswir1
 *    may be used to endorse or promote products derived from this software
23 fc56ef08 blueswir1
 *    without specific prior written permission.
24 fc56ef08 blueswir1
 *
25 fc56ef08 blueswir1
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 fc56ef08 blueswir1
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 fc56ef08 blueswir1
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 fc56ef08 blueswir1
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 fc56ef08 blueswir1
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 fc56ef08 blueswir1
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 fc56ef08 blueswir1
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 fc56ef08 blueswir1
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 fc56ef08 blueswir1
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 fc56ef08 blueswir1
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 fc56ef08 blueswir1
 * SUCH DAMAGE.
36 fc56ef08 blueswir1
 *
37 fc56ef08 blueswir1
 *      @(#)queue.h     8.5 (Berkeley) 8/20/94
38 fc56ef08 blueswir1
 */
39 fc56ef08 blueswir1
40 72cf2d4f Blue Swirl
#ifndef QEMU_SYS_QUEUE_H_
41 72cf2d4f Blue Swirl
#define QEMU_SYS_QUEUE_H_
42 fc56ef08 blueswir1
43 fc56ef08 blueswir1
/*
44 c616bbe1 Pierre Riteau
 * This file defines four types of data structures:
45 c616bbe1 Pierre Riteau
 * lists, simple queues, tail queues, and circular queues.
46 fc56ef08 blueswir1
 *
47 fc56ef08 blueswir1
 * A list is headed by a single forward pointer (or an array of forward
48 fc56ef08 blueswir1
 * pointers for a hash table header). The elements are doubly linked
49 fc56ef08 blueswir1
 * so that an arbitrary element can be removed without a need to
50 fc56ef08 blueswir1
 * traverse the list. New elements can be added to the list before
51 fc56ef08 blueswir1
 * or after an existing element or at the head of the list. A list
52 fc56ef08 blueswir1
 * may only be traversed in the forward direction.
53 fc56ef08 blueswir1
 *
54 c616bbe1 Pierre Riteau
 * A simple queue is headed by a pair of pointers, one the head of the
55 c616bbe1 Pierre Riteau
 * list and the other to the tail of the list. The elements are singly
56 c616bbe1 Pierre Riteau
 * linked to save space, so elements can only be removed from the
57 c616bbe1 Pierre Riteau
 * head of the list. New elements can be added to the list after
58 c616bbe1 Pierre Riteau
 * an existing element, at the head of the list, or at the end of the
59 c616bbe1 Pierre Riteau
 * list. A simple queue may only be traversed in the forward direction.
60 c616bbe1 Pierre Riteau
 *
61 fc56ef08 blueswir1
 * A tail queue is headed by a pair of pointers, one to the head of the
62 fc56ef08 blueswir1
 * list and the other to the tail of the list. The elements are doubly
63 fc56ef08 blueswir1
 * linked so that an arbitrary element can be removed without a need to
64 fc56ef08 blueswir1
 * traverse the list. New elements can be added to the list before or
65 fc56ef08 blueswir1
 * after an existing element, at the head of the list, or at the end of
66 fc56ef08 blueswir1
 * the list. A tail queue may be traversed in either direction.
67 fc56ef08 blueswir1
 *
68 fc56ef08 blueswir1
 * A circle queue is headed by a pair of pointers, one to the head of the
69 fc56ef08 blueswir1
 * list and the other to the tail of the list. The elements are doubly
70 fc56ef08 blueswir1
 * linked so that an arbitrary element can be removed without a need to
71 fc56ef08 blueswir1
 * traverse the list. New elements can be added to the list before or after
72 fc56ef08 blueswir1
 * an existing element, at the head of the list, or at the end of the list.
73 fc56ef08 blueswir1
 * A circle queue may be traversed in either direction, but has a more
74 fc56ef08 blueswir1
 * complex end of list detection.
75 fc56ef08 blueswir1
 *
76 fc56ef08 blueswir1
 * For details on the use of these macros, see the queue(3) manual page.
77 fc56ef08 blueswir1
 */
78 fc56ef08 blueswir1
79 fc56ef08 blueswir1
/*
80 fc56ef08 blueswir1
 * List definitions.
81 fc56ef08 blueswir1
 */
82 72cf2d4f Blue Swirl
#define QLIST_HEAD(name, type)                                          \
83 fc56ef08 blueswir1
struct name {                                                           \
84 fc56ef08 blueswir1
        struct type *lh_first;  /* first element */                     \
85 fc56ef08 blueswir1
}
86 fc56ef08 blueswir1
87 72cf2d4f Blue Swirl
#define QLIST_HEAD_INITIALIZER(head)                                    \
88 fc56ef08 blueswir1
        { NULL }
89 fc56ef08 blueswir1
90 72cf2d4f Blue Swirl
#define QLIST_ENTRY(type)                                               \
91 fc56ef08 blueswir1
struct {                                                                \
92 fc56ef08 blueswir1
        struct type *le_next;   /* next element */                      \
93 fc56ef08 blueswir1
        struct type **le_prev;  /* address of previous next element */  \
94 fc56ef08 blueswir1
}
95 fc56ef08 blueswir1
96 fc56ef08 blueswir1
/*
97 fc56ef08 blueswir1
 * List functions.
98 fc56ef08 blueswir1
 */
99 72cf2d4f Blue Swirl
#define QLIST_INIT(head) do {                                           \
100 fc56ef08 blueswir1
        (head)->lh_first = NULL;                                        \
101 fc56ef08 blueswir1
} while (/*CONSTCOND*/0)
102 fc56ef08 blueswir1
103 72cf2d4f Blue Swirl
#define QLIST_INSERT_AFTER(listelm, elm, field) do {                    \
104 fc56ef08 blueswir1
        if (((elm)->field.le_next = (listelm)->field.le_next) != NULL)  \
105 fc56ef08 blueswir1
                (listelm)->field.le_next->field.le_prev =               \
106 fc56ef08 blueswir1
                    &(elm)->field.le_next;                              \
107 fc56ef08 blueswir1
        (listelm)->field.le_next = (elm);                               \
108 fc56ef08 blueswir1
        (elm)->field.le_prev = &(listelm)->field.le_next;               \
109 fc56ef08 blueswir1
} while (/*CONSTCOND*/0)
110 fc56ef08 blueswir1
111 72cf2d4f Blue Swirl
#define QLIST_INSERT_BEFORE(listelm, elm, field) do {                   \
112 fc56ef08 blueswir1
        (elm)->field.le_prev = (listelm)->field.le_prev;                \
113 fc56ef08 blueswir1
        (elm)->field.le_next = (listelm);                               \
114 fc56ef08 blueswir1
        *(listelm)->field.le_prev = (elm);                              \
115 fc56ef08 blueswir1
        (listelm)->field.le_prev = &(elm)->field.le_next;               \
116 fc56ef08 blueswir1
} while (/*CONSTCOND*/0)
117 fc56ef08 blueswir1
118 72cf2d4f Blue Swirl
#define QLIST_INSERT_HEAD(head, elm, field) do {                        \
119 fc56ef08 blueswir1
        if (((elm)->field.le_next = (head)->lh_first) != NULL)          \
120 fc56ef08 blueswir1
                (head)->lh_first->field.le_prev = &(elm)->field.le_next;\
121 fc56ef08 blueswir1
        (head)->lh_first = (elm);                                       \
122 fc56ef08 blueswir1
        (elm)->field.le_prev = &(head)->lh_first;                       \
123 fc56ef08 blueswir1
} while (/*CONSTCOND*/0)
124 fc56ef08 blueswir1
125 72cf2d4f Blue Swirl
#define QLIST_REMOVE(elm, field) do {                                   \
126 fc56ef08 blueswir1
        if ((elm)->field.le_next != NULL)                               \
127 fc56ef08 blueswir1
                (elm)->field.le_next->field.le_prev =                   \
128 fc56ef08 blueswir1
                    (elm)->field.le_prev;                               \
129 fc56ef08 blueswir1
        *(elm)->field.le_prev = (elm)->field.le_next;                   \
130 fc56ef08 blueswir1
} while (/*CONSTCOND*/0)
131 fc56ef08 blueswir1
132 72cf2d4f Blue Swirl
#define QLIST_FOREACH(var, head, field)                                 \
133 fc56ef08 blueswir1
        for ((var) = ((head)->lh_first);                                \
134 fc56ef08 blueswir1
                (var);                                                  \
135 fc56ef08 blueswir1
                (var) = ((var)->field.le_next))
136 fc56ef08 blueswir1
137 72cf2d4f Blue Swirl
#define QLIST_FOREACH_SAFE(var, head, field, next_var)                  \
138 bb150dc8 Juan Quintela
        for ((var) = ((head)->lh_first);                                \
139 bb150dc8 Juan Quintela
                (var) && ((next_var) = ((var)->field.le_next), 1);      \
140 bb150dc8 Juan Quintela
                (var) = (next_var))
141 bb150dc8 Juan Quintela
142 fc56ef08 blueswir1
/*
143 fc56ef08 blueswir1
 * List access methods.
144 fc56ef08 blueswir1
 */
145 72cf2d4f Blue Swirl
#define QLIST_EMPTY(head)                ((head)->lh_first == NULL)
146 72cf2d4f Blue Swirl
#define QLIST_FIRST(head)                ((head)->lh_first)
147 72cf2d4f Blue Swirl
#define QLIST_NEXT(elm, field)           ((elm)->field.le_next)
148 fc56ef08 blueswir1
149 fc56ef08 blueswir1
150 fc56ef08 blueswir1
/*
151 c616bbe1 Pierre Riteau
 * Simple queue definitions.
152 c616bbe1 Pierre Riteau
 */
153 c616bbe1 Pierre Riteau
#define QSIMPLEQ_HEAD(name, type)                                       \
154 c616bbe1 Pierre Riteau
struct name {                                                           \
155 c616bbe1 Pierre Riteau
    struct type *sqh_first;    /* first element */                      \
156 c616bbe1 Pierre Riteau
    struct type **sqh_last;    /* addr of last next element */          \
157 c616bbe1 Pierre Riteau
}
158 c616bbe1 Pierre Riteau
159 c616bbe1 Pierre Riteau
#define QSIMPLEQ_HEAD_INITIALIZER(head)                                 \
160 c616bbe1 Pierre Riteau
    { NULL, &(head).sqh_first }
161 c616bbe1 Pierre Riteau
162 c616bbe1 Pierre Riteau
#define QSIMPLEQ_ENTRY(type)                                            \
163 c616bbe1 Pierre Riteau
struct {                                                                \
164 c616bbe1 Pierre Riteau
    struct type *sqe_next;    /* next element */                        \
165 c616bbe1 Pierre Riteau
}
166 c616bbe1 Pierre Riteau
167 c616bbe1 Pierre Riteau
/*
168 c616bbe1 Pierre Riteau
 * Simple queue functions.
169 c616bbe1 Pierre Riteau
 */
170 c616bbe1 Pierre Riteau
#define QSIMPLEQ_INIT(head) do {                                        \
171 c616bbe1 Pierre Riteau
    (head)->sqh_first = NULL;                                           \
172 c616bbe1 Pierre Riteau
    (head)->sqh_last = &(head)->sqh_first;                              \
173 c616bbe1 Pierre Riteau
} while (/*CONSTCOND*/0)
174 c616bbe1 Pierre Riteau
175 c616bbe1 Pierre Riteau
#define QSIMPLEQ_INSERT_HEAD(head, elm, field) do {                     \
176 c616bbe1 Pierre Riteau
    if (((elm)->field.sqe_next = (head)->sqh_first) == NULL)            \
177 c616bbe1 Pierre Riteau
        (head)->sqh_last = &(elm)->field.sqe_next;                      \
178 c616bbe1 Pierre Riteau
    (head)->sqh_first = (elm);                                          \
179 c616bbe1 Pierre Riteau
} while (/*CONSTCOND*/0)
180 c616bbe1 Pierre Riteau
181 c616bbe1 Pierre Riteau
#define QSIMPLEQ_INSERT_TAIL(head, elm, field) do {                     \
182 c616bbe1 Pierre Riteau
    (elm)->field.sqe_next = NULL;                                       \
183 c616bbe1 Pierre Riteau
    *(head)->sqh_last = (elm);                                          \
184 c616bbe1 Pierre Riteau
    (head)->sqh_last = &(elm)->field.sqe_next;                          \
185 c616bbe1 Pierre Riteau
} while (/*CONSTCOND*/0)
186 c616bbe1 Pierre Riteau
187 c616bbe1 Pierre Riteau
#define QSIMPLEQ_INSERT_AFTER(head, listelm, elm, field) do {           \
188 c616bbe1 Pierre Riteau
    if (((elm)->field.sqe_next = (listelm)->field.sqe_next) == NULL)    \
189 c616bbe1 Pierre Riteau
        (head)->sqh_last = &(elm)->field.sqe_next;                      \
190 c616bbe1 Pierre Riteau
    (listelm)->field.sqe_next = (elm);                                  \
191 c616bbe1 Pierre Riteau
} while (/*CONSTCOND*/0)
192 c616bbe1 Pierre Riteau
193 c616bbe1 Pierre Riteau
#define QSIMPLEQ_REMOVE_HEAD(head, field) do {                          \
194 c616bbe1 Pierre Riteau
    if (((head)->sqh_first = (head)->sqh_first->field.sqe_next) == NULL)\
195 c616bbe1 Pierre Riteau
        (head)->sqh_last = &(head)->sqh_first;                          \
196 c616bbe1 Pierre Riteau
} while (/*CONSTCOND*/0)
197 c616bbe1 Pierre Riteau
198 c616bbe1 Pierre Riteau
#define QSIMPLEQ_REMOVE(head, elm, type, field) do {                    \
199 c616bbe1 Pierre Riteau
    if ((head)->sqh_first == (elm)) {                                   \
200 c616bbe1 Pierre Riteau
        QSIMPLEQ_REMOVE_HEAD((head), field);                            \
201 c616bbe1 Pierre Riteau
    } else {                                                            \
202 c616bbe1 Pierre Riteau
        struct type *curelm = (head)->sqh_first;                        \
203 c616bbe1 Pierre Riteau
        while (curelm->field.sqe_next != (elm))                         \
204 c616bbe1 Pierre Riteau
            curelm = curelm->field.sqe_next;                            \
205 c616bbe1 Pierre Riteau
        if ((curelm->field.sqe_next =                                   \
206 c616bbe1 Pierre Riteau
            curelm->field.sqe_next->field.sqe_next) == NULL)            \
207 c616bbe1 Pierre Riteau
                (head)->sqh_last = &(curelm)->field.sqe_next;           \
208 c616bbe1 Pierre Riteau
    }                                                                   \
209 c616bbe1 Pierre Riteau
} while (/*CONSTCOND*/0)
210 c616bbe1 Pierre Riteau
211 c616bbe1 Pierre Riteau
#define QSIMPLEQ_FOREACH(var, head, field)                              \
212 c616bbe1 Pierre Riteau
    for ((var) = ((head)->sqh_first);                                   \
213 c616bbe1 Pierre Riteau
        (var);                                                          \
214 c616bbe1 Pierre Riteau
        (var) = ((var)->field.sqe_next))
215 c616bbe1 Pierre Riteau
216 c616bbe1 Pierre Riteau
#define QSIMPLEQ_FOREACH_SAFE(var, head, field, next)                   \
217 c616bbe1 Pierre Riteau
    for ((var) = ((head)->sqh_first);                                   \
218 c616bbe1 Pierre Riteau
        (var) && ((next = ((var)->field.sqe_next)), 1);                 \
219 c616bbe1 Pierre Riteau
        (var) = (next))
220 c616bbe1 Pierre Riteau
221 c616bbe1 Pierre Riteau
#define QSIMPLEQ_CONCAT(head1, head2) do {                              \
222 c616bbe1 Pierre Riteau
    if (!QSIMPLEQ_EMPTY((head2))) {                                     \
223 c616bbe1 Pierre Riteau
        *(head1)->sqh_last = (head2)->sqh_first;                        \
224 c616bbe1 Pierre Riteau
        (head1)->sqh_last = (head2)->sqh_last;                          \
225 c616bbe1 Pierre Riteau
        QSIMPLEQ_INIT((head2));                                         \
226 c616bbe1 Pierre Riteau
    }                                                                   \
227 c616bbe1 Pierre Riteau
} while (/*CONSTCOND*/0)
228 c616bbe1 Pierre Riteau
229 c616bbe1 Pierre Riteau
#define QSIMPLEQ_LAST(head, type, field)                                \
230 c616bbe1 Pierre Riteau
    (QSIMPLEQ_EMPTY((head)) ?                                           \
231 c616bbe1 Pierre Riteau
        NULL :                                                          \
232 c616bbe1 Pierre Riteau
            ((struct type *)(void *)                                    \
233 c616bbe1 Pierre Riteau
        ((char *)((head)->sqh_last) - offsetof(struct type, field))))
234 c616bbe1 Pierre Riteau
235 c616bbe1 Pierre Riteau
/*
236 c616bbe1 Pierre Riteau
 * Simple queue access methods.
237 c616bbe1 Pierre Riteau
 */
238 c616bbe1 Pierre Riteau
#define QSIMPLEQ_EMPTY(head)        ((head)->sqh_first == NULL)
239 c616bbe1 Pierre Riteau
#define QSIMPLEQ_FIRST(head)        ((head)->sqh_first)
240 c616bbe1 Pierre Riteau
#define QSIMPLEQ_NEXT(elm, field)   ((elm)->field.sqe_next)
241 c616bbe1 Pierre Riteau
242 c616bbe1 Pierre Riteau
243 c616bbe1 Pierre Riteau
/*
244 fc56ef08 blueswir1
 * Tail queue definitions.
245 fc56ef08 blueswir1
 */
246 72cf2d4f Blue Swirl
#define Q_TAILQ_HEAD(name, type, qual)                                  \
247 fc56ef08 blueswir1
struct name {                                                           \
248 fc56ef08 blueswir1
        qual type *tqh_first;           /* first element */             \
249 fc56ef08 blueswir1
        qual type *qual *tqh_last;      /* addr of last next element */ \
250 fc56ef08 blueswir1
}
251 72cf2d4f Blue Swirl
#define QTAILQ_HEAD(name, type)  Q_TAILQ_HEAD(name, struct type,)
252 fc56ef08 blueswir1
253 72cf2d4f Blue Swirl
#define QTAILQ_HEAD_INITIALIZER(head)                                   \
254 fc56ef08 blueswir1
        { NULL, &(head).tqh_first }
255 fc56ef08 blueswir1
256 72cf2d4f Blue Swirl
#define Q_TAILQ_ENTRY(type, qual)                                       \
257 fc56ef08 blueswir1
struct {                                                                \
258 fc56ef08 blueswir1
        qual type *tqe_next;            /* next element */              \
259 fc56ef08 blueswir1
        qual type *qual *tqe_prev;      /* address of previous next element */\
260 fc56ef08 blueswir1
}
261 72cf2d4f Blue Swirl
#define QTAILQ_ENTRY(type)       Q_TAILQ_ENTRY(struct type,)
262 fc56ef08 blueswir1
263 fc56ef08 blueswir1
/*
264 fc56ef08 blueswir1
 * Tail queue functions.
265 fc56ef08 blueswir1
 */
266 72cf2d4f Blue Swirl
#define QTAILQ_INIT(head) do {                                          \
267 fc56ef08 blueswir1
        (head)->tqh_first = NULL;                                       \
268 fc56ef08 blueswir1
        (head)->tqh_last = &(head)->tqh_first;                          \
269 fc56ef08 blueswir1
} while (/*CONSTCOND*/0)
270 fc56ef08 blueswir1
271 72cf2d4f Blue Swirl
#define QTAILQ_INSERT_HEAD(head, elm, field) do {                       \
272 fc56ef08 blueswir1
        if (((elm)->field.tqe_next = (head)->tqh_first) != NULL)        \
273 fc56ef08 blueswir1
                (head)->tqh_first->field.tqe_prev =                     \
274 fc56ef08 blueswir1
                    &(elm)->field.tqe_next;                             \
275 fc56ef08 blueswir1
        else                                                            \
276 fc56ef08 blueswir1
                (head)->tqh_last = &(elm)->field.tqe_next;              \
277 fc56ef08 blueswir1
        (head)->tqh_first = (elm);                                      \
278 fc56ef08 blueswir1
        (elm)->field.tqe_prev = &(head)->tqh_first;                     \
279 fc56ef08 blueswir1
} while (/*CONSTCOND*/0)
280 fc56ef08 blueswir1
281 72cf2d4f Blue Swirl
#define QTAILQ_INSERT_TAIL(head, elm, field) do {                       \
282 fc56ef08 blueswir1
        (elm)->field.tqe_next = NULL;                                   \
283 fc56ef08 blueswir1
        (elm)->field.tqe_prev = (head)->tqh_last;                       \
284 fc56ef08 blueswir1
        *(head)->tqh_last = (elm);                                      \
285 fc56ef08 blueswir1
        (head)->tqh_last = &(elm)->field.tqe_next;                      \
286 fc56ef08 blueswir1
} while (/*CONSTCOND*/0)
287 fc56ef08 blueswir1
288 72cf2d4f Blue Swirl
#define QTAILQ_INSERT_AFTER(head, listelm, elm, field) do {             \
289 fc56ef08 blueswir1
        if (((elm)->field.tqe_next = (listelm)->field.tqe_next) != NULL)\
290 fc56ef08 blueswir1
                (elm)->field.tqe_next->field.tqe_prev =                 \
291 fc56ef08 blueswir1
                    &(elm)->field.tqe_next;                             \
292 fc56ef08 blueswir1
        else                                                            \
293 fc56ef08 blueswir1
                (head)->tqh_last = &(elm)->field.tqe_next;              \
294 fc56ef08 blueswir1
        (listelm)->field.tqe_next = (elm);                              \
295 fc56ef08 blueswir1
        (elm)->field.tqe_prev = &(listelm)->field.tqe_next;             \
296 fc56ef08 blueswir1
} while (/*CONSTCOND*/0)
297 fc56ef08 blueswir1
298 72cf2d4f Blue Swirl
#define QTAILQ_INSERT_BEFORE(listelm, elm, field) do {                  \
299 fc56ef08 blueswir1
        (elm)->field.tqe_prev = (listelm)->field.tqe_prev;              \
300 fc56ef08 blueswir1
        (elm)->field.tqe_next = (listelm);                              \
301 fc56ef08 blueswir1
        *(listelm)->field.tqe_prev = (elm);                             \
302 fc56ef08 blueswir1
        (listelm)->field.tqe_prev = &(elm)->field.tqe_next;             \
303 fc56ef08 blueswir1
} while (/*CONSTCOND*/0)
304 fc56ef08 blueswir1
305 72cf2d4f Blue Swirl
#define QTAILQ_REMOVE(head, elm, field) do {                            \
306 fc56ef08 blueswir1
        if (((elm)->field.tqe_next) != NULL)                            \
307 fc56ef08 blueswir1
                (elm)->field.tqe_next->field.tqe_prev =                 \
308 fc56ef08 blueswir1
                    (elm)->field.tqe_prev;                              \
309 fc56ef08 blueswir1
        else                                                            \
310 fc56ef08 blueswir1
                (head)->tqh_last = (elm)->field.tqe_prev;               \
311 fc56ef08 blueswir1
        *(elm)->field.tqe_prev = (elm)->field.tqe_next;                 \
312 fc56ef08 blueswir1
} while (/*CONSTCOND*/0)
313 fc56ef08 blueswir1
314 72cf2d4f Blue Swirl
#define QTAILQ_FOREACH(var, head, field)                                \
315 fc56ef08 blueswir1
        for ((var) = ((head)->tqh_first);                               \
316 fc56ef08 blueswir1
                (var);                                                  \
317 fc56ef08 blueswir1
                (var) = ((var)->field.tqe_next))
318 fc56ef08 blueswir1
319 72cf2d4f Blue Swirl
#define QTAILQ_FOREACH_SAFE(var, head, field, next_var)                 \
320 fc56ef08 blueswir1
        for ((var) = ((head)->tqh_first);                               \
321 fc56ef08 blueswir1
                (var) && ((next_var) = ((var)->field.tqe_next), 1);     \
322 fc56ef08 blueswir1
                (var) = (next_var))
323 fc56ef08 blueswir1
324 72cf2d4f Blue Swirl
#define QTAILQ_FOREACH_REVERSE(var, head, headname, field)              \
325 fc56ef08 blueswir1
        for ((var) = (*(((struct headname *)((head)->tqh_last))->tqh_last));    \
326 fc56ef08 blueswir1
                (var);                                                  \
327 fc56ef08 blueswir1
                (var) = (*(((struct headname *)((var)->field.tqe_prev))->tqh_last)))
328 fc56ef08 blueswir1
329 fc56ef08 blueswir1
/*
330 fc56ef08 blueswir1
 * Tail queue access methods.
331 fc56ef08 blueswir1
 */
332 72cf2d4f Blue Swirl
#define QTAILQ_EMPTY(head)               ((head)->tqh_first == NULL)
333 72cf2d4f Blue Swirl
#define QTAILQ_FIRST(head)               ((head)->tqh_first)
334 72cf2d4f Blue Swirl
#define QTAILQ_NEXT(elm, field)          ((elm)->field.tqe_next)
335 fc56ef08 blueswir1
336 72cf2d4f Blue Swirl
#define QTAILQ_LAST(head, headname) \
337 fc56ef08 blueswir1
        (*(((struct headname *)((head)->tqh_last))->tqh_last))
338 72cf2d4f Blue Swirl
#define QTAILQ_PREV(elm, headname, field) \
339 fc56ef08 blueswir1
        (*(((struct headname *)((elm)->field.tqe_prev))->tqh_last))
340 fc56ef08 blueswir1
341 fc56ef08 blueswir1
342 fc56ef08 blueswir1
/*
343 fc56ef08 blueswir1
 * Circular queue definitions.
344 fc56ef08 blueswir1
 */
345 72cf2d4f Blue Swirl
#define QCIRCLEQ_HEAD(name, type)                                       \
346 fc56ef08 blueswir1
struct name {                                                           \
347 fc56ef08 blueswir1
        struct type *cqh_first;         /* first element */             \
348 fc56ef08 blueswir1
        struct type *cqh_last;          /* last element */              \
349 fc56ef08 blueswir1
}
350 fc56ef08 blueswir1
351 72cf2d4f Blue Swirl
#define QCIRCLEQ_HEAD_INITIALIZER(head)                                 \
352 fc56ef08 blueswir1
        { (void *)&head, (void *)&head }
353 fc56ef08 blueswir1
354 72cf2d4f Blue Swirl
#define QCIRCLEQ_ENTRY(type)                                            \
355 fc56ef08 blueswir1
struct {                                                                \
356 fc56ef08 blueswir1
        struct type *cqe_next;          /* next element */              \
357 fc56ef08 blueswir1
        struct type *cqe_prev;          /* previous element */          \
358 fc56ef08 blueswir1
}
359 fc56ef08 blueswir1
360 fc56ef08 blueswir1
/*
361 fc56ef08 blueswir1
 * Circular queue functions.
362 fc56ef08 blueswir1
 */
363 72cf2d4f Blue Swirl
#define QCIRCLEQ_INIT(head) do {                                        \
364 fc56ef08 blueswir1
        (head)->cqh_first = (void *)(head);                             \
365 fc56ef08 blueswir1
        (head)->cqh_last = (void *)(head);                              \
366 fc56ef08 blueswir1
} while (/*CONSTCOND*/0)
367 fc56ef08 blueswir1
368 72cf2d4f Blue Swirl
#define QCIRCLEQ_INSERT_AFTER(head, listelm, elm, field) do {           \
369 fc56ef08 blueswir1
        (elm)->field.cqe_next = (listelm)->field.cqe_next;              \
370 fc56ef08 blueswir1
        (elm)->field.cqe_prev = (listelm);                              \
371 fc56ef08 blueswir1
        if ((listelm)->field.cqe_next == (void *)(head))                \
372 fc56ef08 blueswir1
                (head)->cqh_last = (elm);                               \
373 fc56ef08 blueswir1
        else                                                            \
374 fc56ef08 blueswir1
                (listelm)->field.cqe_next->field.cqe_prev = (elm);      \
375 fc56ef08 blueswir1
        (listelm)->field.cqe_next = (elm);                              \
376 fc56ef08 blueswir1
} while (/*CONSTCOND*/0)
377 fc56ef08 blueswir1
378 72cf2d4f Blue Swirl
#define QCIRCLEQ_INSERT_BEFORE(head, listelm, elm, field) do {          \
379 fc56ef08 blueswir1
        (elm)->field.cqe_next = (listelm);                              \
380 fc56ef08 blueswir1
        (elm)->field.cqe_prev = (listelm)->field.cqe_prev;              \
381 fc56ef08 blueswir1
        if ((listelm)->field.cqe_prev == (void *)(head))                \
382 fc56ef08 blueswir1
                (head)->cqh_first = (elm);                              \
383 fc56ef08 blueswir1
        else                                                            \
384 fc56ef08 blueswir1
                (listelm)->field.cqe_prev->field.cqe_next = (elm);      \
385 fc56ef08 blueswir1
        (listelm)->field.cqe_prev = (elm);                              \
386 fc56ef08 blueswir1
} while (/*CONSTCOND*/0)
387 fc56ef08 blueswir1
388 72cf2d4f Blue Swirl
#define QCIRCLEQ_INSERT_HEAD(head, elm, field) do {                     \
389 fc56ef08 blueswir1
        (elm)->field.cqe_next = (head)->cqh_first;                      \
390 fc56ef08 blueswir1
        (elm)->field.cqe_prev = (void *)(head);                         \
391 fc56ef08 blueswir1
        if ((head)->cqh_last == (void *)(head))                         \
392 fc56ef08 blueswir1
                (head)->cqh_last = (elm);                               \
393 fc56ef08 blueswir1
        else                                                            \
394 fc56ef08 blueswir1
                (head)->cqh_first->field.cqe_prev = (elm);              \
395 fc56ef08 blueswir1
        (head)->cqh_first = (elm);                                      \
396 fc56ef08 blueswir1
} while (/*CONSTCOND*/0)
397 fc56ef08 blueswir1
398 72cf2d4f Blue Swirl
#define QCIRCLEQ_INSERT_TAIL(head, elm, field) do {                     \
399 fc56ef08 blueswir1
        (elm)->field.cqe_next = (void *)(head);                         \
400 fc56ef08 blueswir1
        (elm)->field.cqe_prev = (head)->cqh_last;                       \
401 fc56ef08 blueswir1
        if ((head)->cqh_first == (void *)(head))                        \
402 fc56ef08 blueswir1
                (head)->cqh_first = (elm);                              \
403 fc56ef08 blueswir1
        else                                                            \
404 fc56ef08 blueswir1
                (head)->cqh_last->field.cqe_next = (elm);               \
405 fc56ef08 blueswir1
        (head)->cqh_last = (elm);                                       \
406 fc56ef08 blueswir1
} while (/*CONSTCOND*/0)
407 fc56ef08 blueswir1
408 72cf2d4f Blue Swirl
#define QCIRCLEQ_REMOVE(head, elm, field) do {                          \
409 fc56ef08 blueswir1
        if ((elm)->field.cqe_next == (void *)(head))                    \
410 fc56ef08 blueswir1
                (head)->cqh_last = (elm)->field.cqe_prev;               \
411 fc56ef08 blueswir1
        else                                                            \
412 fc56ef08 blueswir1
                (elm)->field.cqe_next->field.cqe_prev =                 \
413 fc56ef08 blueswir1
                    (elm)->field.cqe_prev;                              \
414 fc56ef08 blueswir1
        if ((elm)->field.cqe_prev == (void *)(head))                    \
415 fc56ef08 blueswir1
                (head)->cqh_first = (elm)->field.cqe_next;              \
416 fc56ef08 blueswir1
        else                                                            \
417 fc56ef08 blueswir1
                (elm)->field.cqe_prev->field.cqe_next =                 \
418 fc56ef08 blueswir1
                    (elm)->field.cqe_next;                              \
419 fc56ef08 blueswir1
} while (/*CONSTCOND*/0)
420 fc56ef08 blueswir1
421 72cf2d4f Blue Swirl
#define QCIRCLEQ_FOREACH(var, head, field)                              \
422 fc56ef08 blueswir1
        for ((var) = ((head)->cqh_first);                               \
423 fc56ef08 blueswir1
                (var) != (const void *)(head);                          \
424 fc56ef08 blueswir1
                (var) = ((var)->field.cqe_next))
425 fc56ef08 blueswir1
426 72cf2d4f Blue Swirl
#define QCIRCLEQ_FOREACH_REVERSE(var, head, field)                      \
427 fc56ef08 blueswir1
        for ((var) = ((head)->cqh_last);                                \
428 fc56ef08 blueswir1
                (var) != (const void *)(head);                          \
429 fc56ef08 blueswir1
                (var) = ((var)->field.cqe_prev))
430 fc56ef08 blueswir1
431 fc56ef08 blueswir1
/*
432 fc56ef08 blueswir1
 * Circular queue access methods.
433 fc56ef08 blueswir1
 */
434 72cf2d4f Blue Swirl
#define QCIRCLEQ_EMPTY(head)             ((head)->cqh_first == (void *)(head))
435 72cf2d4f Blue Swirl
#define QCIRCLEQ_FIRST(head)             ((head)->cqh_first)
436 72cf2d4f Blue Swirl
#define QCIRCLEQ_LAST(head)              ((head)->cqh_last)
437 72cf2d4f Blue Swirl
#define QCIRCLEQ_NEXT(elm, field)        ((elm)->field.cqe_next)
438 72cf2d4f Blue Swirl
#define QCIRCLEQ_PREV(elm, field)        ((elm)->field.cqe_prev)
439 fc56ef08 blueswir1
440 72cf2d4f Blue Swirl
#define QCIRCLEQ_LOOP_NEXT(head, elm, field)                            \
441 fc56ef08 blueswir1
        (((elm)->field.cqe_next == (void *)(head))                      \
442 fc56ef08 blueswir1
            ? ((head)->cqh_first)                                       \
443 fc56ef08 blueswir1
            : (elm->field.cqe_next))
444 72cf2d4f Blue Swirl
#define QCIRCLEQ_LOOP_PREV(head, elm, field)                            \
445 fc56ef08 blueswir1
        (((elm)->field.cqe_prev == (void *)(head))                      \
446 fc56ef08 blueswir1
            ? ((head)->cqh_last)                                        \
447 fc56ef08 blueswir1
            : (elm->field.cqe_prev))
448 fc56ef08 blueswir1
449 72cf2d4f Blue Swirl
#endif  /* !QEMU_SYS_QUEUE_H_ */