]> git.ipfire.org Git - thirdparty/systemd.git/blame - list.h
[PATCH] respect prefix= setting in built udev.conf (updated)
[thirdparty/systemd.git] / list.h
CommitLineData
2232cac8
GKH
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 * Ripped out the rcu stuff, as it's not needed.
7 */
8
9#ifndef _LINUX_LIST_H
10#define _LINUX_LIST_H
11
12//#include <linux/stddef.h>
13/**
14 * container_of - cast a member of a structure out to the containing structure
15 *
16 * @ptr: the pointer to the member.
17 * @type: the type of the container struct this is embedded in.
18 * @member: the name of the member within the struct.
19 *
20 */
21#define container_of(ptr, type, member) ({ \
22 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
23 (type *)( (char *)__mptr - offsetof(type,member) );})
24
25//#include <linux/prefetch.h>
26static inline void prefetch(const void *x) {;}
27
28//#include <asm/system.h>
29
30/*
31 * These are non-NULL pointers that will result in page faults
32 * under normal circumstances, used to verify that nobody uses
33 * non-initialized list entries.
34 */
35#define LIST_POISON1 ((void *) 0x00100100)
36#define LIST_POISON2 ((void *) 0x00200200)
37
38/*
39 * Simple doubly linked list implementation.
40 *
41 * Some of the internal functions ("__xxx") are useful when
42 * manipulating whole lists rather than single entries, as
43 * sometimes we already know the next/prev entries and we can
44 * generate better code by using them directly rather than
45 * using the generic single-entry routines.
46 */
47
48struct list_head {
49 struct list_head *next, *prev;
50};
51
52#define LIST_HEAD_INIT(name) { &(name), &(name) }
53
54#define LIST_HEAD(name) \
55 struct list_head name = LIST_HEAD_INIT(name)
56
57#define INIT_LIST_HEAD(ptr) do { \
58 (ptr)->next = (ptr); (ptr)->prev = (ptr); \
59} while (0)
60
61/*
62 * Insert a new entry between two known consecutive entries.
63 *
64 * This is only for internal list manipulation where we know
65 * the prev/next entries already!
66 */
67static inline void __list_add(struct list_head *new,
68 struct list_head *prev,
69 struct list_head *next)
70{
71 next->prev = new;
72 new->next = next;
73 new->prev = prev;
74 prev->next = new;
75}
76
77/**
78 * list_add - add a new entry
79 * @new: new entry to be added
80 * @head: list head to add it after
81 *
82 * Insert a new entry after the specified head.
83 * This is good for implementing stacks.
84 */
85static inline void list_add(struct list_head *new, struct list_head *head)
86{
87 __list_add(new, head, head->next);
88}
89
90/**
91 * list_add_tail - add a new entry
92 * @new: new entry to be added
93 * @head: list head to add it before
94 *
95 * Insert a new entry before the specified head.
96 * This is useful for implementing queues.
97 */
98static inline void list_add_tail(struct list_head *new, struct list_head *head)
99{
100 __list_add(new, head->prev, head);
101}
102
103/*
104 * Delete a list entry by making the prev/next entries
105 * point to each other.
106 *
107 * This is only for internal list manipulation where we know
108 * the prev/next entries already!
109 */
110static inline void __list_del(struct list_head * prev, struct list_head * next)
111{
112 next->prev = prev;
113 prev->next = next;
114}
115
116/**
117 * list_del - deletes entry from list.
118 * @entry: the element to delete from the list.
119 * Note: list_empty on entry does not return true after this, the entry is
120 * in an undefined state.
121 */
122static inline void list_del(struct list_head *entry)
123{
124 __list_del(entry->prev, entry->next);
125 entry->next = LIST_POISON1;
126 entry->prev = LIST_POISON2;
127}
128
129/**
130 * list_del_init - deletes entry from list and reinitialize it.
131 * @entry: the element to delete from the list.
132 */
133static inline void list_del_init(struct list_head *entry)
134{
135 __list_del(entry->prev, entry->next);
136 INIT_LIST_HEAD(entry);
137}
138
139/**
140 * list_move - delete from one list and add as another's head
141 * @list: the entry to move
142 * @head: the head that will precede our entry
143 */
144static inline void list_move(struct list_head *list, struct list_head *head)
145{
146 __list_del(list->prev, list->next);
147 list_add(list, head);
148}
149
150/**
151 * list_move_tail - delete from one list and add as another's tail
152 * @list: the entry to move
153 * @head: the head that will follow our entry
154 */
155static inline void list_move_tail(struct list_head *list,
156 struct list_head *head)
157{
158 __list_del(list->prev, list->next);
159 list_add_tail(list, head);
160}
161
162/**
163 * list_empty - tests whether a list is empty
164 * @head: the list to test.
165 */
166static inline int list_empty(struct list_head *head)
167{
168 return head->next == head;
169}
170
171static inline void __list_splice(struct list_head *list,
172 struct list_head *head)
173{
174 struct list_head *first = list->next;
175 struct list_head *last = list->prev;
176 struct list_head *at = head->next;
177
178 first->prev = head;
179 head->next = first;
180
181 last->next = at;
182 at->prev = last;
183}
184
185/**
186 * list_splice - join two lists
187 * @list: the new list to add.
188 * @head: the place to add it in the first list.
189 */
190static inline void list_splice(struct list_head *list, struct list_head *head)
191{
192 if (!list_empty(list))
193 __list_splice(list, head);
194}
195
196/**
197 * list_splice_init - join two lists and reinitialise the emptied list.
198 * @list: the new list to add.
199 * @head: the place to add it in the first list.
200 *
201 * The list at @list is reinitialised
202 */
203static inline void list_splice_init(struct list_head *list,
204 struct list_head *head)
205{
206 if (!list_empty(list)) {
207 __list_splice(list, head);
208 INIT_LIST_HEAD(list);
209 }
210}
211
212/**
213 * list_entry - get the struct for this entry
214 * @ptr: the &struct list_head pointer.
215 * @type: the type of the struct this is embedded in.
216 * @member: the name of the list_struct within the struct.
217 */
218#define list_entry(ptr, type, member) \
219 container_of(ptr, type, member)
220
221/**
222 * list_for_each - iterate over a list
223 * @pos: the &struct list_head to use as a loop counter.
224 * @head: the head for your list.
225 */
226#define list_for_each(pos, head) \
227 for (pos = (head)->next, prefetch(pos->next); pos != (head); \
228 pos = pos->next, prefetch(pos->next))
229
230/**
231 * __list_for_each - iterate over a list
232 * @pos: the &struct list_head to use as a loop counter.
233 * @head: the head for your list.
234 *
235 * This variant differs from list_for_each() in that it's the
236 * simplest possible list iteration code, no prefetching is done.
237 * Use this for code that knows the list to be very short (empty
238 * or 1 entry) most of the time.
239 */
240#define __list_for_each(pos, head) \
241 for (pos = (head)->next; pos != (head); pos = pos->next)
242
243/**
244 * list_for_each_prev - iterate over a list backwards
245 * @pos: the &struct list_head to use as a loop counter.
246 * @head: the head for your list.
247 */
248#define list_for_each_prev(pos, head) \
249 for (pos = (head)->prev, prefetch(pos->prev); pos != (head); \
250 pos = pos->prev, prefetch(pos->prev))
251
252/**
253 * list_for_each_safe - iterate over a list safe against removal of list entry
254 * @pos: the &struct list_head to use as a loop counter.
255 * @n: another &struct list_head to use as temporary storage
256 * @head: the head for your list.
257 */
258#define list_for_each_safe(pos, n, head) \
259 for (pos = (head)->next, n = pos->next; pos != (head); \
260 pos = n, n = pos->next)
261
262/**
263 * list_for_each_entry - iterate over list of given type
264 * @pos: the type * to use as a loop counter.
265 * @head: the head for your list.
266 * @member: the name of the list_struct within the struct.
267 */
268#define list_for_each_entry(pos, head, member) \
269 for (pos = list_entry((head)->next, typeof(*pos), member), \
270 prefetch(pos->member.next); \
271 &pos->member != (head); \
272 pos = list_entry(pos->member.next, typeof(*pos), member), \
273 prefetch(pos->member.next))
274
275/**
276 * list_for_each_entry_reverse - iterate backwards over list of given type.
277 * @pos: the type * to use as a loop counter.
278 * @head: the head for your list.
279 * @member: the name of the list_struct within the struct.
280 */
281#define list_for_each_entry_reverse(pos, head, member) \
282 for (pos = list_entry((head)->prev, typeof(*pos), member), \
283 prefetch(pos->member.prev); \
284 &pos->member != (head); \
285 pos = list_entry(pos->member.prev, typeof(*pos), member), \
286 prefetch(pos->member.prev))
287
288
289/**
290 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
291 * @pos: the type * to use as a loop counter.
292 * @n: another type * to use as temporary storage
293 * @head: the head for your list.
294 * @member: the name of the list_struct within the struct.
295 */
296#define list_for_each_entry_safe(pos, n, head, member) \
297 for (pos = list_entry((head)->next, typeof(*pos), member), \
298 n = list_entry(pos->member.next, typeof(*pos), member); \
299 &pos->member != (head); \
300 pos = n, n = list_entry(n->member.next, typeof(*n), member))
301
302/*
303 * Double linked lists with a single pointer list head.
304 * Mostly useful for hash tables where the two pointer list head is
305 * too wasteful.
306 * You lose the ability to access the tail in O(1).
307 */
308
309struct hlist_head {
310 struct hlist_node *first;
311};
312
313struct hlist_node {
314 struct hlist_node *next, **pprev;
315};
316
317#define HLIST_HEAD_INIT { .first = NULL }
318#define HLIST_HEAD(name) struct hlist_head name = { .first = NULL }
319#define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
320#define INIT_HLIST_NODE(ptr) ((ptr)->next = NULL, (ptr)->pprev = NULL)
321
322static __inline__ int hlist_unhashed(struct hlist_node *h)
323{
324 return !h->pprev;
325}
326
327static __inline__ int hlist_empty(struct hlist_head *h)
328{
329 return !h->first;
330}
331
332static __inline__ void __hlist_del(struct hlist_node *n)
333{
334 struct hlist_node *next = n->next;
335 struct hlist_node **pprev = n->pprev;
336 *pprev = next;
337 if (next)
338 next->pprev = pprev;
339}
340
341static __inline__ void hlist_del(struct hlist_node *n)
342{
343 __hlist_del(n);
344 n->next = LIST_POISON1;
345 n->pprev = LIST_POISON2;
346}
347
348static __inline__ void hlist_del_init(struct hlist_node *n)
349{
350 if (n->pprev) {
351 __hlist_del(n);
352 INIT_HLIST_NODE(n);
353 }
354}
355
356static __inline__ void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
357{
358 struct hlist_node *first = h->first;
359 n->next = first;
360 if (first)
361 first->pprev = &n->next;
362 h->first = n;
363 n->pprev = &h->first;
364}
365
366/* next must be != NULL */
367static __inline__ void hlist_add_before(struct hlist_node *n, struct hlist_node *next)
368{
369 n->pprev = next->pprev;
370 n->next = next;
371 next->pprev = &n->next;
372 *(n->pprev) = n;
373}
374
375static __inline__ void hlist_add_after(struct hlist_node *n,
376 struct hlist_node *next)
377{
378 next->next = n->next;
379 *(next->pprev) = n;
380 n->next = next;
381}
382
383#define hlist_entry(ptr, type, member) container_of(ptr,type,member)
384
385/* Cannot easily do prefetch unfortunately */
386#define hlist_for_each(pos, head) \
387 for (pos = (head)->first; pos && ({ prefetch(pos->next); 1; }); \
388 pos = pos->next)
389
390#define hlist_for_each_safe(pos, n, head) \
391 for (pos = (head)->first; n = pos ? pos->next : 0, pos; \
392 pos = n)
393
394/**
395 * hlist_for_each_entry - iterate over list of given type
396 * @tpos: the type * to use as a loop counter.
397 * @pos: the &struct hlist_node to use as a loop counter.
398 * @head: the head for your list.
399 * @member: the name of the hlist_node within the struct.
400 */
401#define hlist_for_each_entry(tpos, pos, head, member) \
402 for (pos = (head)->first; \
403 pos && ({ prefetch(pos->next); 1;}) && \
404 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
405 pos = pos->next)
406
407/**
408 * hlist_for_each_entry_continue - iterate over a hlist continuing after existing point
409 * @tpos: the type * to use as a loop counter.
410 * @pos: the &struct hlist_node to use as a loop counter.
411 * @member: the name of the hlist_node within the struct.
412 */
413#define hlist_for_each_entry_continue(tpos, pos, member) \
414 for (pos = (pos)->next; \
415 pos && ({ prefetch(pos->next); 1;}) && \
416 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
417 pos = pos->next)
418
419/**
420 * hlist_for_each_entry_from - iterate over a hlist continuing from existing point
421 * @tpos: the type * to use as a loop counter.
422 * @pos: the &struct hlist_node to use as a loop counter.
423 * @member: the name of the hlist_node within the struct.
424 */
425#define hlist_for_each_entry_from(tpos, pos, member) \
426 for (; pos && ({ prefetch(pos->next); 1;}) && \
427 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
428 pos = pos->next)
429
430/**
431 * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
432 * @tpos: the type * to use as a loop counter.
433 * @pos: the &struct hlist_node to use as a loop counter.
434 * @n: another &struct hlist_node to use as temporary storage
435 * @head: the head for your list.
436 * @member: the name of the hlist_node within the struct.
437 */
438#define hlist_for_each_entry_safe(tpos, pos, n, head, member) \
439 for (pos = (head)->first; \
440 pos && ({ n = pos->next; 1; }) && \
441 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
442 pos = n)
443
444#endif