Statistics
| Branch: | Revision:

root / qemu-queue.h @ 6a957025

History | View | Annotate | Download (17.1 kB)

1
/*      $NetBSD: queue.h,v 1.45.14.1 2007/07/18 20:13:24 liamjfoy Exp $ */
2

    
3
/*
4
 * Qemu version: Copy from netbsd, removed debug code, removed some of
5
 * the implementations.  Left in lists, tail queues and circular queues.
6
 */
7

    
8
/*
9
 * Copyright (c) 1991, 1993
10
 *      The Regents of the University of California.  All rights reserved.
11
 *
12
 * Redistribution and use in source and binary forms, with or without
13
 * modification, are permitted provided that the following conditions
14
 * are met:
15
 * 1. Redistributions of source code must retain the above copyright
16
 *    notice, this list of conditions and the following disclaimer.
17
 * 2. Redistributions in binary form must reproduce the above copyright
18
 *    notice, this list of conditions and the following disclaimer in the
19
 *    documentation and/or other materials provided with the distribution.
20
 * 3. Neither the name of the University nor the names of its contributors
21
 *    may be used to endorse or promote products derived from this software
22
 *    without specific prior written permission.
23
 *
24
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34
 * SUCH DAMAGE.
35
 *
36
 *      @(#)queue.h     8.5 (Berkeley) 8/20/94
37
 */
38

    
39
#ifndef QEMU_SYS_QUEUE_H_
40
#define QEMU_SYS_QUEUE_H_
41

    
42
/*
43
 * This file defines three types of data structures:
44
 * lists, tail queues, and circular queues.
45
 *
46
 * A list is headed by a single forward pointer (or an array of forward
47
 * pointers for a hash table header). The elements are doubly linked
48
 * so that an arbitrary element can be removed without a need to
49
 * traverse the list. New elements can be added to the list before
50
 * or after an existing element or at the head of the list. A list
51
 * may only be traversed in the forward direction.
52
 *
53
 * A tail queue is headed by a pair of pointers, one to the head of the
54
 * list and the other to the tail of the list. The elements are doubly
55
 * linked so that an arbitrary element can be removed without a need to
56
 * traverse the list. New elements can be added to the list before or
57
 * after an existing element, at the head of the list, or at the end of
58
 * the list. A tail queue may be traversed in either direction.
59
 *
60
 * A circle queue is headed by a pair of pointers, one to the head of the
61
 * list and the other to the tail of the list. The elements are doubly
62
 * linked so that an arbitrary element can be removed without a need to
63
 * traverse the list. New elements can be added to the list before or after
64
 * an existing element, at the head of the list, or at the end of the list.
65
 * A circle queue may be traversed in either direction, but has a more
66
 * complex end of list detection.
67
 *
68
 * For details on the use of these macros, see the queue(3) manual page.
69
 */
70

    
71
/*
72
 * List definitions.
73
 */
74
#define QLIST_HEAD(name, type)                                          \
75
struct name {                                                           \
76
        struct type *lh_first;  /* first element */                     \
77
}
78

    
79
#define QLIST_HEAD_INITIALIZER(head)                                    \
80
        { NULL }
81

    
82
#define QLIST_ENTRY(type)                                               \
83
struct {                                                                \
84
        struct type *le_next;   /* next element */                      \
85
        struct type **le_prev;  /* address of previous next element */  \
86
}
87

    
88
/*
89
 * List functions.
90
 */
91
#define QLIST_INIT(head) do {                                           \
92
        (head)->lh_first = NULL;                                        \
93
} while (/*CONSTCOND*/0)
94

    
95
#define QLIST_INSERT_AFTER(listelm, elm, field) do {                    \
96
        if (((elm)->field.le_next = (listelm)->field.le_next) != NULL)  \
97
                (listelm)->field.le_next->field.le_prev =               \
98
                    &(elm)->field.le_next;                              \
99
        (listelm)->field.le_next = (elm);                               \
100
        (elm)->field.le_prev = &(listelm)->field.le_next;               \
101
} while (/*CONSTCOND*/0)
102

    
103
#define QLIST_INSERT_BEFORE(listelm, elm, field) do {                   \
104
        (elm)->field.le_prev = (listelm)->field.le_prev;                \
105
        (elm)->field.le_next = (listelm);                               \
