Check for SYS/GL during library init. Reason is that
[AROS.git] / workbench / network / common / include / sys / queue.h
blobb93015e3b3bba249ecbf7da7360a5976d1a5283d
1 /*
2 * Copyright (c) 1991, 1993
3 * The Regents of the University of California. All rights reserved.
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.
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.
33 * @(#)queue.h 8.5 (Berkeley) 8/20/94
34 * $FreeBSD: src/sys/sys/queue.h,v 1.56 2003/08/14 14:49:26 kan Exp $
37 #ifndef _SYS_QUEUE_H_
38 #define _SYS_QUEUE_H_
40 #include <sys/cdefs.h>
43 * This file defines four types of data structures: singly-linked lists,
44 * singly-linked tail queues, lists and tail queues.
46 * A singly-linked list is headed by a single forward pointer. The elements
47 * are singly linked for minimum space and pointer manipulation overhead at
48 * the expense of O(n) removal for arbitrary elements. New elements can be
49 * added to the list after an existing element or at the head of the list.
50 * Elements being removed from the head of the list should use the explicit
51 * macro for this purpose for optimum efficiency. A singly-linked list may
52 * only be traversed in the forward direction. Singly-linked lists are ideal
53 * for applications with large datasets and few or no removals or for
54 * implementing a LIFO queue.
56 * A singly-linked tail queue is headed by a pair of pointers, one to the
57 * head of the list and the other to the tail of the list. The elements are
58 * singly linked for minimum space and pointer manipulation overhead at the
59 * expense of O(n) removal for arbitrary elements. New elements can be added
60 * to the list after an existing element, at the head of the list, or at the
61 * end of the list. Elements being removed from the head of the tail queue
62 * should use the explicit macro for this purpose for optimum efficiency.
63 * A singly-linked tail queue may only be traversed in the forward direction.
64 * Singly-linked tail queues are ideal for applications with large datasets
65 * and few or no removals or for implementing a FIFO queue.
67 * A list is headed by a single forward pointer (or an array of forward
68 * pointers for a hash table header). The elements are doubly linked
69 * so that an arbitrary element can be removed without a need to
70 * traverse the list. New elements can be added to the list before
71 * or after an existing element or at the head of the list. A list
72 * may only be traversed in the forward direction.
74 * A tail queue is headed by a pair of pointers, one to the head of the
75 * list and the other to the tail of the list. The elements are doubly
76 * linked so that an arbitrary element can be removed without a need to
77 * traverse the list. New elements can be added to the list before or
78 * after an existing element, at the head of the list, or at the end of
79 * the list. A tail queue may be traversed in either direction.
81 * For details on the use of these macros, see the queue(3) manual page.
84 * SLIST LIST STAILQ TAILQ
85 * _HEAD + + + +
86 * _HEAD_INITIALIZER + + + +
87 * _ENTRY + + + +
88 * _INIT + + + +
89 * _EMPTY + + + +
90 * _FIRST + + + +
91 * _NEXT + + + +
92 * _PREV - - - +
93 * _LAST - - + +
94 * _FOREACH + + + +
95 * _FOREACH_SAFE + + + +
96 * _FOREACH_REVERSE - - - +
97 * _FOREACH_REVERSE_SAFE - - - +
98 * _INSERT_HEAD + + + +
99 * _INSERT_BEFORE - + - +
100 * _INSERT_AFTER + + + +
101 * _INSERT_TAIL - - + +
102 * _CONCAT - - + +
103 * _REMOVE_HEAD + - + -
104 * _REMOVE + + + +
107 #define QUEUE_MACRO_DEBUG 0
108 #if QUEUE_MACRO_DEBUG
109 /* Store the last 2 places the queue element or head was altered */
110 struct qm_trace {
111 char * lastfile;
112 int lastline;
113 char * prevfile;
114 int prevline;
117 #define TRACEBUF struct qm_trace trace;
118 #define TRASHIT(x) do {(x) = (void *)-1;} while (0)
120 #define QMD_TRACE_HEAD(head) do { \
121 (head)->trace.prevline = (head)->trace.lastline; \
122 (head)->trace.prevfile = (head)->trace.lastfile; \
123 (head)->trace.lastline = __LINE__; \
124 (head)->trace.lastfile = __FILE__; \
125 } while (0)
127 #define QMD_TRACE_ELEM(elem) do { \
128 (elem)->trace.prevline = (elem)->trace.lastline; \
129 (elem)->trace.prevfile = (elem)->trace.lastfile; \
130 (elem)->trace.lastline = __LINE__; \
131 (elem)->trace.lastfile = __FILE__; \
132 } while (0)
134 #else
135 #define QMD_TRACE_ELEM(elem)
136 #define QMD_TRACE_HEAD(head)
137 #define TRACEBUF
138 #define TRASHIT(x)
139 #endif /* QUEUE_MACRO_DEBUG */
142 * Singly-linked List declarations.
144 #define SLIST_HEAD(name, type) \
145 struct name { \
146 struct type *slh_first; /* first element */ \
149 #define SLIST_HEAD_INITIALIZER(head) \
150 { NULL }
152 #define SLIST_ENTRY(type) \
153 struct { \
154 struct type *sle_next; /* next element */ \
158 * Singly-linked List functions.
160 #define SLIST_EMPTY(head) ((head)->slh_first == NULL)
162 #define SLIST_FIRST(head) ((head)->slh_first)
164 #define SLIST_FOREACH(var, head, field) \
165 for ((var) = SLIST_FIRST((head)); \
166 (var); \
167 (var) = SLIST_NEXT((var), field))
169 #define SLIST_FOREACH_SAFE(var, head, field, tvar) \
170 for ((var) = SLIST_FIRST((head)); \
171 (var) && ((tvar) = SLIST_NEXT((var), field), 1); \
172 (var) = (tvar))
174 #define SLIST_FOREACH_PREVPTR(var, varp, head, field) \
175 for ((varp) = &SLIST_FIRST((head)); \
176 ((var) = *(varp)) != NULL; \
177 (varp) = &SLIST_NEXT((var), field))
179 #define SLIST_INIT(head) do { \
180 SLIST_FIRST((head)) = NULL; \
181 } while (0)
183 #define SLIST_INSERT_AFTER(slistelm, elm, field) do { \
184 SLIST_NEXT((elm), field) = SLIST_NEXT((slistelm), field); \
185 SLIST_NEXT((slistelm), field) = (elm); \
186 } while (0)
188 #define SLIST_INSERT_HEAD(head, elm, field) do { \
189 SLIST_NEXT((elm), field) = SLIST_FIRST((head)); \
190 SLIST_FIRST((head)) = (elm); \
191 } while (0)
193 #define SLIST_NEXT(elm, field) ((elm)->field.sle_next)
195 #define SLIST_REMOVE(head, elm, type, field) do { \
196 if (SLIST_FIRST((head)) == (elm)) { \
197 SLIST_REMOVE_HEAD((head), field); \
199 else { \
200 struct type *curelm = SLIST_FIRST((head)); \
201 while (SLIST_NEXT(curelm, field) != (elm)) \
202 curelm = SLIST_NEXT(curelm, field); \
203 SLIST_NEXT(curelm, field) = \
204 SLIST_NEXT(SLIST_NEXT(curelm, field), field); \
206 } while (0)
208 #define SLIST_REMOVE_HEAD(head, field) do { \
209 SLIST_FIRST((head)) = SLIST_NEXT(SLIST_FIRST((head)), field); \
210 } while (0)
213 * Singly-linked Tail queue declarations.
215 #define STAILQ_HEAD(name, type) \
216 struct name { \
217 struct type *stqh_first;/* first element */ \
218 struct type **stqh_last;/* addr of last next element */ \
221 #define STAILQ_HEAD_INITIALIZER(head) \
222 { NULL, &(head).stqh_first }
224 #define STAILQ_ENTRY(type) \
225 struct { \
226 struct type *stqe_next; /* next element */ \
230 * Singly-linked Tail queue functions.
232 #define STAILQ_CONCAT(head1, head2) do { \
233 if (!STAILQ_EMPTY((head2))) { \
234 *(head1)->stqh_last = (head2)->stqh_first; \
235 (head1)->stqh_last = (head2)->stqh_last; \
236 STAILQ_INIT((head2)); \
238 } while (0)
240 #define STAILQ_EMPTY(head) ((head)->stqh_first == NULL)
242 #define STAILQ_FIRST(head) ((head)->stqh_first)
244 #define STAILQ_FOREACH(var, head, field) \
245 for((var) = STAILQ_FIRST((head)); \
246 (var); \
247 (var) = STAILQ_NEXT((var), field))
250 #define STAILQ_FOREACH_SAFE(var, head, field, tvar) \
251 for ((var) = STAILQ_FIRST((head)); \
252 (var) && ((tvar) = STAILQ_NEXT((var), field), 1); \
253 (var) = (tvar))
255 #define STAILQ_INIT(head) do { \
256 STAILQ_FIRST((head)) = NULL; \
257 (head)->stqh_last = &STAILQ_FIRST((head)); \
258 } while (0)
260 #define STAILQ_INSERT_AFTER(head, tqelm, elm, field) do { \
261 if ((STAILQ_NEXT((elm), field) = STAILQ_NEXT((tqelm), field)) == NULL)\
262 (head)->stqh_last = &STAILQ_NEXT((elm), field); \
263 STAILQ_NEXT((tqelm), field) = (elm); \
264 } while (0)
266 #define STAILQ_INSERT_HEAD(head, elm, field) do { \
267 if ((STAILQ_NEXT((elm), field) = STAILQ_FIRST((head))) == NULL) \
268 (head)->stqh_last = &STAILQ_NEXT((elm), field); \
269 STAILQ_FIRST((head)) = (elm); \
270 } while (0)
272 #define STAILQ_INSERT_TAIL(head, elm, field) do { \
273 STAILQ_NEXT((elm), field) = NULL; \
274 *(head)->stqh_last = (elm); \
275 (head)->stqh_last = &STAILQ_NEXT((elm), field); \
276 } while (0)
278 #define STAILQ_LAST(head, type, field) \
279 (STAILQ_EMPTY((head)) ? \
280 NULL : \
281 ((struct type *) \
282 ((char *)((head)->stqh_last) - __offsetof(struct type, field))))
284 #define STAILQ_NEXT(elm, field) ((elm)->field.stqe_next)
286 #define STAILQ_REMOVE(head, elm, type, field) do { \
287 if (STAILQ_FIRST((head)) == (elm)) { \
288 STAILQ_REMOVE_HEAD((head), field); \
290 else { \
291 struct type *curelm = STAILQ_FIRST((head)); \
292 while (STAILQ_NEXT(curelm, field) != (elm)) \
293 curelm = STAILQ_NEXT(curelm, field); \
294 if ((STAILQ_NEXT(curelm, field) = \
295 STAILQ_NEXT(STAILQ_NEXT(curelm, field), field)) == NULL)\
296 (head)->stqh_last = &STAILQ_NEXT((curelm), field);\
298 } while (0)
300 #define STAILQ_REMOVE_HEAD(head, field) do { \
301 if ((STAILQ_FIRST((head)) = \
302 STAILQ_NEXT(STAILQ_FIRST((head)), field)) == NULL) \
303 (head)->stqh_last = &STAILQ_FIRST((head)); \
304 } while (0)
306 #define STAILQ_REMOVE_HEAD_UNTIL(head, elm, field) do { \
307 if ((STAILQ_FIRST((head)) = STAILQ_NEXT((elm), field)) == NULL) \
308 (head)->stqh_last = &STAILQ_FIRST((head)); \
309 } while (0)
312 * List declarations.
314 #define LIST_HEAD(name, type) \
315 struct name { \
316 struct type *lh_first; /* first element */ \
319 #define LIST_HEAD_INITIALIZER(head) \
320 { NULL }
322 #define LIST_ENTRY(type) \
323 struct { \
324 struct type *le_next; /* next element */ \
325 struct type **le_prev; /* address of previous next element */ \
329 * List functions.
332 #define LIST_EMPTY(head) ((head)->lh_first == NULL)
334 #define LIST_FIRST(head) ((head)->lh_first)
336 #define LIST_FOREACH(var, head, field) \
337 for ((var) = LIST_FIRST((head)); \
338 (var); \
339 (var) = LIST_NEXT((var), field))
341 #define LIST_FOREACH_SAFE(var, head, field, tvar) \
342 for ((var) = LIST_FIRST((head)); \
343 (var) && ((tvar) = LIST_NEXT((var), field), 1); \
344 (var) = (tvar))
346 #define LIST_INIT(head) do { \
347 LIST_FIRST((head)) = NULL; \
348 } while (0)
350 #define LIST_INSERT_AFTER(listelm, elm, field) do { \
351 if ((LIST_NEXT((elm), field) = LIST_NEXT((listelm), field)) != NULL)\
352 LIST_NEXT((listelm), field)->field.le_prev = \
353 &LIST_NEXT((elm), field); \
354 LIST_NEXT((listelm), field) = (elm); \
355 (elm)->field.le_prev = &LIST_NEXT((listelm), field); \
356 } while (0)
358 #define LIST_INSERT_BEFORE(listelm, elm, field) do { \
359 (elm)->field.le_prev = (listelm)->field.le_prev; \
360 LIST_NEXT((elm), field) = (listelm); \
361 *(listelm)->field.le_prev = (elm); \
362 (listelm)->field.le_prev = &LIST_NEXT((elm), field); \
363 } while (0)
365 #define LIST_INSERT_HEAD(head, elm, field) do { \
366 if ((LIST_NEXT((elm), field) = LIST_FIRST((head))) != NULL) \
367 LIST_FIRST((head))->field.le_prev = &LIST_NEXT((elm), field);\
368 LIST_FIRST((head)) = (elm); \
369 (elm)->field.le_prev = &LIST_FIRST((head)); \
370 } while (0)
372 #define LIST_NEXT(elm, field) ((elm)->field.le_next)
374 #define LIST_REMOVE(elm, field) do { \
375 if (LIST_NEXT((elm), field) != NULL) \
376 LIST_NEXT((elm), field)->field.le_prev = \
377 (elm)->field.le_prev; \
378 *(elm)->field.le_prev = LIST_NEXT((elm), field); \
379 } while (0)
382 * Tail queue declarations.
384 #define TAILQ_HEAD(name, type) \
385 struct name { \
386 struct type *tqh_first; /* first element */ \
387 struct type **tqh_last; /* addr of last next element */ \
388 TRACEBUF \
391 #define TAILQ_HEAD_INITIALIZER(head) \
392 { NULL, &(head).tqh_first }
394 #define TAILQ_ENTRY(type) \
395 struct { \
396 struct type *tqe_next; /* next element */ \
397 struct type **tqe_prev; /* address of previous next element */ \
398 TRACEBUF \
402 * Tail queue functions.
404 #define TAILQ_CONCAT(head1, head2, field) do { \
405 if (!TAILQ_EMPTY(head2)) { \
406 *(head1)->tqh_last = (head2)->tqh_first; \
407 (head2)->tqh_first->field.tqe_prev = (head1)->tqh_last; \
408 (head1)->tqh_last = (head2)->tqh_last; \
409 TAILQ_INIT((head2)); \
410 QMD_TRACE_HEAD(head); \
411 QMD_TRACE_HEAD(head2); \
413 } while (0)
415 #define TAILQ_EMPTY(head) ((head)->tqh_first == NULL)
417 #define TAILQ_FIRST(head) ((head)->tqh_first)
419 #define TAILQ_FOREACH(var, head, field) \
420 for ((var) = TAILQ_FIRST((head)); \
421 (var); \
422 (var) = TAILQ_NEXT((var), field))
424 #define TAILQ_FOREACH_SAFE(var, head, field, tvar) \
425 for ((var) = TAILQ_FIRST((head)); \
426 (var) && ((tvar) = TAILQ_NEXT((var), field), 1); \
427 (var) = (tvar))
429 #define TAILQ_FOREACH_REVERSE(var, head, headname, field) \
430 for ((var) = TAILQ_LAST((head), headname); \
431 (var); \
432 (var) = TAILQ_PREV((var), headname, field))
434 #define TAILQ_FOREACH_REVERSE_SAFE(var, head, headname, field, tvar) \
435 for ((var) = TAILQ_LAST((head), headname); \
436 (var) && ((tvar) = TAILQ_PREV((var), headname, field), 1); \
437 (var) = (tvar))
439 #define TAILQ_INIT(head) do { \
440 TAILQ_FIRST((head)) = NULL; \
441 (head)->tqh_last = &TAILQ_FIRST((head)); \
442 QMD_TRACE_HEAD(head); \
443 } while (0)
445 #define TAILQ_INSERT_AFTER(head, listelm, elm, field) do { \
446 if ((TAILQ_NEXT((elm), field) = TAILQ_NEXT((listelm), field)) != NULL)\
447 TAILQ_NEXT((elm), field)->field.tqe_prev = \
448 &TAILQ_NEXT((elm), field); \
449 else { \
450 (head)->tqh_last = &TAILQ_NEXT((elm), field); \
451 QMD_TRACE_HEAD(head); \
453 TAILQ_NEXT((listelm), field) = (elm); \
454 (elm)->field.tqe_prev = &TAILQ_NEXT((listelm), field); \
455 QMD_TRACE_ELEM(&(elm)->field); \
456 QMD_TRACE_ELEM(&listelm->field); \
457 } while (0)
459 #define TAILQ_INSERT_BEFORE(listelm, elm, field) do { \
460 (elm)->field.tqe_prev = (listelm)->field.tqe_prev; \
461 TAILQ_NEXT((elm), field) = (listelm); \
462 *(listelm)->field.tqe_prev = (elm); \
463 (listelm)->field.tqe_prev = &TAILQ_NEXT((elm), field); \
464 QMD_TRACE_ELEM(&(elm)->field); \
465 QMD_TRACE_ELEM(&listelm->field); \
466 } while (0)
468 #define TAILQ_INSERT_HEAD(head, elm, field) do { \
469 if ((TAILQ_NEXT((elm), field) = TAILQ_FIRST((head))) != NULL) \
470 TAILQ_FIRST((head))->field.tqe_prev = \
471 &TAILQ_NEXT((elm), field); \
472 else \
473 (head)->tqh_last = &TAILQ_NEXT((elm), field); \
474 TAILQ_FIRST((head)) = (elm); \
475 (elm)->field.tqe_prev = &TAILQ_FIRST((head)); \
476 QMD_TRACE_HEAD(head); \
477 QMD_TRACE_ELEM(&(elm)->field); \
478 } while (0)
480 #define TAILQ_INSERT_TAIL(head, elm, field) do { \
481 TAILQ_NEXT((elm), field) = NULL; \
482 (elm)->field.tqe_prev = (head)->tqh_last; \
483 *(head)->tqh_last = (elm); \
484 (head)->tqh_last = &TAILQ_NEXT((elm), field); \
485 QMD_TRACE_HEAD(head); \
486 QMD_TRACE_ELEM(&(elm)->field); \
487 } while (0)
489 #define TAILQ_LAST(head, headname) \
490 (*(((struct headname *)((head)->tqh_last))->tqh_last))
492 #define TAILQ_NEXT(elm, field) ((elm)->field.tqe_next)
494 #define TAILQ_PREV(elm, headname, field) \
495 (*(((struct headname *)((elm)->field.tqe_prev))->tqh_last))
497 #define TAILQ_REMOVE(head, elm, field) do { \
498 if ((TAILQ_NEXT((elm), field)) != NULL) \
499 TAILQ_NEXT((elm), field)->field.tqe_prev = \
500 (elm)->field.tqe_prev; \
501 else { \
502 (head)->tqh_last = (elm)->field.tqe_prev; \
503 QMD_TRACE_HEAD(head); \
505 *(elm)->field.tqe_prev = TAILQ_NEXT((elm), field); \
506 TRASHIT((elm)->field.tqe_next); \
507 TRASHIT((elm)->field.tqe_prev); \
508 QMD_TRACE_ELEM(&(elm)->field); \
509 } while (0)
512 #ifdef _KERNEL
515 * XXX insque() and remque() are an old way of handling certain queues.
516 * They bogusly assumes that all queue heads look alike.
519 struct quehead {
520 struct quehead *qh_link;
521 struct quehead *qh_rlink;
524 #ifdef __GNUC__
526 static __inline void
527 insque(void *a, void *b)
529 struct quehead *element = (struct quehead *)a,
530 *head = (struct quehead *)b;
532 element->qh_link = head->qh_link;
533 element->qh_rlink = head;
534 head->qh_link = element;
535 element->qh_link->qh_rlink = element;
538 static __inline void
539 remque(void *a)
541 struct quehead *element = (struct quehead *)a;
543 element->qh_link->qh_rlink = element->qh_rlink;
544 element->qh_rlink->qh_link = element->qh_link;
545 element->qh_rlink = 0;
548 #else /* !__GNUC__ */
550 void insque(void *a, void *b);
551 void remque(void *a);
553 #endif /* __GNUC__ */
555 #endif /* _KERNEL */
557 #endif /* !_SYS_QUEUE_H_ */