]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/list.h
Merge pull request #11827 from keszybz/pkgconfig-variables
[thirdparty/systemd.git] / src / basic / list.h
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 #pragma once
3
4 #include "macro.h"
5
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) \
9 t *name
10
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
14
15 /* Initialize the list's head */
16 #define LIST_HEAD_INIT(head) \
17 do { \
18 (head) = NULL; \
19 } while (false)
20
21 /* Initialize a list item */
22 #define LIST_INIT(name,item) \
23 do { \
24 typeof(*(item)) *_item = (item); \
25 assert(_item); \
26 _item->name##_prev = _item->name##_next = NULL; \
27 } while (false)
28
29 /* Prepend an item to the list */
30 #define LIST_PREPEND(name,head,item) \
31 do { \
32 typeof(*(head)) **_head = &(head), *_item = (item); \
33 assert(_item); \
34 if ((_item->name##_next = *_head)) \
35 _item->name##_next->name##_prev = _item; \
36 _item->name##_prev = NULL; \
37 *_head = _item; \
38 } while (false)
39
40 /* Append an item to the list */
41 #define LIST_APPEND(name,head,item) \
42 do { \
43 typeof(*(head)) **_hhead = &(head), *_tail; \
44 LIST_FIND_TAIL(name, *_hhead, _tail); \
45 LIST_INSERT_AFTER(name, *_hhead, _tail, item); \
46 } while (false)
47
48 /* Remove an item from the list */
49 #define LIST_REMOVE(name,head,item) \
50 do { \
51 typeof(*(head)) **_head = &(head), *_item = (item); \
52 assert(_item); \
53 if (_item->name##_next) \
54 _item->name##_next->name##_prev = _item->name##_prev; \
55 if (_item->name##_prev) \
56 _item->name##_prev->name##_next = _item->name##_next; \
57 else { \
58 assert(*_head == _item); \
59 *_head = _item->name##_next; \
60 } \
61 _item->name##_next = _item->name##_prev = NULL; \
62 } while (false)
63
64 /* Find the head of the list */
65 #define LIST_FIND_HEAD(name,item,head) \
66 do { \
67 typeof(*(item)) *_item = (item); \
68 if (!_item) \
69 (head) = NULL; \
70 else { \
71 while (_item->name##_prev) \
72 _item = _item->name##_prev; \
73 (head) = _item; \
74 } \
75 } while (false)
76
77 /* Find the tail of the list */
78 #define LIST_FIND_TAIL(name,item,tail) \
79 do { \
80 typeof(*(item)) *_item = (item); \
81 if (!_item) \
82 (tail) = NULL; \
83 else { \
84 while (_item->name##_next) \
85 _item = _item->name##_next; \
86 (tail) = _item; \
87 } \
88 } while (false)
89
90 /* Insert an item after another one (a = where, b = what) */
91 #define LIST_INSERT_AFTER(name,head,a,b) \
92 do { \
93 typeof(*(head)) **_head = &(head), *_a = (a), *_b = (b); \
94 assert(_b); \
95 if (!_a) { \
96 if ((_b->name##_next = *_head)) \
97 _b->name##_next->name##_prev = _b; \
98 _b->name##_prev = NULL; \
99 *_head = _b; \
100 } else { \
101 if ((_b->name##_next = _a->name##_next)) \
102 _b->name##_next->name##_prev = _b; \
103 _b->name##_prev = _a; \
104 _a->name##_next = _b; \
105 } \
106 } while (false)
107
108 /* Insert an item before another one (a = where, b = what) */
109 #define LIST_INSERT_BEFORE(name,head,a,b) \
110 do { \
111 typeof(*(head)) **_head = &(head), *_a = (a), *_b = (b); \
112 assert(_b); \
113 if (!_a) { \
114 if (!*_head) { \
115 _b->name##_next = NULL; \
116 _b->name##_prev = NULL; \
117 *_head = _b; \
118 } else { \
119 typeof(*(head)) *_tail = (head); \
120 while (_tail->name##_next) \
121 _tail = _tail->name##_next; \
122 _b->name##_next = NULL; \
123 _b->name##_prev = _tail; \
124 _tail->name##_next = _b; \
125 } \
126 } else { \
127 if ((_b->name##_prev = _a->name##_prev)) \
128 _b->name##_prev->name##_next = _b; \
129 else \
130 *_head = _b; \
131 _b->name##_next = _a; \
132 _a->name##_prev = _b; \
133 } \
134 } while (false)
135
136 #define LIST_JUST_US(name,item) \
137 (!(item)->name##_prev && !(item)->name##_next) \
138
139 #define LIST_FOREACH(name,i,head) \
140 for ((i) = (head); (i); (i) = (i)->name##_next)
141
142 #define LIST_FOREACH_SAFE(name,i,n,head) \
143 for ((i) = (head); (i) && (((n) = (i)->name##_next), 1); (i) = (n))
144
145 #define LIST_FOREACH_BEFORE(name,i,p) \
146 for ((i) = (p)->name##_prev; (i); (i) = (i)->name##_prev)
147
148 #define LIST_FOREACH_AFTER(name,i,p) \
149 for ((i) = (p)->name##_next; (i); (i) = (i)->name##_next)
150
151 /* Iterate through all the members of the list p is included in, but skip over p */
152 #define LIST_FOREACH_OTHERS(name,i,p) \
153 for (({ \
154 (i) = (p); \
155 while ((i) && (i)->name##_prev) \
156 (i) = (i)->name##_prev; \
157 if ((i) == (p)) \
158 (i) = (p)->name##_next; \
159 }); \
160 (i); \
161 (i) = (i)->name##_next == (p) ? (p)->name##_next : (i)->name##_next)
162
163 /* Loop starting from p->next until p->prev.
164 p can be adjusted meanwhile. */
165 #define LIST_LOOP_BUT_ONE(name,i,head,p) \
166 for ((i) = (p)->name##_next ? (p)->name##_next : (head); \
167 (i) != (p); \
168 (i) = (i)->name##_next ? (i)->name##_next : (head))
169
170 #define LIST_IS_EMPTY(head) \
171 (!(head))