]>
git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - include/linux/list.h
4 #include <linux/types.h>
5 #include <linux/stddef.h>
6 #include <linux/poison.h>
7 #include <linux/const.h>
8 #include <linux/kernel.h>
11 * Simple doubly linked list implementation.
13 * Some of the internal functions ("__xxx") are useful when
14 * manipulating whole lists rather than single entries, as
15 * sometimes we already know the next/prev entries and we can
16 * generate better code by using them directly rather than
17 * using the generic single-entry routines.
20 #define LIST_HEAD_INIT(name) { &(name), &(name) }
22 #define LIST_HEAD(name) \
23 struct list_head name = LIST_HEAD_INIT(name)
25 static inline void INIT_LIST_HEAD(struct list_head
*list
)
27 WRITE_ONCE(list
->next
, list
);
31 #ifdef CONFIG_DEBUG_LIST
32 extern bool __list_add_valid(struct list_head
*new,
33 struct list_head
*prev
,
34 struct list_head
*next
);
36 static inline bool __list_add_valid(struct list_head
*new,
37 struct list_head
*prev
,
38 struct list_head
*next
)
45 * Insert a new entry between two known consecutive entries.
47 * This is only for internal list manipulation where we know
48 * the prev/next entries already!
50 static inline void __list_add(struct list_head
*new,
51 struct list_head
*prev
,
52 struct list_head
*next
)
54 if (!__list_add_valid(new, prev
, next
))
60 WRITE_ONCE(prev
->next
, new);
64 * list_add - add a new entry
65 * @new: new entry to be added
66 * @head: list head to add it after
68 * Insert a new entry after the specified head.
69 * This is good for implementing stacks.
71 static inline void list_add(struct list_head
*new, struct list_head
*head
)
73 __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
)
100 WRITE_ONCE(prev
->next
, 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 #ifndef CONFIG_DEBUG_LIST
110 static inline void __list_del_entry(struct list_head
*entry
)
112 __list_del(entry
->prev
, entry
->next
);
115 static inline void list_del(struct list_head
*entry
)
117 __list_del(entry
->prev
, entry
->next
);
118 entry
->next
= LIST_POISON1
;
119 entry
->prev
= LIST_POISON2
;
122 extern void __list_del_entry(struct list_head
*entry
);
123 extern void list_del(struct list_head
*entry
);
127 * list_replace - replace old entry by new one
128 * @old : the element to be replaced
129 * @new : the new element to insert
131 * If @old was empty, it will be overwritten.
133 static inline void list_replace(struct list_head
*old
,
134 struct list_head
*new)
136 new->next
= old
->next
;
137 new->next
->prev
= new;
138 new->prev
= old
->prev
;
139 new->prev
->next
= new;
142 static inline void list_replace_init(struct list_head
*old
,
143 struct list_head
*new)
145 list_replace(old
, new);
150 * list_del_init - deletes entry from list and reinitialize it.
151 * @entry: the element to delete from the list.
153 static inline void list_del_init(struct list_head
*entry
)
155 __list_del_entry(entry
);
156 INIT_LIST_HEAD(entry
);
160 * list_move - delete from one list and add as another's head
161 * @list: the entry to move
162 * @head: the head that will precede our entry
164 static inline void list_move(struct list_head
*list
, struct list_head
*head
)
166 __list_del_entry(list
);
167 list_add(list
, head
);
171 * list_move_tail - delete from one list and add as another's tail
172 * @list: the entry to move
173 * @head: the head that will follow our entry
175 static inline void list_move_tail(struct list_head
*list
,
176 struct list_head
*head
)
178 __list_del_entry(list
);
179 list_add_tail(list
, head
);
183 * list_is_last - tests whether @list is the last entry in list @head
184 * @list: the entry to test
185 * @head: the head of the list
187 static inline int list_is_last(const struct list_head
*list
,
188 const struct list_head
*head
)
190 return list
->next
== head
;
194 * list_empty - tests whether a list is empty
195 * @head: the list to test.
197 static inline int list_empty(const struct list_head
*head
)
199 return READ_ONCE(head
->next
) == head
;
203 * list_empty_careful - tests whether a list is empty and not being modified
204 * @head: the list to test
207 * tests whether a list is empty _and_ checks that no other CPU might be
208 * in the process of modifying either member (next or prev)
210 * NOTE: using list_empty_careful() without synchronization
211 * can only be safe if the only activity that can happen
212 * to the list entry is list_del_init(). Eg. it cannot be used
213 * if another CPU could re-list_add() it.
215 static inline int list_empty_careful(const struct list_head
*head
)
217 struct list_head
*next
= head
->next
;
218 return (next
== head
) && (next
== head
->prev
);
222 * list_rotate_left - rotate the list to the left
223 * @head: the head of the list
225 static inline void list_rotate_left(struct list_head
*head
)
227 struct list_head
*first
;
229 if (!list_empty(head
)) {
231 list_move_tail(first
, head
);
236 * list_is_singular - tests whether a list has just one entry.
237 * @head: the list to test.
239 static inline int list_is_singular(const struct list_head
*head
)
241 return !list_empty(head
) && (head
->next
== head
->prev
);
244 static inline void __list_cut_position(struct list_head
*list
,
245 struct list_head
*head
, struct list_head
*entry
)
247 struct list_head
*new_first
= entry
->next
;
248 list
->next
= head
->next
;
249 list
->next
->prev
= list
;
252 head
->next
= new_first
;
253 new_first
->prev
= head
;
257 * list_cut_position - cut a list into two
258 * @list: a new list to add all removed entries
259 * @head: a list with entries
260 * @entry: an entry within head, could be the head itself
261 * and if so we won't cut the list
263 * This helper moves the initial part of @head, up to and
264 * including @entry, from @head to @list. You should
265 * pass on @entry an element you know is on @head. @list
266 * should be an empty list or a list you do not care about
270 static inline void list_cut_position(struct list_head
*list
,
271 struct list_head
*head
, struct list_head
*entry
)
273 if (list_empty(head
))
275 if (list_is_singular(head
) &&
276 (head
->next
!= entry
&& head
!= entry
))
279 INIT_LIST_HEAD(list
);
281 __list_cut_position(list
, head
, entry
);
284 static inline void __list_splice(const struct list_head
*list
,
285 struct list_head
*prev
,
286 struct list_head
*next
)
288 struct list_head
*first
= list
->next
;
289 struct list_head
*last
= list
->prev
;
299 * list_splice - join two lists, this is designed for stacks
300 * @list: the new list to add.
301 * @head: the place to add it in the first list.
303 static inline void list_splice(const struct list_head
*list
,
304 struct list_head
*head
)
306 if (!list_empty(list
))
307 __list_splice(list
, head
, head
->next
);
311 * list_splice_tail - join two lists, each list being a queue
312 * @list: the new list to add.
313 * @head: the place to add it in the first list.
315 static inline void list_splice_tail(struct list_head
*list
,
316 struct list_head
*head
)
318 if (!list_empty(list
))
319 __list_splice(list
, head
->prev
, head
);
323 * list_splice_init - join two lists and reinitialise the emptied list.
324 * @list: the new list to add.
325 * @head: the place to add it in the first list.
327 * The list at @list is reinitialised
329 static inline void list_splice_init(struct list_head
*list
,
330 struct list_head
*head
)
332 if (!list_empty(list
)) {
333 __list_splice(list
, head
, head
->next
);
334 INIT_LIST_HEAD(list
);
339 * list_splice_tail_init - join two lists and reinitialise the emptied list
340 * @list: the new list to add.
341 * @head: the place to add it in the first list.
343 * Each of the lists is a queue.
344 * The list at @list is reinitialised
346 static inline void list_splice_tail_init(struct list_head
*list
,
347 struct list_head
*head
)
349 if (!list_empty(list
)) {
350 __list_splice(list
, head
->prev
, head
);
351 INIT_LIST_HEAD(list
);
356 * list_entry - get the struct for this entry
357 * @ptr: the &struct list_head pointer.
358 * @type: the type of the struct this is embedded in.
359 * @member: the name of the list_head within the struct.
361 #define list_entry(ptr, type, member) \
362 container_of(ptr, type, member)
365 * list_first_entry - get the first element from a list
366 * @ptr: the list head to take the element from.
367 * @type: the type of the struct this is embedded in.
368 * @member: the name of the list_head within the struct.
370 * Note, that list is expected to be not empty.
372 #define list_first_entry(ptr, type, member) \
373 list_entry((ptr)->next, type, member)
376 * list_last_entry - get the last element from a list
377 * @ptr: the list head to take the element from.
378 * @type: the type of the struct this is embedded in.
379 * @member: the name of the list_head within the struct.
381 * Note, that list is expected to be not empty.
383 #define list_last_entry(ptr, type, member) \
384 list_entry((ptr)->prev, type, member)
387 * list_first_entry_or_null - get the first element from a list
388 * @ptr: the list head to take the element from.
389 * @type: the type of the struct this is embedded in.
390 * @member: the name of the list_head within the struct.
392 * Note that if the list is empty, it returns NULL.
394 #define list_first_entry_or_null(ptr, type, member) ({ \
395 struct list_head *head__ = (ptr); \
396 struct list_head *pos__ = READ_ONCE(head__->next); \
397 pos__ != head__ ? list_entry(pos__, type, member) : NULL; \
401 * list_next_entry - get the next element in list
402 * @pos: the type * to cursor
403 * @member: the name of the list_head within the struct.
405 #define list_next_entry(pos, member) \
406 list_entry((pos)->member.next, typeof(*(pos)), member)
409 * list_prev_entry - get the prev element in list
410 * @pos: the type * to cursor
411 * @member: the name of the list_head within the struct.
413 #define list_prev_entry(pos, member) \
414 list_entry((pos)->member.prev, typeof(*(pos)), member)
417 * list_for_each - iterate over a list
418 * @pos: the &struct list_head to use as a loop cursor.
419 * @head: the head for your list.
421 #define list_for_each(pos, head) \
422 for (pos = (head)->next; pos != (head); pos = pos->next)
425 * list_for_each_prev - iterate over a list backwards
426 * @pos: the &struct list_head to use as a loop cursor.
427 * @head: the head for your list.
429 #define list_for_each_prev(pos, head) \
430 for (pos = (head)->prev; pos != (head); pos = pos->prev)
433 * list_for_each_safe - iterate over a list safe against removal of list entry
434 * @pos: the &struct list_head to use as a loop cursor.
435 * @n: another &struct list_head to use as temporary storage
436 * @head: the head for your list.
438 #define list_for_each_safe(pos, n, head) \
439 for (pos = (head)->next, n = pos->next; pos != (head); \
440 pos = n, n = pos->next)
443 * list_for_each_prev_safe - iterate over a list backwards safe against removal of list entry
444 * @pos: the &struct list_head to use as a loop cursor.
445 * @n: another &struct list_head to use as temporary storage
446 * @head: the head for your list.
448 #define list_for_each_prev_safe(pos, n, head) \
449 for (pos = (head)->prev, n = pos->prev; \
451 pos = n, n = pos->prev)
454 * list_for_each_entry - iterate over list of given type
455 * @pos: the type * to use as a loop cursor.
456 * @head: the head for your list.
457 * @member: the name of the list_head within the struct.
459 #define list_for_each_entry(pos, head, member) \
460 for (pos = list_first_entry(head, typeof(*pos), member); \
461 &pos->member != (head); \
462 pos = list_next_entry(pos, member))
465 * list_for_each_entry_reverse - iterate backwards over list of given type.
466 * @pos: the type * to use as a loop cursor.
467 * @head: the head for your list.
468 * @member: the name of the list_head within the struct.
470 #define list_for_each_entry_reverse(pos, head, member) \
471 for (pos = list_last_entry(head, typeof(*pos), member); \
472 &pos->member != (head); \
473 pos = list_prev_entry(pos, member))
476 * list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue()
477 * @pos: the type * to use as a start point
478 * @head: the head of the list
479 * @member: the name of the list_head within the struct.
481 * Prepares a pos entry for use as a start point in list_for_each_entry_continue().
483 #define list_prepare_entry(pos, head, member) \
484 ((pos) ? : list_entry(head, typeof(*pos), member))
487 * list_for_each_entry_continue - continue iteration over list of given type
488 * @pos: the type * to use as a loop cursor.
489 * @head: the head for your list.
490 * @member: the name of the list_head within the struct.
492 * Continue to iterate over list of given type, continuing after
493 * the current position.
495 #define list_for_each_entry_continue(pos, head, member) \
496 for (pos = list_next_entry(pos, member); \
497 &pos->member != (head); \
498 pos = list_next_entry(pos, member))
501 * list_for_each_entry_continue_reverse - iterate backwards from the given point
502 * @pos: the type * to use as a loop cursor.
503 * @head: the head for your list.
504 * @member: the name of the list_head within the struct.
506 * Start to iterate over list of given type backwards, continuing after
507 * the current position.
509 #define list_for_each_entry_continue_reverse(pos, head, member) \
510 for (pos = list_prev_entry(pos, member); \
511 &pos->member != (head); \
512 pos = list_prev_entry(pos, member))
515 * list_for_each_entry_from - iterate over list of given type from the current point
516 * @pos: the type * to use as a loop cursor.
517 * @head: the head for your list.
518 * @member: the name of the list_head within the struct.
520 * Iterate over list of given type, continuing from current position.
522 #define list_for_each_entry_from(pos, head, member) \
523 for (; &pos->member != (head); \
524 pos = list_next_entry(pos, member))
527 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
528 * @pos: the type * to use as a loop cursor.
529 * @n: another type * to use as temporary storage
530 * @head: the head for your list.
531 * @member: the name of the list_head within the struct.
533 #define list_for_each_entry_safe(pos, n, head, member) \
534 for (pos = list_first_entry(head, typeof(*pos), member), \
535 n = list_next_entry(pos, member); \
536 &pos->member != (head); \
537 pos = n, n = list_next_entry(n, member))
540 * list_for_each_entry_safe_continue - continue list iteration safe against removal
541 * @pos: the type * to use as a loop cursor.
542 * @n: another type * to use as temporary storage
543 * @head: the head for your list.
544 * @member: the name of the list_head within the struct.
546 * Iterate over list of given type, continuing after current point,
547 * safe against removal of list entry.
549 #define list_for_each_entry_safe_continue(pos, n, head, member) \
550 for (pos = list_next_entry(pos, member), \
551 n = list_next_entry(pos, member); \
552 &pos->member != (head); \
553 pos = n, n = list_next_entry(n, member))
556 * list_for_each_entry_safe_from - iterate over list from current point safe against removal
557 * @pos: the type * to use as a loop cursor.
558 * @n: another type * to use as temporary storage
559 * @head: the head for your list.
560 * @member: the name of the list_head within the struct.
562 * Iterate over list of given type from current point, safe against
563 * removal of list entry.
565 #define list_for_each_entry_safe_from(pos, n, head, member) \
566 for (n = list_next_entry(pos, member); \
567 &pos->member != (head); \
568 pos = n, n = list_next_entry(n, member))
571 * list_for_each_entry_safe_reverse - iterate backwards over list safe against removal
572 * @pos: the type * to use as a loop cursor.
573 * @n: another type * to use as temporary storage
574 * @head: the head for your list.
575 * @member: the name of the list_head within the struct.
577 * Iterate backwards over list of given type, safe against removal
580 #define list_for_each_entry_safe_reverse(pos, n, head, member) \
581 for (pos = list_last_entry(head, typeof(*pos), member), \
582 n = list_prev_entry(pos, member); \
583 &pos->member != (head); \
584 pos = n, n = list_prev_entry(n, member))
587 * list_safe_reset_next - reset a stale list_for_each_entry_safe loop
588 * @pos: the loop cursor used in the list_for_each_entry_safe loop
589 * @n: temporary storage used in list_for_each_entry_safe
590 * @member: the name of the list_head within the struct.
592 * list_safe_reset_next is not safe to use in general if the list may be
593 * modified concurrently (eg. the lock is dropped in the loop body). An
594 * exception to this is if the cursor element (pos) is pinned in the list,
595 * and list_safe_reset_next is called after re-taking the lock and before
596 * completing the current iteration of the loop body.
598 #define list_safe_reset_next(pos, n, member) \
599 n = list_next_entry(pos, member)
602 * Double linked lists with a single pointer list head.
603 * Mostly useful for hash tables where the two pointer list head is
605 * You lose the ability to access the tail in O(1).
608 #define HLIST_HEAD_INIT { .first = NULL }
609 #define HLIST_HEAD(name) struct hlist_head name = { .first = NULL }
610 #define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
611 static inline void INIT_HLIST_NODE(struct hlist_node
*h
)
617 static inline int hlist_unhashed(const struct hlist_node
*h
)
622 static inline int hlist_empty(const struct hlist_head
*h
)
624 return !READ_ONCE(h
->first
);
627 static inline void __hlist_del(struct hlist_node
*n
)
629 struct hlist_node
*next
= n
->next
;
630 struct hlist_node
**pprev
= n
->pprev
;
632 WRITE_ONCE(*pprev
, next
);
637 static inline void hlist_del(struct hlist_node
*n
)
640 n
->next
= LIST_POISON1
;
641 n
->pprev
= LIST_POISON2
;
644 static inline void hlist_del_init(struct hlist_node
*n
)
646 if (!hlist_unhashed(n
)) {
652 static inline void hlist_add_head(struct hlist_node
*n
, struct hlist_head
*h
)
654 struct hlist_node
*first
= h
->first
;
657 first
->pprev
= &n
->next
;
658 WRITE_ONCE(h
->first
, n
);
659 n
->pprev
= &h
->first
;
662 /* next must be != NULL */
663 static inline void hlist_add_before(struct hlist_node
*n
,
664 struct hlist_node
*next
)
666 n
->pprev
= next
->pprev
;
668 next
->pprev
= &n
->next
;
669 WRITE_ONCE(*(n
->pprev
), n
);
672 static inline void hlist_add_behind(struct hlist_node
*n
,
673 struct hlist_node
*prev
)
675 n
->next
= prev
->next
;
676 WRITE_ONCE(prev
->next
, n
);
677 n
->pprev
= &prev
->next
;
680 n
->next
->pprev
= &n
->next
;
683 /* after that we'll appear to be on some hlist and hlist_del will work */
684 static inline void hlist_add_fake(struct hlist_node
*n
)
689 static inline bool hlist_fake(struct hlist_node
*h
)
691 return h
->pprev
== &h
->next
;
695 * Check whether the node is the only node of the head without
699 hlist_is_singular_node(struct hlist_node
*n
, struct hlist_head
*h
)
701 return !n
->next
&& n
->pprev
== &h
->first
;
705 * Move a list from one list head to another. Fixup the pprev
706 * reference of the first entry if it exists.
708 static inline void hlist_move_list(struct hlist_head
*old
,
709 struct hlist_head
*new)
711 new->first
= old
->first
;
713 new->first
->pprev
= &new->first
;
717 #define hlist_entry(ptr, type, member) container_of(ptr,type,member)
719 #define hlist_for_each(pos, head) \
720 for (pos = (head)->first; pos ; pos = pos->next)
722 #define hlist_for_each_safe(pos, n, head) \
723 for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
726 #define hlist_entry_safe(ptr, type, member) \
727 ({ typeof(ptr) ____ptr = (ptr); \
728 ____ptr ? hlist_entry(____ptr, type, member) : NULL; \
732 * hlist_for_each_entry - iterate over list of given type
733 * @pos: the type * to use as a loop cursor.
734 * @head: the head for your list.
735 * @member: the name of the hlist_node within the struct.
737 #define hlist_for_each_entry(pos, head, member) \
738 for (pos = hlist_entry_safe((head)->first, typeof(*(pos)), member);\
740 pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
743 * hlist_for_each_entry_continue - iterate over a hlist continuing after current point
744 * @pos: the type * to use as a loop cursor.
745 * @member: the name of the hlist_node within the struct.
747 #define hlist_for_each_entry_continue(pos, member) \
748 for (pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member);\
750 pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
753 * hlist_for_each_entry_from - iterate over a hlist continuing from current point
754 * @pos: the type * to use as a loop cursor.
755 * @member: the name of the hlist_node within the struct.
757 #define hlist_for_each_entry_from(pos, member) \
759 pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
762 * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
763 * @pos: the type * to use as a loop cursor.
764 * @n: another &struct hlist_node to use as temporary storage
765 * @head: the head for your list.
766 * @member: the name of the hlist_node within the struct.
768 #define hlist_for_each_entry_safe(pos, n, head, member) \
769 for (pos = hlist_entry_safe((head)->first, typeof(*pos), member);\
770 pos && ({ n = pos->member.next; 1; }); \
771 pos = hlist_entry_safe(n, typeof(*pos), member))