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 singly-linked lists, lists, simple
6 * queues, and tail 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: singly-linked lists,
45 * lists, simple queues, and tail queues.
46 *
47 * A singly-linked list is headed by a single forward pointer. The
48 * elements are singly linked for minimum space and pointer manipulation
49 * overhead at the expense of O(n) removal for arbitrary elements. New
50 * elements can be added to the list after an existing element or at the
51 * head of the list. Elements being removed from the head of the list
52 * should use the explicit macro for this purpose for optimum
53 * efficiency. A singly-linked list may only be traversed in the forward
54 * direction. Singly-linked lists are ideal for applications with large
55 * datasets and few or no removals or for implementing a LIFO queue.
56 *
57 * A list is headed by a single forward pointer (or an array of forward
58 * pointers for a hash table header). The elements are doubly linked
59 * so that an arbitrary element can be removed without a need to
60 * traverse the list. New elements can be added to the list before
61 * or after an existing element or at the head of the list. A list
62 * may only be traversed in the forward direction.
63 *
64 * A simple queue is headed by a pair of pointers, one the head of the
65 * list and the other to the tail of the list. The elements are singly
66 * linked to save space, so elements can only be removed from the
67 * head of the list. New elements can be added to the list after
68 * an existing element, at the head of the list, or at the end of the
69 * list. A simple queue may only be traversed in the forward direction.
70 *
71 * A tail queue is headed by a pair of pointers, one to the head of the
72 * list and the other to the tail of the list. The elements are doubly
73 * linked so that an arbitrary element can be removed without a need to
74 * traverse the list. New elements can be added to the list before or
75 * after an existing element, at the head of the list, or at the end of
76 * the list. A tail queue may be traversed in either direction.
77 *
78 * For details on the use of these macros, see the queue(3) manual page.
79 */
80
81/*
82 * List definitions.
83 */
84#define QLIST_HEAD(name, type) \
85struct name { \
86 struct type *lh_first; /* first element */ \
87}
88
89#define QLIST_HEAD_INITIALIZER(head) \
90 { NULL }
91
92#define QLIST_ENTRY(type) \
93struct { \
94 struct type *le_next; /* next element */ \
95 struct type **le_prev; /* address of previous next element */ \
96}
97
98/*
99 * List functions.
100 */
101#define QLIST_INIT(head) do { \
102 (head)->lh_first = NULL; \
103} while (/*CONSTCOND*/0)
104
105#define QLIST_SWAP(dstlist, srclist, field) do { \
106 void *tmplist; \
107 tmplist = (srclist)->lh_first; \
108 (srclist)->lh_first = (dstlist)->lh_first; \
109 if ((srclist)->lh_first != NULL) { \
110 (srclist)->lh_first->field.le_prev = &(srclist)->lh_first; \
111 } \
112 (dstlist)->lh_first = tmplist; \
113 if ((dstlist)->lh_first != NULL) { \
114 (dstlist)->lh_first->field.le_prev = &(dstlist)->lh_first; \
115 } \
116} while (/*CONSTCOND*/0)
117
118#define QLIST_INSERT_AFTER(listelm, elm, field) do { \
119 if (((elm)->field.le_next = (listelm)->field.le_next) != NULL) \
120 (listelm)->field.le_next->field.le_prev = \
121 &(elm)->field.le_next; \
122 (listelm)->field.le_next = (elm); \
123 (elm)->field.le_prev = &(listelm)->field.le_next; \
124} while (/*CONSTCOND*/0)
125
126#define QLIST_INSERT_BEFORE(listelm, elm, field) do { \
127 (elm)->field.le_prev = (listelm)->field.le_prev; \
128 (elm)->field.le_next = (listelm); \
129 *(listelm)->field.le_prev = (elm); \
130 (listelm)->field.le_prev = &(elm)->field.le_next; \
131} while (/*CONSTCOND*/0)
132
133#define QLIST_INSERT_HEAD(head, elm, field) do { \
134 if (((elm)->field.le_next = (head)->lh_first) != NULL) \
135 (head)->lh_first->field.le_prev = &(elm)->field.le_next;\
136 (head)->lh_first = (elm); \
137 (elm)->field.le_prev = &(head)->lh_first; \
138} while (/*CONSTCOND*/0)
139
140#define QLIST_REMOVE(elm, field) do { \
141 if ((elm)->field.le_next != NULL) \
142 (elm)->field.le_next->field.le_prev = \
143 (elm)->field.le_prev; \
144 *(elm)->field.le_prev = (elm)->field.le_next; \
145} while (/*CONSTCOND*/0)
146
147#define QLIST_FOREACH(var, head, field) \
148 for ((var) = ((head)->lh_first); \
149 (var); \
150 (var) = ((var)->field.le_next))
151
152#define QLIST_FOREACH_SAFE(var, head, field, next_var) \
153 for ((var) = ((head)->lh_first); \
154 (var) && ((next_var) = ((var)->field.le_next), 1); \
155 (var) = (next_var))
156
157/*
158 * List access methods.
159 */
160#define QLIST_EMPTY(head) ((head)->lh_first == NULL)
161#define QLIST_FIRST(head) ((head)->lh_first)
162#define QLIST_NEXT(elm, field) ((elm)->field.le_next)
163
164
165/*
166 * Singly-linked List definitions.
167 */
168#define QSLIST_HEAD(name, type) \
169struct name { \
170 struct type *slh_first; /* first element */ \
171}
172
173#define QSLIST_HEAD_INITIALIZER(head) \
174 { NULL }
175
176#define QSLIST_ENTRY(type) \
177struct { \
178 struct type *sle_next; /* next element */ \
179}
180
181/*
182 * Singly-linked List functions.
183 */
184#define QSLIST_INIT(head) do { \
185 (head)->slh_first = NULL; \
186} while (/*CONSTCOND*/0)
187
188#define QSLIST_INSERT_AFTER(slistelm, elm, field) do { \
189 (elm)->field.sle_next = (slistelm)->field.sle_next; \
190 (slistelm)->field.sle_next = (elm); \
191} while (/*CONSTCOND*/0)
192
193#define QSLIST_INSERT_HEAD(head, elm, field) do { \
194 (elm)->field.sle_next = (head)->slh_first; \
195 (head)->slh_first = (elm); \
196} while (/*CONSTCOND*/0)
197
198#define QSLIST_INSERT_HEAD_ATOMIC(head, elm, field) do { \
199 typeof(elm) save_sle_next; \
200 do { \
201 save_sle_next = (elm)->field.sle_next = (head)->slh_first; \
202 } while (atomic_cmpxchg(&(head)->slh_first, save_sle_next, (elm)) != \
203 save_sle_next); \
204} while (/*CONSTCOND*/0)
205
206#define QSLIST_MOVE_ATOMIC(dest, src) do { \
207 (dest)->slh_first = atomic_xchg(&(src)->slh_first, NULL); \
208} while (/*CONSTCOND*/0)
209
210#define QSLIST_REMOVE_HEAD(head, field) do { \
211 (head)->slh_first = (head)->slh_first->field.sle_next; \
212} while (/*CONSTCOND*/0)
213
214#define QSLIST_REMOVE_AFTER(slistelm, field) do { \
215 (slistelm)->field.sle_next = \
216 QSLIST_NEXT(QSLIST_NEXT((slistelm), field), field); \
217} while (/*CONSTCOND*/0)
218
219#define QSLIST_FOREACH(var, head, field) \
220 for((var) = (head)->slh_first; (var); (var) = (var)->field.sle_next)
221
222#define QSLIST_FOREACH_SAFE(var, head, field, tvar) \
223 for ((var) = QSLIST_FIRST((head)); \
224 (var) && ((tvar) = QSLIST_NEXT((var), field), 1); \
225 (var) = (tvar))
226
227/*
228 * Singly-linked List access methods.
229 */
230#define QSLIST_EMPTY(head) ((head)->slh_first == NULL)
231#define QSLIST_FIRST(head) ((head)->slh_first)
232#define QSLIST_NEXT(elm, field) ((elm)->field.sle_next)
233
234
235/*
236 * Simple queue definitions.
237 */
238#define QSIMPLEQ_HEAD(name, type) \
239struct name { \
240 struct type *sqh_first; /* first element */ \
241 struct type **sqh_last; /* addr of last next element */ \
242}
243
244#define QSIMPLEQ_HEAD_INITIALIZER(head) \
245 { NULL, &(head).sqh_first }
246
247#define QSIMPLEQ_ENTRY(type) \
248struct { \
249 struct type *sqe_next; /* next element */ \
250}
251
252/*
253 * Simple queue functions.
254 */
255#define QSIMPLEQ_INIT(head) do { \
256 (head)->sqh_first = NULL; \
257 (head)->sqh_last = &(head)->sqh_first; \
258} while (/*CONSTCOND*/0)
259
260#define QSIMPLEQ_INSERT_HEAD(head, elm, field) do { \
261 if (((elm)->field.sqe_next = (head)->sqh_first) == NULL) \
262 (head)->sqh_last = &(elm)->field.sqe_next; \
263 (head)->sqh_first = (elm); \
264} while (/*CONSTCOND*/0)
265
266#define QSIMPLEQ_INSERT_TAIL(head, elm, field) do { \
267 (elm)->field.sqe_next = NULL; \
268 *(head)->sqh_last = (elm); \
269 (head)->sqh_last = &(elm)->field.sqe_next; \
270} while (/*CONSTCOND*/0)
271
272#define QSIMPLEQ_INSERT_AFTER(head, listelm, elm, field) do { \
273 if (((elm)->field.sqe_next = (listelm)->field.sqe_next) == NULL) \
274 (head)->sqh_last = &(elm)->field.sqe_next; \
275 (listelm)->field.sqe_next = (elm); \
276} while (/*CONSTCOND*/0)
277
278#define QSIMPLEQ_REMOVE_HEAD(head, field) do { \
279 if (((head)->sqh_first = (head)->sqh_first->field.sqe_next) == NULL)\
280 (head)->sqh_last = &(head)->sqh_first; \
281} while (/*CONSTCOND*/0)
282
283#define QSIMPLEQ_SPLIT_AFTER(head, elm, field, removed) do { \
284 QSIMPLEQ_INIT(removed); \
285 if (((removed)->sqh_first = (head)->sqh_first) != NULL) { \
286 if (((head)->sqh_first = (elm)->field.sqe_next) == NULL) { \
287 (head)->sqh_last = &(head)->sqh_first; \
288 } \
289 (removed)->sqh_last = &(elm)->field.sqe_next; \
290 (elm)->field.sqe_next = NULL; \
291 } \
292} while (/*CONSTCOND*/0)
293
294#define QSIMPLEQ_REMOVE(head, elm, type, field) do { \
295 if ((head)->sqh_first == (elm)) { \
296 QSIMPLEQ_REMOVE_HEAD((head), field); \
297 } else { \
298 struct type *curelm = (head)->sqh_first; \
299 while (curelm->field.sqe_next != (elm)) \
300 curelm = curelm->field.sqe_next; \
301 if ((curelm->field.sqe_next = \
302 curelm->field.sqe_next->field.sqe_next) == NULL) \
303 (head)->sqh_last = &(curelm)->field.sqe_next; \
304 } \
305} while (/*CONSTCOND*/0)
306
307#define QSIMPLEQ_FOREACH(var, head, field) \
308 for ((var) = ((head)->sqh_first); \
309 (var); \
310 (var) = ((var)->field.sqe_next))
311
312#define QSIMPLEQ_FOREACH_SAFE(var, head, field, next) \
313 for ((var) = ((head)->sqh_first); \
314 (var) && ((next = ((var)->field.sqe_next)), 1); \
315 (var) = (next))
316
317#define QSIMPLEQ_CONCAT(head1, head2) do { \
318 if (!QSIMPLEQ_EMPTY((head2))) { \
319 *(head1)->sqh_last = (head2)->sqh_first; \
320 (head1)->sqh_last = (head2)->sqh_last; \
321 QSIMPLEQ_INIT((head2)); \
322 } \
323} while (/*CONSTCOND*/0)
324
325#define QSIMPLEQ_PREPEND(head1, head2) do { \
326 if (!QSIMPLEQ_EMPTY((head2))) { \
327 *(head2)->sqh_last = (head1)->sqh_first; \
328 (head1)->sqh_first = (head2)->sqh_first; \
329 QSIMPLEQ_INIT((head2)); \
330 } \
331} while (/*CONSTCOND*/0)
332
333#define QSIMPLEQ_LAST(head, type, field) \
334 (QSIMPLEQ_EMPTY((head)) ? \
335 NULL : \
336 ((struct type *)(void *) \
337 ((char *)((head)->sqh_last) - offsetof(struct type, field))))
338
339/*
340 * Simple queue access methods.
341 */
342#define QSIMPLEQ_EMPTY_ATOMIC(head) (atomic_read(&((head)->sqh_first)) == NULL)
343#define QSIMPLEQ_EMPTY(head) ((head)->sqh_first == NULL)
344#define QSIMPLEQ_FIRST(head) ((head)->sqh_first)
345#define QSIMPLEQ_NEXT(elm, field) ((elm)->field.sqe_next)
346
347typedef struct QTailQLink {
348 void *tql_next;
349 struct QTailQLink *tql_prev;
350} QTailQLink;
351
352/*
353 * Tail queue definitions. The union acts as a poor man template, as if
354 * it were QTailQLink<type>.
355 */
356#define QTAILQ_HEAD(name, type) \
357union name { \
358 struct type *tqh_first; /* first element */ \
359 QTailQLink tqh_circ; /* link for circular backwards list */ \
360}
361
362#define QTAILQ_HEAD_INITIALIZER(head) \
363 { .tqh_circ = { NULL, &(head).tqh_circ } }
364
365#define QTAILQ_ENTRY(type) \
366union { \
367 struct type *tqe_next; /* next element */ \
368 QTailQLink tqe_circ; /* link for circular backwards list */ \
369}
370
371/*
372 * Tail queue functions.
373 */
374#define QTAILQ_INIT(head) do { \
375 (head)->tqh_first = NULL; \
376 (head)->tqh_circ.tql_prev = &(head)->tqh_circ; \
377} while (/*CONSTCOND*/0)
378
379#define QTAILQ_INSERT_HEAD(head, elm, field) do { \
380 if (((elm)->field.tqe_next = (head)->tqh_first) != NULL) \
381 (head)->tqh_first->field.tqe_circ.tql_prev = \
382 &(elm)->field.tqe_circ; \
383 else \
384 (head)->tqh_circ.tql_prev = &(elm)->field.tqe_circ; \
385 (head)->tqh_first = (elm); \
386 (elm)->field.tqe_circ.tql_prev = &(head)->tqh_circ; \
387} while (/*CONSTCOND*/0)
388
389#define QTAILQ_INSERT_TAIL(head, elm, field) do { \
390 (elm)->field.tqe_next = NULL; \
391 (elm)->field.tqe_circ.tql_prev = (head)->tqh_circ.tql_prev; \
392 (head)->tqh_circ.tql_prev->tql_next = (elm); \
393 (head)->tqh_circ.tql_prev = &(elm)->field.tqe_circ; \
394} while (/*CONSTCOND*/0)
395
396#define QTAILQ_INSERT_AFTER(head, listelm, elm, field) do { \
397 if (((elm)->field.tqe_next = (listelm)->field.tqe_next) != NULL)\
398 (elm)->field.tqe_next->field.tqe_circ.tql_prev = \
399 &(elm)->field.tqe_circ; \
400 else \
401 (head)->tqh_circ.tql_prev = &(elm)->field.tqe_circ; \
402 (listelm)->field.tqe_next = (elm); \
403 (elm)->field.tqe_circ.tql_prev = &(listelm)->field.tqe_circ; \
404} while (/*CONSTCOND*/0)
405
406#define QTAILQ_INSERT_BEFORE(listelm, elm, field) do { \
407 (elm)->field.tqe_circ.tql_prev = (listelm)->field.tqe_circ.tql_prev; \
408 (elm)->field.tqe_next = (listelm); \
409 (listelm)->field.tqe_circ.tql_prev->tql_next = (elm); \
410 (listelm)->field.tqe_circ.tql_prev = &(elm)->field.tqe_circ; \
411} while (/*CONSTCOND*/0)
412
413#define QTAILQ_REMOVE(head, elm, field) do { \
414 if (((elm)->field.tqe_next) != NULL) \
415 (elm)->field.tqe_next->field.tqe_circ.tql_prev = \
416 (elm)->field.tqe_circ.tql_prev; \
417 else \
418 (head)->tqh_circ.tql_prev = (elm)->field.tqe_circ.tql_prev; \
419 (elm)->field.tqe_circ.tql_prev->tql_next = (elm)->field.tqe_next; \
420 (elm)->field.tqe_circ.tql_prev = NULL; \
421} while (/*CONSTCOND*/0)
422
423#define QTAILQ_FOREACH(var, head, field) \
424 for ((var) = ((head)->tqh_first); \
425 (var); \
426 (var) = ((var)->field.tqe_next))
427
428#define QTAILQ_FOREACH_SAFE(var, head, field, next_var) \
429 for ((var) = ((head)->tqh_first); \
430 (var) && ((next_var) = ((var)->field.tqe_next), 1); \
431 (var) = (next_var))
432
433#define QTAILQ_FOREACH_REVERSE(var, head, field) \
434 for ((var) = QTAILQ_LAST(head); \
435 (var); \
436 (var) = QTAILQ_PREV(var, field))
437
438#define QTAILQ_FOREACH_REVERSE_SAFE(var, head, field, prev_var) \
439 for ((var) = QTAILQ_LAST(head); \
440 (var) && ((prev_var) = QTAILQ_PREV(var, field), 1); \
441 (var) = (prev_var))
442
443/*
444 * Tail queue access methods.
445 */
446#define QTAILQ_EMPTY(head) ((head)->tqh_first == NULL)
447#define QTAILQ_FIRST(head) ((head)->tqh_first)
448#define QTAILQ_NEXT(elm, field) ((elm)->field.tqe_next)
449#define QTAILQ_IN_USE(elm, field) ((elm)->field.tqe_circ.tql_prev != NULL)
450
451#define QTAILQ_LINK_PREV(link) \
452 ((link).tql_prev->tql_prev->tql_next)
453#define QTAILQ_LAST(head) \
454 ((typeof((head)->tqh_first)) QTAILQ_LINK_PREV((head)->tqh_circ))
455#define QTAILQ_PREV(elm, field) \
456 ((typeof((elm)->field.tqe_next)) QTAILQ_LINK_PREV((elm)->field.tqe_circ))
457
458#define field_at_offset(base, offset, type) \
459 ((type *) (((char *) (base)) + (offset)))
460
461/*
462 * Raw access of elements of a tail queue head. Offsets are all zero
463 * because it's a union.
464 */
465#define QTAILQ_RAW_FIRST(head) \
466 field_at_offset(head, 0, void *)
467#define QTAILQ_RAW_TQH_CIRC(head) \
468 field_at_offset(head, 0, QTailQLink)
469
470/*
471 * Raw access of elements of a tail entry
472 */
473#define QTAILQ_RAW_NEXT(elm, entry) \
474 field_at_offset(elm, entry, void *)
475#define QTAILQ_RAW_TQE_CIRC(elm, entry) \
476 field_at_offset(elm, entry, QTailQLink)
477/*
478 * Tail queue traversal using pointer arithmetic.
479 */
480#define QTAILQ_RAW_FOREACH(elm, head, entry) \
481 for ((elm) = *QTAILQ_RAW_FIRST(head); \
482 (elm); \
483 (elm) = *QTAILQ_RAW_NEXT(elm, entry))
484/*
485 * Tail queue insertion using pointer arithmetic.
486 */
487#define QTAILQ_RAW_INSERT_TAIL(head, elm, entry) do { \
488 *QTAILQ_RAW_NEXT(elm, entry) = NULL; \
489 QTAILQ_RAW_TQE_CIRC(elm, entry)->tql_prev = QTAILQ_RAW_TQH_CIRC(head)->tql_prev; \
490 QTAILQ_RAW_TQH_CIRC(head)->tql_prev->tql_next = (elm); \
491 QTAILQ_RAW_TQH_CIRC(head)->tql_prev = QTAILQ_RAW_TQE_CIRC(elm, entry); \
492} while (/*CONSTCOND*/0)
493
494#endif /* QEMU_SYS_QUEUE_H */
495