3 * Shamelessly adapted from the linux kernel.
12 * These are non-NULL pointers that will result in page faults
13 * under normal circumstances, used to verify that nobody uses
14 * non-initialized list entries.
16 #define LIST_POISON1 ((void *) 0x00100100)
17 #define LIST_POISON2 ((void *) 0x00200200)
20 * Simple doubly linked list implementation.
22 * Some of the internal functions ("__xxx") are useful when
23 * manipulating whole lists rather than single entries, as
24 * sometimes we already know the next/prev entries and we can
25 * generate better code by using them directly rather than
26 * using the generic single-entry routines.
30 struct list_head *next, *prev;
33 #define LIST_HEAD_INIT(name) { &(name), &(name) }
35 #define LIST_HEAD(name) \
36 struct list_head name = LIST_HEAD_INIT(name)
38 #define INIT_LIST_HEAD(ptr) do { \
39 (ptr)->next = (ptr); (ptr)->prev = (ptr); \
42 #define _list_offsetof(type,member) \
43 ((char *) &(((type *) 0)->member) - (char *) 0)
45 #define _list_container_of(ptr, type, member) \
46 ((type *) ((char *) (ptr) - _list_offsetof(type, member)))
49 * Insert a new entry between two known consecutive entries.
51 * This is only for internal list manipulation where we know
52 * the prev/next entries already!
54 static INLINE void __list_add(struct list_head *new,
55 struct list_head *prev,
56 struct list_head *next)
65 * list_add - add a new entry
66 * @new: new entry to be added
67 * @head: list head to add it after
69 * Insert a new entry after the specified head.
70 * This is good for implementing stacks.
72 static INLINE void list_add(struct list_head *new, struct list_head *head)
74 __list_add(new, head, head->next);
78 * list_add_tail - add a new entry
79 * @new: new entry to be added
80 * @head: list head to add it before
82 * Insert a new entry before the specified head.
83 * This is useful for implementing queues.
85 static INLINE void list_add_tail(struct list_head *new, struct list_head *head)
87 __list_add(new, head->prev, head);
91 * Delete a list entry by making the prev/next entries
92 * point to each other.
94 * This is only for internal list manipulation where we know
95 * the prev/next entries already!
97 static INLINE void __list_del(struct list_head * prev, struct list_head * next)
104 * list_del - deletes entry from list.
105 * @entry: the element to delete from the list.
106 * Note: list_empty on entry does not return true after this, the entry is
107 * in an undefined state.
109 static INLINE void list_del(struct list_head *entry)
111 __list_del(entry->prev, entry->next);
112 entry->next = LIST_POISON1;
113 entry->prev = LIST_POISON2;
118 * list_del_init - deletes entry from list and reinitialize it.
119 * @entry: the element to delete from the list.
121 static INLINE void list_del_init(struct list_head *entry)
123 __list_del(entry->prev, entry->next);
124 INIT_LIST_HEAD(entry);
128 * list_move - delete from one list and add as another's head
129 * @list: the entry to move
130 * @head: the head that will precede our entry
132 static INLINE void list_move(struct list_head *list, struct list_head *head)
134 __list_del(list->prev, list->next);
135 list_add(list, head);
139 * list_move_tail - delete from one list and add as another's tail
140 * @list: the entry to move
141 * @head: the head that will follow our entry
143 static INLINE void list_move_tail(struct list_head *list,
144 struct list_head *head)
146 __list_del(list->prev, list->next);
147 list_add_tail(list, head);
151 * list_empty - tests whether a list is empty
152 * @head: the list to test.
154 static INLINE int list_empty(struct list_head *head)
156 return head->next == head;
159 static INLINE void __list_splice(struct list_head *list,
160 struct list_head *head)
162 struct list_head *first = list->next;
163 struct list_head *last = list->prev;
164 struct list_head *at = head->next;
174 * list_splice - join two lists
175 * @list: the new list to add.
176 * @head: the place to add it in the first list.
178 static INLINE void list_splice(struct list_head *list, struct list_head *head)
180 if (!list_empty(list))
181 __list_splice(list, head);
185 * list_splice_init - join two lists and reinitialise the emptied list.
186 * @list: the new list to add.
187 * @head: the place to add it in the first list.
189 * The list at @list is reinitialised
191 static INLINE void list_splice_init(struct list_head *list,
192 struct list_head *head)
194 if (!list_empty(list)) {
195 __list_splice(list, head);
196 INIT_LIST_HEAD(list);
201 * list_entry - get the struct for this entry
202 * @ptr: the &struct list_head pointer.
203 * @type: the type of the struct this is embedded in.
204 * @member: the name of the list_struct within the struct.
206 #define list_entry(ptr, type, member) \
207 _list_container_of(ptr, type, member)
210 * list_for_each - iterate over a list
211 * @pos: the &struct list_head to use as a loop counter.
212 * @head: the head for your list.
214 #define list_for_each(pos, head) \
215 for (pos = (head)->next, (pos->next); pos != (head); \
216 pos = pos->next, (pos->next))
219 * __list_for_each - iterate over a list
220 * @pos: the &struct list_head to use as a loop counter.
221 * @head: the head for your list.
223 * This variant differs from list_for_each() in that it's the
224 * simplest possible list iteration code, no ing is done.
225 * Use this for code that knows the list to be very short (empty
226 * or 1 entry) most of the time.
228 #define __list_for_each(pos, head) \
229 for (pos = (head)->next; pos != (head); pos = pos->next)
232 * list_for_each_prev - iterate over a list backwards
233 * @pos: the &struct list_head to use as a loop counter.
234 * @head: the head for your list.
236 #define list_for_each_prev(pos, head) \
237 for (pos = (head)->prev, (pos->prev); pos != (head); \
238 pos = pos->prev, (pos->prev))
241 * list_for_each_safe - iterate over a list safe against removal of list entry
242 * @pos: the &struct list_head to use as a loop counter.
243 * @n: another &struct list_head to use as temporary storage
244 * @head: the head for your list.
246 #define list_for_each_safe(pos, n, head) \
247 for (pos = (head)->next, n = pos->next; pos != (head); \
248 pos = n, n = pos->next)
251 * list_for_each_entry - iterate over list of given type
252 * @pos: the type * to use as a loop counter.
253 * @head: the head for your list.
254 * @member: the name of the list_struct within the struct.
256 #define list_for_each_entry(type, pos, head, member) \
257 for (pos = list_entry((head)->next, type, member); \
258 &pos->member != (head); \
259 pos = list_entry(pos->member.next, type, member))
262 * list_for_each_entry_reverse - iterate backwards over list of given type.
263 * @pos: the type * to use as a loop counter.
264 * @head: the head for your list.
265 * @member: the name of the list_struct within the struct.
267 #define list_for_each_entry_reverse(type, pos, head, member) \
268 for (pos = list_entry((head)->prev, type, member); \
269 &pos->member != (head); \
270 pos = list_entry(pos->member.prev, type, 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(type, pos, n, head, member) \
281 for (pos = list_entry((head)->next, type, member), \
282 n = list_entry(pos->member.next, type, member); \
283 &pos->member != (head); \
284 pos = n, n = list_entry(n->member.next, type, member))