Merge branch 'master' into net-dev
[akaros.git] / kern / include / sys / queue.h
1 /*
2  * Copyright (c) 1991, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by the University of
16  *      California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  *      @(#)queue.h     8.5 (Berkeley) 8/20/94
34  * $FreeBSD: src/sys/sys/queue.h,v 1.48 2002/04/17 14:00:37 tmm Exp $
35  *
36  * Minor changes for ROS and Ivy annotations (Berkeley: 2009-06-08)
37  */
38
39 #ifndef ROS_INCLUDE_SYS_QUEUE_H
40 #define ROS_INCLUDE_SYS_QUEUE_H
41
42 //#include <machine/ansi.h>     /* for __offsetof */
43 #include <arch/types.h> /* for __offsetof */
44
45 /*
46  * This file defines four types of data structures: singly-linked lists,
47  * singly-linked tail queues, lists and tail queues.
48  *
49  * A singly-linked list is headed by a single forward pointer. The elements
50  * are singly linked for minimum space and pointer manipulation overhead at
51  * the expense of O(n) removal for arbitrary elements. New elements can be
52  * added to the list after an existing element or at the head of the list.
53  * Elements being removed from the head of the list should use the explicit
54  * macro for this purpose for optimum efficiency. A singly-linked list may
55  * only be traversed in the forward direction.  Singly-linked lists are ideal
56  * for applications with large datasets and few or no removals or for
57  * implementing a LIFO queue.
58  *
59  * A singly-linked tail queue is headed by a pair of pointers, one to the
60  * head of the list and the other to the tail of the list. The elements are
61  * singly linked for minimum space and pointer manipulation overhead at the
62  * expense of O(n) removal for arbitrary elements. New elements can be added
63  * to the list after an existing element, at the head of the list, or at the
64  * end of the list. Elements being removed from the head of the tail queue
65  * should use the explicit macro for this purpose for optimum efficiency.
66  * A singly-linked tail queue may only be traversed in the forward direction.
67  * Singly-linked tail queues are ideal for applications with large datasets
68  * and few or no removals or for implementing a FIFO queue.
69  *
70  * A list is headed by a single forward pointer (or an array of forward
71  * pointers for a hash table header). The elements are doubly linked
72  * so that an arbitrary element can be removed without a need to
73  * traverse the list. New elements can be added to the list before
74  * or after an existing element or at the head of the list. A list
75  * may only be traversed in the forward direction.
76  *
77  * A tail queue is headed by a pair of pointers, one to the head of the
78  * list and the other to the tail of the list. The elements are doubly
79  * linked so that an arbitrary element can be removed without a need to
80  * traverse the list. New elements can be added to the list before or
81  * after an existing element, at the head of the list, or at the end of
82  * the list. A tail queue may be traversed in either direction.
83  *
84  * For details on the use of these macros, see the queue(3) manual page.
85  *
86  *
87  *                      SLIST   LIST    STAILQ  TAILQ
88  * _HEAD                +       +       +       +
89  * _HEAD_INITIALIZER    +       +       +       +
90  * _ENTRY               +       +       +       +
91  * _INIT                +       +       +       +
92  * _EMPTY               +       +       +       +
93  * _FIRST               +       +       +       +
94  * _NEXT                +       +       +       +
95  * _PREV                -       -       -       +
96  * _LAST                -       -       +       +
97  * _FOREACH             +       +       +       +
98  * _FOREACH_REVERSE     -       -       -       +
99  * _INSERT_HEAD         +       +       +       +
100  * _INSERT_BEFORE       -       +       -       +
101  * _INSERT_AFTER        +       +       +       +
102  * _INSERT_TAIL         -       -       +       +
103  * _CONCAT              -       -       +       +
104  * _REMOVE_HEAD         +       -       +       -
105  * _REMOVE              +       +       +       +
106  *
107  */
108
109 /*
110  * Ivy Annotations:
111  *      SAFE: pointers that only point to one item, and not an array.
112  *      NOINIT: items that do not need to be initialized in blocks of code that
113  *              initialize items, for which Ivy statically can check.
114  *      These haven't been tested much, just for the current uses of LIST and TAILQ
115  */
116
117 /*
118  * Singly-linked List declarations.
119  */
120 #define SLIST_HEAD(name, type)                                          \
121 struct name {                                                           \
122         struct type *SAFE slh_first;    /* first element */                     \
123 }
124
125 #define SLIST_HEAD_INITIALIZER(head)                                    \
126         { NULL }
127  
128 #define SLIST_ENTRY(type)                                               \
129 struct {                                                                \
130         struct type *sle_next NOINIT;   /* next element */                      \
131 }
132  
133 /*
134  * Singly-linked List functions.
135  */
136 #define SLIST_EMPTY(head)       ((head)->slh_first == NULL)
137
138 #define SLIST_FIRST(head)       ((head)->slh_first)
139
140 #define SLIST_FOREACH(var, head, field)                                 \
141         for ((var) = SLIST_FIRST((head));                               \
142             (var);                                                      \
143             (var) = SLIST_NEXT((var), field))
144
145 #define SLIST_INIT(head) do {                                           \
146         SLIST_FIRST((head)) = NULL;                                     \
147 } while (0)
148
149 #define SLIST_INSERT_AFTER(slistelm, elm, field) do {                   \
150         SLIST_NEXT((elm), field) = SLIST_NEXT((slistelm), field);       \
151         SLIST_NEXT((slistelm), field) = (elm);                          \
152 } while (0)
153
154 #define SLIST_INSERT_HEAD(head, elm, field) do {                        \
155         SLIST_NEXT((elm), field) = SLIST_FIRST((head));                 \
156         SLIST_FIRST((head)) = (elm);                                    \
157 } while (0)
158
159 #define SLIST_NEXT(elm, field)  ((elm)->field.sle_next)
160
161 #define SLIST_REMOVE(head, elm, type, field) do {                       \
162         if (SLIST_FIRST((head)) == (elm)) {                             \
163                 SLIST_REMOVE_HEAD((head), field);                       \
164         }                                                               \
165         else {                                                          \
166                 struct type *curelm = SLIST_FIRST((head));              \
167                 while (SLIST_NEXT(curelm, field) != (elm))              \
168                         curelm = SLIST_NEXT(curelm, field);             \
169                 SLIST_NEXT(curelm, field) =                             \
170                     SLIST_NEXT(SLIST_NEXT(curelm, field), field);       \
171         }                                                               \
172 } while (0)
173
174 #define SLIST_REMOVE_HEAD(head, field) do {                             \
175         SLIST_FIRST((head)) = SLIST_NEXT(SLIST_FIRST((head)), field);   \
176 } while (0)
177
178 /*
179  * Singly-linked Tail queue declarations.
180  */
181 #define STAILQ_HEAD(name, type)                                         \
182 struct name {                                                           \
183         struct type *SAFE stqh_first;/* first element */                        \
184         struct type *SAFE*SAFE stqh_last;/* addr of last next element */                \
185 }
186
187 #define STAILQ_HEAD_INITIALIZER(head)                                   \
188         { NULL, &(head).stqh_first }
189
190 #define STAILQ_ENTRY(type)                                              \
191 struct {                                                                \
192         struct type *stqe_next NOINIT;  /* next element */                      \
193 }
194
195 /*
196  * Singly-linked Tail queue functions.
197  */
198 #define STAILQ_CONCAT(head1, head2) do {                                \
199         if (!STAILQ_EMPTY((head2))) {                                   \
200                 *(head1)->stqh_last = (head2)->stqh_first;              \
201                 (head1)->stqh_last = (head2)->stqh_last;                \
202                 STAILQ_INIT((head2));                                   \
203         }                                                               \
204 } while (0)
205
206 #define STAILQ_EMPTY(head)      ((head)->stqh_first == NULL)
207
208 #define STAILQ_FIRST(head)      ((head)->stqh_first)
209
210 #define STAILQ_FOREACH(var, head, field)                                \
211         for((var) = STAILQ_FIRST((head));                               \
212            (var);                                                       \
213            (var) = STAILQ_NEXT((var), field))
214
215 #define STAILQ_INIT(head) do {                                          \
216         STAILQ_FIRST((head)) = NULL;                                    \
217         (head)->stqh_last = &STAILQ_FIRST((head));                      \
218 } while (0)
219
220 #define STAILQ_INSERT_AFTER(head, tqelm, elm, field) do {               \
221         if ((STAILQ_NEXT((elm), field) = STAILQ_NEXT((tqelm), field)) == NULL)\
222                 (head)->stqh_last = &STAILQ_NEXT((elm), field);         \
223         STAILQ_NEXT((tqelm), field) = (elm);                            \
224 } while (0)
225
226 #define STAILQ_INSERT_HEAD(head, elm, field) do {                       \
227         if ((STAILQ_NEXT((elm), field) = STAILQ_FIRST((head))) == NULL) \
228                 (head)->stqh_last = &STAILQ_NEXT((elm), field);         \
229         STAILQ_FIRST((head)) = (elm);                                   \
230 } while (0)
231
232 #define STAILQ_INSERT_TAIL(head, elm, field) do {                       \
233         STAILQ_NEXT((elm), field) = NULL;                               \
234         *(head)->stqh_last = (elm);                                     \
235         (head)->stqh_last = &STAILQ_NEXT((elm), field);                 \
236 } while (0)
237
238 #define STAILQ_LAST(head, type, field)                                  \
239         (STAILQ_EMPTY((head)) ?                                         \
240                 NULL :                                                  \
241                 ((struct type *)                                        \
242                 ((char *)((head)->stqh_last) - __offsetof(struct type, field))))
243
244 #define STAILQ_NEXT(elm, field) ((elm)->field.stqe_next)
245
246 #define STAILQ_REMOVE(head, elm, type, field) do {                      \
247         if (STAILQ_FIRST((head)) == (elm)) {                            \
248                 STAILQ_REMOVE_HEAD((head), field);                      \
249         }                                                               \
250         else {                                                          \
251                 struct type *curelm = STAILQ_FIRST((head));             \
252                 while (STAILQ_NEXT(curelm, field) != (elm))             \
253                         curelm = STAILQ_NEXT(curelm, field);            \
254                 if ((STAILQ_NEXT(curelm, field) =                       \
255                      STAILQ_NEXT(STAILQ_NEXT(curelm, field), field)) == NULL)\
256                         (head)->stqh_last = &STAILQ_NEXT((curelm), field);\
257         }                                                               \
258 } while (0)
259
260 #define STAILQ_REMOVE_HEAD(head, field) do {                            \
261         if ((STAILQ_FIRST((head)) =                                     \
262              STAILQ_NEXT(STAILQ_FIRST((head)), field)) == NULL)         \
263                 (head)->stqh_last = &STAILQ_FIRST((head));              \
264 } while (0)
265
266 #define STAILQ_REMOVE_HEAD_UNTIL(head, elm, field) do {                 \
267         if ((STAILQ_FIRST((head)) = STAILQ_NEXT((elm), field)) == NULL) \
268                 (head)->stqh_last = &STAILQ_FIRST((head));              \
269 } while (0)
270
271 /*
272  * List declarations.
273  */
274 #define LIST_HEAD(name, type)                                           \
275 struct name {                                                           \
276         struct type *SAFE lh_first;     /* first element */                     \
277 }
278
279 #define LIST_HEAD_INITIALIZER(head)                                     \
280         { NULL }
281
282 #define LIST_ENTRY(type)                                                \
283 struct {                                                                \
284         struct type *le_next NOINIT;    /* next element */                      \
285         struct type **le_prev NOINIT;   /* address of previous next element */  \
286 }
287
288 /*
289  * List functions.
290  */
291
292 #define LIST_EMPTY(head)        ((head)->lh_first == NULL)
293
294 #define LIST_FIRST(head)        ((head)->lh_first)
295
296 #define LIST_FOREACH(var, head, field)                                  \
297         for ((var) = LIST_FIRST((head));                                \
298             (var);                                                      \
299             (var) = LIST_NEXT((var), field))
300
301 #define LIST_INIT(head) do {                                            \
302         LIST_FIRST((head)) = NULL;                                      \
303 } while (0)
304
305 #define LIST_INSERT_AFTER(listelm, elm, field) do {                     \
306         if ((LIST_NEXT((elm), field) = LIST_NEXT((listelm), field)) != NULL)\
307                 LIST_NEXT((listelm), field)->field.le_prev =            \
308                     &LIST_NEXT((elm), field);                           \
309         LIST_NEXT((listelm), field) = (elm);                            \
310         (elm)->field.le_prev = &LIST_NEXT((listelm), field);            \
311 } while (0)
312
313 #define LIST_INSERT_BEFORE(listelm, elm, field) do {                    \
314         (elm)->field.le_prev = (listelm)->field.le_prev;                \
315         LIST_NEXT((elm), field) = (listelm);                            \
316         *(listelm)->field.le_prev = (elm);                              \
317         (listelm)->field.le_prev = &LIST_NEXT((elm), field);            \
318 } while (0)
319
320 #define LIST_INSERT_HEAD(head, elm, field) do {                         \
321         if ((LIST_NEXT((elm), field) = LIST_FIRST((head))) != NULL)     \
322                 LIST_FIRST((head))->field.le_prev = &LIST_NEXT((elm), field);\
323         LIST_FIRST((head)) = (elm);                                     \
324         (elm)->field.le_prev = &LIST_FIRST((head));                     \
325 } while (0)
326
327 #define LIST_NEXT(elm, field)   ((elm)->field.le_next)
328
329 #define LIST_REMOVE(elm, field) do {                                    \
330         if (LIST_NEXT((elm), field) != NULL)                            \
331                 LIST_NEXT((elm), field)->field.le_prev =                \
332                     (elm)->field.le_prev;                               \
333         *(elm)->field.le_prev = LIST_NEXT((elm), field);                \
334 } while (0)
335
336 /*
337  * Tail queue declarations.
338  */
339 #define TAILQ_HEAD(name, type)                                          \
340 struct name {                                                           \
341         struct type *SAFE tqh_first;    /* first element */                     \
342         struct type *SAFE*SAFE tqh_last;        /* addr of last next element */         \
343 }
344
345 #define TAILQ_HEAD_INITIALIZER(head)                                    \
346         { NULL, &(head).tqh_first }
347
348 #define TAILQ_ENTRY(type)                                               \
349 struct {                                                                \
350         struct type *tqe_next NOINIT;   /* next element */                      \
351         struct type **tqe_prev NOINIT;  /* address of previous next element */  \
352 }
353
354 /*
355  * Tail queue functions.
356  */
357 #define TAILQ_CONCAT(head1, head2, field) do {                          \
358         if (!TAILQ_EMPTY(head2)) {                                      \
359                 *(head1)->tqh_last = (head2)->tqh_first;                \
360                 (head2)->tqh_first->field.tqe_prev = (head1)->tqh_last; \
361                 (head1)->tqh_last = (head2)->tqh_last;                  \
362                 TAILQ_INIT((head2));                                    \
363         }                                                               \
364 } while (0)
365
366 #define TAILQ_EMPTY(head)       ((head)->tqh_first == NULL)
367
368 #define TAILQ_FIRST(head)       ((head)->tqh_first)
369
370 #define TAILQ_FOREACH(var, head, field)                                 \
371         for ((var) = TAILQ_FIRST((head));                               \
372             (var);                                                      \
373             (var) = TAILQ_NEXT((var), field))
374
375 #define TAILQ_FOREACH_REVERSE(var, head, headname, field)               \
376         for ((var) = TAILQ_LAST((head), headname);                      \
377             (var);                                                      \
378             (var) = TAILQ_PREV((var), headname, field))
379
380 #define TAILQ_INIT(head) do {                                           \
381         TAILQ_FIRST((head)) = NULL;                                     \
382         (head)->tqh_last = &TAILQ_FIRST((head));                        \
383 } while (0)
384
385 #define TAILQ_INSERT_AFTER(head, listelm, elm, field) do {              \
386         if ((TAILQ_NEXT((elm), field) = TAILQ_NEXT((listelm), field)) != NULL)\
387                 TAILQ_NEXT((elm), field)->field.tqe_prev =              \
388                     &TAILQ_NEXT((elm), field);                          \
389         else                                                            \
390                 (head)->tqh_last = &TAILQ_NEXT((elm), field);           \
391         TAILQ_NEXT((listelm), field) = (elm);                           \
392         (elm)->field.tqe_prev = &TAILQ_NEXT((listelm), field);          \
393 } while (0)
394
395 #define TAILQ_INSERT_BEFORE(listelm, elm, field) do {                   \
396         (elm)->field.tqe_prev = (listelm)->field.tqe_prev;              \
397         TAILQ_NEXT((elm), field) = (listelm);                           \
398         *(listelm)->field.tqe_prev = (elm);                             \
399         (listelm)->field.tqe_prev = &TAILQ_NEXT((elm), field);          \
400 } while (0)
401
402 #define TAILQ_INSERT_HEAD(head, elm, field) do {                        \
403         if ((TAILQ_NEXT((elm), field) = TAILQ_FIRST((head))) != NULL)   \
404                 TAILQ_FIRST((head))->field.tqe_prev =                   \
405                     &TAILQ_NEXT((elm), field);                          \
406         else                                                            \
407                 (head)->tqh_last = &TAILQ_NEXT((elm), field);           \
408         TAILQ_FIRST((head)) = (elm);                                    \
409         (elm)->field.tqe_prev = &TAILQ_FIRST((head));                   \
410 } while (0)
411
412 #define TAILQ_INSERT_TAIL(head, elm, field) do {                        \
413         TAILQ_NEXT((elm), field) = NULL;                                \
414         (elm)->field.tqe_prev = (head)->tqh_last;                       \
415         *(head)->tqh_last = (elm);                                      \
416         (head)->tqh_last = &TAILQ_NEXT((elm), field);                   \
417 } while (0)
418
419 #define TAILQ_LAST(head, headname)                                      \
420         (*(((struct headname *)((head)->tqh_last))->tqh_last))
421
422 #define TAILQ_NEXT(elm, field) ((elm)->field.tqe_next)
423
424 #define TAILQ_PREV(elm, headname, field)                                \
425         (*(((struct headname *)((elm)->field.tqe_prev))->tqh_last))
426
427 #define TAILQ_REMOVE(head, elm, field) do {                             \
428         if ((TAILQ_NEXT((elm), field)) != NULL)                         \
429                 TAILQ_NEXT((elm), field)->field.tqe_prev =              \
430                     (elm)->field.tqe_prev;                              \
431         else                                                            \
432                 (head)->tqh_last = (elm)->field.tqe_prev;               \
433         *(elm)->field.tqe_prev = TAILQ_NEXT((elm), field);              \
434 } while (0)
435
436
437 #ifdef _KERNEL
438
439 /*
440  * XXX insque() and remque() are an old way of handling certain queues.
441  * They bogusly assumes that all queue heads look alike.
442  */
443
444 struct quehead {
445         struct quehead *qh_link;
446         struct quehead *qh_rlink;
447 };
448
449 #ifdef  __GNUC__
450
451 static __inline void
452 insque(void *a, void *b)
453 {
454         struct quehead *element = (struct quehead *)a,
455                  *head = (struct quehead *)b;
456
457         element->qh_link = head->qh_link;
458         element->qh_rlink = head;
459         head->qh_link = element;
460         element->qh_link->qh_rlink = element;
461 }
462
463 static __inline void
464 remque(void *a)
465 {
466         struct quehead *element = (struct quehead *)a;
467
468         element->qh_link->qh_rlink = element->qh_rlink;
469         element->qh_rlink->qh_link = element->qh_link;
470         element->qh_rlink = 0;
471 }
472
473 #else /* !__GNUC__ */
474
475 void    insque(void *a, void *b);
476 void    remque(void *a);
477
478 #endif /* __GNUC__ */
479
480 #endif /* _KERNEL */
481
482 #endif /* ROS_INCLUDE_SYS_QUEUE_H */