4 #include <linux/stddef.h>
5 #include <linux/poison.h>
6 #include <linux/prefetch.h>
7 #include <asm/system.h>
10 * Simple doubly linked list implementation.
12 * Some of the internal functions ("__xxx") are useful when
13 * manipulating whole lists rather than single entries, as
14 * sometimes we already know the next/prev entries and we can
15 * generate better code by using them directly rather than
16 * using the generic single-entry routines.
20 struct list_head *next, *prev;
23 #define LIST_HEAD_INIT(name) { &(name), &(name) }
25 #define LIST_HEAD(name) \
26 struct list_head name = LIST_HEAD_INIT(name)
28 static inline void INIT_LIST_HEAD(struct list_head *list)
35 * Insert a new entry between two known consecutive entries.
37 * This is only for internal list manipulation where we know
38 * the prev/next entries already!
40 #ifndef CONFIG_DEBUG_LIST
41 static inline void __list_add(struct list_head *new,
42 struct list_head *prev,
43 struct list_head *next)
51 extern void __list_add(struct list_head *new,
52 struct list_head *prev,
53 struct list_head *next);
57 * list_add - add a new entry
58 * @new: new entry to be added
59 * @head: list head to add it after
61 * Insert a new entry after the specified head.
62 * This is good for implementing stacks.
64 #ifndef CONFIG_DEBUG_LIST
65 static inline void list_add(struct list_head *new, struct list_head *head)
67 __list_add(new, head, head->next);
70 extern void list_add(struct list_head *new, struct list_head *head);
75 * list_add_tail - add a new entry
76 * @new: new entry to be added
77 * @head: list head to add it before
79 * Insert a new entry before the specified head.
80 * This is useful for implementing queues.
82 static inline void list_add_tail(struct list_head *new, struct list_head *head)
84 __list_add(new, head->prev, head);
88 * Delete a list entry by making the prev/next entries
89 * point to each other.
91 * This is only for internal list manipulation where we know
92 * the prev/next entries already!
94 static inline void __list_del(struct list_head * prev, struct list_head * next)
101 * list_del - deletes entry from list.
102 * @entry: the element to delete from the list.
103 * Note: list_empty() on entry does not return true after this, the entry is
104 * in an undefined state.
106 #ifndef CONFIG_DEBUG_LIST
107 static inline void list_del(struct list_head *entry)
109 __list_del(entry->prev, entry->next);
110 entry->next = LIST_POISON1;
111 entry->prev = LIST_POISON2;
114 extern void list_del(struct list_head *entry);
118 * list_replace - replace old entry by new one
119 * @old : the element to be replaced
120 * @new : the new element to insert
122 * If @old was empty, it will be overwritten.
124 static inline void list_replace(struct list_head *old,
125 struct list_head *new)
127 new->next = old->next;
128 new->next->prev = new;
129 new->prev = old->prev;
130 new->prev->next = new;
133 static inline void list_replace_init(struct list_head *old,
134 struct list_head *new)
136 list_replace(old, new);
141 * list_del_init - deletes entry from list and reinitialize it.
142 * @entry: the element to delete from the list.
144 static inline void list_del_init(struct list_head *entry)
146 __list_del(entry->prev, entry->next);
147 INIT_LIST_HEAD(entry);
151 * list_move - delete from one list and add as another's head
152 * @list: the entry to move
153 * @head: the head that will precede our entry
155 static inline void list_move(struct list_head *list, struct list_head *head)
157 __list_del(list->prev, list->next);
158 list_add(list, head);
162 * list_move_tail - delete from one list and add as another's tail
163 * @list: the entry to move
164 * @head: the head that will follow our entry
166 static inline void list_move_tail(struct list_head *list,
167 struct list_head *head)
169 __list_del(list->prev, list->next);
170 list_add_tail(list, head);
174 * list_is_last - tests whether @list is the last entry in list @head
175 * @list: the entry to test
176 * @head: the head of the list
178 static inline int list_is_last(const struct list_head *list,
179 const struct list_head *head)
181 return list->next == head;
185 * list_empty - tests whether a list is empty
186 * @head: the list to test.
188 static inline int list_empty(const struct list_head *head)
190 return head->next == head;
194 * list_empty_careful - tests whether a list is empty and not being modified
195 * @head: the list to test
198 * tests whether a list is empty _and_ checks that no other CPU might be
199 * in the process of modifying either member (next or prev)
201 * NOTE: using list_empty_careful() without synchronization
202 * can only be safe if the only activity that can happen
203 * to the list entry is list_del_init(). Eg. it cannot be used
204 * if another CPU could re-list_add() it.
206 static inline int list_empty_careful(const struct list_head *head)
208 struct list_head *next = head->next;
209 return (next == head) && (next == head->prev);
213 * list_is_singular - tests whether a list has just one entry.
214 * @head: the list to test.
216 static inline int list_is_singular(const struct list_head *head)
218 return !list_empty(head) && (head->next == head->prev);
221 static inline void __list_splice(const struct list_head *list,
222 struct list_head *head)
224 struct list_head *first = list->next;
225 struct list_head *last = list->prev;
226 struct list_head *at = head->next;
236 * list_splice - join two lists
237 * @list: the new list to add.
238 * @head: the place to add it in the first list.
240 static inline void list_splice(const struct list_head *list,
241 struct list_head *head)
243 if (!list_empty(list))
244 __list_splice(list, head);
248 * list_splice_init - join two lists and reinitialise the emptied list.
249 * @list: the new list to add.
250 * @head: the place to add it in the first list.
252 * The list at @list is reinitialised
254 static inline void list_splice_init(struct list_head *list,
255 struct list_head *head)
257 if (!list_empty(list)) {
258 __list_splice(list, head);
259 INIT_LIST_HEAD(list);
264 * list_entry - get the struct for this entry
265 * @ptr: the &struct list_head pointer.
266 * @type: the type of the struct this is embedded in.
267 * @member: the name of the list_struct within the struct.
269 #define list_entry(ptr, type, member) \
270 container_of(ptr, type, member)
273 * list_first_entry - get the first element from a list
274 * @ptr: the list head to take the element from.
275 * @type: the type of the struct this is embedded in.
276 * @member: the name of the list_struct within the struct.
278 * Note, that list is expected to be not empty.
280 #define list_first_entry(ptr, type, member) \
281 list_entry((ptr)->next, type, member)
284 * list_for_each - iterate over a list
285 * @pos: the &struct list_head to use as a loop cursor.
286 * @head: the head for your list.
288 #define list_for_each(pos, head) \
289 for (pos = (head)->next; prefetch(pos->next), pos != (head); \
293 * __list_for_each - iterate over a list
294 * @pos: the &struct list_head to use as a loop cursor.
295 * @head: the head for your list.
297 * This variant differs from list_for_each() in that it's the
298 * simplest possible list iteration code, no prefetching is done.
299 * Use this for code that knows the list to be very short (empty
300 * or 1 entry) most of the time.
302 #define __list_for_each(pos, head) \
303 for (pos = (head)->next; pos != (head); pos = pos->next)
306 * list_for_each_prev - iterate over a list backwards
307 * @pos: the &struct list_head to use as a loop cursor.
308 * @head: the head for your list.
310 #define list_for_each_prev(pos, head) \
311 for (pos = (head)->prev; prefetch(pos->prev), pos != (head); \
315 * list_for_each_safe - iterate over a list safe against removal of list entry
316 * @pos: the &struct list_head to use as a loop cursor.
317 * @n: another &struct list_head to use as temporary storage
318 * @head: the head for your list.
320 #define list_for_each_safe(pos, n, head) \
321 for (pos = (head)->next, n = pos->next; pos != (head); \
322 pos = n, n = pos->next)
325 * list_for_each_prev_safe - iterate over a list backwards safe against removal of list entry
326 * @pos: the &struct list_head to use as a loop cursor.
327 * @n: another &struct list_head to use as temporary storage
328 * @head: the head for your list.
330 #define list_for_each_prev_safe(pos, n, head) \
331 for (pos = (head)->prev, n = pos->prev; \
332 prefetch(pos->prev), pos != (head); \
333 pos = n, n = pos->prev)
336 * list_for_each_entry - iterate over list of given type
337 * @pos: the type * to use as a loop cursor.
338 * @head: the head for your list.
339 * @member: the name of the list_struct within the struct.
341 #define list_for_each_entry(pos, head, member) \
342 for (pos = list_entry((head)->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_reverse - iterate backwards over list of given type.
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 #define list_for_each_entry_reverse(pos, head, member) \
353 for (pos = list_entry((head)->prev, typeof(*pos), member); \
354 prefetch(pos->member.prev), &pos->member != (head); \
355 pos = list_entry(pos->member.prev, typeof(*pos), member))
358 * list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue()
359 * @pos: the type * to use as a start point
360 * @head: the head of the list
361 * @member: the name of the list_struct within the struct.
363 * Prepares a pos entry for use as a start point in list_for_each_entry_continue().
365 #define list_prepare_entry(pos, head, member) \
366 ((pos) ? : list_entry(head, typeof(*pos), member))
369 * list_for_each_entry_continue - continue iteration over list of given type
370 * @pos: the type * to use as a loop cursor.
371 * @head: the head for your list.
372 * @member: the name of the list_struct within the struct.
374 * Continue to iterate over list of given type, continuing after
375 * the current position.
377 #define list_for_each_entry_continue(pos, head, member) \
378 for (pos = list_entry(pos->member.next, typeof(*pos), member); \
379 prefetch(pos->member.next), &pos->member != (head); \
380 pos = list_entry(pos->member.next, typeof(*pos), member))
383 * list_for_each_entry_continue_reverse - iterate backwards from the given point
384 * @pos: the type * to use as a loop cursor.
385 * @head: the head for your list.
386 * @member: the name of the list_struct within the struct.
388 * Start to iterate over list of given type backwards, continuing after
389 * the current position.
391 #define list_for_each_entry_continue_reverse(pos, head, member) \
392 for (pos = list_entry(pos->member.prev, typeof(*pos), member); \
393 prefetch(pos->member.prev), &pos->member != (head); \
394 pos = list_entry(pos->member.prev, typeof(*pos), member))
397 * list_for_each_entry_from - iterate over list of given type from the current point
398 * @pos: the type * to use as a loop cursor.
399 * @head: the head for your list.
400 * @member: the name of the list_struct within the struct.
402 * Iterate over list of given type, continuing from current position.
404 #define list_for_each_entry_from(pos, head, member) \
405 for (; prefetch(pos->member.next), &pos->member != (head); \
406 pos = list_entry(pos->member.next, typeof(*pos), member))
409 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
410 * @pos: the type * to use as a loop cursor.
411 * @n: another type * to use as temporary storage
412 * @head: the head for your list.
413 * @member: the name of the list_struct within the struct.
415 #define list_for_each_entry_safe(pos, n, head, member) \
416 for (pos = list_entry((head)->next, typeof(*pos), member), \
417 n = list_entry(pos->member.next, typeof(*pos), member); \
418 &pos->member != (head); \
419 pos = n, n = list_entry(n->member.next, typeof(*n), member))
422 * list_for_each_entry_safe_continue
423 * @pos: the type * to use as a loop cursor.
424 * @n: another type * to use as temporary storage
425 * @head: the head for your list.
426 * @member: the name of the list_struct within the struct.
428 * Iterate over list of given type, continuing after current point,
429 * safe against removal of list entry.
431 #define list_for_each_entry_safe_continue(pos, n, head, member) \
432 for (pos = list_entry(pos->member.next, typeof(*pos), member), \
433 n = list_entry(pos->member.next, typeof(*pos), member); \
434 &pos->member != (head); \
435 pos = n, n = list_entry(n->member.next, typeof(*n), member))
438 * list_for_each_entry_safe_from
439 * @pos: the type * to use as a loop cursor.
440 * @n: another type * to use as temporary storage
441 * @head: the head for your list.
442 * @member: the name of the list_struct within the struct.
444 * Iterate over list of given type from current point, safe against
445 * removal of list entry.
447 #define list_for_each_entry_safe_from(pos, n, head, member) \
448 for (n = list_entry(pos->member.next, typeof(*pos), member); \
449 &pos->member != (head); \
450 pos = n, n = list_entry(n->member.next, typeof(*n), member))
453 * list_for_each_entry_safe_reverse
454 * @pos: the type * to use as a loop cursor.
455 * @n: another type * to use as temporary storage
456 * @head: the head for your list.
457 * @member: the name of the list_struct within the struct.
459 * Iterate backwards over list of given type, safe against removal
462 #define list_for_each_entry_safe_reverse(pos, n, head, member) \
463 for (pos = list_entry((head)->prev, typeof(*pos), member), \
464 n = list_entry(pos->member.prev, typeof(*pos), member); \
465 &pos->member != (head); \
466 pos = n, n = list_entry(n->member.prev, typeof(*n), member))
469 * Double linked lists with a single pointer list head.
470 * Mostly useful for hash tables where the two pointer list head is
472 * You lose the ability to access the tail in O(1).
476 struct hlist_node *first;
480 struct hlist_node *next, **pprev;
483 #define HLIST_HEAD_INIT { .first = NULL }
484 #define HLIST_HEAD(name) struct hlist_head name = { .first = NULL }
485 #define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
486 static inline void INIT_HLIST_NODE(struct hlist_node *h)
492 static inline int hlist_unhashed(const struct hlist_node *h)
497 static inline int hlist_empty(const struct hlist_head *h)
502 static inline void __hlist_del(struct hlist_node *n)
504 struct hlist_node *next = n->next;
505 struct hlist_node **pprev = n->pprev;
511 static inline void hlist_del(struct hlist_node *n)
514 n->next = LIST_POISON1;
515 n->pprev = LIST_POISON2;
518 static inline void hlist_del_init(struct hlist_node *n)
520 if (!hlist_unhashed(n)) {
526 static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
528 struct hlist_node *first = h->first;
531 first->pprev = &n->next;
533 n->pprev = &h->first;
536 /* next must be != NULL */
537 static inline void hlist_add_before(struct hlist_node *n,
538 struct hlist_node *next)
540 n->pprev = next->pprev;
542 next->pprev = &n->next;
546 static inline void hlist_add_after(struct hlist_node *n,
547 struct hlist_node *next)
549 next->next = n->next;
551 next->pprev = &n->next;
554 next->next->pprev = &next->next;
557 #define hlist_entry(ptr, type, member) container_of(ptr,type,member)
559 #define hlist_for_each(pos, head) \
560 for (pos = (head)->first; pos && ({ prefetch(pos->next); 1; }); \
563 #define hlist_for_each_safe(pos, n, head) \
564 for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
568 * hlist_for_each_entry - iterate over list of given type
569 * @tpos: the type * to use as a loop cursor.
570 * @pos: the &struct hlist_node to use as a loop cursor.
571 * @head: the head for your list.
572 * @member: the name of the hlist_node within the struct.
574 #define hlist_for_each_entry(tpos, pos, head, member) \
575 for (pos = (head)->first; \
576 pos && ({ prefetch(pos->next); 1;}) && \
577 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
581 * hlist_for_each_entry_continue - iterate over a hlist continuing after current point
582 * @tpos: the type * to use as a loop cursor.
583 * @pos: the &struct hlist_node to use as a loop cursor.
584 * @member: the name of the hlist_node within the struct.
586 #define hlist_for_each_entry_continue(tpos, pos, member) \
587 for (pos = (pos)->next; \
588 pos && ({ prefetch(pos->next); 1;}) && \
589 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
593 * hlist_for_each_entry_from - iterate over a hlist continuing from current point
594 * @tpos: the type * to use as a loop cursor.
595 * @pos: the &struct hlist_node to use as a loop cursor.
596 * @member: the name of the hlist_node within the struct.
598 #define hlist_for_each_entry_from(tpos, pos, member) \
599 for (; pos && ({ prefetch(pos->next); 1;}) && \
600 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
604 * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
605 * @tpos: the type * to use as a loop cursor.
606 * @pos: the &struct hlist_node to use as a loop cursor.
607 * @n: another &struct hlist_node to use as temporary storage
608 * @head: the head for your list.
609 * @member: the name of the hlist_node within the struct.
611 #define hlist_for_each_entry_safe(tpos, pos, n, head, member) \
612 for (pos = (head)->first; \
613 pos && ({ n = pos->next; 1; }) && \
614 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \