5 #define __offsetof(type, field) ((size_t)(&((type *)0)->field))
6 #define __rangeof(type, start, end) \
7 (__offsetof(type, end) - __offsetof(type, start))
9 #define QMD_TRACE_ELEM(elem)
10 #define QMD_TRACE_HEAD(head)
17 #define LIST_HEAD(name, type) \
19 struct type *lh_first; /* first element */ \
22 #define LIST_HEAD_INITIALIZER(head) \
25 #define LIST_ENTRY(type) \
27 struct type *le_next; /* next element */ \
28 struct type **le_prev; /* address of previous next element */ \
35 #define LIST_EMPTY(head) ((head)->lh_first == NULL)
37 #define LIST_FIRST(head) ((head)->lh_first)
39 #define LIST_FOREACH(var, head, field) \
40 for ((var) = LIST_FIRST((head)); \
42 (var) = LIST_NEXT((var), field))
44 #define LIST_FOREACH_SAFE(var, head, field, tvar) \
45 for ((var) = LIST_FIRST((head)); \
46 (var) && ((tvar) = LIST_NEXT((var), field), 1); \
49 #define LIST_INIT(head) do { \
50 LIST_FIRST((head)) = NULL; \
53 #define LIST_INSERT_AFTER(listelm, elm, field) do { \
54 if ((LIST_NEXT((elm), field) = LIST_NEXT((listelm), field)) != NULL)\
55 LIST_NEXT((listelm), field)->field.le_prev = \
56 &LIST_NEXT((elm), field); \
57 LIST_NEXT((listelm), field) = (elm); \
58 (elm)->field.le_prev = &LIST_NEXT((listelm), field); \
61 #define LIST_INSERT_BEFORE(listelm, elm, field) do { \
62 (elm)->field.le_prev = (listelm)->field.le_prev; \
63 LIST_NEXT((elm), field) = (listelm); \
64 *(listelm)->field.le_prev = (elm); \
65 (listelm)->field.le_prev = &LIST_NEXT((elm), field); \
68 #define LIST_INSERT_HEAD(head, elm, field) do { \
69 if ((LIST_NEXT((elm), field) = LIST_FIRST((head))) != NULL) \
70 LIST_FIRST((head))->field.le_prev = &LIST_NEXT((elm), field);\
71 LIST_FIRST((head)) = (elm); \
72 (elm)->field.le_prev = &LIST_FIRST((head)); \
75 #define LIST_NEXT(elm, field) ((elm)->field.le_next)
77 #define LIST_REMOVE(elm, field) do { \
78 if (LIST_NEXT((elm), field) != NULL) \
79 LIST_NEXT((elm), field)->field.le_prev = \
80 (elm)->field.le_prev; \
81 *(elm)->field.le_prev = LIST_NEXT((elm), field); \
85 * Tail queue declarations.
87 #define TAILQ_HEAD(name, type) \
89 struct type *tqh_first; /* first element */ \
90 struct type **tqh_last; /* addr of last next element */ \
94 #define TAILQ_HEAD_INITIALIZER(head) \
95 { NULL, &(head).tqh_first }
97 #define TAILQ_ENTRY(type) \
99 struct type *tqe_next; /* next element */ \
100 struct type **tqe_prev; /* address of previous next element */ \
105 * Tail queue functions.
107 #define TAILQ_CONCAT(head1, head2, field) do { \
108 if (!TAILQ_EMPTY(head2)) { \
109 *(head1)->tqh_last = (head2)->tqh_first; \
110 (head2)->tqh_first->field.tqe_prev = (head1)->tqh_last; \
111 (head1)->tqh_last = (head2)->tqh_last; \
112 TAILQ_INIT((head2)); \
113 QMD_TRACE_HEAD(head); \
114 QMD_TRACE_HEAD(head2); \
118 #define TAILQ_EMPTY(head) ((head)->tqh_first == NULL)
120 #define TAILQ_FIRST(head) ((head)->tqh_first)
122 #define TAILQ_FOREACH(var, head, field) \
123 for ((var) = TAILQ_FIRST((head)); \
125 (var) = TAILQ_NEXT((var), field))
127 #define TAILQ_FOREACH_SAFE(var, head, field, tvar) \
128 for ((var) = TAILQ_FIRST((head)); \
129 (var) && ((tvar) = TAILQ_NEXT((var), field), 1); \
132 #define TAILQ_FOREACH_REVERSE(var, head, headname, field) \
133 for ((var) = TAILQ_LAST((head), headname); \
135 (var) = TAILQ_PREV((var), headname, field))
137 #define TAILQ_FOREACH_REVERSE_SAFE(var, head, headname, field, tvar) \
138 for ((var) = TAILQ_LAST((head), headname); \
139 (var) && ((tvar) = TAILQ_PREV((var), headname, field), 1); \
142 #define TAILQ_INIT(head) do { \
143 TAILQ_FIRST((head)) = NULL; \
144 (head)->tqh_last = &TAILQ_FIRST((head)); \
145 QMD_TRACE_HEAD(head); \
148 #define TAILQ_INSERT_AFTER(head, listelm, elm, field) do { \
149 if ((TAILQ_NEXT((elm), field) = TAILQ_NEXT((listelm), field)) != NULL)\
150 TAILQ_NEXT((elm), field)->field.tqe_prev = \
151 &TAILQ_NEXT((elm), field); \
153 (head)->tqh_last = &TAILQ_NEXT((elm), field); \
154 QMD_TRACE_HEAD(head); \
156 TAILQ_NEXT((listelm), field) = (elm); \
157 (elm)->field.tqe_prev = &TAILQ_NEXT((listelm), field); \
158 QMD_TRACE_ELEM(&(elm)->field); \
159 QMD_TRACE_ELEM(&listelm->field); \
162 #define TAILQ_INSERT_BEFORE(listelm, elm, field) do { \
163 (elm)->field.tqe_prev = (listelm)->field.tqe_prev; \
164 TAILQ_NEXT((elm), field) = (listelm); \
165 *(listelm)->field.tqe_prev = (elm); \
166 (listelm)->field.tqe_prev = &TAILQ_NEXT((elm), field); \
167 QMD_TRACE_ELEM(&(elm)->field); \
168 QMD_TRACE_ELEM(&listelm->field); \
171 #define TAILQ_INSERT_HEAD(head, elm, field) do { \
172 if ((TAILQ_NEXT((elm), field) = TAILQ_FIRST((head))) != NULL) \
173 TAILQ_FIRST((head))->field.tqe_prev = \
174 &TAILQ_NEXT((elm), field); \
176 (head)->tqh_last = &TAILQ_NEXT((elm), field); \
177 TAILQ_FIRST((head)) = (elm); \
178 (elm)->field.tqe_prev = &TAILQ_FIRST((head)); \
179 QMD_TRACE_HEAD(head); \
180 QMD_TRACE_ELEM(&(elm)->field); \
183 #define TAILQ_INSERT_TAIL(head, elm, field) do { \
184 TAILQ_NEXT((elm), field) = NULL; \
185 (elm)->field.tqe_prev = (head)->tqh_last; \
186 *(head)->tqh_last = (elm); \
187 (head)->tqh_last = &TAILQ_NEXT((elm), field); \
188 QMD_TRACE_HEAD(head); \
189 QMD_TRACE_ELEM(&(elm)->field); \
192 #define TAILQ_LAST(head, headname) \
193 (*(((struct headname *)((head)->tqh_last))->tqh_last))
195 #define TAILQ_NEXT(elm, field) ((elm)->field.tqe_next)
197 #define TAILQ_PREV(elm, headname, field) \
198 (*(((struct headname *)((elm)->field.tqe_prev))->tqh_last))
200 #define TAILQ_REMOVE(head, elm, field) do { \
201 if ((TAILQ_NEXT((elm), field)) != NULL) \
202 TAILQ_NEXT((elm), field)->field.tqe_prev = \
203 (elm)->field.tqe_prev; \
205 (head)->tqh_last = (elm)->field.tqe_prev; \
206 QMD_TRACE_HEAD(head); \
208 *(elm)->field.tqe_prev = TAILQ_NEXT((elm), field); \
209 TRASHIT((elm)->field.tqe_next); \
210 TRASHIT((elm)->field.tqe_prev); \
211 QMD_TRACE_ELEM(&(elm)->field); \
215 #endif /* _SYS_QUEUE_H */