chiark / gitweb /
3ec7e73fc58b7f43424f613a421d74513eb05413
[elogind.git] / src / basic / list.h
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 #pragma once
3
4 /***
5   This file is part of systemd.
6
7   Copyright 2010 Lennart Poettering
8 ***/
9
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)                                               \
13         t *name
14
15 /* The pointers in the linked list's items. Use this in the item structure */
16 #define LIST_FIELDS(t,name)                                             \
17         t *name##_next, *name##_prev
18
19 /* Initialize the list's head */
20 #define LIST_HEAD_INIT(head)                                            \
21         do {                                                            \
22                 (head) = NULL; }                                        \
23         while (false)
24
25 /* Initialize a list item */
26 #define LIST_INIT(name,item)                                            \
27         do {                                                            \
28                 typeof(*(item)) *_item = (item);                        \
29                 assert(_item);                                          \
30                 _item->name##_prev = _item->name##_next = NULL;         \
31         } while (false)
32
33 /* Prepend an item to the list */
34 #define LIST_PREPEND(name,head,item)                                    \
35         do {                                                            \
36                 typeof(*(head)) **_head = &(head), *_item = (item);     \
37                 assert(_item);                                          \
38                 if ((_item->name##_next = *_head))                      \
39                         _item->name##_next->name##_prev = _item;        \
40                 _item->name##_prev = NULL;                              \
41                 *_head = _item;                                         \
42         } while (false)
43
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);                \
50         } while (false)
51
52 /* Remove an item from the list */
53 #define LIST_REMOVE(name,head,item)                                     \
54         do {                                                            \
55                 typeof(*(head)) **_head = &(head), *_item = (item);     \
56                 assert(_item);                                          \
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; \
61                 else {                                                  \
62                         assert(*_head == _item);                        \
63                         *_head = _item->name##_next;                    \
64                 }                                                       \
65                 _item->name##_next = _item->name##_prev = NULL;         \
66         } while (false)
67
68 /* Find the head of the list */
69 #define LIST_FIND_HEAD(name,item,head)                                  \
70         do {                                                            \
71                 typeof(*(item)) *_item = (item);                        \
72                 if (!_item)                                             \
73                         (head) = NULL;                                  \
74                 else {                                                  \
75                         while (_item->name##_prev)                      \
76                                 _item = _item->name##_prev;             \
77                         (head) = _item;                                 \
78                 }                                                       \
79         } while (false)
80
81 /* Find the tail of the list */
82 #define LIST_FIND_TAIL(name,item,tail)                                  \
83         do {                                                            \
84                 typeof(*(item)) *_item = (item);                        \
85                 if (!_item)                                             \
86                         (tail) = NULL;                                  \
87                 else {                                                  \
88                         while (_item->name##_next)                      \
89                                 _item = _item->name##_next;             \
90                         (tail) = _item;                                 \
91                 }                                                       \
92         } while (false)
93
94 /* Insert an item after another one (a = where, b = what) */
95 #define LIST_INSERT_AFTER(name,head,a,b)                                \
96         do {                                                            \
97                 typeof(*(head)) **_head = &(head), *_a = (a), *_b = (b); \
98                 assert(_b);                                             \
99                 if (!_a) {                                              \
100                         if ((_b->name##_next = *_head))                 \
101                                 _b->name##_next->name##_prev = _b;      \
102                         _b->name##_prev = NULL;                         \
103                         *_head = _b;                                    \
104                 } else {                                                \
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;                           \
109                 }                                                       \
110         } while (false)
111
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;      \
133                         else                                            \
134                                 *_head = _b;                            \
135                         _b->name##_next = _a;                           \
136                         _a->name##_prev = _b;                           \
137                 }                                                       \
138         } while (false)
139
140 #define LIST_JUST_US(name,item)                                         \
141         (!(item)->name##_prev && !(item)->name##_next)                  \
142
143 #define LIST_FOREACH(name,i,head)                                       \
144         for ((i) = (head); (i); (i) = (i)->name##_next)
145
146 #define LIST_FOREACH_SAFE(name,i,n,head)                                \
147         for ((i) = (head); (i) && (((n) = (i)->name##_next), 1); (i) = (n))
148
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)
154
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;                         \
161                 if ((i) == (p))                                         \
162                         (i) = (p)->name##_next;                         \
163              });                                                        \
164              (i);                                                       \
165              (i) = (i)->name##_next == (p) ? (p)->name##_next : (i)->name##_next)
166
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))
173
174 #define LIST_IS_EMPTY(head)                                             \
175         (!(head))