Statistics
| Branch: | Revision:

root / include / qemu / queue.h @ 10f5bff6

History | View | Annotate | Download (19.5 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 5cbdb3a3 Stefan Weil
 * QEMU version: Copy from netbsd, removed debug code, removed some of
5 6095aa88 Paolo Bonzini
 * the implementations.  Left in singly-linked lists, lists, simple
6 cf904cfa Paolo Bonzini
 * queues, and tail 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 cf904cfa Paolo Bonzini
 * This file defines four types of data structures: singly-linked lists,
45 cf904cfa Paolo Bonzini
 * lists, simple queues, and tail queues.
46 fc56ef08 blueswir1
 *
47 6095aa88 Paolo Bonzini
 * A singly-linked list is headed by a single forward pointer. The
48 6095aa88 Paolo Bonzini
 * elements are singly linked for minimum space and pointer manipulation
49 6095aa88 Paolo Bonzini
 * overhead at the expense of O(n) removal for arbitrary elements. New
50 6095aa88 Paolo Bonzini
 * elements can be added to the list after an existing element or at the
51 6095aa88 Paolo Bonzini
 * head of the list.  Elements being removed from the head of the list
52 6095aa88 Paolo Bonzini
 * should use the explicit macro for this purpose for optimum
53 6095aa88 Paolo Bonzini
 * efficiency. A singly-linked list may only be traversed in the forward
54 6095aa88 Paolo Bonzini
 * direction.  Singly-linked lists are ideal for applications with large
55 6095aa88 Paolo Bonzini
 * datasets and few or no removals or for implementing a LIFO queue.
56 6095aa88 Paolo Bonzini
 *
57 fc56ef08 blueswir1
 * A list is headed by a single forward pointer (or an array of forward
58 fc56ef08 blueswir1
 * pointers for a hash table header). The elements are doubly linked
59 fc56ef08 blueswir1
 * so that an arbitrary element can be removed without a need to
60 fc56ef08 blueswir1
 * traverse the list. New elements can be added to the list before
61 fc56ef08 blueswir1
 * or after an existing element or at the head of the list. A list
62 fc56ef08 blueswir1
 * may only be traversed in the forward direction.
63 fc56ef08 blueswir1
 *
64 c616bbe1 Pierre Riteau
 * A simple queue is headed by a pair of pointers, one the head of the
65 c616bbe1 Pierre Riteau
 * list and the other to the tail of the list. The elements are singly
66 c616bbe1 Pierre Riteau
 * linked to save space, so elements can only be removed from the
67 c616bbe1 Pierre Riteau
 * head of the list. New elements can be added to the list after
68 c616bbe1 Pierre Riteau
 * an existing element, at the head of the list, or at the end of the
69 c616bbe1 Pierre Riteau
 * list. A simple queue may only be traversed in the forward direction.
70 c616bbe1 Pierre Riteau
 *
71 fc56ef08 blueswir1
 * A tail queue is headed by a pair of pointers, one to the head of the
72 fc56ef08 blueswir1
 * list and the other to the tail of the list. The elements are doubly
73 fc56ef08 blueswir1
 * linked so that an arbitrary element can be removed without a need to
74 fc56ef08 blueswir1
 * traverse the list. New elements can be added to the list before or
75 fc56ef08 blueswir1
 * after an existing element, at the head of the list, or at the end of
76 fc56ef08 blueswir1
 * the list. A tail queue may be traversed in either direction.
77 fc56ef08 blueswir1
 *
78 fc56ef08 blueswir1
 * For details on the use of these macros, see the queue(3) manual page.
79 fc56ef08 blueswir1
 */
80 fc56ef08 blueswir1
81 1de7afc9 Paolo Bonzini
#include "qemu/atomic.h" /* for smp_wmb() */
82 5f7d05ec Harsh Prateek Bora
83 fc56ef08 blueswir1
/*
84 fc56ef08 blueswir1
 * List definitions.
85 fc56ef08 blueswir1
 */
86 72cf2d4f Blue Swirl
#define QLIST_HEAD(name, type)                                          \
87 fc56ef08 blueswir1
struct name {                                                           \
88 fc56ef08 blueswir1
        struct type *lh_first;  /* first element */                     \
89 fc56ef08 blueswir1
}
90 fc56ef08 blueswir1
91 72cf2d4f Blue Swirl
#define QLIST_HEAD_INITIALIZER(head)                                    \
92 fc56ef08 blueswir1
        { NULL }
93 fc56ef08 blueswir1
94 72cf2d4f Blue Swirl
#define QLIST_ENTRY(type)                                               \
95 fc56ef08 blueswir1
struct {                                                                \
96 fc56ef08 blueswir1
        struct type *le_next;   /* next element */                      \
97 fc56ef08 blueswir1
        struct type **le_prev;  /* address of previous next element */  \
98 fc56ef08 blueswir1
}
99 fc56ef08 blueswir1
100 fc56ef08 blueswir1
/*
101 fc56ef08 blueswir1
 * List functions.
102 fc56ef08 blueswir1
 */
103 72cf2d4f Blue Swirl
#define QLIST_INIT(head) do {                                           \
104 fc56ef08 blueswir1
        (head)->lh_first = NULL;                                        \
105 fc56ef08 blueswir1
} while (/*CONSTCOND*/0)
106 fc56ef08 blueswir1
107 72cf2d4f Blue Swirl
#define QLIST_INSERT_AFTER(listelm, elm, field) do {                    \
108 fc56ef08 blueswir1
        if (((elm)->field.le_next = (listelm)->field.le_next) != NULL)  \
109 fc56ef08 blueswir1
                (listelm)->field.le_next->field.le_prev =               \
110 fc56ef08 blueswir1
                    &(elm)->field.le_next;                              \
111 fc56ef08 blueswir1
        (listelm)->field.le_next = (elm);                               \
112 fc56ef08 blueswir1
        (elm)->field.le_prev = &(listelm)->field.le_next;               \
113 fc56ef08 blueswir1
} while (/*CONSTCOND*/0)
114 fc56ef08 blueswir1
115 72cf2d4f Blue Swirl
#define QLIST_INSERT_BEFORE(listelm, elm, field) do {                   \
116 fc56ef08 blueswir1
        (elm)->field.le_prev = (listelm)->field.le_prev;                \
117 fc56ef08 blueswir1
        (elm)->field.le_next = (listelm);                               \
118 fc56ef08 blueswir1
        *(listelm)->field.le_prev = (elm);                              \
119 fc56ef08 blueswir1
        (listelm)->field.le_prev = &(elm)->field.le_next;               \
120 fc56ef08 blueswir1
} while (/*CONSTCOND*/0)
121 fc56ef08 blueswir1
122 72cf2d4f Blue Swirl
#define QLIST_INSERT_HEAD(head, elm, field) do {                        \
123 fc56ef08 blueswir1
        if (((elm)->field.le_next = (head)->lh_first) != NULL)          \
124 fc56ef08 blueswir1
                (head)->lh_first->field.le_prev = &(elm)->field.le_next;\
125 fc56ef08 blueswir1
        (head)->lh_first = (elm);                                       \
126 fc56ef08 blueswir1
        (elm)->field.le_prev = &(head)->lh_first;                       \
127 fc56ef08 blueswir1
} while (/*CONSTCOND*/0)
128 fc56ef08 blueswir1
129 5f7d05ec Harsh Prateek Bora
#define QLIST_INSERT_HEAD_RCU(head, elm, field) do {                    \
130 5f7d05ec Harsh Prateek Bora
        (elm)->field.le_prev = &(head)->lh_first;                       \
131 5f7d05ec Harsh Prateek Bora
        (elm)->field.le_next = (head)->lh_first;                        \
132 5f7d05ec Harsh Prateek Bora
        smp_wmb(); /* fill elm before linking it */                     \
133 5f7d05ec Harsh Prateek Bora
        if ((head)->lh_first != NULL)  {                                \
134 5f7d05ec Harsh Prateek Bora
            (head)->lh_first->field.le_prev = &(elm)->field.le_next;    \
135 5f7d05ec Harsh Prateek Bora
        }                                                               \
136 5f7d05ec Harsh Prateek Bora
        (head)->lh_first = (elm);                                       \
137 5f7d05ec Harsh Prateek Bora
        smp_wmb();                                                      \
138 5f7d05ec Harsh Prateek Bora
} while (/* CONSTCOND*/0)
139 5f7d05ec Harsh Prateek Bora
140 72cf2d4f Blue Swirl
#define QLIST_REMOVE(elm, field) do {                                   \
141 fc56ef08 blueswir1
        if ((elm)->field.le_next != NULL)                               \
142 fc56ef08 blueswir1
                (elm)->field.le_next->field.le_prev =                   \
143 fc56ef08 blueswir1
                    (elm)->field.le_prev;                               \
144 fc56ef08 blueswir1
        *(elm)->field.le_prev = (elm)->field.le_next;                   \
145 fc56ef08 blueswir1
} while (/*CONSTCOND*/0)
146 fc56ef08 blueswir1
147 72cf2d4f Blue Swirl
#define QLIST_FOREACH(var, head, field)                                 \
148 fc56ef08 blueswir1
        for ((var) = ((head)->lh_first);                                \
149 fc56ef08 blueswir1
                (var);                                                  \
150 fc56ef08 blueswir1
                (var) = ((var)->field.le_next))
151 fc56ef08 blueswir1
152 72cf2d4f Blue Swirl
#define QLIST_FOREACH_SAFE(var, head, field, next_var)                  \
153 bb150dc8 Juan Quintela
        for ((var) = ((head)->lh_first);                                \
154 bb150dc8 Juan Quintela
                (var) && ((next_var) = ((var)->field.le_next), 1);      \
155 bb150dc8 Juan Quintela
                (var) = (next_var))
156 bb150dc8 Juan Quintela
157 fc56ef08 blueswir1
/*
158 fc56ef08 blueswir1
 * List access methods.
159 fc56ef08 blueswir1
 */
160 72cf2d4f Blue Swirl
#define QLIST_EMPTY(head)                ((head)->lh_first == NULL)
161 72cf2d4f Blue Swirl
#define QLIST_FIRST(head)                ((head)->lh_first)
162 72cf2d4f Blue Swirl
#define QLIST_NEXT(elm, field)           ((elm)->field.le_next)
163 fc56ef08 blueswir1
164 fc56ef08 blueswir1
165 fc56ef08 blueswir1
/*
166 6095aa88 Paolo Bonzini
 * Singly-linked List definitions.
167 6095aa88 Paolo Bonzini
 */
168 6095aa88 Paolo Bonzini
#define QSLIST_HEAD(name, type)                                          \
169 6095aa88 Paolo Bonzini
struct name {                                                           \
170 6095aa88 Paolo Bonzini
        struct type *slh_first; /* first element */                     \
171 6095aa88 Paolo Bonzini
}
172 6095aa88 Paolo Bonzini
173 6095aa88 Paolo Bonzini
#define QSLIST_HEAD_INITIALIZER(head)                                    \
174 6095aa88 Paolo Bonzini
        { NULL }
175 6095aa88 Paolo Bonzini
176 6095aa88 Paolo Bonzini
#define QSLIST_ENTRY(type)                                               \
177 6095aa88 Paolo Bonzini
struct {                                                                \
178 6095aa88 Paolo Bonzini
        struct type *sle_next;  /* next element */                      \
179 6095aa88 Paolo Bonzini
}
180 6095aa88 Paolo Bonzini
181 6095aa88 Paolo Bonzini
/*
182 6095aa88 Paolo Bonzini
 * Singly-linked List functions.
183 6095aa88 Paolo Bonzini
 */
184 6095aa88 Paolo Bonzini
#define QSLIST_INIT(head) do {                                           \
185 6095aa88 Paolo Bonzini
        (head)->slh_first = NULL;                                       \
186 6095aa88 Paolo Bonzini
} while (/*CONSTCOND*/0)
187 6095aa88 Paolo Bonzini
188 6095aa88 Paolo Bonzini
#define QSLIST_INSERT_AFTER(slistelm, elm, field) do {                   \
189 6095aa88 Paolo Bonzini
        (elm)->field.sle_next = (slistelm)->field.sle_next;             \
190 6095aa88 Paolo Bonzini
        (slistelm)->field.sle_next = (elm);                             \
191 6095aa88 Paolo Bonzini
} while (/*CONSTCOND*/0)
192 6095aa88 Paolo Bonzini
193 6095aa88 Paolo Bonzini
#define QSLIST_INSERT_HEAD(head, elm, field) do {                        \
194 6095aa88 Paolo Bonzini
        (elm)->field.sle_next = (head)->slh_first;                      \
195 6095aa88 Paolo Bonzini
        (head)->slh_first = (elm);                                      \
196 6095aa88 Paolo Bonzini
} while (/*CONSTCOND*/0)
197 6095aa88 Paolo Bonzini
198 6095aa88 Paolo Bonzini
#define QSLIST_REMOVE_HEAD(head, field) do {                             \
199 6095aa88 Paolo Bonzini
        (head)->slh_first = (head)->slh_first->field.sle_next;          \
200 6095aa88 Paolo Bonzini
} while (/*CONSTCOND*/0)
201 6095aa88 Paolo Bonzini
202 6095aa88 Paolo Bonzini
#define QSLIST_REMOVE_AFTER(slistelm, field) do {                        \
203 6095aa88 Paolo Bonzini
        (slistelm)->field.sle_next =                                    \
204 6095aa88 Paolo Bonzini
            QSLIST_NEXT(QSLIST_NEXT((slistelm), field), field);           \
205 6095aa88 Paolo Bonzini
} while (/*CONSTCOND*/0)
206 6095aa88 Paolo Bonzini
207 6095aa88 Paolo Bonzini
#define QSLIST_FOREACH(var, head, field)                                 \
208 6095aa88 Paolo Bonzini
        for((var) = (head)->slh_first; (var); (var) = (var)->field.sle_next)
209 6095aa88 Paolo Bonzini
210 6095aa88 Paolo Bonzini
#define QSLIST_FOREACH_SAFE(var, head, field, tvar)                      \
211 6095aa88 Paolo Bonzini
        for ((var) = QSLIST_FIRST((head));                               \
212 6095aa88 Paolo Bonzini
            (var) && ((tvar) = QSLIST_NEXT((var), field), 1);            \
213 6095aa88 Paolo Bonzini
            (var) = (tvar))
214 6095aa88 Paolo Bonzini
215 6095aa88 Paolo Bonzini
/*
216 6095aa88 Paolo Bonzini
 * Singly-linked List access methods.
217 6095aa88 Paolo Bonzini
 */
218 6095aa88 Paolo Bonzini
#define QSLIST_EMPTY(head)       ((head)->slh_first == NULL)
219 6095aa88 Paolo Bonzini
#define QSLIST_FIRST(head)       ((head)->slh_first)
220 6095aa88 Paolo Bonzini
#define QSLIST_NEXT(elm, field)  ((elm)->field.sle_next)
221 6095aa88 Paolo Bonzini
222 6095aa88 Paolo Bonzini
223 6095aa88 Paolo Bonzini
/*
224 c616bbe1 Pierre Riteau
 * Simple queue definitions.
225 c616bbe1 Pierre Riteau
 */
226 c616bbe1 Pierre Riteau
#define QSIMPLEQ_HEAD(name, type)                                       \
227 c616bbe1 Pierre Riteau
struct name {                                                           \
228 c616bbe1 Pierre Riteau
    struct type *sqh_first;    /* first element */                      \
229 c616bbe1 Pierre Riteau
    struct type **sqh_last;    /* addr of last next element */          \
230 c616bbe1 Pierre Riteau
}
231 c616bbe1 Pierre Riteau
232 c616bbe1 Pierre Riteau
#define QSIMPLEQ_HEAD_INITIALIZER(head)                                 \
233 c616bbe1 Pierre Riteau
    { NULL, &(head).sqh_first }
234 c616bbe1 Pierre Riteau
235 c616bbe1 Pierre Riteau
#define QSIMPLEQ_ENTRY(type)                                            \
236 c616bbe1 Pierre Riteau
struct {                                                                \
237 c616bbe1 Pierre Riteau
    struct type *sqe_next;    /* next element */                        \
238 c616bbe1 Pierre Riteau
}
239 c616bbe1 Pierre Riteau
240 c616bbe1 Pierre Riteau
/*
241 c616bbe1 Pierre Riteau
 * Simple queue functions.
242 c616bbe1 Pierre Riteau
 */
243 c616bbe1 Pierre Riteau
#define QSIMPLEQ_INIT(head) do {                                        \
244 c616bbe1 Pierre Riteau
    (head)->sqh_first = NULL;                                           \
245 c616bbe1 Pierre Riteau
    (head)->sqh_last = &(head)->sqh_first;                              \
246 c616bbe1 Pierre Riteau
} while (/*CONSTCOND*/0)
247 c616bbe1 Pierre Riteau
248 c616bbe1 Pierre Riteau
#define QSIMPLEQ_INSERT_HEAD(head, elm, field) do {                     \
249 c616bbe1 Pierre Riteau
    if (((elm)->field.sqe_next = (head)->sqh_first) == NULL)            \
250 c616bbe1 Pierre Riteau
        (head)->sqh_last = &(elm)->field.sqe_next;                      \
251 c616bbe1 Pierre Riteau
    (head)->sqh_first = (elm);                                          \
252 c616bbe1 Pierre Riteau
} while (/*CONSTCOND*/0)
253 c616bbe1 Pierre Riteau
254 c616bbe1 Pierre Riteau
#define QSIMPLEQ_INSERT_TAIL(head, elm, field) do {                     \
255 c616bbe1 Pierre Riteau
    (elm)->field.sqe_next = NULL;                                       \
256 c616bbe1 Pierre Riteau
    *(head)->sqh_last = (elm);                                          \
257 c616bbe1 Pierre Riteau
    (head)->sqh_last = &(elm)->field.sqe_next;                          \
258 c616bbe1 Pierre Riteau
} while (/*CONSTCOND*/0)
259 c616bbe1 Pierre Riteau
260 c616bbe1 Pierre Riteau
#define QSIMPLEQ_INSERT_AFTER(head, listelm, elm, field) do {           \
261 c616bbe1 Pierre Riteau
    if (((elm)->field.sqe_next = (listelm)->field.sqe_next) == NULL)    \
262 c616bbe1 Pierre Riteau
        (head)->sqh_last = &(elm)->field.sqe_next;                      \
263 c616bbe1 Pierre Riteau
    (listelm)->field.sqe_next = (elm);                                  \
264 c616bbe1 Pierre Riteau
} while (/*CONSTCOND*/0)
265 c616bbe1 Pierre Riteau
266 c616bbe1 Pierre Riteau
#define QSIMPLEQ_REMOVE_HEAD(head, field) do {                          \
267 c616bbe1 Pierre Riteau
    if (((head)->sqh_first = (head)->sqh_first->field.sqe_next) == NULL)\
268 c616bbe1 Pierre Riteau
        (head)->sqh_last = &(head)->sqh_first;                          \
269 c616bbe1 Pierre Riteau
} while (/*CONSTCOND*/0)
270 c616bbe1 Pierre Riteau
271 c616bbe1 Pierre Riteau
#define QSIMPLEQ_REMOVE(head, elm, type, field) do {                    \
272 c616bbe1 Pierre Riteau
    if ((head)->sqh_first == (elm)) {                                   \
273 c616bbe1 Pierre Riteau
        QSIMPLEQ_REMOVE_HEAD((head), field);                            \
274 c616bbe1 Pierre Riteau
    } else {                                                            \
275 c616bbe1 Pierre Riteau
        struct type *curelm = (head)->sqh_first;                        \
276 c616bbe1 Pierre Riteau
        while (curelm->field.sqe_next != (elm))                         \
277 c616bbe1 Pierre Riteau
            curelm = curelm->field.sqe_next;                            \
278 c616bbe1 Pierre Riteau
        if ((curelm->field.sqe_next =                                   \
279 c616bbe1 Pierre Riteau
            curelm->field.sqe_next->field.sqe_next) == NULL)            \
280 c616bbe1 Pierre Riteau
                (head)->sqh_last = &(curelm)->field.sqe_next;           \
281 c616bbe1 Pierre Riteau
    }                                                                   \
282 c616bbe1 Pierre Riteau
} while (/*CONSTCOND*/0)
283 c616bbe1 Pierre Riteau
284 c616bbe1 Pierre Riteau
#define QSIMPLEQ_FOREACH(var, head, field)                              \
285 c616bbe1 Pierre Riteau
    for ((var) = ((head)->sqh_first);                                   \
286 c616bbe1 Pierre Riteau
        (var);                                                          \
287 c616bbe1 Pierre Riteau
        (var) = ((var)->field.sqe_next))
288 c616bbe1 Pierre Riteau
289 c616bbe1 Pierre Riteau
#define QSIMPLEQ_FOREACH_SAFE(var, head, field, next)                   \
290 c616bbe1 Pierre Riteau
    for ((var) = ((head)->sqh_first);                                   \
291 c616bbe1 Pierre Riteau
        (var) && ((next = ((var)->field.sqe_next)), 1);                 \
292 c616bbe1 Pierre Riteau
        (var) = (next))
293 c616bbe1 Pierre Riteau
294 c616bbe1 Pierre Riteau
#define QSIMPLEQ_CONCAT(head1, head2) do {                              \
295 c616bbe1 Pierre Riteau
    if (!QSIMPLEQ_EMPTY((head2))) {                                     \
296 c616bbe1 Pierre Riteau
        *(head1)->sqh_last = (head2)->sqh_first;                        \
297 c616bbe1 Pierre Riteau
        (head1)->sqh_last = (head2)->sqh_last;                          \
298 c616bbe1 Pierre Riteau
        QSIMPLEQ_INIT((head2));                                         \
299 c616bbe1 Pierre Riteau
    }                                                                   \
300 c616bbe1 Pierre Riteau
} while (/*CONSTCOND*/0)
301 c616bbe1 Pierre Riteau
302 c616bbe1 Pierre Riteau
#define QSIMPLEQ_LAST(head, type, field)                                \
303 c616bbe1 Pierre Riteau
    (QSIMPLEQ_EMPTY((head)) ?                                           \
304 c616bbe1 Pierre Riteau
        NULL :                                                          \
305 c616bbe1 Pierre Riteau
            ((struct type *)(void *)                                    \
306 c616bbe1 Pierre Riteau
        ((char *)((head)->sqh_last) - offsetof(struct type, field))))
307 c616bbe1 Pierre Riteau
308 c616bbe1 Pierre Riteau
/*
309 c616bbe1 Pierre Riteau
 * Simple queue access methods.
310 c616bbe1 Pierre Riteau
 */
311 c616bbe1 Pierre Riteau
#define QSIMPLEQ_EMPTY(head)        ((head)->sqh_first == NULL)
312 c616bbe1 Pierre Riteau
#define QSIMPLEQ_FIRST(head)        ((head)->sqh_first)
313 c616bbe1 Pierre Riteau
#define QSIMPLEQ_NEXT(elm, field)   ((elm)->field.sqe_next)
314 c616bbe1 Pierre Riteau
315 c616bbe1 Pierre Riteau
316 c616bbe1 Pierre Riteau
/*
317 fc56ef08 blueswir1
 * Tail queue definitions.
318 fc56ef08 blueswir1
 */
319 72cf2d4f Blue Swirl
#define Q_TAILQ_HEAD(name, type, qual)                                  \
320 fc56ef08 blueswir1
struct name {                                                           \
321 fc56ef08 blueswir1
        qual type *tqh_first;           /* first element */             \
322 fc56ef08 blueswir1
        qual type *qual *tqh_last;      /* addr of last next element */ \
323 fc56ef08 blueswir1
}
324 72cf2d4f Blue Swirl
#define QTAILQ_HEAD(name, type)  Q_TAILQ_HEAD(name, struct type,)
325 fc56ef08 blueswir1
326 72cf2d4f Blue Swirl
#define QTAILQ_HEAD_INITIALIZER(head)                                   \
327 fc56ef08 blueswir1
        { NULL, &(head).tqh_first }
328 fc56ef08 blueswir1
329 72cf2d4f Blue Swirl
#define Q_TAILQ_ENTRY(type, qual)                                       \
330 fc56ef08 blueswir1
struct {                                                                \
331 fc56ef08 blueswir1
        qual type *tqe_next;            /* next element */              \
332 fc56ef08 blueswir1
        qual type *qual *tqe_prev;      /* address of previous next element */\
333 fc56ef08 blueswir1
}
334 72cf2d4f Blue Swirl
#define QTAILQ_ENTRY(type)       Q_TAILQ_ENTRY(struct type,)
335 fc56ef08 blueswir1
336 fc56ef08 blueswir1
/*
337 fc56ef08 blueswir1
 * Tail queue functions.
338 fc56ef08 blueswir1
 */
339 72cf2d4f Blue Swirl
#define QTAILQ_INIT(head) do {                                          \
340 fc56ef08 blueswir1
        (head)->tqh_first = NULL;                                       \
341 fc56ef08 blueswir1
        (head)->tqh_last = &(head)->tqh_first;                          \
342 fc56ef08 blueswir1
} while (/*CONSTCOND*/0)
343 fc56ef08 blueswir1
344 72cf2d4f Blue Swirl
#define QTAILQ_INSERT_HEAD(head, elm, field) do {                       \
345 fc56ef08 blueswir1
        if (((elm)->field.tqe_next = (head)->tqh_first) != NULL)        \
346 fc56ef08 blueswir1
                (head)->tqh_first->field.tqe_prev =                     \
347 fc56ef08 blueswir1
                    &(elm)->field.tqe_next;                             \
348 fc56ef08 blueswir1
        else                                                            \
349 fc56ef08 blueswir1
                (head)->tqh_last = &(elm)->field.tqe_next;              \
350 fc56ef08 blueswir1
        (head)->tqh_first = (elm);                                      \
351 fc56ef08 blueswir1
        (elm)->field.tqe_prev = &(head)->tqh_first;                     \
352 fc56ef08 blueswir1
} while (/*CONSTCOND*/0)
353 fc56ef08 blueswir1
354 72cf2d4f Blue Swirl
#define QTAILQ_INSERT_TAIL(head, elm, field) do {                       \
355 fc56ef08 blueswir1
        (elm)->field.tqe_next = NULL;                                   \
356 fc56ef08 blueswir1
        (elm)->field.tqe_prev = (head)->tqh_last;                       \
357 fc56ef08 blueswir1
        *(head)->tqh_last = (elm);                                      \
358 fc56ef08 blueswir1
        (head)->tqh_last = &(elm)->field.tqe_next;                      \
359 fc56ef08 blueswir1
} while (/*CONSTCOND*/0)
360 fc56ef08 blueswir1
361 72cf2d4f Blue Swirl
#define QTAILQ_INSERT_AFTER(head, listelm, elm, field) do {             \
362 fc56ef08 blueswir1
        if (((elm)->field.tqe_next = (listelm)->field.tqe_next) != NULL)\
363 fc56ef08 blueswir1
                (elm)->field.tqe_next->field.tqe_prev =                 \
364 fc56ef08 blueswir1
                    &(elm)->field.tqe_next;                             \
365 fc56ef08 blueswir1
        else                                                            \
366 fc56ef08 blueswir1
                (head)->tqh_last = &(elm)->field.tqe_next;              \
367 fc56ef08 blueswir1
        (listelm)->field.tqe_next = (elm);                              \
368 fc56ef08 blueswir1
        (elm)->field.tqe_prev = &(listelm)->field.tqe_next;             \
369 fc56ef08 blueswir1
} while (/*CONSTCOND*/0)
370 fc56ef08 blueswir1
371 72cf2d4f Blue Swirl
#define QTAILQ_INSERT_BEFORE(listelm, elm, field) do {                  \
372 fc56ef08 blueswir1
        (elm)->field.tqe_prev = (listelm)->field.tqe_prev;              \
373 fc56ef08 blueswir1
        (elm)->field.tqe_next = (listelm);                              \
374 fc56ef08 blueswir1
        *(listelm)->field.tqe_prev = (elm);                             \
375 fc56ef08 blueswir1
        (listelm)->field.tqe_prev = &(elm)->field.tqe_next;             \
376 fc56ef08 blueswir1
} while (/*CONSTCOND*/0)
377 fc56ef08 blueswir1
378 72cf2d4f Blue Swirl
#define QTAILQ_REMOVE(head, elm, field) do {                            \
379 fc56ef08 blueswir1
        if (((elm)->field.tqe_next) != NULL)                            \
380 fc56ef08 blueswir1
                (elm)->field.tqe_next->field.tqe_prev =                 \
381 fc56ef08 blueswir1
                    (elm)->field.tqe_prev;                              \
382 fc56ef08 blueswir1
        else                                                            \
383 fc56ef08 blueswir1
                (head)->tqh_last = (elm)->field.tqe_prev;               \
384 fc56ef08 blueswir1
        *(elm)->field.tqe_prev = (elm)->field.tqe_next;                 \
385 fc56ef08 blueswir1
} while (/*CONSTCOND*/0)
386 fc56ef08 blueswir1
387 72cf2d4f Blue Swirl
#define QTAILQ_FOREACH(var, head, field)                                \
388 fc56ef08 blueswir1
        for ((var) = ((head)->tqh_first);                               \
389 fc56ef08 blueswir1
                (var);                                                  \
390 fc56ef08 blueswir1
                (var) = ((var)->field.tqe_next))
391 fc56ef08 blueswir1
392 72cf2d4f Blue Swirl
#define QTAILQ_FOREACH_SAFE(var, head, field, next_var)                 \
393 fc56ef08 blueswir1
        for ((var) = ((head)->tqh_first);                               \
394 fc56ef08 blueswir1
                (var) && ((next_var) = ((var)->field.tqe_next), 1);     \
395 fc56ef08 blueswir1
                (var) = (next_var))
396 fc56ef08 blueswir1
397 72cf2d4f Blue Swirl
#define QTAILQ_FOREACH_REVERSE(var, head, headname, field)              \
398 fc56ef08 blueswir1
        for ((var) = (*(((struct headname *)((head)->tqh_last))->tqh_last));    \
399 fc56ef08 blueswir1
                (var);                                                  \
400 fc56ef08 blueswir1
                (var) = (*(((struct headname *)((var)->field.tqe_prev))->tqh_last)))
401 fc56ef08 blueswir1
402 fc56ef08 blueswir1
/*
403 fc56ef08 blueswir1
 * Tail queue access methods.
404 fc56ef08 blueswir1
 */
405 72cf2d4f Blue Swirl
#define QTAILQ_EMPTY(head)               ((head)->tqh_first == NULL)
406 72cf2d4f Blue Swirl
#define QTAILQ_FIRST(head)               ((head)->tqh_first)
407 72cf2d4f Blue Swirl
#define QTAILQ_NEXT(elm, field)          ((elm)->field.tqe_next)
408 fc56ef08 blueswir1
409 72cf2d4f Blue Swirl
#define QTAILQ_LAST(head, headname) \
410 fc56ef08 blueswir1
        (*(((struct headname *)((head)->tqh_last))->tqh_last))
411 72cf2d4f Blue Swirl
#define QTAILQ_PREV(elm, headname, field) \
412 fc56ef08 blueswir1
        (*(((struct headname *)((elm)->field.tqe_prev))->tqh_last))
413 fc56ef08 blueswir1
414 72cf2d4f Blue Swirl
#endif  /* !QEMU_SYS_QUEUE_H_ */