3ed06843c1064efdabe6272a3d1b9ec7b921a44e
[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 <ros/common.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 #define STAILQ_ENTRY_WITH_ATTRS(type,attrs)                                             \
196 struct type##_link {                                                            \
197         struct type attrs *stqe_next NOINIT;    /* next element */                      \
198 };\
199 typedef struct type##_link attrs type##_link_t;
200
201 /*
202  * Singly-linked Tail queue functions.
203  */
204 #define STAILQ_CONCAT(head1, head2) do {                                \
205         if (!STAILQ_EMPTY((head2))) {                                   \
206                 *(head1)->stqh_last = (head2)->stqh_first;              \
207                 (head1)->stqh_last = (head2)->stqh_last;                \
208                 STAILQ_INIT((head2));                                   \
209         }                                                               \
210 } while (0)
211
212 #define STAILQ_EMPTY(head)      ((head)->stqh_first == NULL)
213
214 #define STAILQ_FIRST(head)      ((head)->stqh_first)
215
216 #define STAILQ_FOREACH(var, head, field)                                \
217         for((var) = STAILQ_FIRST((head));                               \
218            (var);                                                       \
219            (var) = STAILQ_NEXT((var), field))
220
221 #define STAILQ_INIT(head) do {                                          \
222         STAILQ_FIRST((head)) = NULL;                                    \
223         (head)->stqh_last = &STAILQ_FIRST((head));                      \
224 } while (0)
225
226 #define STAILQ_INSERT_AFTER(head, tqelm, elm, field) do {               \
227         if ((STAILQ_NEXT((elm), field) = STAILQ_NEXT((tqelm), field)) == NULL)\
228                 (head)->stqh_last = &STAILQ_NEXT((elm), field);         \
229         STAILQ_NEXT((tqelm), field) = (elm);                            \
230 } while (0)
231
232 #define STAILQ_INSERT_HEAD(head, elm, field) do {                       \
233         if ((STAILQ_NEXT((elm), field) = STAILQ_FIRST((head))) == NULL) \
234                 (head)->stqh_last = &STAILQ_NEXT((elm), field);         \
235         STAILQ_FIRST((head)) = (elm);                                   \
236 } while (0)
237
238 #define STAILQ_INSERT_TAIL(head, elm, field) do {                       \
239         STAILQ_NEXT((elm), field) = NULL;                               \
240         *(head)->stqh_last = (elm);                                     \
241         (head)->stqh_last = &STAILQ_NEXT((elm), field);                 \
242 } while (0)
243
244 #define STAILQ_LAST(head, type, field)                                  \
245         (STAILQ_EMPTY((head)) ?                                         \
246                 NULL :                                                  \
247                 ((struct type *)                                        \
248                 ((char *)((head)->stqh_last) - __offsetof(struct type, field))))
249
250 #define STAILQ_NEXT(elm, field) ((elm)->field.stqe_next)
251
252 #define STAILQ_REMOVE(head, elm, type, field) do {                      \
253         if (STAILQ_FIRST((head)) == (elm)) {                            \
254                 STAILQ_REMOVE_HEAD((head), field);                      \
255         }                                                               \
256         else {                                                          \
257                 struct type *curelm = STAILQ_FIRST((head));             \
258                 while (STAILQ_NEXT(curelm, field) != (elm))             \
259                         curelm = STAILQ_NEXT(curelm, field);            \
260                 if ((STAILQ_NEXT(curelm, field) =                       \
261                      STAILQ_NEXT(STAILQ_NEXT(curelm, field), field)) == NULL)\
262                         (head)->stqh_last = &STAILQ_NEXT((curelm), field);\
263         }                                                               \
264 } while (0)
265
266 #define STAILQ_REMOVE_HEAD(head, field) do {                            \
267         if ((STAILQ_FIRST((head)) =                                     \
268              STAILQ_NEXT(STAILQ_FIRST((head)), field)) == NULL)         \
269                 (head)->stqh_last = &STAILQ_FIRST((head));              \
270 } while (0)
271
272 #define STAILQ_REMOVE_HEAD_UNTIL(head, elm, field) do {                 \
273         if ((STAILQ_FIRST((head)) = STAILQ_NEXT((elm), field)) == NULL) \
274                 (head)->stqh_last = &STAILQ_FIRST((head));              \
275 } while (0)
276
277 /*
278  * List declarations.
279  */
280 #define LIST_HEAD(name, type)                                           \
281 struct name {                                                           \
282         struct type *COUNT(1) lh_first; /* first element */                     \
283 }
284
285 #define LIST_HEAD_INITIALIZER(head)                                     \
286         { NULL }
287
288 #define LIST_ENTRY(type)                                                \
289 struct {                                                                \
290         struct type *COUNT(1) le_next NOINIT;   /* next element */                      \
291         struct type *COUNT(1) *le_prev NOINIT;  /* address of previous next element */  \
292 }
293
294 /*
295  * List functions.
296  */
297
298 #define LIST_EMPTY(head)        ((head)->lh_first == NULL)
299
300 #define LIST_FIRST(head)        ((head)->lh_first)
301
302 #define LIST_FOREACH(var, head, field)                                  \
303         for ((var) = LIST_FIRST((head));                                \
304             (var);                                                      \
305             (var) = LIST_NEXT((var), field))
306
307 #define LIST_INIT(head) do {                                            \
308         LIST_FIRST((head)) = NULL;                                      \
309 } while (0)
310
311 #define LIST_INSERT_AFTER(listelm, elm, field) do {                     \
312         if ((LIST_NEXT((elm), field) = LIST_NEXT((listelm), field)) != NULL)\
313                 LIST_NEXT((listelm), field)->field.le_prev =            \
314                     &LIST_NEXT((elm), field);                           \
315         LIST_NEXT((listelm), field) = (elm);                            \
316         (elm)->field.le_prev = &LIST_NEXT((listelm), field);            \
317 } while (0)
318
319 #define LIST_INSERT_BEFORE(listelm, elm, field) do {                    \
320         (elm)->field.le_prev = (listelm)->field.le_prev;                \
321         LIST_NEXT((elm), field) = (listelm);                            \
322         *(listelm)->field.le_prev = (elm);                              \
323         (listelm)->field.le_prev = &LIST_NEXT((elm), field);            \
324 } while (0)
325
326 #define LIST_INSERT_HEAD(head, elm, field) do {                         \
327         if ((LIST_NEXT((elm), field) = LIST_FIRST((head))) != NULL)     \
328                 LIST_FIRST((head))->field.le_prev = &LIST_NEXT((elm), field);\
329         LIST_FIRST((head)) = (elm);                                     \
330         (elm)->field.le_prev = &LIST_FIRST((head));                     \
331 } while (0)
332
333 #define LIST_NEXT(elm, field)   ((elm)->field.le_next)
334
335 #define LIST_REMOVE(elm, field) do {                                    \
336         if (LIST_NEXT((elm), field) != NULL)                            \
337                 LIST_NEXT((elm), field)->field.le_prev =                \
338                     (elm)->field.le_prev;                               \
339         *(elm)->field.le_prev = LIST_NEXT((elm), field);                \
340 } while (0)
341
342 /*
343  * Tail queue declarations.
344  */
345 #define TAILQ_HEAD(name, type)                                          \
346 struct name {                                                           \
347         struct type *SAFE tqh_first;    /* first element */                     \
348         struct type *SAFE*SAFE tqh_last;        /* addr of last next element */         \
349 }
350
351 #define TAILQ_HEAD_INITIALIZER(head)                                    \
352         { NULL, &(head).tqh_first }
353
354 #define TAILQ_ENTRY(type)                                               \
355 struct {                                                                \
356         struct type *tqe_next NOINIT;   /* next element */                      \
357         struct type **tqe_prev NOINIT;  /* address of previous next element */  \
358 }
359
360 /*
361  * Tail queue functions.
362  */
363 #define TAILQ_CONCAT(head1, head2, field) do {                          \
364         if (!TAILQ_EMPTY(head2)) {                                      \
365                 *(head1)->tqh_last = (head2)->tqh_first;                \
366                 (head2)->tqh_first->field.tqe_prev = (head1)->tqh_last; \
367                 (head1)->tqh_last = (head2)->tqh_last;                  \
368                 TAILQ_INIT((head2));                                    \
369         }                                                               \
370 } while (0)
371
372 #define TAILQ_EMPTY(head)       ((head)->tqh_first == NULL)
373
374 #define TAILQ_FIRST(head)       ((head)->tqh_first)
375
376 #define TAILQ_FOREACH(var, head, field)                                 \
377         for ((var) = TAILQ_FIRST((head));                               \
378             (var);                                                      \
379             (var) = TAILQ_NEXT((var), field))
380
381 #define TAILQ_FOREACH_REVERSE(var, head, headname, field)               \
382         for ((var) = TAILQ_LAST((head), headname);                      \
383             (var);                                                      \
384             (var) = TAILQ_PREV((var), headname, field))
385
386 #define TAILQ_INIT(head) do {                                           \
387         TAILQ_FIRST((head)) = NULL;                                     \
388         (head)->tqh_last = &TAILQ_FIRST((head));                        \
389 } while (0)
390
391 #define TAILQ_INSERT_AFTER(head, listelm, elm, field) do {              \
392         if ((TAILQ_NEXT((elm), field) = TAILQ_NEXT((listelm), field)) != NULL)\
393                 TAILQ_NEXT((elm), field)->field.tqe_prev =              \
394                     &TAILQ_NEXT((elm), field);                          \
395         else                                                            \
396                 (head)->tqh_last = &TAILQ_NEXT((elm), field);           \
397         TAILQ_NEXT((listelm), field) = (elm);                           \
398         (elm)->field.tqe_prev = &TAILQ_NEXT((listelm), field);          \
399 } while (0)
400
401 #define TAILQ_INSERT_BEFORE(listelm, elm, field) do {                   \
402         (elm)->field.tqe_prev = (listelm)->field.tqe_prev;              \
403         TAILQ_NEXT((elm), field) = (listelm);                           \
404         *(listelm)->field.tqe_prev = (elm);                             \
405         (listelm)->field.tqe_prev = &TAILQ_NEXT((elm), field);          \
406 } while (0)
407
408 #define TAILQ_INSERT_HEAD(head, elm, field) do {                        \
409         if ((TAILQ_NEXT((elm), field) = TAILQ_FIRST((head))) != NULL)   \
410                 TAILQ_FIRST((head))->field.tqe_prev =                   \
411                     &TAILQ_NEXT((elm), field);                          \
412         else                                                            \
413                 (head)->tqh_last = &TAILQ_NEXT((elm), field);           \
414         TAILQ_FIRST((head)) = (elm);                                    \
415         (elm)->field.tqe_prev = &TAILQ_FIRST((head));                   \
416 } while (0)
417
418 #define TAILQ_INSERT_TAIL(head, elm, field) do {                        \
419         TAILQ_NEXT((elm), field) = NULL;                                \
420         (elm)->field.tqe_prev = (head)->tqh_last;                       \
421         *(head)->tqh_last = (elm);                                      \
422         (head)->tqh_last = &TAILQ_NEXT((elm), field);                   \
423 } while (0)
424
425 #define TAILQ_LAST(head, headname)                                      \
426         (*(((struct headname *)((head)->tqh_last))->tqh_last))
427
428 #define TAILQ_NEXT(elm, field) ((elm)->field.tqe_next)
429
430 #define TAILQ_PREV(elm, headname, field)                                \
431         (*(((struct headname *)((elm)->field.tqe_prev))->tqh_last))
432
433 #define TAILQ_REMOVE(head, elm, field) do {                             \
434         if ((TAILQ_NEXT((elm), field)) != NULL)                         \
435                 TAILQ_NEXT((elm), field)->field.tqe_prev =              \
436                     (elm)->field.tqe_prev;                              \
437         else                                                            \
438                 (head)->tqh_last = (elm)->field.tqe_prev;               \
439         *(elm)->field.tqe_prev = TAILQ_NEXT((elm), field);              \
440 } while (0)
441
442
443 #ifdef _KERNEL
444
445 /*
446  * XXX insque() and remque() are an old way of handling certain queues.
447  * They bogusly assumes that all queue heads look alike.
448  */
449
450 struct quehead {
451         struct quehead *qh_link;
452         struct quehead *qh_rlink;
453 };
454
455 #ifdef  __GNUC__
456
457 static __inline void
458 insque(void *a, void *b)
459 {
460         struct quehead *element = (struct quehead *)a,
461                  *head = (struct quehead *)b;
462
463         element->qh_link = head->qh_link;
464         element->qh_rlink = head;
465         head->qh_link = element;
466         element->qh_link->qh_rlink = element;
467 }
468
469 static __inline void
470 remque(void *a)
471 {
472         struct quehead *element = (struct quehead *)a;
473
474         element->qh_link->qh_rlink = element->qh_rlink;
475         element->qh_rlink->qh_link = element->qh_link;
476         element->qh_rlink = 0;
477 }
478
479 #else /* !__GNUC__ */
480
481 void    insque(void *a, void *b);
482 void    remque(void *a);
483
484 #endif /* __GNUC__ */
485
486 #endif /* _KERNEL */
487
488 #endif /* ROS_INCLUDE_SYS_QUEUE_H */