4 #define LIST_POISON1 ((void *) 0x00100100)
5 #define LIST_POISON2 ((void *) 0x00200200)
8 * Simple doubly linked list implementation.
10 * Some of the internal functions ("__xxx") are useful when
11 * manipulating whole lists rather than single entries, as
12 * sometimes we already know the next/prev entries and we can
13 * generate better code by using them directly rather than
14 * using the generic single-entry routines.
18 struct list_head *next, *prev;
21 #define LIST_HEAD_INIT(name) { &(name), &(name) }
23 #define LIST_HEAD(name) \
24 struct list_head name = LIST_HEAD_INIT(name)
26 static inline void INIT_LIST_HEAD(struct list_head *list)
33 * Insert a new entry between two known consecutive entries.
35 * This is only for internal list manipulation where we know
36 * the prev/next entries already!
38 #ifndef CONFIG_DEBUG_LIST
39 static inline void __list_add(struct list_head *new,
40 struct list_head *prev,
41 struct list_head *next)
49 extern void __list_add(struct list_head *new,
50 struct list_head *prev,
51 struct list_head *next);
55 * list_add - add a new entry
56 * @new: new entry to be added
57 * @head: list head to add it after
59 * Insert a new entry after the specified head.
60 * This is good for implementing stacks.
62 #ifndef CONFIG_DEBUG_LIST
63 static inline void list_add(struct list_head *new, struct list_head *head)
65 __list_add(new, head, head->next);
68 extern void list_add(struct list_head *new, struct list_head *head);
73 * list_add_tail - add a new entry
74 * @new: new entry to be added
75 * @head: list head to add it before
77 * Insert a new entry before the specified head.
78 * This is useful for implementing queues.
80 static inline void list_add_tail(struct list_head *new, struct list_head *head)
82 __list_add(new, head->prev, head);
86 * Delete a list entry by making the prev/next entries
87 * point to each other.
89 * This is only for internal list manipulation where we know
90 * the prev/next entries already!
92 static inline void __list_del(struct list_head * prev, struct list_head * next)
99 * list_del - deletes entry from list.
100 * @entry: the element to delete from the list.
101 * Note: list_empty on entry does not return true after this, the entry is
102 * in an undefined state.
104 #ifndef CONFIG_DEBUG_LIST
105 static inline void list_del(struct list_head *entry)
107 __list_del(entry->prev, entry->next);
108 entry->next = LIST_POISON1;
109 entry->prev = LIST_POISON2;
112 extern void list_del(struct list_head *entry);
116 * list_replace - replace old entry by new one
117 * @old : the element to be replaced
118 * @new : the new element to insert
119 * Note: if 'old' was empty, it will be overwritten.
121 static inline void list_replace(struct list_head *old,
122 struct list_head *new)
124 new->next = old->next;
125 new->next->prev = new;
126 new->prev = old->prev;
127 new->prev->next = new;
130 static inline void list_replace_init(struct list_head *old,
131 struct list_head *new)
133 list_replace(old, new);
137 * list_del_init - deletes entry from list and reinitialize it.
138 * @entry: the element to delete from the list.
140 static inline void list_del_init(struct list_head *entry)
142 __list_del(entry->prev, entry->next);
143 INIT_LIST_HEAD(entry);
147 * list_move - delete from one list and add as another's head
148 * @list: the entry to move
149 * @head: the head that will precede our entry
151 static inline void list_move(struct list_head *list, struct list_head *head)
153 __list_del(list->prev, list->next);
154 list_add(list, head);
158 * list_move_tail - delete from one list and add as another's tail
159 * @list: the entry to move
160 * @head: the head that will follow our entry
162 static inline void list_move_tail(struct list_head *list,
163 struct list_head *head)
165 __list_del(list->prev, list->next);
166 list_add_tail(list, head);
170 * list_is_last - tests whether @list is the last entry in list @head
171 * @list: the entry to test
172 * @head: the head of the list
174 static inline int list_is_last(const struct list_head *list,
175 const struct list_head *head)
177 return list->next == head;
181 * list_empty - tests whether a list is empty
182 * @head: the list to test.
184 static inline int list_empty(const struct list_head *head)
186 return head->next == head;
190 * list_empty_careful - tests whether a list is empty and not being modified
191 * @head: the list to test
194 * tests whether a list is empty _and_ checks that no other CPU might be
195 * in the process of modifying either member (next or prev)
197 * NOTE: using list_empty_careful() without synchronization
198 * can only be safe if the only activity that can happen
199 * to the list entry is list_del_init(). Eg. it cannot be used
200 * if another CPU could re-list_add() it.
202 static inline int list_empty_careful(const struct list_head *head)
204 struct list_head *next = head->next;
205 return (next == head) && (next == head->prev);
208 static inline void __list_splice(struct list_head *list,
209 struct list_head *head)
211 struct list_head *first = list->next;
212 struct list_head *last = list->prev;
213 struct list_head *at = head->next;
223 * list_splice - join two lists
224 * @list: the new list to add.
225 * @head: the place to add it in the first list.
227 static inline void list_splice(struct list_head *list, struct list_head *head)
229 if (!list_empty(list))
230 __list_splice(list, head);
234 * list_splice_init - join two lists and reinitialise the emptied list.
235 * @list: the new list to add.
236 * @head: the place to add it in the first list.
238 * The list at @list is reinitialised
240 static inline void list_splice_init(struct list_head *list,
241 struct list_head *head)
243 if (!list_empty(list)) {
244 __list_splice(list, head);
245 INIT_LIST_HEAD(list);
250 * list_entry - get the struct for this entry
251 * @ptr: the &struct list_head pointer.
252 * @type: the type of the struct this is embedded in.
253 * @member: the name of the list_struct within the struct.
255 #define list_entry(ptr, type, member) \
256 container_of(ptr, type, member)
259 * list_for_each - iterate over a list
260 * @pos: the &struct list_head to use as a loop cursor.
261 * @head: the head for your list.
263 #define list_for_each(pos, head) \
264 for (pos = (head)->next; prefetch(pos->next), pos != (head); \
268 * __list_for_each - iterate over a list
269 * @pos: the &struct list_head to use as a loop cursor.
270 * @head: the head for your list.
272 * This variant differs from list_for_each() in that it's the
273 * simplest possible list iteration code, no prefetching is done.
274 * Use this for code that knows the list to be very short (empty
275 * or 1 entry) most of the time.
277 #define __list_for_each(pos, head) \
278 for (pos = (head)->next; pos != (head); pos = pos->next)
281 * list_for_each_prev - iterate over a list backwards
282 * @pos: the &struct list_head to use as a loop cursor.
283 * @head: the head for your list.
285 #define list_for_each_prev(pos, head) \
286 for (pos = (head)->prev; prefetch(pos->prev), pos != (head); \
290 * list_for_each_safe - iterate over a list safe against removal of list entry
291 * @pos: the &struct list_head to use as a loop cursor.
292 * @n: another &struct list_head to use as temporary storage
293 * @head: the head for your list.
295 #define list_for_each_safe(pos, n, head) \
296 for (pos = (head)->next, n = pos->next; pos != (head); \
297 pos = n, n = pos->next)
300 * list_for_each_entry - iterate over list of given type
301 * @pos: the type * to use as a loop cursor.
302 * @head: the head for your list.
303 * @member: the name of the list_struct within the struct.
305 #define list_for_each_entry(pos, head, member) \
306 for (pos = list_entry((head)->next, typeof(*pos), member); \
307 prefetch(pos->member.next), &pos->member != (head); \
308 pos = list_entry(pos->member.next, typeof(*pos), member))
311 * list_for_each_entry_reverse - iterate backwards over list of given type.
312 * @pos: the type * to use as a loop cursor.
313 * @head: the head for your list.
314 * @member: the name of the list_struct within the struct.
316 #define list_for_each_entry_reverse(pos, head, member) \
317 for (pos = list_entry((head)->prev, typeof(*pos), member); \
318 prefetch(pos->member.prev), &pos->member != (head); \
319 pos = list_entry(pos->member.prev, typeof(*pos), member))
322 * list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue
323 * @pos: the type * to use as a start point
324 * @head: the head of the list
325 * @member: the name of the list_struct within the struct.
327 * Prepares a pos entry for use as a start point in list_for_each_entry_continue.
329 #define list_prepare_entry(pos, head, member) \
330 ((pos) ? : list_entry(head, typeof(*pos), member))
333 * list_for_each_entry_continue - continue iteration over list of given type
334 * @pos: the type * to use as a loop cursor.
335 * @head: the head for your list.
336 * @member: the name of the list_struct within the struct.
338 * Continue to iterate over list of given type, continuing after
339 * the current position.
341 #define list_for_each_entry_continue(pos, head, member) \
342 for (pos = list_entry(pos->member.next, typeof(*pos), member); \
343 prefetch(pos->member.next), &pos->member != (head); \
344 pos = list_entry(pos->member.next, typeof(*pos), member))
347 * list_for_each_entry_from - iterate over list of given type from the current point
348 * @pos: the type * to use as a loop cursor.
349 * @head: the head for your list.
350 * @member: the name of the list_struct within the struct.
352 * Iterate over list of given type, continuing from current position.
354 #define list_for_each_entry_from(pos, head, member) \
355 for (; prefetch(pos->member.next), &pos->member != (head); \
356 pos = list_entry(pos->member.next, typeof(*pos), member))
359 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
360 * @pos: the type * to use as a loop cursor.
361 * @n: another type * to use as temporary storage
362 * @head: the head for your list.
363 * @member: the name of the list_struct within the struct.
365 #define list_for_each_entry_safe(pos, n, head, member) \
366 for (pos = list_entry((head)->next, typeof(*pos), member), \
367 n = list_entry(pos->member.next, typeof(*pos), member); \
368 &pos->member != (head); \
369 pos = n, n = list_entry(n->member.next, typeof(*n), member))
372 * list_for_each_entry_safe_continue
373 * @pos: the type * to use as a loop cursor.
374 * @n: another type * to use as temporary storage
375 * @head: the head for your list.
376 * @member: the name of the list_struct within the struct.
378 * Iterate over list of given type, continuing after current point,
379 * safe against removal of list entry.
381 #define list_for_each_entry_safe_continue(pos, n, head, member) \
382 for (pos = list_entry(pos->member.next, typeof(*pos), member), \
383 n = list_entry(pos->member.next, typeof(*pos), member); \
384 &pos->member != (head); \
385 pos = n, n = list_entry(n->member.next, typeof(*n), member))
388 * list_for_each_entry_safe_from
389 * @pos: the type * to use as a loop cursor.
390 * @n: another type * to use as temporary storage
391 * @head: the head for your list.
392 * @member: the name of the list_struct within the struct.
394 * Iterate over list of given type from current point, safe against
395 * removal of list entry.
397 #define list_for_each_entry_safe_from(pos, n, head, member) \
398 for (n = list_entry(pos->member.next, typeof(*pos), member); \
399 &pos->member != (head); \
400 pos = n, n = list_entry(n->member.next, typeof(*n), member))
403 * list_for_each_entry_safe_reverse
404 * @pos: the type * to use as a loop cursor.
405 * @n: another type * to use as temporary storage
406 * @head: the head for your list.
407 * @member: the name of the list_struct within the struct.
409 * Iterate backwards over list of given type, safe against removal
412 #define list_for_each_entry_safe_reverse(pos, n, head, member) \
413 for (pos = list_entry((head)->prev, typeof(*pos), member), \
414 n = list_entry(pos->member.prev, typeof(*pos), member); \
415 &pos->member != (head); \
416 pos = n, n = list_entry(n->member.prev, typeof(*n), member))