]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
c2f1db8f | 2 | #pragma once |
60918275 | 3 | |
a7334b09 | 4 | /*** |
a7334b09 | 5 | Copyright 2010 Lennart Poettering |
a7334b09 LP |
6 | ***/ |
7 | ||
60918275 LP |
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) \ | |
034c6ed7 | 11 | t *name |
60918275 LP |
12 | |
13 | /* The pointers in the linked list's items. Use this in the item structure */ | |
034c6ed7 LP |
14 | #define LIST_FIELDS(t,name) \ |
15 | t *name##_next, *name##_prev | |
60918275 LP |
16 | |
17 | /* Initialize the list's head */ | |
71fda00f | 18 | #define LIST_HEAD_INIT(head) \ |
60918275 | 19 | do { \ |
034c6ed7 | 20 | (head) = NULL; } \ |
9ed794a3 | 21 | while (false) |
60918275 LP |
22 | |
23 | /* Initialize a list item */ | |
71fda00f | 24 | #define LIST_INIT(name,item) \ |
60918275 | 25 | do { \ |
71fda00f | 26 | typeof(*(item)) *_item = (item); \ |
60918275 | 27 | assert(_item); \ |
034c6ed7 | 28 | _item->name##_prev = _item->name##_next = NULL; \ |
9ed794a3 | 29 | } while (false) |
60918275 LP |
30 | |
31 | /* Prepend an item to the list */ | |
71fda00f | 32 | #define LIST_PREPEND(name,head,item) \ |
60918275 | 33 | do { \ |
71fda00f | 34 | typeof(*(head)) **_head = &(head), *_item = (item); \ |
60918275 | 35 | assert(_item); \ |
034c6ed7 LP |
36 | if ((_item->name##_next = *_head)) \ |
37 | _item->name##_next->name##_prev = _item; \ | |
38 | _item->name##_prev = NULL; \ | |
60918275 | 39 | *_head = _item; \ |
9ed794a3 | 40 | } while (false) |
60918275 | 41 | |
502f1733 ZJS |
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); \ | |
9ed794a3 | 48 | } while (false) |
502f1733 | 49 | |
60918275 | 50 | /* Remove an item from the list */ |
71fda00f | 51 | #define LIST_REMOVE(name,head,item) \ |
60918275 | 52 | do { \ |
71fda00f | 53 | typeof(*(head)) **_head = &(head), *_item = (item); \ |
60918275 | 54 | assert(_item); \ |
034c6ed7 LP |
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; \ | |
60918275 LP |
59 | else { \ |
60 | assert(*_head == _item); \ | |
034c6ed7 | 61 | *_head = _item->name##_next; \ |
60918275 | 62 | } \ |
034c6ed7 | 63 | _item->name##_next = _item->name##_prev = NULL; \ |
9ed794a3 | 64 | } while (false) |
60918275 LP |
65 | |
66 | /* Find the head of the list */ | |
71fda00f | 67 | #define LIST_FIND_HEAD(name,item,head) \ |
60918275 | 68 | do { \ |
71fda00f | 69 | typeof(*(item)) *_item = (item); \ |
376cd3b8 LP |
70 | if (!_item) \ |
71 | (head) = NULL; \ | |
72 | else { \ | |
73 | while (_item->name##_prev) \ | |
74 | _item = _item->name##_prev; \ | |
75 | (head) = _item; \ | |
76 | } \ | |
034c6ed7 LP |
77 | } while (false) |
78 | ||
3b18ae68 | 79 | /* Find the tail of the list */ |
71fda00f | 80 | #define LIST_FIND_TAIL(name,item,tail) \ |
034c6ed7 | 81 | do { \ |
71fda00f | 82 | typeof(*(item)) *_item = (item); \ |
376cd3b8 LP |
83 | if (!_item) \ |
84 | (tail) = NULL; \ | |
85 | else { \ | |
86 | while (_item->name##_next) \ | |
87 | _item = _item->name##_next; \ | |
88 | (tail) = _item; \ | |
89 | } \ | |
60918275 LP |
90 | } while (false) |
91 | ||
92 | /* Insert an item after another one (a = where, b = what) */ | |
71fda00f | 93 | #define LIST_INSERT_AFTER(name,head,a,b) \ |
60918275 | 94 | do { \ |
71fda00f | 95 | typeof(*(head)) **_head = &(head), *_a = (a), *_b = (b); \ |
60918275 LP |
96 | assert(_b); \ |
97 | if (!_a) { \ | |
034c6ed7 LP |
98 | if ((_b->name##_next = *_head)) \ |
99 | _b->name##_next->name##_prev = _b; \ | |
100 | _b->name##_prev = NULL; \ | |
60918275 LP |
101 | *_head = _b; \ |
102 | } else { \ | |
034c6ed7 LP |
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; \ | |
60918275 | 107 | } \ |
9ed794a3 | 108 | } while (false) |
60918275 | 109 | |
dbe465c9 AC |
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; \ | |
5076f421 MO |
131 | else \ |
132 | *_head = _b; \ | |
dbe465c9 AC |
133 | _b->name##_next = _a; \ |
134 | _a->name##_prev = _b; \ | |
135 | } \ | |
9ed794a3 | 136 | } while (false) |
dbe465c9 | 137 | |
6210e7fc LP |
138 | #define LIST_JUST_US(name,item) \ |
139 | (!(item)->name##_prev && !(item)->name##_next) \ | |
140 | ||
034c6ed7 LP |
141 | #define LIST_FOREACH(name,i,head) \ |
142 | for ((i) = (head); (i); (i) = (i)->name##_next) | |
60918275 | 143 | |
034c6ed7 LP |
144 | #define LIST_FOREACH_SAFE(name,i,n,head) \ |
145 | for ((i) = (head); (i) && (((n) = (i)->name##_next), 1); (i) = (n)) | |
60918275 | 146 | |
e04aad61 LP |
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) | |
2bc8ca0c | 152 | |
7663df37 LP |
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; \ | |
cdda4aa8 LP |
159 | if ((i) == (p)) \ |
160 | (i) = (p)->name##_next; \ | |
7663df37 LP |
161 | }); \ |
162 | (i); \ | |
163 | (i) = (i)->name##_next == (p) ? (p)->name##_next : (i)->name##_next) | |
164 | ||
2bc8ca0c ZJS |
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)) | |
40a57716 SP |
171 | |
172 | #define LIST_IS_EMPTY(head) \ | |
173 | (!(head)) |