106
        *(listelm)->field.le_prev = (elm);                              \
107
        (listelm)->field.le_prev = &(elm)->field.le_next;               \
108
} while (/*CONSTCOND*/0)
109

    
110
#define QLIST_INSERT_HEAD(head, elm, field) do {                        \
111
        if (((elm)->field.le_next = (head)->lh_first) != NULL)          \
112
                (head)->lh_first->field.le_prev = &(elm)->field.le_next;\
113
        (head)->lh_first = (elm);                                       \
114
        (elm)->field.le_prev = &(head)->lh_first;                       \
115
} while (/*CONSTCOND*/0)
116

    
117
#define QLIST_REMOVE(elm, field) do {                                   \
118
        if ((elm)->field.le_next != NULL)                               \
119
                (elm)->field.le_next->field.le_prev =                   \
120
                    (elm)->field.le_prev;                               \
121
        *(elm)->field.le_prev = (elm)->field.le_next;                   \
122
} while (/*CONSTCOND*/0)
123

    
124
#define QLIST_FOREACH(var, head, field)                                 \
125
        for ((var) = ((head)->lh_first);                                \
126
                (var);                                                  \
127
                (var) = ((var)->field.le_next))
128

    
129
#define QLIST_FOREACH_SAFE(var, head, field, next_var)                  \
130
        for ((var) = ((head)->lh_first);                                \
131
                (var) && ((next_var) = ((var)->field.le_next), 1);      \
132
                (var) = (next_var))
133

    
134
/*
135
 * List access methods.
136
 */
137
#define QLIST_EMPTY(head)                ((head)->lh_first == NULL)
138
#define QLIST_FIRST(head)                ((head)->lh_first)
139
#define QLIST_NEXT(elm, field)           ((elm)->field.le_next)
140

    
141

    
142
/*
143
 * Tail queue definitions.
144
 */
145
#define Q_TAILQ_HEAD(name, type, qual)                                  \
146
struct name {                                                           \
147
        qual type *tqh_first;           /* first element */             \
148
        qual type *qual *tqh_last;      /* addr of last next element */ \
149
}
150
#define QTAILQ_HEAD(name, type)  Q_TAILQ_HEAD(name, struct type,)
151

    
152
#define QTAILQ_HEAD_INITIALIZER(head)                                   \
153
        { NULL, &(head).tqh_first }
154

    
155
#define Q_TAILQ_ENTRY(type, qual)                                       \
156
struct {                                                                \
157
        qual type *tqe_next;            /* next element */              \
158
        qual type *qual *tqe_prev;      /* address of previous next element */\
159
}
160
#define QTAILQ_ENTRY(type)       Q_TAILQ_ENTRY(struct type,)
161

    
162
/*
163
 * Tail queue functions.
164
 */
165
#define QTAILQ_INIT(head) do {                                          \
166
        (head)->tqh_first = NULL;                                       \
167
        (head)->tqh_last = &(head)->tqh_first;                          \
168
} while (/*CONSTCOND*/0)
169

    
170
#define QTAILQ_INSERT_HEAD(head, elm, field) do {                       \
171
        if (((elm)->field.tqe_next = (head)->tqh_first) != NULL)        \
172
                (head)->tqh_first->field.tqe_prev =                     \
173
                    &(elm)->field.tqe_next;                             \
174
        else                                                            \
175
                (head)->tqh_last = &(elm)->field.tqe_next;              \
176
        (head)->tqh_first = (elm);                                      \
177
        (elm)->field.tqe_prev = &(head)->tqh_first;                     \
178
} while (/*CONSTCOND*/0)
179

    
180
#define QTAILQ_INSERT_TAIL(head, elm, field) do {                       \
181
        (elm)->field.tqe_next = NULL;                                   \
182
        (elm)->field.tqe_prev = (head)->tqh_last;                       \
183
        *(head)->tqh_last = (elm);                                      \
184
        (head)->tqh_last = &(elm)->field.tqe_next;                      \
185
} while (/*CONSTCOND*/0)
186

    
187
#define QTAILQ_INSERT_AFTER(head, listelm, elm, field) do {             \
188
        if (((elm)->field.tqe_next = (listelm)->field.tqe_next) != NULL)\
189
                (elm)->field.tqe_next->field.tqe_prev =                 \
190
                    &(elm)->field.tqe_next;                             \
191
        else                                                            \
192
                (head)->tqh_last = &(elm)->field.tqe_next;              \
193
        (listelm)->field.tqe_next = (elm);                              \
194
        (elm)->field.tqe_prev = &(listelm)->field.tqe_next;             \
195
} while (/*CONSTCOND*/0)
196

    
197
#define QTAILQ_INSERT_BEFORE(listelm, elm, field) do {                  \
198
        (elm)->field.tqe_prev = (listelm)->field.tqe_prev;              \
199
        (elm)->field.tqe_next = (listelm);                              \
200
        *(listelm)->field.tqe_prev = (elm);                             \
201
        (listelm)->field.tqe_prev = &(elm)->field.tqe_next;             \
202
} while (/*CONSTCOND*/0)
203

    
204
#define QTAILQ_REMOVE(head, elm, field) do {                            \
205
        if (((elm)->field.tqe_next) != NULL)                            \
206
                (elm)->field.tqe_next->field.tqe_prev =                 \
207
                    (elm)->field.tqe_prev;                              \
208
        else                                                            \
209
                (head)->tqh_last = (elm)->field.tqe_prev;               \
210
        *(elm)->field.tqe_prev = (elm)->field.tqe_next;                 \
211
} while (/*CONSTCOND*/0)
212

    
213
#define QTAILQ_FOREACH(var, head, field)                                \
214
        for ((var) = ((head)->tqh_first);                               \
215
                (var);                                                  \
216
                (var) = ((var)->field.tqe_next))
217

    
218
#define QTAILQ_FOREACH_SAFE(var, head, field, next_var)                 \
219
        for ((var) = ((head)->tqh_first);                               \
220
                (var) && ((next_var) = ((var)->field.tqe_next), 1);     \
221
                (var) = (next_var))
222

    
223
#define QTAILQ_FOREACH_REVERSE(var, head, headname, field)              \
224
        for ((var) = (*(((struct headname *)((head)->tqh_last))->tqh_last));    \
225
                (var);                                                  \
226
                (var) = (*(((struct headname *)((var)->field.tqe_prev))->tqh_last)))
227

    
228
/*
229
 * Tail queue access methods.
230
 */
231
#define QTAILQ_EMPTY(head)               ((head)->tqh_first == NULL)
232
#define QTAILQ_FIRST(head)               ((head)->tqh_first)
233
#define QTAILQ_NEXT(elm, field)          ((elm)->field.tqe_next)
234

    
235
#define QTAILQ_LAST(head, headname) \
236
        (*(((struct headname *)((head)->tqh_last))->tqh_last))
237
#define QTAILQ_PREV(elm, headname, field) \
238
        (*(((struct headname *)((elm)->field.tqe_prev))->tqh_last))
239

    
240

    
241
/*
242
 * Circular queue definitions.
243
 */
244
#define QCIRCLEQ_HEAD(name, type)                                       \
245
struct name {                                                           \
246
        struct type *cqh_first;         /* first element */             \
247
        struct type *cqh_last;          /* last element */              \
248
}
249

    
250
#define QCIRCLEQ_HEAD_INITIALIZER(head)                                 \
251
        { (void *)&head, (void *)&head }
252

    
253
#define QCIRCLEQ_ENTRY(type)                                            \
254
struct {                                                                \
255
        struct type *cqe_next;          /* next element */              \
256
        struct type *cqe_prev;          /* previous element */          \
257
}
258

    
259
/*
260
 * Circular queue functions.
261
 */
262
#define QCIRCLEQ_INIT(head) do {                                        \
263
        (head)->cqh_first = (void *)(head);                             \
264
        (head)->cqh_last = (void *)(head);                              \
265
} while (/*CONSTCOND*/0)
266

    
267
#define QCIRCLEQ_INSERT_AFTER(head, listelm, elm, field) do {           \
268
        (elm)->field.cqe_next = (listelm)->field.cqe_next;              \
269
        (elm)->field.cqe_prev = (listelm);                              \
270
        if ((listelm)->field.cqe_next == (void *)(head))                \
271
                (head)->cqh_last = (elm);                               \
272
        else                                                            \
273
                (listelm)->field.cqe_next->field.cqe_prev = (elm);      \
