1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
6 This file is part of systemd.
8 Copyright 2010 Lennart Poettering
10 systemd is free software; you can redistribute it and/or modify it
11 under the terms of the GNU Lesser General Public License as published by
12 the Free Software Foundation; either version 2.1 of the License, or
13 (at your option) any later version.
15 systemd is distributed in the hope that it will be useful, but
16 WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 Lesser General Public License for more details.
20 You should have received a copy of the GNU Lesser General Public License
21 along with systemd; If not, see <http://www.gnu.org/licenses/>.
24 /* The head of the linked list. Use this in the structure that shall
25 * contain the head of the linked list */
26 #define LIST_HEAD(t,name) \
29 /* The pointers in the linked list's items. Use this in the item structure */
30 #define LIST_FIELDS(t,name) \
31 t *name##_next, *name##_prev
33 /* Initialize the list's head */
34 #define LIST_HEAD_INIT(head) \
39 /* Initialize a list item */
40 #define LIST_INIT(name,item) \
42 typeof(*(item)) *_item = (item); \
44 _item->name##_prev = _item->name##_next = NULL; \
47 /* Prepend an item to the list */
48 #define LIST_PREPEND(name,head,item) \
50 typeof(*(head)) **_head = &(head), *_item = (item); \
52 if ((_item->name##_next = *_head)) \
53 _item->name##_next->name##_prev = _item; \
54 _item->name##_prev = NULL; \
58 /* Append an item to the list */
59 #define LIST_APPEND(name,head,item) \
61 typeof(*(head)) *_tail; \
62 LIST_FIND_TAIL(name,head,_tail); \
63 LIST_INSERT_AFTER(name,head,_tail,item); \
66 /* Remove an item from the list */
67 #define LIST_REMOVE(name,head,item) \
69 typeof(*(head)) **_head = &(head), *_item = (item); \
71 if (_item->name##_next) \
72 _item->name##_next->name##_prev = _item->name##_prev; \
73 if (_item->name##_prev) \
74 _item->name##_prev->name##_next = _item->name##_next; \
76 assert(*_head == _item); \
77 *_head = _item->name##_next; \
79 _item->name##_next = _item->name##_prev = NULL; \
82 /* Find the head of the list */
83 #define LIST_FIND_HEAD(name,item,head) \
85 typeof(*(item)) *_item = (item); \
89 while (_item->name##_prev) \
90 _item = _item->name##_prev; \
95 /* Find the tail of the list */
96 #define LIST_FIND_TAIL(name,item,tail) \
98 typeof(*(item)) *_item = (item); \
102 while (_item->name##_next) \
103 _item = _item->name##_next; \
108 /* Insert an item after another one (a = where, b = what) */
109 #define LIST_INSERT_AFTER(name,head,a,b) \
111 typeof(*(head)) **_head = &(head), *_a = (a), *_b = (b); \
114 if ((_b->name##_next = *_head)) \
115 _b->name##_next->name##_prev = _b; \
116 _b->name##_prev = NULL; \
119 if ((_b->name##_next = _a->name##_next)) \
120 _b->name##_next->name##_prev = _b; \
121 _b->name##_prev = _a; \
122 _a->name##_next = _b; \
126 /* Insert an item before another one (a = where, b = what) */
127 #define LIST_INSERT_BEFORE(name,head,a,b) \
129 typeof(*(head)) **_head = &(head), *_a = (a), *_b = (b); \
133 _b->name##_next = NULL; \
134 _b->name##_prev = NULL; \
137 typeof(*(head)) *_tail = (head); \
138 while (_tail->name##_next) \
139 _tail = _tail->name##_next; \
140 _b->name##_next = NULL; \
141 _b->name##_prev = _tail; \
142 _tail->name##_next = _b; \
145 if ((_b->name##_prev = _a->name##_prev)) \
146 _b->name##_prev->name##_next = _b; \
147 _b->name##_next = _a; \
148 _a->name##_prev = _b; \
152 #define LIST_JUST_US(name,item) \
153 (!(item)->name##_prev && !(item)->name##_next) \
155 #define LIST_FOREACH(name,i,head) \
156 for ((i) = (head); (i); (i) = (i)->name##_next)
158 #define LIST_FOREACH_SAFE(name,i,n,head) \
159 for ((i) = (head); (i) && (((n) = (i)->name##_next), 1); (i) = (n))
161 #define LIST_FOREACH_BEFORE(name,i,p) \
162 for ((i) = (p)->name##_prev; (i); (i) = (i)->name##_prev)
164 #define LIST_FOREACH_AFTER(name,i,p) \
165 for ((i) = (p)->name##_next; (i); (i) = (i)->name##_next)
167 /* Iterate through all the members of the list p is included in, but skip over p */
168 #define LIST_FOREACH_OTHERS(name,i,p) \
171 while ((i) && (i)->name##_prev) \
172 (i) = (i)->name##_prev; \
174 (i) = (p)->name##_next; \
177 (i) = (i)->name##_next == (p) ? (p)->name##_next : (i)->name##_next)
179 /* Loop starting from p->next until p->prev.
180 p can be adjusted meanwhile. */
181 #define LIST_LOOP_BUT_ONE(name,i,head,p) \
182 for ((i) = (p)->name##_next ? (p)->name##_next : (head); \
184 (i) = (i)->name##_next ? (i)->name##_next : (head))