]>
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
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) \
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
19 /* Initialize the list's head */
20 #define LIST_HEAD_INIT(head) \
25 /* Initialize a list item */
26 #define LIST_INIT(name,item) \
28 typeof(*(item)) *_item = (item); \
30 _item->name##_prev = _item->name##_next = NULL; \
33 /* Prepend an item to the list */
34 #define LIST_PREPEND(name,head,item) \
36 typeof(*(head)) **_head = &(head), *_item = (item); \
38 if ((_item->name##_next = *_head)) \
39 _item->name##_next->name##_prev = _item; \
40 _item->name##_prev = NULL; \
44 /* Append an item to the list */
45 #define LIST_APPEND(name,head,item) \
47 typeof(*(head)) *_tail; \
48 LIST_FIND_TAIL(name,head,_tail); \
49 LIST_INSERT_AFTER(name,head,_tail,item); \
52 /* Remove an item from the list */
53 #define LIST_REMOVE(name,head,item) \
55 typeof(*(head)) **_head = &(head), *_item = (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; \
62 assert(*_head == _item); \
63 *_head = _item->name##_next; \
65 _item->name##_next = _item->name##_prev = NULL; \
68 /* Find the head of the list */
69 #define LIST_FIND_HEAD(name,item,head) \
71 typeof(*(item)) *_item = (item); \
75 while (_item->name##_prev) \
76 _item = _item->name##_prev; \
81 /* Find the tail of the list */
82 #define LIST_FIND_TAIL(name,item,tail) \
84 typeof(*(item)) *_item = (item); \
88 while (_item->name##_next) \
89 _item = _item->name##_next; \
94 /* Insert an item after another one (a = where, b = what) */
95 #define LIST_INSERT_AFTER(name,head,a,b) \
97 typeof(*(head)) **_head = &(head), *_a = (a), *_b = (b); \
100 if ((_b->name##_next = *_head)) \
101 _b->name##_next->name##_prev = _b; \
102 _b->name##_prev = NULL; \
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; \
112 /* Insert an item before another one (a = where, b = what) */
113 #define LIST_INSERT_BEFORE(name,head,a,b) \
115 typeof(*(head)) **_head = &(head), *_a = (a), *_b = (b); \
119 _b->name##_next = NULL; \
120 _b->name##_prev = NULL; \
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; \
131 if ((_b->name##_prev = _a->name##_prev)) \
132 _b->name##_prev->name##_next = _b; \
135 _b->name##_next = _a; \
136 _a->name##_prev = _b; \
140 #define LIST_JUST_US(name,item) \
141 (!(item)->name##_prev && !(item)->name##_next) \
143 #define LIST_FOREACH(name,i,head) \
144 for ((i) = (head); (i); (i) = (i)->name##_next)
146 #define LIST_FOREACH_SAFE(name,i,n,head) \
147 for ((i) = (head); (i) && (((n) = (i)->name##_next), 1); (i) = (n))
149 #define LIST_FOREACH_BEFORE(name,i,p) \
150 for ((i) = (p)->name##_prev; (i); (i) = (i)->name##_prev)
152 #define LIST_FOREACH_AFTER(name,i,p) \
153 for ((i) = (p)->name##_next; (i); (i) = (i)->name##_next)
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) \
159 while ((i) && (i)->name##_prev) \
160 (i) = (i)->name##_prev; \
162 (i) = (p)->name##_next; \
165 (i) = (i)->name##_next == (p) ? (p)->name##_next : (i)->name##_next)
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); \
172 (i) = (i)->name##_next ? (i)->name##_next : (head))
174 #define LIST_IS_EMPTY(head) \