]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/basic/list.h
tree-wide: drop license boilerplate
[thirdparty/systemd.git] / src / basic / list.h
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
c2f1db8f 2#pragma once
60918275 3
a7334b09
LP
4/***
5 This file is part of systemd.
6
7 Copyright 2010 Lennart Poettering
a7334b09
LP
8***/
9
60918275
LP
10/* The head of the linked list. Use this in the structure that shall
11 * contain the head of the linked list */
12#define LIST_HEAD(t,name) \
034c6ed7 13 t *name
60918275
LP
14
15/* The pointers in the linked list's items. Use this in the item structure */
034c6ed7
LP
16#define LIST_FIELDS(t,name) \
17 t *name##_next, *name##_prev
60918275
LP
18
19/* Initialize the list's head */
71fda00f 20#define LIST_HEAD_INIT(head) \
60918275 21 do { \
034c6ed7 22 (head) = NULL; } \
9ed794a3 23 while (false)
60918275
LP
24
25/* Initialize a list item */
71fda00f 26#define LIST_INIT(name,item) \
60918275 27 do { \
71fda00f 28 typeof(*(item)) *_item = (item); \
60918275 29 assert(_item); \
034c6ed7 30 _item->name##_prev = _item->name##_next = NULL; \
9ed794a3 31 } while (false)
60918275
LP
32
33/* Prepend an item to the list */
71fda00f 34#define LIST_PREPEND(name,head,item) \
60918275 35 do { \
71fda00f 36 typeof(*(head)) **_head = &(head), *_item = (item); \
60918275 37 assert(_item); \
034c6ed7
LP
38 if ((_item->name##_next = *_head)) \
39 _item->name##_next->name##_prev = _item; \
40 _item->name##_prev = NULL; \
60918275 41 *_head = _item; \
9ed794a3 42 } while (false)
60918275 43
502f1733
ZJS
44/* Append an item to the list */
45#define LIST_APPEND(name,head,item) \
46 do { \
47 typeof(*(head)) *_tail; \
48 LIST_FIND_TAIL(name,head,_tail); \
49 LIST_INSERT_AFTER(name,head,_tail,item); \
9ed794a3 50 } while (false)
502f1733 51
60918275 52/* Remove an item from the list */
71fda00f 53#define LIST_REMOVE(name,head,item) \
60918275 54 do { \
71fda00f 55 typeof(*(head)) **_head = &(head), *_item = (item); \
60918275 56 assert(_item); \
034c6ed7
LP
57 if (_item->name##_next) \
58 _item->name##_next->name##_prev = _item->name##_prev; \
59 if (_item->name##_prev) \
60 _item->name##_prev->name##_next = _item->name##_next; \
60918275
LP
61 else { \
62 assert(*_head == _item); \
034c6ed7 63 *_head = _item->name##_next; \
60918275 64 } \
034c6ed7 65 _item->name##_next = _item->name##_prev = NULL; \
9ed794a3 66 } while (false)
60918275
LP
67
68/* Find the head of the list */
71fda00f 69#define LIST_FIND_HEAD(name,item,head) \
60918275 70 do { \
71fda00f 71 typeof(*(item)) *_item = (item); \
376cd3b8
LP
72 if (!_item) \
73 (head) = NULL; \
74 else { \
75 while (_item->name##_prev) \
76 _item = _item->name##_prev; \
77 (head) = _item; \
78 } \
034c6ed7
LP
79 } while (false)
80
3b18ae68 81/* Find the tail of the list */
71fda00f 82#define LIST_FIND_TAIL(name,item,tail) \
034c6ed7 83 do { \
71fda00f 84 typeof(*(item)) *_item = (item); \
376cd3b8
LP
85 if (!_item) \
86 (tail) = NULL; \
87 else { \
88 while (_item->name##_next) \
89 _item = _item->name##_next; \
90 (tail) = _item; \
91 } \
60918275
LP
92 } while (false)
93
94/* Insert an item after another one (a = where, b = what) */
71fda00f 95#define LIST_INSERT_AFTER(name,head,a,b) \
60918275 96 do { \
71fda00f 97 typeof(*(head)) **_head = &(head), *_a = (a), *_b = (b); \
60918275
LP
98 assert(_b); \
99 if (!_a) { \
034c6ed7
LP
100 if ((_b->name##_next = *_head)) \
101 _b->name##_next->name##_prev = _b; \
102 _b->name##_prev = NULL; \
60918275
LP
103 *_head = _b; \
104 } else { \
034c6ed7
LP
105 if ((_b->name##_next = _a->name##_next)) \
106 _b->name##_next->name##_prev = _b; \
107 _b->name##_prev = _a; \
108 _a->name##_next = _b; \
60918275 109 } \
9ed794a3 110 } while (false)
60918275 111
dbe465c9
AC
112/* Insert an item before another one (a = where, b = what) */
113#define LIST_INSERT_BEFORE(name,head,a,b) \
114 do { \
115 typeof(*(head)) **_head = &(head), *_a = (a), *_b = (b); \
116 assert(_b); \
117 if (!_a) { \
118 if (!*_head) { \
119 _b->name##_next = NULL; \
120 _b->name##_prev = NULL; \
121 *_head = _b; \
122 } else { \
123 typeof(*(head)) *_tail = (head); \
124 while (_tail->name##_next) \
125 _tail = _tail->name##_next; \
126 _b->name##_next = NULL; \
127 _b->name##_prev = _tail; \
128 _tail->name##_next = _b; \
129 } \
130 } else { \
131 if ((_b->name##_prev = _a->name##_prev)) \
132 _b->name##_prev->name##_next = _b; \
5076f421
MO
133 else \
134 *_head = _b; \
dbe465c9
AC
135 _b->name##_next = _a; \
136 _a->name##_prev = _b; \
137 } \
9ed794a3 138 } while (false)
dbe465c9 139
6210e7fc
LP
140#define LIST_JUST_US(name,item) \
141 (!(item)->name##_prev && !(item)->name##_next) \
142
034c6ed7
LP
143#define LIST_FOREACH(name,i,head) \
144 for ((i) = (head); (i); (i) = (i)->name##_next)
60918275 145
034c6ed7
LP
146#define LIST_FOREACH_SAFE(name,i,n,head) \
147 for ((i) = (head); (i) && (((n) = (i)->name##_next), 1); (i) = (n))
60918275 148
e04aad61
LP
149#define LIST_FOREACH_BEFORE(name,i,p) \
150 for ((i) = (p)->name##_prev; (i); (i) = (i)->name##_prev)
151
152#define LIST_FOREACH_AFTER(name,i,p) \
153 for ((i) = (p)->name##_next; (i); (i) = (i)->name##_next)
2bc8ca0c 154
7663df37
LP
155/* Iterate through all the members of the list p is included in, but skip over p */
156#define LIST_FOREACH_OTHERS(name,i,p) \
157 for (({ \
158 (i) = (p); \
159 while ((i) && (i)->name##_prev) \
160 (i) = (i)->name##_prev; \
cdda4aa8
LP
161 if ((i) == (p)) \
162 (i) = (p)->name##_next; \
7663df37
LP
163 }); \
164 (i); \
165 (i) = (i)->name##_next == (p) ? (p)->name##_next : (i)->name##_next)
166
2bc8ca0c
ZJS
167/* Loop starting from p->next until p->prev.
168 p can be adjusted meanwhile. */
169#define LIST_LOOP_BUT_ONE(name,i,head,p) \
170 for ((i) = (p)->name##_next ? (p)->name##_next : (head); \
171 (i) != (p); \
172 (i) = (i)->name##_next ? (i)->name##_next : (head))
40a57716
SP
173
174#define LIST_IS_EMPTY(head) \
175 (!(head))