]>
Commit | Line | Data |
---|---|---|
03467c88 | 1 | /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ |
60918275 LP |
2 | |
3 | #ifndef foolisthfoo | |
4 | #define foolisthfoo | |
5 | ||
a7334b09 LP |
6 | /*** |
7 | This file is part of systemd. | |
8 | ||
9 | Copyright 2010 Lennart Poettering | |
10 | ||
11 | systemd is free software; you can redistribute it and/or modify it | |
12 | under the terms of the GNU General Public License as published by | |
13 | the Free Software Foundation; either version 2 of the License, or | |
14 | (at your option) any later version. | |
15 | ||
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 | General Public License for more details. | |
20 | ||
21 | You should have received a copy of the GNU General Public License | |
22 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
23 | ***/ | |
24 | ||
60918275 LP |
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) \ | |
034c6ed7 | 28 | t *name |
60918275 LP |
29 | |
30 | /* The pointers in the linked list's items. Use this in the item structure */ | |
034c6ed7 LP |
31 | #define LIST_FIELDS(t,name) \ |
32 | t *name##_next, *name##_prev | |
60918275 LP |
33 | |
34 | /* Initialize the list's head */ | |
034c6ed7 | 35 | #define LIST_HEAD_INIT(t,head) \ |
60918275 | 36 | do { \ |
034c6ed7 | 37 | (head) = NULL; } \ |
60918275 LP |
38 | while(false) |
39 | ||
40 | /* Initialize a list item */ | |
034c6ed7 | 41 | #define LIST_INIT(t,name,item) \ |
60918275 LP |
42 | do { \ |
43 | t *_item = (item); \ | |
44 | assert(_item); \ | |
034c6ed7 | 45 | _item->name##_prev = _item->name##_next = NULL; \ |
60918275 LP |
46 | } while(false) |
47 | ||
48 | /* Prepend an item to the list */ | |
034c6ed7 | 49 | #define LIST_PREPEND(t,name,head,item) \ |
60918275 LP |
50 | do { \ |
51 | t **_head = &(head), *_item = (item); \ | |
52 | assert(_item); \ | |
034c6ed7 LP |
53 | if ((_item->name##_next = *_head)) \ |
54 | _item->name##_next->name##_prev = _item; \ | |
55 | _item->name##_prev = NULL; \ | |
60918275 LP |
56 | *_head = _item; \ |
57 | } while(false) | |
58 | ||
59 | /* Remove an item from the list */ | |
034c6ed7 | 60 | #define LIST_REMOVE(t,name,head,item) \ |
60918275 LP |
61 | do { \ |
62 | t **_head = &(head), *_item = (item); \ | |
63 | assert(_item); \ | |
034c6ed7 LP |
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; \ | |
60918275 LP |
68 | else { \ |
69 | assert(*_head == _item); \ | |
034c6ed7 | 70 | *_head = _item->name##_next; \ |
60918275 | 71 | } \ |
034c6ed7 | 72 | _item->name##_next = _item->name##_prev = NULL; \ |
60918275 LP |
73 | } while(false) |
74 | ||
75 | /* Find the head of the list */ | |
034c6ed7 | 76 | #define LIST_FIND_HEAD(t,name,item,head) \ |
60918275 | 77 | do { \ |
034c6ed7 LP |
78 | t *_item = (item); \ |
79 | assert(_item); \ | |
95e26a69 | 80 | while (_item->name##_prev) \ |
034c6ed7 LP |
81 | _item = _item->name##_prev; \ |
82 | (head) = _item; \ | |
83 | } while (false) | |
84 | ||
85 | /* Find the head of the list */ | |
86 | #define LIST_FIND_TAIL(t,name,item,tail) \ | |
87 | do { \ | |
88 | t *_item = (item); \ | |
89 | assert(_item); \ | |
90 | while (_item->name##_next) \ | |
91 | _item = _item->name##_next; \ | |
92 | (tail) = _item; \ | |
60918275 LP |
93 | } while (false) |
94 | ||
95 | /* Insert an item after another one (a = where, b = what) */ | |
034c6ed7 | 96 | #define LIST_INSERT_AFTER(t,name,head,a,b) \ |
60918275 LP |
97 | do { \ |
98 | t **_head = &(head), *_a = (a), *_b = (b); \ | |
99 | assert(_b); \ | |
100 | if (!_a) { \ | |
034c6ed7 LP |
101 | if ((_b->name##_next = *_head)) \ |
102 | _b->name##_next->name##_prev = _b; \ | |
103 | _b->name##_prev = NULL; \ | |
60918275 LP |
104 | *_head = _b; \ |
105 | } else { \ | |
034c6ed7 LP |
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; \ | |
60918275 LP |
110 | } \ |
111 | } while(false) | |
112 | ||
6210e7fc LP |
113 | #define LIST_JUST_US(name,item) \ |
114 | (!(item)->name##_prev && !(item)->name##_next) \ | |
115 | ||
034c6ed7 LP |
116 | #define LIST_FOREACH(name,i,head) \ |
117 | for ((i) = (head); (i); (i) = (i)->name##_next) | |
60918275 | 118 | |
034c6ed7 LP |
119 | #define LIST_FOREACH_SAFE(name,i,n,head) \ |
120 | for ((i) = (head); (i) && (((n) = (i)->name##_next), 1); (i) = (n)) | |
60918275 | 121 | |
e04aad61 LP |
122 | #define LIST_FOREACH_BEFORE(name,i,p) \ |
123 | for ((i) = (p)->name##_prev; (i); (i) = (i)->name##_prev) | |
124 | ||
125 | #define LIST_FOREACH_AFTER(name,i,p) \ | |
126 | for ((i) = (p)->name##_next; (i); (i) = (i)->name##_next) | |
127 | ||
60918275 | 128 | #endif |