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