]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/list.h
1 /* SPDX-License-Identifier: LGPL-2.1+ */
5 This file is part of systemd.
7 Copyright 2010 Lennart Poettering
9 systemd is free software; you can redistribute it and/or modify it
10 under the terms of the GNU Lesser General Public License as published by
11 the Free Software Foundation; either version 2.1 of the License, or
12 (at your option) any later version.
14 systemd is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Lesser General Public License for more details.
19 You should have received a copy of the GNU Lesser General Public License
20 along with systemd; If not, see <http://www.gnu.org/licenses/>.
23 /* The head of the linked list. Use this in the structure that shall
24 * contain the head of the linked list */
25 #define LIST_HEAD(t,name) \
28 /* The pointers in the linked list's items. Use this in the item structure */
29 #define LIST_FIELDS(t,name) \
30 t *name##_next, *name##_prev
32 /* Initialize the list's head */
33 #define LIST_HEAD_INIT(head) \
38 /* Initialize a list item */
39 #define LIST_INIT(name,item) \
41 typeof(*(item)) *_item = (item); \
43 _item->name##_prev = _item->name##_next = NULL; \
46 /* Prepend an item to the list */
47 #define LIST_PREPEND(name,head,item) \
49 typeof(*(head)) **_head = &(head), *_item = (item); \
51 if ((_item->name##_next = *_head)) \
52 _item->name##_next->name##_prev = _item; \
53 _item->name##_prev = NULL; \
57 /* Append an item to the list */
58 #define LIST_APPEND(name,head,item) \
60 typeof(*(head)) *_tail; \
61 LIST_FIND_TAIL(name,head,_tail); \
62 LIST_INSERT_AFTER(name,head,_tail,item); \
65 /* Remove an item from the list */
66 #define LIST_REMOVE(name,head,item) \
68 typeof(*(head)) **_head = &(head), *_item = (item); \
70 if (_item->name##_next) \
71 _item->name##_next->name##_prev = _item->name##_prev; \
72 if (_item->name##_prev) \
73 _item->name##_prev->name##_next = _item->name##_next; \
75 assert(*_head == _item); \
76 *_head = _item->name##_next; \
78 _item->name##_next = _item->name##_prev = NULL; \
81 /* Find the head of the list */
82 #define LIST_FIND_HEAD(name,item,head) \
84 typeof(*(item)) *_item = (item); \
88 while (_item->name##_prev) \
89 _item = _item->name##_prev; \
94 /* Find the tail of the list */
95 #define LIST_FIND_TAIL(name,item,tail) \
97 typeof(*(item)) *_item = (item); \
101 while (_item->name##_next) \
102 _item = _item->name##_next; \
107 /* Insert an item after another one (a = where, b = what) */
108 #define LIST_INSERT_AFTER(name,head,a,b) \
110 typeof(*(head)) **_head = &(head), *_a = (a), *_b = (b); \
113 if ((_b->name##_next = *_head)) \
114 _b->name##_next->name##_prev = _b; \
115 _b->name##_prev = NULL; \
118 if ((_b->name##_next = _a->name##_next)) \
119 _b->name##_next->name##_prev = _b; \
120 _b->name##_prev = _a; \
121 _a->name##_next = _b; \
125 /* Insert an item before another one (a = where, b = what) */
126 #define LIST_INSERT_BEFORE(name,head,a,b) \
128 typeof(*(head)) **_head = &(head), *_a = (a), *_b = (b); \
132 _b->name##_next = NULL; \
133 _b->name##_prev = NULL; \
136 typeof(*(head)) *_tail = (head); \
137 while (_tail->name##_next) \
138 _tail = _tail->name##_next; \
139 _b->name##_next = NULL; \
140 _b->name##_prev = _tail; \
141 _tail->name##_next = _b; \
144 if ((_b->name##_prev = _a->name##_prev)) \
145 _b->name##_prev->name##_next = _b; \
148 _b->name##_next = _a; \
149 _a->name##_prev = _b; \
153 #define LIST_JUST_US(name,item) \
154 (!(item)->name##_prev && !(item)->name##_next) \
156 #define LIST_FOREACH(name,i,head) \
157 for ((i) = (head); (i); (i) = (i)->name##_next)
159 #define LIST_FOREACH_SAFE(name,i,n,head) \
160 for ((i) = (head); (i) && (((n) = (i)->name##_next), 1); (i) = (n))
162 #define LIST_FOREACH_BEFORE(name,i,p) \
163 for ((i) = (p)->name##_prev; (i); (i) = (i)->name##_prev)
165 #define LIST_FOREACH_AFTER(name,i,p) \
166 for ((i) = (p)->name##_next; (i); (i) = (i)->name##_next)
168 /* Iterate through all the members of the list p is included in, but skip over p */
169 #define LIST_FOREACH_OTHERS(name,i,p) \
172 while ((i) && (i)->name##_prev) \
173 (i) = (i)->name##_prev; \
175 (i) = (p)->name##_next; \
178 (i) = (i)->name##_next == (p) ? (p)->name##_next : (i)->name##_next)
180 /* Loop starting from p->next until p->prev.
181 p can be adjusted meanwhile. */
182 #define LIST_LOOP_BUT_ONE(name,i,head,p) \
183 for ((i) = (p)->name##_next ? (p)->name##_next : (head); \
185 (i) = (i)->name##_next ? (i)->name##_next : (head))