chiark / gitweb /
tree-wide: drop 'This file is part of systemd' blurb
[elogind.git] / src / basic / list.h
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 #pragma once
3
4 /***
5   Copyright 2010 Lennart Poettering
6 ***/
7
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)                                               \
11         t *name
12
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
16
17 /* Initialize the list's head */
18 #define LIST_HEAD_INIT(head)                                            \
19         do {                                                            \
20                 (head) = NULL; }                                        \
21         while (false)
22
23 /* Initialize a list item */
24 #define LIST_INIT(name,item)                                            \
25         do {                                                            \
26                 typeof(*(item)) *_item = (item);                        \
27                 assert(_item);                                          \
28                 _item->name##_prev = _item->name##_next = NULL;         \
29         } while (false)
30
31 /* Prepend an item to the list */
32 #define LIST_PREPEND(name,head,item)                                    \
33         do {                                                            \
34                 typeof(*(head)) **_head = &(head), *_item = (item);     \
35                 assert(_item);                                          \
36                 if ((_item->name##_next = *_head))                      \
37                         _item->name##_next->name##_prev = _item;        \
38                 _item->name##_prev = NULL;                              \
39                 *_head = _item;                                         \
40         } while (false)
41
42 /* Append an item to the list */
43 #define LIST_APPEND(name,head,item)                                     \
44         do {                                                            \
45                 typeof(*(head)) *_tail;                                 \
46                 LIST_FIND_TAIL(name,head,_tail);                        \
47                 LIST_INSERT_AFTER(name,head,_tail,item);                \
48         } while (false)
49
50 /* Remove an item from the list */
51 #define LIST_REMOVE(name,head,item)                                     \
52         do {                                                            \
53                 typeof(*(head)) **_head = &(head), *_item = (item);     \
54                 assert(_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; \
59                 else {                                                  \
60                         assert(*_head == _item);                        \
61                         *_head = _item->name##_next;                    \
62                 }                                                       \
63                 _item->name##_next = _item->name##_prev = NULL;         \
64         } while (false)
65
66 /* Find the head of the list */
67 #define LIST_FIND_HEAD(name,item,head)                                  \
68         do {                                                            \
69                 typeof(*(item)) *_item = (item);                        \
70                 if (!_item)                                             \
71                         (head) = NULL;                                  \
72                 else {                                                  \
73                         while (_item->name##_prev)                      \
74                                 _item = _item->name##_prev;             \
75                         (head) = _item;                                 \
76                 }                                                       \
77         } while (false)
78
79 /* Find the tail of the list */
80 #define LIST_FIND_TAIL(name,item,tail)                                  \
81         do {                                                            \
82                 typeof(*(item)) *_item = (item);                        \
83                 if (!_item)                                             \
84                         (tail) = NULL;                                  \
85                 else {                                                  \
86                         while (_item->name##_next)                      \
87                                 _item = _item->name##_next;             \
88                         (tail) = _item;                                 \
89                 }                                                       \
90         } while (false)
91
92 /* Insert an item after another one (a = where, b = what) */
93 #define LIST_INSERT_AFTER(name,head,a,b)                                \
94         do {                                                            \
95                 typeof(*(head)) **_head = &(head), *_a = (a), *_b = (b); \
96                 assert(_b);                                             \
97                 if (!_a) {                                              \
98                         if ((_b->name##_next = *_head))                 \
99                                 _b->name##_next->name##_prev = _b;      \
100                         _b->name##_prev = NULL;                         \
101                         *_head = _b;                                    \
102                 } else {                                                \
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;                           \
107                 }                                                       \
108         } while (false)
109
110 /* Insert an item before another one (a = where, b = what) */
111 #define LIST_INSERT_BEFORE(name,head,a,b)                               \
112         do {                                                            \
113                 typeof(*(head)) **_head = &(head), *_a = (a), *_b = (b); \
114                 assert(_b);                                             \
115                 if (!_a) {                                              \
116                         if (!*_head) {                                  \
117                                 _b->name##_next = NULL;                 \
118                                 _b->name##_prev = NULL;                 \
119                                 *_head = _b;                            \
120                         } else {                                        \
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;                \
127                         }                                               \
128                 } else {                                                \
129                         if ((_b->name##_prev = _a->name##_prev))        \
130                                 _b->name##_prev->name##_next = _b;      \
131                         else                                            \
132                                 *_head = _b;                            \
133                         _b->name##_next = _a;                           \
134                         _a->name##_prev = _b;                           \
135                 }                                                       \
136         } while (false)
137
138 #define LIST_JUST_US(name,item)                                         \
139         (!(item)->name##_prev && !(item)->name##_next)                  \
140
141 #define LIST_FOREACH(name,i,head)                                       \
142         for ((i) = (head); (i); (i) = (i)->name##_next)
143
144 #define LIST_FOREACH_SAFE(name,i,n,head)                                \
145         for ((i) = (head); (i) && (((n) = (i)->name##_next), 1); (i) = (n))
146
147 #define LIST_FOREACH_BEFORE(name,i,p)                                   \
148         for ((i) = (p)->name##_prev; (i); (i) = (i)->name##_prev)
149
150 #define LIST_FOREACH_AFTER(name,i,p)                                    \
151         for ((i) = (p)->name##_next; (i); (i) = (i)->name##_next)
152
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)                                   \
155         for (({                                                         \
156                 (i) = (p);                                              \
157                 while ((i) && (i)->name##_prev)                         \
158                         (i) = (i)->name##_prev;                         \
159                 if ((i) == (p))                                         \
160                         (i) = (p)->name##_next;                         \
161              });                                                        \
162              (i);                                                       \
163              (i) = (i)->name##_next == (p) ? (p)->name##_next : (i)->name##_next)
164
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);        \
169              (i) != (p);                                                \
170              (i) = (i)->name##_next ? (i)->name##_next : (head))
171
172 #define LIST_IS_EMPTY(head)                                             \
173         (!(head))