1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
7 This file is part of systemd.
9 Copyright 2010 Lennart Poettering
11 systemd is free software; you can redistribute it and/or modify it
12 under the terms of the GNU Lesser General Public License as published by
13 the Free Software Foundation; either version 2.1 of the License, or
14 (at your option) any later version.
16 systemd is distributed in the hope that it will be useful, but
17 WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 Lesser General Public License for more details.
21 You should have received a copy of the GNU Lesser General Public License
22 along with systemd; If not, see <http://www.gnu.org/licenses/>.
25 /* The head of the linked list. Use this in the structure that shall
26 * contain the head of the linked list */
27 #define LIST_HEAD(t,name) \
30 /* The pointers in the linked list's items. Use this in the item structure */
31 #define LIST_FIELDS(t,name) \
32 t *name##_next, *name##_prev
34 /* Initialize the list's head */
35 #define LIST_HEAD_INIT(t,head) \
40 /* Initialize a list item */
41 #define LIST_INIT(t,name,item) \
45 _item->name##_prev = _item->name##_next = NULL; \
48 /* Prepend an item to the list */
49 #define LIST_PREPEND(t,name,head,item) \
51 t **_head = &(head), *_item = (item); \
53 if ((_item->name##_next = *_head)) \
54 _item->name##_next->name##_prev = _item; \
55 _item->name##_prev = NULL; \
59 /* Remove an item from the list */
60 #define LIST_REMOVE(t,name,head,item) \
62 t **_head = &(head), *_item = (item); \
64 if (_item->name##_next) \
65 _item->name##_next->name##_prev = _item->name##_prev; \
66 if (_item->name##_prev) \
67 _item->name##_prev->name##_next = _item->name##_next; \
69 assert(*_head == _item); \
70 *_head = _item->name##_next; \
72 _item->name##_next = _item->name##_prev = NULL; \
75 /* Find the head of the list */
76 #define LIST_FIND_HEAD(t,name,item,head) \
80 while (_item->name##_prev) \
81 _item = _item->name##_prev; \
85 /* Find the head of the list */
86 #define LIST_FIND_TAIL(t,name,item,tail) \
90 while (_item->name##_next) \
91 _item = _item->name##_next; \
95 /* Insert an item after another one (a = where, b = what) */
96 #define LIST_INSERT_AFTER(t,name,head,a,b) \
98 t **_head = &(head), *_a = (a), *_b = (b); \
101 if ((_b->name##_next = *_head)) \
102 _b->name##_next->name##_prev = _b; \
103 _b->name##_prev = NULL; \
106 if ((_b->name##_next = _a->name##_next)) \
107 _b->name##_next->name##_prev = _b; \
108 _b->name##_prev = _a; \
109 _a->name##_next = _b; \
113 #define LIST_JUST_US(name,item) \
114 (!(item)->name##_prev && !(item)->name##_next) \
116 #define LIST_FOREACH(name,i,head) \
117 for ((i) = (head); (i); (i) = (i)->name##_next)
119 #define LIST_FOREACH_SAFE(name,i,n,head) \
120 for ((i) = (head); (i) && (((n) = (i)->name##_next), 1); (i) = (n))
122 #define LIST_FOREACH_BEFORE(name,i,p) \
123 for ((i) = (p)->name##_prev; (i); (i) = (i)->name##_prev)
125 #define LIST_FOREACH_AFTER(name,i,p) \
126 for ((i) = (p)->name##_next; (i); (i) = (i)->name##_next)