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 /* Remove an item from the list */
59 #define LIST_REMOVE(name,head,item) \
61 typeof(*(head)) **_head = &(head), *_item = (item); \
63 if (_item->name##_next) \
64 _item->name##_next->name##_prev = _item->name##_prev; \
65 if (_item->name##_prev) \
66 _item->name##_prev->name##_next = _item->name##_next; \
68 assert(*_head == _item); \
69 *_head = _item->name##_next; \
71 _item->name##_next = _item->name##_prev = NULL; \
74 /* Find the head of the list */
75 #define LIST_FIND_HEAD(name,item,head) \
77 typeof(*(item)) *_item = (item); \
81 while (_item->name##_prev) \
82 _item = _item->name##_prev; \
87 /* Find the tail of the list */
88 #define LIST_FIND_TAIL(name,item,tail) \
90 typeof(*(item)) *_item = (item); \
94 while (_item->name##_next) \
95 _item = _item->name##_next; \
100 /* Insert an item after another one (a = where, b = what) */
101 #define LIST_INSERT_AFTER(name,head,a,b) \
103 typeof(*(head)) **_head = &(head), *_a = (a), *_b = (b); \
106 if ((_b->name##_next = *_head)) \
107 _b->name##_next->name##_prev = _b; \
108 _b->name##_prev = NULL; \
111 if ((_b->name##_next = _a->name##_next)) \
112 _b->name##_next->name##_prev = _b; \
113 _b->name##_prev = _a; \
114 _a->name##_next = _b; \
118 #define LIST_JUST_US(name,item) \
119 (!(item)->name##_prev && !(item)->name##_next) \
121 #define LIST_FOREACH(name,i,head) \
122 for ((i) = (head); (i); (i) = (i)->name##_next)
124 #define LIST_FOREACH_SAFE(name,i,n,head) \
125 for ((i) = (head); (i) && (((n) = (i)->name##_next), 1); (i) = (n))
127 #define LIST_FOREACH_BEFORE(name,i,p) \
128 for ((i) = (p)->name##_prev; (i); (i) = (i)->name##_prev)
130 #define LIST_FOREACH_AFTER(name,i,p) \
131 for ((i) = (p)->name##_next; (i); (i) = (i)->name##_next)
133 /* Loop starting from p->next until p->prev.
134 p can be adjusted meanwhile. */
135 #define LIST_LOOP_BUT_ONE(name,i,head,p) \
136 for ((i) = (p)->name##_next ? (p)->name##_next : (head); \
138 (i) = (i)->name##_next ? (i)->name##_next : (head))