1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30#ifndef _SYS_QUEUE_H_
31#define _SYS_QUEUE_H_
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107#define SLIST_HEAD(name, type) \
108struct name { \
109 struct type *slh_first; \
110}
111
112#define SLIST_HEAD_INITIALIZER(head) \
113 { NULL }
114
115#define SLIST_ENTRY(type) \
116struct { \
117 struct type *sle_next; \
118}
119
120
121
122
123#define SLIST_EMPTY(head) ((head)->slh_first == NULL)
124
125#define SLIST_FIRST(head) ((head)->slh_first)
126
127#define SLIST_FOREACH(var, head, field) \
128 for ((var) = SLIST_FIRST((head)); \
129 (var); \
130 (var) = SLIST_NEXT((var), field))
131
132#define SLIST_INIT(head) do { \
133 SLIST_FIRST((head)) = NULL; \
134} while (0)
135
136#define SLIST_INSERT_AFTER(slistelm, elm, field) do { \
137 SLIST_NEXT((elm), field) = SLIST_NEXT((slistelm), field); \
138 SLIST_NEXT((slistelm), field) = (elm); \
139} while (0)
140
141#define SLIST_INSERT_HEAD(head, elm, field) do { \
142 SLIST_NEXT((elm), field) = SLIST_FIRST((head)); \
143 SLIST_FIRST((head)) = (elm); \
144} while (0)
145
146#define SLIST_NEXT(elm, field) ((elm)->field.sle_next)
147
148#define SLIST_REMOVE(head, elm, type, field) do { \
149 if (SLIST_FIRST((head)) == (elm)) { \
150 SLIST_REMOVE_HEAD((head), field); \
151 } \
152 else { \
153 struct type *curelm = SLIST_FIRST((head)); \
154 while (SLIST_NEXT(curelm, field) != (elm)) \
155 curelm = SLIST_NEXT(curelm, field); \
156 SLIST_NEXT(curelm, field) = \
157 SLIST_NEXT(SLIST_NEXT(curelm, field), field); \
158 } \
159} while (0)
160
161#define SLIST_REMOVE_HEAD(head, field) do { \
162 SLIST_FIRST((head)) = SLIST_NEXT(SLIST_FIRST((head)), field); \
163} while (0)
164
165
166
167
168#define STAILQ_HEAD(name, type) \
169struct name { \
170 struct type *stqh_first; \
171 struct type **stqh_last; \
172}
173
174#define STAILQ_HEAD_INITIALIZER(head) \
175 { NULL, &(head).stqh_first }
176
177#define STAILQ_ENTRY(type) \
178struct { \
179 struct type *stqe_next; \
180}
181
182
183
184
185#define STAILQ_EMPTY(head) ((head)->stqh_first == NULL)
186
187#define STAILQ_FIRST(head) ((head)->stqh_first)
188
189#define STAILQ_FOREACH(var, head, field) \
190 for((var) = STAILQ_FIRST((head)); \
191 (var); \
192 (var) = STAILQ_NEXT((var), field))
193
194#define STAILQ_INIT(head) do { \
195 STAILQ_FIRST((head)) = NULL; \
196 (head)->stqh_last = &STAILQ_FIRST((head)); \
197} while (0)
198
199#define STAILQ_INSERT_AFTER(head, tqelm, elm, field) do { \
200 if ((STAILQ_NEXT((elm), field) = STAILQ_NEXT((tqelm), field)) == NULL)\
201 (head)->stqh_last = &STAILQ_NEXT((elm), field); \
202 STAILQ_NEXT((tqelm), field) = (elm); \
203} while (0)
204
205#define STAILQ_INSERT_HEAD(head, elm, field) do { \
206 if ((STAILQ_NEXT((elm), field) = STAILQ_FIRST((head))) == NULL) \
207 (head)->stqh_last = &STAILQ_NEXT((elm), field); \
208 STAILQ_FIRST((head)) = (elm); \
209} while (0)
210
211#define STAILQ_INSERT_TAIL(head, elm, field) do { \
212 STAILQ_NEXT((elm), field) = NULL; \
213 STAILQ_LAST((head)) = (elm); \
214 (head)->stqh_last = &STAILQ_NEXT((elm), field); \
215} while (0)
216
217#define STAILQ_LAST(head) (*(head)->stqh_last)
218
219#define STAILQ_NEXT(elm, field) ((elm)->field.stqe_next)
220
221#define STAILQ_REMOVE(head, elm, type, field) do { \
222 if (STAILQ_FIRST((head)) == (elm)) { \
223 STAILQ_REMOVE_HEAD(head, field); \
224 } \
225 else { \
226 struct type *curelm = STAILQ_FIRST((head)); \
227 while (STAILQ_NEXT(curelm, field) != (elm)) \
228 curelm = STAILQ_NEXT(curelm, field); \
229 if ((STAILQ_NEXT(curelm, field) = \
230 STAILQ_NEXT(STAILQ_NEXT(curelm, field), field)) == NULL)\
231 (head)->stqh_last = &STAILQ_NEXT((curelm), field);\
232 } \
233} while (0)
234
235#define STAILQ_REMOVE_HEAD(head, field) do { \
236 if ((STAILQ_FIRST((head)) = \
237 STAILQ_NEXT(STAILQ_FIRST((head)), field)) == NULL) \
238 (head)->stqh_last = &STAILQ_FIRST((head)); \
239} while (0)
240
241#define STAILQ_REMOVE_HEAD_UNTIL(head, elm, field) do { \
242 if ((STAILQ_FIRST((head)) = STAILQ_NEXT((elm), field)) == NULL) \
243 (head)->stqh_last = &STAILQ_FIRST((head)); \
244} while (0)
245
246
247
248
249#define LIST_HEAD(name, type) \
250struct name { \
251 struct type *lh_first; \
252}
253
254#define LIST_HEAD_INITIALIZER(head) \
255 { NULL }
256
257#define LIST_ENTRY(type) \
258struct { \
259 struct type *le_next; \
260 struct type **le_prev; \
261}
262
263
264
265
266
267#define LIST_EMPTY(head) ((head)->lh_first == NULL)
268
269#define LIST_FIRST(head) ((head)->lh_first)
270
271#define LIST_FOREACH(var, head, field) \
272 for ((var) = LIST_FIRST((head)); \
273 (var); \
274 (var) = LIST_NEXT((var), field))
275
276#define LIST_INIT(head) do { \
277 LIST_FIRST((head)) = NULL; \
278} while (0)
279
280#define LIST_INSERT_AFTER(listelm, elm, field) do { \
281 if ((LIST_NEXT((elm), field) = LIST_NEXT((listelm), field)) != NULL)\
282 LIST_NEXT((listelm), field)->field.le_prev = \
283 &LIST_NEXT((elm), field); \
284 LIST_NEXT((listelm), field) = (elm); \
285 (elm)->field.le_prev = &LIST_NEXT((listelm), field); \
286} while (0)
287
288#define LIST_INSERT_BEFORE(listelm, elm, field) do { \
289 (elm)->field.le_prev = (listelm)->field.le_prev; \
290 LIST_NEXT((elm), field) = (listelm); \
291 *(listelm)->field.le_prev = (elm); \
292 (listelm)->field.le_prev = &LIST_NEXT((elm), field); \
293} while (0)
294
295#define LIST_INSERT_HEAD(head, elm, field) do { \
296 if ((LIST_NEXT((elm), field) = LIST_FIRST((head))) != NULL) \
297 LIST_FIRST((head))->field.le_prev = &LIST_NEXT((elm), field);\
298 LIST_FIRST((head)) = (elm); \
299 (elm)->field.le_prev = &LIST_FIRST((head)); \
300} while (0)
301
302#define LIST_NEXT(elm, field) ((elm)->field.le_next)
303
304#define LIST_REMOVE(elm, field) do { \
305 if (LIST_NEXT((elm), field) != NULL) \
306 LIST_NEXT((elm), field)->field.le_prev = \
307 (elm)->field.le_prev; \
308 *(elm)->field.le_prev = LIST_NEXT((elm), field); \
309} while (0)
310
311
312
313
314#define TAILQ_HEAD(name, type) \
315struct name { \
316 struct type *tqh_first; \
317 struct type **tqh_last; \
318}
319
320#define TAILQ_HEAD_INITIALIZER(head) \
321 { NULL, &(head).tqh_first }
322
323#define TAILQ_ENTRY(type) \
324struct { \
325 struct type *tqe_next; \
326 struct type **tqe_prev; \
327}
328
329
330
331
332#define TAILQ_EMPTY(head) ((head)->tqh_first == NULL)
333
334#define TAILQ_FIRST(head) ((head)->tqh_first)
335
336#define TAILQ_FOREACH(var, head, field) \
337 for ((var) = TAILQ_FIRST((head)); \
338 (var); \
339 (var) = TAILQ_NEXT((var), field))
340
341#define TAILQ_FOREACH_REVERSE(var, head, headname, field) \
342 for ((var) = TAILQ_LAST((head), headname); \
343 (var); \
344 (var) = TAILQ_PREV((var), headname, field))
345
346#define TAILQ_INIT(head) do { \
347 TAILQ_FIRST((head)) = NULL; \
348 (head)->tqh_last = &TAILQ_FIRST((head)); \
349} while (0)
350
351#define TAILQ_INSERT_AFTER(head, listelm, elm, field) do { \
352 if ((TAILQ_NEXT((elm), field) = TAILQ_NEXT((listelm), field)) != NULL)\
353 TAILQ_NEXT((elm), field)->field.tqe_prev = \
354 &TAILQ_NEXT((elm), field); \
355 else \
356 (head)->tqh_last = &TAILQ_NEXT((elm), field); \
357 TAILQ_NEXT((listelm), field) = (elm); \
358 (elm)->field.tqe_prev = &TAILQ_NEXT((listelm), field); \
359} while (0)
360
361#define TAILQ_INSERT_BEFORE(listelm, elm, field) do { \
362 (elm)->field.tqe_prev = (listelm)->field.tqe_prev; \
363 TAILQ_NEXT((elm), field) = (listelm); \
364 *(listelm)->field.tqe_prev = (elm); \
365 (listelm)->field.tqe_prev = &TAILQ_NEXT((elm), field); \
366} while (0)
367
368#define TAILQ_INSERT_HEAD(head, elm, field) do { \
369 if ((TAILQ_NEXT((elm), field) = TAILQ_FIRST((head))) != NULL) \
370 TAILQ_FIRST((head))->field.tqe_prev = \
371 &TAILQ_NEXT((elm), field); \
372 else \
373 (head)->tqh_last = &TAILQ_NEXT((elm), field); \
374 TAILQ_FIRST((head)) = (elm); \
375 (elm)->field.tqe_prev = &TAILQ_FIRST((head)); \
376} while (0)
377
378#define TAILQ_INSERT_TAIL(head, elm, field) do { \
379 TAILQ_NEXT((elm), field) = NULL; \
380 (elm)->field.tqe_prev = (head)->tqh_last; \
381 *(head)->tqh_last = (elm); \
382 (head)->tqh_last = &TAILQ_NEXT((elm), field); \
383} while (0)
384
385#define TAILQ_LAST(head, headname) \
386 (*(((struct headname *)((head)->tqh_last))->tqh_last))
387
388#define TAILQ_NEXT(elm, field) ((elm)->field.tqe_next)
389
390#define TAILQ_PREV(elm, headname, field) \
391 (*(((struct headname *)((elm)->field.tqe_prev))->tqh_last))
392
393#define TAILQ_REMOVE(head, elm, field) do { \
394 if ((TAILQ_NEXT((elm), field)) != NULL) \
395 TAILQ_NEXT((elm), field)->field.tqe_prev = \
396 (elm)->field.tqe_prev; \
397 else \
398 (head)->tqh_last = (elm)->field.tqe_prev; \
399 *(elm)->field.tqe_prev = TAILQ_NEXT((elm), field); \
400} while (0)
401
402
403
404
405#define CIRCLEQ_HEAD(name, type) \
406struct name { \
407 struct type *cqh_first; \
408 struct type *cqh_last; \
409}
410
411#define CIRCLEQ_HEAD_INITIALIZER(head) \
412 { (void *)&(head), (void *)&(head) }
413
414#define CIRCLEQ_ENTRY(type) \
415struct { \
416 struct type *cqe_next; \
417 struct type *cqe_prev; \
418}
419
420
421
422
423#define CIRCLEQ_EMPTY(head) ((head)->cqh_first == (void *)(head))
424
425#define CIRCLEQ_FIRST(head) ((head)->cqh_first)
426
427#define CIRCLEQ_FOREACH(var, head, field) \
428 for ((var) = CIRCLEQ_FIRST((head)); \
429 (var) != (void *)(head); \
430 (var) = CIRCLEQ_NEXT((var), field))
431
432#define CIRCLEQ_FOREACH_REVERSE(var, head, field) \
433 for ((var) = CIRCLEQ_LAST((head)); \
434 (var) != (void *)(head); \
435 (var) = CIRCLEQ_PREV((var), field))
436
437#define CIRCLEQ_INIT(head) do { \
438 CIRCLEQ_FIRST((head)) = (void *)(head); \
439 CIRCLEQ_LAST((head)) = (void *)(head); \
440} while (0)
441
442#define CIRCLEQ_INSERT_AFTER(head, listelm, elm, field) do { \
443 CIRCLEQ_NEXT((elm), field) = CIRCLEQ_NEXT((listelm), field); \
444 CIRCLEQ_PREV((elm), field) = (listelm); \
445 if (CIRCLEQ_NEXT((listelm), field) == (void *)(head)) \
446 CIRCLEQ_LAST((head)) = (elm); \
447 else \
448 CIRCLEQ_PREV(CIRCLEQ_NEXT((listelm), field), field) = (elm);\
449 CIRCLEQ_NEXT((listelm), field) = (elm); \
450} while (0)
451
452#define CIRCLEQ_INSERT_BEFORE(head, listelm, elm, field) do { \
453 CIRCLEQ_NEXT((elm), field) = (listelm); \
454 CIRCLEQ_PREV((elm), field) = CIRCLEQ_PREV((listelm), field); \
455 if (CIRCLEQ_PREV((listelm), field) == (void *)(head)) \
456 CIRCLEQ_FIRST((head)) = (elm); \
457 else \
458 CIRCLEQ_NEXT(CIRCLEQ_PREV((listelm), field), field) = (elm);\
459 CIRCLEQ_PREV((listelm), field) = (elm); \
460} while (0)
461
462#define CIRCLEQ_INSERT_HEAD(head, elm, field) do { \
463 CIRCLEQ_NEXT((elm), field) = CIRCLEQ_FIRST((head)); \
464 CIRCLEQ_PREV((elm), field) = (void *)(head); \
465 if (CIRCLEQ_LAST((head)) == (void *)(head)) \
466 CIRCLEQ_LAST((head)) = (elm); \
467 else \
468 CIRCLEQ_PREV(CIRCLEQ_FIRST((head)), field) = (elm); \
469 CIRCLEQ_FIRST((head)) = (elm); \
470} while (0)
471
472#define CIRCLEQ_INSERT_TAIL(head, elm, field) do { \
473 CIRCLEQ_NEXT((elm), field) = (void *)(head); \
474 CIRCLEQ_PREV((elm), field) = CIRCLEQ_LAST((head)); \
475 if (CIRCLEQ_FIRST((head)) == (void *)(head)) \
476 CIRCLEQ_FIRST((head)) = (elm); \
477 else \
478 CIRCLEQ_NEXT(CIRCLEQ_LAST((head)), field) = (elm); \
479 CIRCLEQ_LAST((head)) = (elm); \
480} while (0)
481
482#define CIRCLEQ_LAST(head) ((head)->cqh_last)
483
484#define CIRCLEQ_NEXT(elm,field) ((elm)->field.cqe_next)
485
486#define CIRCLEQ_PREV(elm,field) ((elm)->field.cqe_prev)
487
488#define CIRCLEQ_REMOVE(head, elm, field) do { \
489 if (CIRCLEQ_NEXT((elm), field) == (void *)(head)) \
490 CIRCLEQ_LAST((head)) = CIRCLEQ_PREV((elm), field); \
491 else \
492 CIRCLEQ_PREV(CIRCLEQ_NEXT((elm), field), field) = \
493 CIRCLEQ_PREV((elm), field); \
494 if (CIRCLEQ_PREV((elm), field) == (void *)(head)) \
495 CIRCLEQ_FIRST((head)) = CIRCLEQ_NEXT((elm), field); \
496 else \
497 CIRCLEQ_NEXT(CIRCLEQ_PREV((elm), field), field) = \
498 CIRCLEQ_NEXT((elm), field); \
499} while (0)
500
501#endif
502