1 /* $NetBSD: queue.h,v 1.52 2009/04/20 09:56:08 mschuett Exp $ */
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.
10 * Copyright (c) 1991, 1993
11 * The Regents of the University of California. All rights reserved.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
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.
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
37 * @(#)queue.h 8.5 (Berkeley) 8/20/94
40 #ifndef QEMU_SYS_QUEUE_H_
41 #define QEMU_SYS_QUEUE_H_
44 * This file defines four types of data structures: singly-linked lists,
45 * lists, simple queues, and tail queues.
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.
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.
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.
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.
78 * For details on the use of these macros, see the queue(3) manual page.
81 #include "qemu/atomic.h" /* for smp_wmb() */
86 #define QLIST_HEAD(name, type) \
88 struct type *lh_first; /* first element */ \
91 #define QLIST_HEAD_INITIALIZER(head) \
94 #define QLIST_ENTRY(type) \
96 struct type *le_next; /* next element */ \
97 struct type **le_prev; /* address of previous next element */ \
103 #define QLIST_INIT(head) do { \
104 (head)->lh_first = NULL; \
105 } while (/*CONSTCOND*/0)
107 #define QLIST_INSERT_AFTER(listelm, elm, field) do { \
108 if (((elm)->field.le_next = (listelm)->field.le_next) != NULL) \
109 (listelm)->field.le_next->field.le_prev = \
110 &(elm)->field.le_next; \
111 (listelm)->field.le_next = (elm); \
112 (elm)->field.le_prev = &(listelm)->field.le_next; \
113 } while (/*CONSTCOND*/0)
115 #define QLIST_INSERT_BEFORE(listelm, elm, field) do { \
116 (elm)->field.le_prev = (listelm)->field.le_prev; \
117 (elm)->field.le_next = (listelm); \
118 *(listelm)->field.le_prev = (elm); \
119 (listelm)->field.le_prev = &(elm)->field.le_next; \
120 } while (/*CONSTCOND*/0)
122 #define QLIST_INSERT_HEAD(head, elm, field) do { \
123 if (((elm)->field.le_next = (head)->lh_first) != NULL) \
124 (head)->lh_first->field.le_prev = &(elm)->field.le_next;\
125 (head)->lh_first = (elm); \
126 (elm)->field.le_prev = &(head)->lh_first; \
127 } while (/*CONSTCOND*/0)
129 #define QLIST_INSERT_HEAD_RCU(head, elm, field) do { \
130 (elm)->field.le_prev = &(head)->lh_first; \
131 (elm)->field.le_next = (head)->lh_first; \
132 smp_wmb(); /* fill elm before linking it */ \
133 if ((head)->lh_first != NULL) { \
134 (head)->lh_first->field.le_prev = &(elm)->field.le_next; \
136 (head)->lh_first = (elm); \
138 } while (/* CONSTCOND*/0)
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)
147 #define QLIST_FOREACH(var, head, field) \
148 for ((var) = ((head)->lh_first); \
150 (var) = ((var)->field.le_next))
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); \
158 * List access methods.
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)
166 * Singly-linked List definitions.
168 #define QSLIST_HEAD(name, type) \
170 struct type *slh_first; /* first element */ \
173 #define QSLIST_HEAD_INITIALIZER(head) \
176 #define QSLIST_ENTRY(type) \
178 struct type *sle_next; /* next element */ \
182 * Singly-linked List functions.
184 #define QSLIST_INIT(head) do { \
185 (head)->slh_first = NULL; \
186 } while (/*CONSTCOND*/0)
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)
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)
198 #define QSLIST_REMOVE_HEAD(head, field) do { \
199 (head)->slh_first = (head)->slh_first->field.sle_next; \
200 } while (/*CONSTCOND*/0)
202 #define QSLIST_REMOVE_AFTER(slistelm, field) do { \
203 (slistelm)->field.sle_next = \
204 QSLIST_NEXT(QSLIST_NEXT((slistelm), field), field); \
205 } while (/*CONSTCOND*/0)
207 #define QSLIST_FOREACH(var, head, field) \
208 for((var) = (head)->slh_first; (var); (var) = (var)->field.sle_next)
210 #define QSLIST_FOREACH_SAFE(var, head, field, tvar) \
211 for ((var) = QSLIST_FIRST((head)); \
212 (var) && ((tvar) = QSLIST_NEXT((var), field), 1); \
216 * Singly-linked List access methods.
218 #define QSLIST_EMPTY(head) ((head)->slh_first == NULL)
219 #define QSLIST_FIRST(head) ((head)->slh_first)
220 #define QSLIST_NEXT(elm, field) ((elm)->field.sle_next)
224 * Simple queue definitions.
226 #define QSIMPLEQ_HEAD(name, type) \
228 struct type *sqh_first; /* first element */ \
229 struct type **sqh_last; /* addr of last next element */ \
232 #define QSIMPLEQ_HEAD_INITIALIZER(head) \
233 { NULL, &(head).sqh_first }
235 #define QSIMPLEQ_ENTRY(type) \
237 struct type *sqe_next; /* next element */ \
241 * Simple queue functions.
243 #define QSIMPLEQ_INIT(head) do { \
244 (head)->sqh_first = NULL; \
245 (head)->sqh_last = &(head)->sqh_first; \
246 } while (/*CONSTCOND*/0)
248 #define QSIMPLEQ_INSERT_HEAD(head, elm, field) do { \
249 if (((elm)->field.sqe_next = (head)->sqh_first) == NULL) \
250 (head)->sqh_last = &(elm)->field.sqe_next; \
251 (head)->sqh_first = (elm); \
252 } while (/*CONSTCOND*/0)
254 #define QSIMPLEQ_INSERT_TAIL(head, elm, field) do { \
255 (elm)->field.sqe_next = NULL; \
256 *(head)->sqh_last = (elm); \
257 (head)->sqh_last = &(elm)->field.sqe_next; \
258 } while (/*CONSTCOND*/0)
260 #define QSIMPLEQ_INSERT_AFTER(head, listelm, elm, field) do { \
261 if (((elm)->field.sqe_next = (listelm)->field.sqe_next) == NULL) \
262 (head)->sqh_last = &(elm)->field.sqe_next; \
263 (listelm)->field.sqe_next = (elm); \
264 } while (/*CONSTCOND*/0)
266 #define QSIMPLEQ_REMOVE_HEAD(head, field) do { \
267 if (((head)->sqh_first = (head)->sqh_first->field.sqe_next) == NULL)\
268 (head)->sqh_last = &(head)->sqh_first; \
269 } while (/*CONSTCOND*/0)
271 #define QSIMPLEQ_SPLIT_AFTER(head, elm, field, removed) do { \
272 QSIMPLEQ_INIT(removed); \
273 if (((removed)->sqh_first = (head)->sqh_first) != NULL) { \
274 if (((head)->sqh_first = (elm)->field.sqe_next) == NULL) { \
275 (head)->sqh_last = &(head)->sqh_first; \
277 (removed)->sqh_last = &(elm)->field.sqe_next; \
278 (elm)->field.sqe_next = NULL; \
280 } while (/*CONSTCOND*/0)
282 #define QSIMPLEQ_REMOVE(head, elm, type, field) do { \
283 if ((head)->sqh_first == (elm)) { \
284 QSIMPLEQ_REMOVE_HEAD((head), field); \
286 struct type *curelm = (head)->sqh_first; \
287 while (curelm->field.sqe_next != (elm)) \
288 curelm = curelm->field.sqe_next; \
289 if ((curelm->field.sqe_next = \
290 curelm->field.sqe_next->field.sqe_next) == NULL) \
291 (head)->sqh_last = &(curelm)->field.sqe_next; \
293 } while (/*CONSTCOND*/0)
295 #define QSIMPLEQ_FOREACH(var, head, field) \
296 for ((var) = ((head)->sqh_first); \
298 (var) = ((var)->field.sqe_next))
300 #define QSIMPLEQ_FOREACH_SAFE(var, head, field, next) \
301 for ((var) = ((head)->sqh_first); \
302 (var) && ((next = ((var)->field.sqe_next)), 1); \
305 #define QSIMPLEQ_CONCAT(head1, head2) do { \
306 if (!QSIMPLEQ_EMPTY((head2))) { \
307 *(head1)->sqh_last = (head2)->sqh_first; \
308 (head1)->sqh_last = (head2)->sqh_last; \
309 QSIMPLEQ_INIT((head2)); \
311 } while (/*CONSTCOND*/0)
313 #define QSIMPLEQ_LAST(head, type, field) \
314 (QSIMPLEQ_EMPTY((head)) ? \
316 ((struct type *)(void *) \
317 ((char *)((head)->sqh_last) - offsetof(struct type, field))))
320 * Simple queue access methods.
322 #define QSIMPLEQ_EMPTY(head) ((head)->sqh_first == NULL)
323 #define QSIMPLEQ_FIRST(head) ((head)->sqh_first)
324 #define QSIMPLEQ_NEXT(elm, field) ((elm)->field.sqe_next)
328 * Tail queue definitions.
330 #define Q_TAILQ_HEAD(name, type, qual) \
332 qual type *tqh_first; /* first element */ \
333 qual type *qual *tqh_last; /* addr of last next element */ \
335 #define QTAILQ_HEAD(name, type) Q_TAILQ_HEAD(name, struct type,)
337 #define QTAILQ_HEAD_INITIALIZER(head) \
338 { NULL, &(head).tqh_first }
340 #define Q_TAILQ_ENTRY(type, qual) \
342 qual type *tqe_next; /* next element */ \
343 qual type *qual *tqe_prev; /* address of previous next element */\
345 #define QTAILQ_ENTRY(type) Q_TAILQ_ENTRY(struct type,)
348 * Tail queue functions.
350 #define QTAILQ_INIT(head) do { \
351 (head)->tqh_first = NULL; \
352 (head)->tqh_last = &(head)->tqh_first; \
353 } while (/*CONSTCOND*/0)
355 #define QTAILQ_INSERT_HEAD(head, elm, field) do { \
356 if (((elm)->field.tqe_next = (head)->tqh_first) != NULL) \
357 (head)->tqh_first->field.tqe_prev = \
358 &(elm)->field.tqe_next; \
360 (head)->tqh_last = &(elm)->field.tqe_next; \
361 (head)->tqh_first = (elm); \
362 (elm)->field.tqe_prev = &(head)->tqh_first; \
363 } while (/*CONSTCOND*/0)
365 #define QTAILQ_INSERT_TAIL(head, elm, field) do { \
366 (elm)->field.tqe_next = NULL; \
367 (elm)->field.tqe_prev = (head)->tqh_last; \
368 *(head)->tqh_last = (elm); \
369 (head)->tqh_last = &(elm)->field.tqe_next; \
370 } while (/*CONSTCOND*/0)
372 #define QTAILQ_INSERT_AFTER(head, listelm, elm, field) do { \
373 if (((elm)->field.tqe_next = (listelm)->field.tqe_next) != NULL)\
374 (elm)->field.tqe_next->field.tqe_prev = \
375 &(elm)->field.tqe_next; \
377 (head)->tqh_last = &(elm)->field.tqe_next; \
378 (listelm)->field.tqe_next = (elm); \
379 (elm)->field.tqe_prev = &(listelm)->field.tqe_next; \
380 } while (/*CONSTCOND*/0)
382 #define QTAILQ_INSERT_BEFORE(listelm, elm, field) do { \
383 (elm)->field.tqe_prev = (listelm)->field.tqe_prev; \
384 (elm)->field.tqe_next = (listelm); \
385 *(listelm)->field.tqe_prev = (elm); \
386 (listelm)->field.tqe_prev = &(elm)->field.tqe_next; \
387 } while (/*CONSTCOND*/0)
389 #define QTAILQ_REMOVE(head, elm, field) do { \
390 if (((elm)->field.tqe_next) != NULL) \
391 (elm)->field.tqe_next->field.tqe_prev = \
392 (elm)->field.tqe_prev; \
394 (head)->tqh_last = (elm)->field.tqe_prev; \
395 *(elm)->field.tqe_prev = (elm)->field.tqe_next; \
396 } while (/*CONSTCOND*/0)
398 #define QTAILQ_FOREACH(var, head, field) \
399 for ((var) = ((head)->tqh_first); \
401 (var) = ((var)->field.tqe_next))
403 #define QTAILQ_FOREACH_SAFE(var, head, field, next_var) \
404 for ((var) = ((head)->tqh_first); \
405 (var) && ((next_var) = ((var)->field.tqe_next), 1); \
408 #define QTAILQ_FOREACH_REVERSE(var, head, headname, field) \
409 for ((var) = (*(((struct headname *)((head)->tqh_last))->tqh_last)); \
411 (var) = (*(((struct headname *)((var)->field.tqe_prev))->tqh_last)))
414 * Tail queue access methods.
416 #define QTAILQ_EMPTY(head) ((head)->tqh_first == NULL)
417 #define QTAILQ_FIRST(head) ((head)->tqh_first)
418 #define QTAILQ_NEXT(elm, field) ((elm)->field.tqe_next)
420 #define QTAILQ_LAST(head, headname) \
421 (*(((struct headname *)((head)->tqh_last))->tqh_last))
422 #define QTAILQ_PREV(elm, headname, field) \
423 (*(((struct headname *)((elm)->field.tqe_prev))->tqh_last))
425 #endif /* !QEMU_SYS_QUEUE_H_ */