]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/list.h
6ae88ec6b960e73106b544dba6ded86fc99c3886
1 /* SPDX-License-Identifier: LGPL-2.1+ */
5 Copyright 2010 Lennart Poettering
8 /* The head of the linked list. Use this in the structure that shall
9 * contain the head of the linked list */
10 #define LIST_HEAD(t,name) \
13 /* The pointers in the linked list's items. Use this in the item structure */
14 #define LIST_FIELDS(t,name) \
15 t *name##_next, *name##_prev
17 /* Initialize the list's head */
18 #define LIST_HEAD_INIT(head) \
23 /* Initialize a list item */
24 #define LIST_INIT(name,item) \
26 typeof(*(item)) *_item = (item); \
28 _item->name##_prev = _item->name##_next = NULL; \
31 /* Prepend an item to the list */
32 #define LIST_PREPEND(name,head,item) \
34 typeof(*(head)) **_head = &(head), *_item = (item); \
36 if ((_item->name##_next = *_head)) \
37 _item->name##_next->name##_prev = _item; \
38 _item->name##_prev = NULL; \
42 /* Append an item to the list */
43 #define LIST_APPEND(name,head,item) \
45 typeof(*(head)) *_tail; \
46 LIST_FIND_TAIL(name,head,_tail); \
47 LIST_INSERT_AFTER(name,head,_tail,item); \
50 /* Remove an item from the list */
51 #define LIST_REMOVE(name,head,item) \
53 typeof(*(head)) **_head = &(head), *_item = (item); \
55 if (_item->name##_next) \
56 _item->name##_next->name##_prev = _item->name##_prev; \
57 if (_item->name##_prev) \
58 _item->name##_prev->name##_next = _item->name##_next; \
60 assert(*_head == _item); \
61 *_head = _item->name##_next; \
63 _item->name##_next = _item->name##_prev = NULL; \
66 /* Find the head of the list */
67 #define LIST_FIND_HEAD(name,item,head) \
69 typeof(*(item)) *_item = (item); \
73 while (_item->name##_prev) \
74 _item = _item->name##_prev; \
79 /* Find the tail of the list */
80 #define LIST_FIND_TAIL(name,item,tail) \
82 typeof(*(item)) *_item = (item); \
86 while (_item->name##_next) \
87 _item = _item->name##_next; \
92 /* Insert an item after another one (a = where, b = what) */
93 #define LIST_INSERT_AFTER(name,head,a,b) \
95 typeof(*(head)) **_head = &(head), *_a = (a), *_b = (b); \
98 if ((_b->name##_next = *_head)) \
99 _b->name##_next->name##_prev = _b; \
100 _b->name##_prev = NULL; \
103 if ((_b->name##_next = _a->name##_next)) \
104 _b->name##_next->name##_prev = _b; \
105 _b->name##_prev = _a; \
106 _a->name##_next = _b; \
110 /* Insert an item before another one (a = where, b = what) */
111 #define LIST_INSERT_BEFORE(name,head,a,b) \
113 typeof(*(head)) **_head = &(head), *_a = (a), *_b = (b); \
117 _b->name##_next = NULL; \
118 _b->name##_prev = NULL; \
121 typeof(*(head)) *_tail = (head); \
122 while (_tail->name##_next) \
123 _tail = _tail->name##_next; \
124 _b->name##_next = NULL; \
125 _b->name##_prev = _tail; \
126 _tail->name##_next = _b; \
129 if ((_b->name##_prev = _a->name##_prev)) \
130 _b->name##_prev->name##_next = _b; \
133 _b->name##_next = _a; \
134 _a->name##_prev = _b; \
138 #define LIST_JUST_US(name,item) \
139 (!(item)->name##_prev && !(item)->name##_next) \
141 #define LIST_FOREACH(name,i,head) \
142 for ((i) = (head); (i); (i) = (i)->name##_next)
144 #define LIST_FOREACH_SAFE(name,i,n,head) \
145 for ((i) = (head); (i) && (((n) = (i)->name##_next), 1); (i) = (n))
147 #define LIST_FOREACH_BEFORE(name,i,p) \
148 for ((i) = (p)->name##_prev; (i); (i) = (i)->name##_prev)
150 #define LIST_FOREACH_AFTER(name,i,p) \
151 for ((i) = (p)->name##_next; (i); (i) = (i)->name##_next)
153 /* Iterate through all the members of the list p is included in, but skip over p */
154 #define LIST_FOREACH_OTHERS(name,i,p) \
157 while ((i) && (i)->name##_prev) \
158 (i) = (i)->name##_prev; \
160 (i) = (p)->name##_next; \
163 (i) = (i)->name##_next == (p) ? (p)->name##_next : (i)->name##_next)
165 /* Loop starting from p->next until p->prev.
166 p can be adjusted meanwhile. */
167 #define LIST_LOOP_BUT_ONE(name,i,head,p) \
168 for ((i) = (p)->name##_next ? (p)->name##_next : (head); \
170 (i) = (i)->name##_next ? (i)->name##_next : (head))
172 #define LIST_IS_EMPTY(head) \