1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
10 This file is part of systemd.
12 Copyright 2010 Lennart Poettering
14 systemd is free software; you can redistribute it and/or modify it
15 under the terms of the GNU Lesser General Public License as published by
16 the Free Software Foundation; either version 2.1 of the License, or
17 (at your option) any later version.
19 systemd is distributed in the hope that it will be useful, but
20 WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 Lesser General Public License for more details.
24 You should have received a copy of the GNU Lesser General Public License
25 along with systemd; If not, see <http://www.gnu.org/licenses/>.
28 /* The head of the linked list. Use this in the structure that shall
29 * contain the head of the linked list */
30 #define LIST_HEAD(t,name) \
33 /* The pointers in the linked list's items. Use this in the item structure */
34 #define LIST_FIELDS(t,name) \
35 t *name##_next, *name##_prev
37 /* Initialize the list's head */
38 #define LIST_HEAD_INIT(t,head) \
43 /* Initialize a list item */
44 #define LIST_INIT(t,name,item) \
48 _item->name##_prev = _item->name##_next = NULL; \
51 /* Prepend an item to the list */
52 #define LIST_PREPEND(t,name,head,item) \
54 t **_head = &(head), *_item = (item); \
56 if ((_item->name##_next = *_head)) \
57 _item->name##_next->name##_prev = _item; \
58 _item->name##_prev = NULL; \
62 /* Remove an item from the list */
63 #define LIST_REMOVE(t,name,head,item) \
65 t **_head = &(head), *_item = (item); \
67 if (_item->name##_next) \
68 _item->name##_next->name##_prev = _item->name##_prev; \
69 if (_item->name##_prev) \
70 _item->name##_prev->name##_next = _item->name##_next; \
72 assert(*_head == _item); \
73 *_head = _item->name##_next; \
75 _item->name##_next = _item->name##_prev = NULL; \
78 /* Find the head of the list */
79 #define LIST_FIND_HEAD(t,name,item,head) \
83 while (_item->name##_prev) \
84 _item = _item->name##_prev; \
88 /* Find the tail of the list */
89 #define LIST_FIND_TAIL(t,name,item,tail) \
93 while (_item->name##_next) \
94 _item = _item->name##_next; \
98 /* Insert an item after another one (a = where, b = what) */
99 #define LIST_INSERT_AFTER(t,name,head,a,b) \
101 t **_head = &(head), *_a = (a), *_b = (b); \
104 if ((_b->name##_next = *_head)) \
105 _b->name##_next->name##_prev = _b; \
106 _b->name##_prev = NULL; \
109 if ((_b->name##_next = _a->name##_next)) \
110 _b->name##_next->name##_prev = _b; \
111 _b->name##_prev = _a; \
112 _a->name##_next = _b; \
116 #define LIST_JUST_US(name,item) \
117 (!(item)->name##_prev && !(item)->name##_next) \
119 #define LIST_FOREACH(name,i,head) \
120 for ((i) = (head); (i); (i) = (i)->name##_next)
122 #define LIST_FOREACH_SAFE(name,i,n,head) \
123 for ((i) = (head); (i) && (((n) = (i)->name##_next), 1); (i) = (n))
125 #define LIST_FOREACH_BEFORE(name,i,p) \
126 for ((i) = (p)->name##_prev; (i); (i) = (i)->name##_prev)
128 #define LIST_FOREACH_AFTER(name,i,p) \
129 for ((i) = (p)->name##_next; (i); (i) = (i)->name##_next)
131 /* Loop starting from p->next until p->prev.
132 p can be adjusted meanwhile. */
133 #define LIST_LOOP_BUT_ONE(name,i,head,p) \
134 for ((i) = (p)->name##_next ? (p)->name##_next : (head); \
136 (i) = (i)->name##_next ? (i)->name##_next : (head))