10 #define LIST_POISON1 ((void *) 0)
11 #define LIST_POISON2 ((void *) 0)
14 * Simple doubly linked list implementation.
16 * Some of the internal functions ("__xxx") are useful when
17 * manipulating whole lists rather than single entries, as
18 * sometimes we already know the next/prev entries and we can
19 * generate better code by using them directly rather than
20 * using the generic single-entry routines.
24 struct list_head *next, *prev;
27 #define LIST_HEAD_INIT(name) { &(name), &(name) }
29 #define LIST_HEAD(name) \
30 struct list_head name = LIST_HEAD_INIT(name)
32 #define INIT_LIST_HEAD(ptr) do { \
33 (ptr)->next = (ptr); (ptr)->prev = (ptr); \
37 * Insert a new entry between two known consecutive entries.
39 * This is only for internal list manipulation where we know
40 * the prev/next entries already!
42 static inline void __list_add(struct list_head *new,
43 struct list_head *prev,
44 struct list_head *next)
53 * list_add - add a new entry
54 * @new: new entry to be added
55 * @head: list head to add it after
57 * Insert a new entry after the specified head.
58 * This is good for implementing stacks.
60 static inline void list_add(struct list_head *new, struct list_head *head)
62 __list_add(new, head, head->next);
66 * list_add_tail - add a new entry
67 * @new: new entry to be added
68 * @head: list head to add it before
70 * Insert a new entry before the specified head.
71 * This is useful for implementing queues.
73 static inline void list_add_tail(struct list_head *new, struct list_head *head)
75 __list_add(new, head->prev, head);
79 * Delete a list entry by making the prev/next entries
80 * point to each other.
82 * This is only for internal list manipulation where we know
83 * the prev/next entries already!
85 static inline void __list_del(struct list_head * prev, struct list_head * next)
92 * list_del - deletes entry from list.
93 * @entry: the element to delete from the list.
94 * Note: list_empty on entry does not return true after this, the entry is
95 * in an undefined state.
97 static inline void list_del(struct list_head *entry)
99 __list_del(entry->prev, entry->next);
100 entry->next = LIST_POISON1;
101 entry->prev = LIST_POISON2;
105 * list_del_init - deletes entry from list and reinitialize it.
106 * @entry: the element to delete from the list.
108 static inline void list_del_init(struct list_head *entry)
110 __list_del(entry->prev, entry->next);
111 INIT_LIST_HEAD(entry);
115 * list_move - delete from one list and add as another's head
116 * @list: the entry to move
117 * @head: the head that will precede our entry
119 static inline void list_move(struct list_head *list, struct list_head *head)
121 __list_del(list->prev, list->next);
122 list_add(list, head);
126 * list_move_tail - delete from one list and add as another's tail
127 * @list: the entry to move
128 * @head: the head that will follow our entry
130 static inline void list_move_tail(struct list_head *list,
131 struct list_head *head)
133 __list_del(list->prev, list->next);
134 list_add_tail(list, head);
138 * list_empty - tests whether a list is empty
139 * @head: the list to test.
141 static inline int list_empty(struct list_head *head)
143 return head->next == head;
146 static inline void __list_splice(struct list_head *list,
147 struct list_head *head)
149 struct list_head *first = list->next;
150 struct list_head *last = list->prev;
151 struct list_head *where = head->next;
161 * list_splice - join two lists
162 * @list: the new list to add.
163 * @head: the place to add it in the first list.
165 static inline void list_splice(struct list_head *list, struct list_head *head)
167 if (!list_empty(list))
168 __list_splice(list, head);
172 * list_splice_init - join two lists and reinitialise the emptied list.
173 * @list: the new list to add.
174 * @head: the place to add it in the first list.
176 * The list at @list is reinitialised
178 static inline void list_splice_init(struct list_head *list,
179 struct list_head *head)
181 if (!list_empty(list)) {
182 __list_splice(list, head);
183 INIT_LIST_HEAD(list);
188 * list_entry - get the struct for this entry
189 * @ptr: the &struct list_head pointer.
190 * @type: the type of the struct this is embedded in.
191 * @member: the name of the list_struct within the struct.
193 #define list_entry(ptr, type, member) \
194 container_of(ptr, type, member)
197 * list_for_each - iterate over a list
198 * @pos: the &struct list_head to use as a loop counter.
199 * @head: the head for your list.
201 #define list_for_each(pos, head) \
202 for (pos = (head)->next, prefetch(pos->next); pos != (head); \
203 pos = pos->next, prefetch(pos->next))
206 * __list_for_each - iterate over a list
207 * @pos: the &struct list_head to use as a loop counter.
208 * @head: the head for your list.
210 * This variant differs from list_for_each() in that it's the
211 * simplest possible list iteration code, no prefetching is done.
212 * Use this for code that knows the list to be very short (empty
213 * or 1 entry) most of the time.
215 #define __list_for_each(pos, head) \
216 for (pos = (head)->next; pos != (head); pos = pos->next)
219 * list_for_each_prev - iterate over a list backwards
220 * @pos: the &struct list_head to use as a loop counter.
221 * @head: the head for your list.
223 #define list_for_each_prev(pos, head) \
224 for (pos = (head)->prev, prefetch(pos->prev); pos != (head); \
225 pos = pos->prev, prefetch(pos->prev))
228 * list_for_each_safe - iterate over a list safe against removal of list entry
229 * @pos: the &struct list_head to use as a loop counter.
230 * @n: another &struct list_head to use as temporary storage
231 * @head: the head for your list.
233 #define list_for_each_safe(pos, n, head) \
234 for (pos = (head)->next, n = pos->next; pos != (head); \
235 pos = n, n = pos->next)
238 * list_for_each_entry - iterate over list of given type
239 * @pos: the type * to use as a loop counter.
240 * @head: the head for your list.
241 * @member: the name of the list_struct within the struct.
243 #define list_for_each_entry(pos, head, member) \
244 for (pos = list_entry((head)->next, typeof(*pos), member), \
245 prefetch(pos->member.next); \
246 &pos->member != (head); \
247 pos = list_entry(pos->member.next, typeof(*pos), member), \
248 prefetch(pos->member.next))
251 * list_for_each_entry_reverse - iterate backwards 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_reverse(pos, head, member) \
257 for (pos = list_entry((head)->prev, typeof(*pos), member), \
258 prefetch(pos->member.prev); \
259 &pos->member != (head); \
260 pos = list_entry(pos->member.prev, typeof(*pos), member), \
261 prefetch(pos->member.prev))
265 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
266 * @pos: the type * to use as a loop counter.
267 * @n: another type * to use as temporary storage
268 * @head: the head for your list.
269 * @member: the name of the list_struct within the struct.
271 #define list_for_each_entry_safe(pos, n, head, member) \
272 for (pos = list_entry((head)->next, typeof(*pos), member), \
273 n = list_entry(pos->member.next, typeof(*pos), member); \
274 &pos->member != (head); \
275 pos = n, n = list_entry(n->member.next, typeof(*n), member))
279 #include <linux/list.h>
281 #endif /*__KERNEL__*/
287 #endif /* _UL_LISTBASE_H */