2 * Based on list.h in the Linux kernel source tree.
9 * container_of - cast a member of a structure out to the containing structure
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.
16 #define container_of(ptr, type, member) ({ \
17 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
18 (type *)( (char *)__mptr - offsetof(type,member) );})
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.
25 #define LIST_POISON1 ((void *) 0x00100100)
26 #define LIST_POISON2 ((void *) 0x00200200)
29 * Simple doubly linked list implementation.
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.
39 struct list_head *next, *prev;
42 #define LIST_HEAD_INIT(name) { &(name), &(name) }
44 #define LIST_HEAD(name) \
45 struct list_head name = LIST_HEAD_INIT(name)
47 #define INIT_LIST_HEAD(ptr) do { \
48 (ptr)->next = (ptr); (ptr)->prev = (ptr); \
52 * Insert a new entry between two known consecutive entries.
54 * This is only for internal list manipulation where we know
55 * the prev/next entries already!
57 static inline void __list_add(struct list_head *new,
58 struct list_head *prev,
59 struct list_head *next)
68 * list_add - add a new entry
69 * @new: new entry to be added
70 * @head: list head to add it after
72 * Insert a new entry after the specified head.
73 * This is good for implementing stacks.
75 static inline void list_add(struct list_head *new, struct list_head *head)
77 __list_add(new, head, head->next);
81 * list_add_tail - add a new entry
82 * @new: new entry to be added
83 * @head: list head to add it before
85 * Insert a new entry before the specified head.
86 * This is useful for implementing queues.
88 static inline void list_add_tail(struct list_head *new, struct list_head *head)
90 __list_add(new, head->prev, head);
94 * Delete a list entry by making the prev/next entries
95 * point to each other.
97 * This is only for internal list manipulation where we know
98 * the prev/next entries already!
100 static inline void __list_del(struct list_head * prev, struct list_head * next)
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.
112 static inline void list_del(struct list_head *entry)
114 __list_del(entry->prev, entry->next);
115 entry->next = LIST_POISON1;
116 entry->prev = LIST_POISON2;
120 * list_del_init - deletes entry from list and reinitialize it.
121 * @entry: the element to delete from the list.
123 static inline void list_del_init(struct list_head *entry)
125 __list_del(entry->prev, entry->next);
126 INIT_LIST_HEAD(entry);
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
134 static inline void list_move(struct list_head *list, struct list_head *head)
136 __list_del(list->prev, list->next);
137 list_add(list, head);
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
145 static inline void list_move_tail(struct list_head *list,
146 struct list_head *head)
148 __list_del(list->prev, list->next);
149 list_add_tail(list, head);
153 * list_empty - tests whether a list is empty
154 * @head: the list to test.
156 static inline int list_empty(struct list_head *head)
158 return head->next == head;
161 static inline void __list_splice(struct list_head *list,
162 struct list_head *head)
164 struct list_head *first = list->next;
165 struct list_head *last = list->prev;
166 struct list_head *at = head->next;
176 * list_splice - join two lists
177 * @list: the new list to add.
178 * @head: the place to add it in the first list.
180 static inline void list_splice(struct list_head *list, struct list_head *head)
182 if (!list_empty(list))
183 __list_splice(list, head);
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.
191 * The list at @list is reinitialised
193 static inline void list_splice_init(struct list_head *list,
194 struct list_head *head)
196 if (!list_empty(list)) {
197 __list_splice(list, head);
198 INIT_LIST_HEAD(list);
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.
208 #define list_entry(ptr, type, member) \
209 container_of(ptr, type, member)
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.
216 #define list_for_each(pos, head) \
217 for (pos = (head)->next; pos != (head); \
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.
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.
230 #define __list_for_each(pos, head) \
231 for (pos = (head)->next; pos != (head); pos = pos->next)
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.
238 #define list_for_each_prev(pos, head) \
239 for (pos = (head)->prev; pos != (head); pos = pos->prev)
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.
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)
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.
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))
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.
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))
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.
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))