1 /* This is a stripped down version of FreeBSD's
2 * src/sys/sys/queue.h,v 1.60.2.1
4 * The original file's license:
6 * Copyright (c) 1991, 1993
7 * The Regents of the University of California. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
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
34 #define STAILQ_HEAD(name, type) \
36 struct type *stqh_first;/* first element */ \
37 struct type **stqh_last;/* addr of last next element */ \
40 #define STAILQ_HEAD_INITIALIZER(head) \
41 { NULL, &(head).stqh_first }
43 #define STAILQ_ENTRY(type) \
45 struct type *stqe_next; /* next element */ \
48 #define STAILQ_EMPTY(head) ((head)->stqh_first == NULL)
50 #define STAILQ_FIRST(head) ((head)->stqh_first)
52 #define STAILQ_FOREACH(var, head, field) \
53 for((var) = STAILQ_FIRST((head)); \
55 (var) = STAILQ_NEXT((var), field))
57 #define STAILQ_NEXT(elm, field) ((elm)->field.stqe_next)
59 #ifdef STAILQ_INSERT_TAIL
60 #undef STAILQ_INSERT_TAIL
63 #define STAILQ_INSERT_TAIL(head, elm, field) do { \
64 STAILQ_NEXT((elm), field) = NULL; \
65 *(head)->stqh_last = (elm); \
66 (head)->stqh_last = &STAILQ_NEXT((elm), field); \
69 #ifdef STAILQ_REMOVE_HEAD
70 #undef STAILQ_REMOVE_HEAD
73 #define STAILQ_REMOVE_HEAD(head, field) do { \
74 if ((STAILQ_FIRST((head)) = \
75 STAILQ_NEXT(STAILQ_FIRST((head)), field)) == NULL) \
76 (head)->stqh_last = &STAILQ_FIRST((head)); \
80 #define LIST_HEAD(name, type) \
82 struct type *lh_first; /* first element */ \
85 #undef LIST_HEAD_INITIALIZER
86 #define LIST_HEAD_INITIALIZER(head) \
90 #define LIST_ENTRY(type) \
92 struct type *le_next; /* next element */ \
93 struct type **le_prev; /* address of previous next element */ \
97 #define LIST_FIRST(head) ((head)->lh_first)
100 #define LIST_FOREACH(var, head, field) \
101 for ((var) = LIST_FIRST((head)); \
103 (var) = LIST_NEXT((var), field))
105 #undef LIST_FOREACH_SAFE
106 #define LIST_FOREACH_SAFE(var, head, field, tvar) \
107 for ((var) = LIST_FIRST((head)); \
108 (var) && ((tvar) = LIST_NEXT((var), field), 1); \
112 #define LIST_INIT(head) do { \
113 LIST_FIRST((head)) = NULL; \
116 #undef LIST_INSERT_HEAD
117 #define LIST_INSERT_HEAD(head, elm, field) do { \
118 if ((LIST_NEXT((elm), field) = LIST_FIRST((head))) != NULL) \
119 LIST_FIRST((head))->field.le_prev = &LIST_NEXT((elm), field);\
120 LIST_FIRST((head)) = (elm); \
121 (elm)->field.le_prev = &LIST_FIRST((head)); \
125 #define LIST_NEXT(elm, field) ((elm)->field.le_next)
128 #define LIST_REMOVE(elm, field) do { \
129 if (LIST_NEXT((elm), field) != NULL) \
130 LIST_NEXT((elm), field)->field.le_prev = \
131 (elm)->field.le_prev; \
132 *(elm)->field.le_prev = LIST_NEXT((elm), field); \