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