]>
git.ipfire.org Git - thirdparty/systemd.git/blob - list.h
1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
6 /* The head of the linked list. Use this in the structure that shall
7 * contain the head of the linked list */
8 #define LIST_HEAD(t,name) \
11 /* The pointers in the linked list's items. Use this in the item structure */
12 #define LIST_FIELDS(t,name) \
13 t *name##_next, *name##_prev
15 /* Initialize the list's head */
16 #define LIST_HEAD_INIT(t,head) \
21 /* Initialize a list item */
22 #define LIST_INIT(t,name,item) \
26 _item->name##_prev = _item->name##_next = NULL; \
29 /* Prepend an item to the list */
30 #define LIST_PREPEND(t,name,head,item) \
32 t **_head = &(head), *_item = (item); \
34 if ((_item->name##_next = *_head)) \
35 _item->name##_next->name##_prev = _item; \
36 _item->name##_prev = NULL; \
40 /* Remove an item from the list */
41 #define LIST_REMOVE(t,name,head,item) \
43 t **_head = &(head), *_item = (item); \
45 if (_item->name##_next) \
46 _item->name##_next->name##_prev = _item->name##_prev; \
47 if (_item->name##_prev) \
48 _item->name##_prev->name##_next = _item->name##_next; \
50 assert(*_head == _item); \
51 *_head = _item->name##_next; \
53 _item->name##_next = _item->name##_prev = NULL; \
56 /* Find the head of the list */
57 #define LIST_FIND_HEAD(t,name,item,head) \
61 while ((_item->name##_prev) \
62 _item = _item->name##_prev; \
66 /* Find the head of the list */
67 #define LIST_FIND_TAIL(t,name,item,tail) \
71 while (_item->name##_next) \
72 _item = _item->name##_next; \
76 /* Insert an item after another one (a = where, b = what) */
77 #define LIST_INSERT_AFTER(t,name,head,a,b) \
79 t **_head = &(head), *_a = (a), *_b = (b); \
82 if ((_b->name##_next = *_head)) \
83 _b->name##_next->name##_prev = _b; \
84 _b->name##_prev = NULL; \
87 if ((_b->name##_next = _a->name##_next)) \
88 _b->name##_next->name##_prev = _b; \
89 _b->name##_prev = _a; \
90 _a->name##_next = _b; \
94 #define LIST_FOREACH(name,i,head) \
95 for ((i) = (head); (i); (i) = (i)->name##_next)
97 #define LIST_FOREACH_SAFE(name,i,n,head) \
98 for ((i) = (head); (i) && (((n) = (i)->name##_next), 1); (i) = (n))