274
        (listelm)->field.cqe_next = (elm);                              \
275
} while (/*CONSTCOND*/0)
276

    
277
#define QCIRCLEQ_INSERT_BEFORE(head, listelm, elm, field) do {          \
278
        (elm)->field.cqe_next = (listelm);                              \
279
        (elm)->field.cqe_prev = (listelm)->field.cqe_prev;              \
280
        if ((listelm)->field.cqe_prev == (void *)(head))                \
281
                (head)->cqh_first = (elm);                              \
282
        else                                                            \
283
                (listelm)->field.cqe_prev->field.cqe_next = (elm);      \
284
        (listelm)->field.cqe_prev = (elm);                              \
285
} while (/*CONSTCOND*/0)
286

    
287
#define QCIRCLEQ_INSERT_HEAD(head, elm, field) do {                     \
288
        (elm)->field.cqe_next = (head)->cqh_first;                      \
289
        (elm)->field.cqe_prev = (void *)(head);                         \
290
        if ((head)->cqh_last == (void *)(head))                         \
291
                (head)->cqh_last = (elm);                               \
292
        else                                                            \
293
                (head)->cqh_first->field.cqe_prev = (elm);              \
294
        (head)->cqh_first = (elm);                                      \
295
} while (/*CONSTCOND*/0)
296

    
297
#define QCIRCLEQ_INSERT_TAIL(head, elm, field) do {                     \
298
        (elm)->field.cqe_next = (void *)(head);                         \
299
        (elm)->field.cqe_prev = (head)->cqh_last;                       \
300
        if ((head)->cqh_first == (void *)(head))                        \
301
                (head)->cqh_first = (elm);                              \
302
        else                                                            \
303
                (head)->cqh_last->field.cqe_next = (elm);               \
304
        (head)->cqh_last = (elm);                                       \
305
} while (/*CONSTCOND*/0)
306

    
307
#define QCIRCLEQ_REMOVE(head, elm, field) do {                          \
308
        if ((elm)->field.cqe_next == (void *)(head))                    \
309
                (head)->cqh_last = (elm)->field.cqe_prev;               \
310
        else                                                            \
311
                (elm)->field.cqe_next->field.cqe_prev =                 \
312
                    (elm)->field.cqe_prev;                              \
313
        if ((elm)->field.cqe_prev == (void *)(head))                    \
314
                (head)->cqh_first = (elm)->field.cqe_next;              \
315
        else                                                            \
316
                (elm)->field.cqe_prev->field.cqe_next =                 \
317
                    (elm)->field.cqe_next;                              \
318
} while (/*CONSTCOND*/0)
319

    
320
#define QCIRCLEQ_FOREACH(var, head, field)                              \
321
        for ((var) = ((head)->cqh_first);                               \
322
                (var) != (const void *)(head);                          \
323
                (var) = ((var)->field.cqe_next))
324

    
325
#define QCIRCLEQ_FOREACH_REVERSE(var, head, field)                      \
326
        for ((var) = ((head)->cqh_last);                                \
327
                (var) != (const void *)(head);                          \
328
                (var) = ((var)->field.cqe_prev))
329

    
330
/*
331
 * Circular queue access methods.
332
 */
333
#define QCIRCLEQ_EMPTY(head)             ((head)->cqh_first == (void *)(head))
334
#define QCIRCLEQ_FIRST(head)             ((head)->cqh_first)
335
#define QCIRCLEQ_LAST(head)              ((head)->cqh_last)
336
#define QCIRCLEQ_NEXT(elm, field)        ((elm)->field.cqe_next)
337
#define QCIRCLEQ_PREV(elm, field)        ((elm)->field.cqe_prev)
338

    
339
#define QCIRCLEQ_LOOP_NEXT(head, elm, field)                            \
340
        (((elm)->field.cqe_next == (void *)(head))                      \
341
            ? ((head)->cqh_first)                                       \
342
            : (elm->field.cqe_next))
343
#define QCIRCLEQ_LOOP_PREV(head, elm, field)                            \
344
        (((elm)->field.cqe_prev == (void *)(head))                      \
345
            ? ((head)->cqh_last)                                        \
346
            : (elm->field.cqe_prev))
347

    
348
#endif  /* !QEMU_SYS_QUEUE_H_ */