]> git.ipfire.org Git - thirdparty/systemd.git/blob - list.h
add ID_BUS to *_id programs
[thirdparty/systemd.git] / list.h
1 /*
2 * Copied from the Linux kernel source tree, version 2.6.0-test1.
3 *
4 * Licensed under the GPL v2 as per the whole kernel source tree.
5 *
6 */
7
8 #ifndef _LIST_H
9 #define _LIST_H
10
11 /**
12 * container_of - cast a member of a structure out to the containing structure
13 *
14 * @ptr: the pointer to the member.
15 * @type: the type of the container struct this is embedded in.
16 * @member: the name of the member within the struct.
17 *
18 */
19 #define container_of(ptr, type, member) ({ \
20 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
21 (type *)( (char *)__mptr - offsetof(type,member) );})
22
23 /*
24 * These are non-NULL pointers that will result in page faults
25 * under normal circumstances, used to verify that nobody uses
26 * non-initialized list entries.
27 */
28 #define LIST_POISON1 ((void *) 0x00100100)
29 #define LIST_POISON2 ((void *) 0x00200200)
30
31 /*
32 * Simple doubly linked list implementation.
33 *
34 * Some of the internal functions ("__xxx") are useful when
35 * manipulating whole lists rather than single entries, as
36 * sometimes we already know the next/prev entries and we can
37 * generate better code by using them directly rather than
38 * using the generic single-entry routines.
39 */
40
41 struct list_head {
42 struct list_head *next, *prev;
43 };
44
45 #define LIST_HEAD_INIT(name) { &(name), &(name) }
46
47 #define LIST_HEAD(name) \
48 struct list_head name = LIST_HEAD_INIT(name)
49
50 #define INIT_LIST_HEAD(ptr) do { \
51 (ptr)->next = (ptr); (ptr)->prev = (ptr); \
52 } while (0)
53
54 /*
55 * Insert a new entry between two known consecutive entries.
56 *
57 * This is only for internal list manipulation where we know
58 * the prev/next entries already!
59 */
60 static inline void __list_add(struct list_head *new,
61 struct list_head *prev,
62 struct list_head *next)
63 {
64 next->prev = new;
65 new->next = next;
66 new->prev = prev;
67 prev->next = new;
68 }
69
70 /**
71 * list_add - add a new entry
72 * @new: new entry to be added
73 * @head: list head to add it after
74 *
75 * Insert a new entry after the specified head.
76 * This is good for implementing stacks.
77 */
78 static inline void list_add(struct list_head *new, struct list_head *head)
79 {
80 __list_add(new, head, head->next);
81 }
82
83 /**
84 * list_add_tail - add a new entry
85 * @new: new entry to be added
86 * @head: list head to add it before
87 *
88 * Insert a new entry before the specified head.
89 * This is useful for implementing queues.
90 */
91 static inline void list_add_tail(struct list_head *new, struct list_head *head)
92 {
93 __list_add(new, head->prev, head);
94 }
95
96 /*
97 * Delete a list entry by making the prev/next entries
98 * point to each other.
99 *
100 * This is only for internal list manipulation where we know
101 * the prev/next entries already!
102 */
103 static inline void __list_del(struct list_head * prev, struct list_head * next)
104 {
105 next->prev = prev;
106 prev->next = next;
107 }
108
109 /**
110 * list_del - deletes entry from list.
111 * @entry: the element to delete from the list.
112 * Note: list_empty on entry does not return true after this, the entry is
113 * in an undefined state.
114 */
115 static inline void list_del(struct list_head *entry)
116 {
117 __list_del(entry->prev, entry->next);
118 entry->next = LIST_POISON1;
119 entry->prev = LIST_POISON2;
120 }
121
122 /**
123 * list_del_init - deletes entry from list and reinitialize it.
124 * @entry: the element to delete from the list.
125 */
126 static inline void list_del_init(struct list_head *entry)
127 {
128 __list_del(entry->prev, entry->next);
129 INIT_LIST_HEAD(entry);
130 }
131
132 /**
133 * list_move - delete from one list and add as another's head
134 * @list: the entry to move
135 * @head: the head that will precede our entry
136 */
137 static inline void list_move(struct list_head *list, struct list_head *head)
138 {
139 __list_del(list->prev, list->next);
140 list_add(list, head);
141 }
142
143 /**
144 * list_move_tail - delete from one list and add as another's tail
145 * @list: the entry to move
146 * @head: the head that will follow our entry
147 */
148 static inline void list_move_tail(struct list_head *list,
149 struct list_head *head)
150 {
151 __list_del(list->prev, list->next);
152 list_add_tail(list, head);
153 }
154
155 /**
156 * list_empty - tests whether a list is empty
157 * @head: the list to test.
158 */
159 static inline int list_empty(struct list_head *head)
160 {
161 return head->next == head;
162 }
163
164 static inline void __list_splice(struct list_head *list,
165 struct list_head *head)
166 {
167 struct list_head *first = list->next;
168 struct list_head *last = list->prev;
169 struct list_head *at = head->next;
170
171 first->prev = head;
172 head->next = first;
173
174 last->next = at;
175 at->prev = last;
176 }
177
178 /**
179 * list_splice - join two lists
180 * @list: the new list to add.
181 * @head: the place to add it in the first list.
182 */
183 static inline void list_splice(struct list_head *list, struct list_head *head)
184 {
185 if (!list_empty(list))
186 __list_splice(list, head);
187 }
188
189 /**
190 * list_splice_init - join two lists and reinitialise the emptied list.
191 * @list: the new list to add.
192 * @head: the place to add it in the first list.
193 *
194 * The list at @list is reinitialised
195 */
196 static inline void list_splice_init(struct list_head *list,
197 struct list_head *head)
198 {
199 if (!list_empty(list)) {
200 __list_splice(list, head);
201 INIT_LIST_HEAD(list);
202 }
203 }
204
205 /**
206 * list_entry - get the struct for this entry
207 * @ptr: the &struct list_head pointer.
208 * @type: the type of the struct this is embedded in.
209 * @member: the name of the list_struct within the struct.
210 */
211 #define list_entry(ptr, type, member) \
212 container_of(ptr, type, member)
213
214 /**
215 * list_for_each - iterate over a list
216 * @pos: the &struct list_head to use as a loop counter.
217 * @head: the head for your list.
218 */
219 #define list_for_each(pos, head) \
220 for (pos = (head)->next; pos != (head); \
221 pos = pos->next)
222
223 /**
224 * __list_for_each - iterate over a list
225 * @pos: the &struct list_head to use as a loop counter.
226 * @head: the head for your list.
227 *
228 * This variant differs from list_for_each() in that it's the
229 * simplest possible list iteration code.
230 * Use this for code that knows the list to be very short (empty
231 * or 1 entry) most of the time.
232 */
233 #define __list_for_each(pos, head) \
234 for (pos = (head)->next; pos != (head); pos = pos->next)
235
236 /**
237 * list_for_each_prev - iterate over a list backwards
238 * @pos: the &struct list_head to use as a loop counter.
239 * @head: the head for your list.
240 */
241 #define list_for_each_prev(pos, head) \
242 for (pos = (head)->prev; pos != (head); pos = pos->prev)
243
244 /**
245 * list_for_each_safe - iterate over a list safe against removal of list entry
246 * @pos: the &struct list_head to use as a loop counter.
247 * @n: another &struct list_head to use as temporary storage
248 * @head: the head for your list.
249 */
250 #define list_for_each_safe(pos, n, head) \
251 for (pos = (head)->next, n = pos->next; pos != (head); \
252 pos = n, n = pos->next)
253
254 /**
255 * list_for_each_entry - iterate over list of given type
256 * @pos: the type * to use as a loop counter.
257 * @head: the head for your list.
258 * @member: the name of the list_struct within the struct.
259 */
260 #define list_for_each_entry(pos, head, member) \
261 for (pos = list_entry((head)->next, typeof(*pos), member); \
262 &pos->member != (head); \
263 pos = list_entry(pos->member.next, typeof(*pos), member))
264
265 /**
266 * list_for_each_entry_reverse - iterate backwards over list of given type.
267 * @pos: the type * to use as a loop counter.
268 * @head: the head for your list.
269 * @member: the name of the list_struct within the struct.
270 */
271 #define list_for_each_entry_reverse(pos, head, member) \
272 for (pos = list_entry((head)->prev, typeof(*pos), member); \
273 &pos->member != (head); \
274 pos = list_entry(pos->member.prev, typeof(*pos), member))
275
276 /**
277 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
278 * @pos: the type * to use as a loop counter.
279 * @n: another type * to use as temporary storage
280 * @head: the head for your list.
281 * @member: the name of the list_struct within the struct.
282 */
283 #define list_for_each_entry_safe(pos, n, head, member) \
284 for (pos = list_entry((head)->next, typeof(*pos), member), \
285 n = list_entry(pos->member.next, typeof(*pos), member); \
286 &pos->member != (head); \
287 pos = n, n = list_entry(n->member.next, typeof(*n), member))
288
289 #endif /* _LIST_H */