4 #include <linux/types.h>
5 #include <linux/stddef.h>
6 #include <linux/poison.h>
7 #include <linux/prefetch.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.
19 #define LIST_HEAD_INIT(name) { &(name), &(name) }
21 #define LIST_HEAD(name) \
22 struct list_head name = LIST_HEAD_INIT(name)
24 static inline void INIT_LIST_HEAD(struct list_head *list)
31 * Insert a new entry between two known consecutive entries.
33 * This is only for internal list manipulation where we know
34 * the prev/next entries already!
36 #ifndef CONFIG_DEBUG_LIST
37 static inline void __list_add(struct list_head *new,
38 struct list_head *prev,
39 struct list_head *next)
47 extern void __list_add(struct list_head *new,
48 struct list_head *prev,
49 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);
67 * list_add_tail - add a new entry
68 * @new: new entry to be added
69 * @head: list head to add it before
71 * Insert a new entry before the specified head.
72 * This is useful for implementing queues.
74 static inline void list_add_tail(struct list_head *new, struct list_head *head)
76 __list_add(new, head->prev, head);
80 * Delete a list entry by making the prev/next entries
81 * point to each other.
83 * This is only for internal list manipulation where we know
84 * the prev/next entries already!
86 static inline void __list_del(struct list_head * prev, struct list_head * next)
93 * list_del - deletes entry from list.
94 * @entry: the element to delete from the list.
95 * Note: list_empty() on entry does not return true after this, the entry is
96 * in an undefined state.
98 #ifndef CONFIG_DEBUG_LIST
99 static inline void list_del(struct list_head *entry)
101 __list_del(entry->prev, entry->next);
102 entry->next = LIST_POISON1;
103 entry->prev = LIST_POISON2;
106 extern void list_del(struct list_head *entry);
110 * list_replace - replace old entry by new one
111 * @old : the element to be replaced
112 * @new : the new element to insert
114 * If @old was empty, it will be overwritten.
116 static inline void list_replace(struct list_head *old,
117 struct list_head *new)
119 new->next = old->next;
120 new->next->prev = new;
121 new->prev = old->prev;
122 new->prev->next = new;
125 static inline void list_replace_init(struct list_head *old,
126 struct list_head *new)
128 list_replace(old, new);
133 * list_del_init - deletes entry from list and reinitialize it.
134 * @entry: the element to delete from the list.
136 static inline void list_del_init(struct list_head *entry)
138 __list_del(entry->prev, entry->next);
139 INIT_LIST_HEAD(entry);
143 * list_move - delete from one list and add as another's head
144 * @list: the entry to move
145 * @head: the head that will precede our entry
147 static inline void list_move(struct list_head *list, struct list_head *head)
149 __list_del(list->prev, list->next);
150 list_add(list, head);
154 * list_move_tail - delete from one list and add as another's tail
155 * @list: the entry to move
156 * @head: the head that will follow our entry
158 static inline void list_move_tail(struct list_head *list,
159 struct list_head *head)
161 __list_del(list->prev, list->next);
162 list_add_tail(list, head);
166 * list_is_last - tests whether @list is the last entry in list @head
167 * @list: the entry to test
168 * @head: the head of the list
170 static inline int list_is_last(const struct list_head *list,
171 const struct list_head *head)
173 return list->next == head;
177 * list_empty - tests whether a list is empty
178 * @head: the list to test.
180 static inline int list_empty(const struct list_head *head)
182 return head->next == head;
186 * list_empty_careful - tests whether a list is empty and not being modified
187 * @head: the list to test
190 * tests whether a list is empty _and_ checks that no other CPU might be
191 * in the process of modifying either member (next or prev)
193 * NOTE: using list_empty_careful() without synchronization
194 * can only be safe if the only activity that can happen
195 * to the list entry is list_del_init(). Eg. it cannot be used
196 * if another CPU could re-list_add() it.
198 static inline int list_empty_careful(const struct list_head *head)
200 struct list_head *next = head->next;
201 return (next == head) && (next == head->prev);
205 * list_rotate_left - rotate the list to the left
206 * @head: the head of the list
208 static inline void list_rotate_left(struct list_head *head)
210 struct list_head *first;
212 if (!list_empty(head)) {
214 list_move_tail(first, head);
219 * list_is_singular - tests whether a list has just one entry.
220 * @head: the list to test.
222 static inline int list_is_singular(const struct list_head *head)
224 return !list_empty(head) && (head->next == head->prev);
227 static inline void __list_cut_position(struct list_head *list,
228 struct list_head *head, struct list_head *entry)
230 struct list_head *new_first = entry->next;
231 list->next = head->next;
232 list->next->prev = list;
235 head->next = new_first;
236 new_first->prev = head;
240 * list_cut_position - cut a list into two
241 * @list: a new list to add all removed entries
242 * @head: a list with entries
243 * @entry: an entry within head, could be the head itself
244 * and if so we won't cut the list
246 * This helper moves the initial part of @head, up to and
247 * including @entry, from @head to @list. You should
248 * pass on @entry an element you know is on @head. @list
249 * should be an empty list or a list you do not care about
253 static inline void list_cut_position(struct list_head *list,
254 struct list_head *head, struct list_head *entry)
256 if (list_empty(head))
258 if (list_is_singular(head) &&
259 (head->next != entry && head != entry))
262 INIT_LIST_HEAD(list);
264 __list_cut_position(list, head, entry);
267 static inline void __list_splice(const struct list_head *list,
268 struct list_head *prev,
269 struct list_head *next)
271 struct list_head *first = list->next;
272 struct list_head *last = list->prev;
282 * list_splice - join two lists, this is designed for stacks
283 * @list: the new list to add.
284 * @head: the place to add it in the first list.
286 static inline void list_splice(const struct list_head *list,
287 struct list_head *head)
289 if (!list_empty(list))
290 __list_splice(list, head, head->next);
294 * list_splice_tail - join two lists, each list being a queue
295 * @list: the new list to add.
296 * @head: the place to add it in the first list.
298 static inline void list_splice_tail(struct list_head *list,
299 struct list_head *head)
301 if (!list_empty(list))
302 __list_splice(list, head->prev, head);
306 * list_splice_init - join two lists and reinitialise the emptied list.
307 * @list: the new list to add.
308 * @head: the place to add it in the first list.
310 * The list at @list is reinitialised
312 static inline void list_splice_init(struct list_head *list,
313 struct list_head *head)
315 if (!list_empty(list)) {
316 __list_splice(list, head, head->next);
317 INIT_LIST_HEAD(list);
322 * list_splice_tail_init - join two lists and reinitialise the emptied list
323 * @list: the new list to add.
324 * @head: the place to add it in the first list.
326 * Each of the lists is a queue.
327 * The list at @list is reinitialised
329 static inline void list_splice_tail_init(struct list_head *list,
330 struct list_head *head)
332 if (!list_empty(list)) {
333 __list_splice(list, head->prev, head);
334 INIT_LIST_HEAD(list);
339 * list_entry - get the struct for this entry
340 * @ptr: the &struct list_head pointer.
341 * @type: the type of the struct this is embedded in.
342 * @member: the name of the list_struct within the struct.
344 #define list_entry(ptr, type, member) \
345 container_of(ptr, type, member)
348 * list_first_entry - get the first element from a list
349 * @ptr: the list head to take the element from.
350 * @type: the type of the struct this is embedded in.
351 * @member: the name of the list_struct within the struct.
353 * Note, that list is expected to be not empty.
355 #define list_first_entry(ptr, type, member) \
356 list_entry((ptr)->next, type, member)
359 * list_for_each - iterate over a list
360 * @pos: the &struct list_head to use as a loop cursor.
361 * @head: the head for your list.
363 #define list_for_each(pos, head) \
364 for (pos = (head)->next; prefetch(pos->next), pos != (head); \
368 * __list_for_each - iterate over a list
369 * @pos: the &struct list_head to use as a loop cursor.
370 * @head: the head for your list.
372 * This variant differs from list_for_each() in that it's the
373 * simplest possible list iteration code, no prefetching is done.
374 * Use this for code that knows the list to be very short (empty
375 * or 1 entry) most of the time.
377 #define __list_for_each(pos, head) \
378 for (pos = (head)->next; pos != (head); pos = pos->next)
381 * list_for_each_prev - iterate over a list backwards
382 * @pos: the &struct list_head to use as a loop cursor.
383 * @head: the head for your list.
385 #define list_for_each_prev(pos, head) \
386 for (pos = (head)->prev; prefetch(pos->prev), pos != (head); \
390 * list_for_each_safe - iterate over a list safe against removal of list entry
391 * @pos: the &struct list_head to use as a loop cursor.
392 * @n: another &struct list_head to use as temporary storage
393 * @head: the head for your list.
395 #define list_for_each_safe(pos, n, head) \
396 for (pos = (head)->next, n = pos->next; pos != (head); \
397 pos = n, n = pos->next)
400 * list_for_each_prev_safe - iterate over a list backwards safe against removal of list entry
401 * @pos: the &struct list_head to use as a loop cursor.
402 * @n: another &struct list_head to use as temporary storage
403 * @head: the head for your list.
405 #define list_for_each_prev_safe(pos, n, head) \
406 for (pos = (head)->prev, n = pos->prev; \
407 prefetch(pos->prev), pos != (head); \
408 pos = n, n = pos->prev)
411 * list_for_each_entry - iterate over list of given type
412 * @pos: the type * to use as a loop cursor.
413 * @head: the head for your list.
414 * @member: the name of the list_struct within the struct.
416 #define list_for_each_entry(pos, head, member) \
417 for (pos = list_entry((head)->next, typeof(*pos), member); \
418 prefetch(pos->member.next), &pos->member != (head); \
419 pos = list_entry(pos->member.next, typeof(*pos), member))
422 * list_for_each_entry_reverse - iterate backwards over list of given type.
423 * @pos: the type * to use as a loop cursor.
424 * @head: the head for your list.
425 * @member: the name of the list_struct within the struct.
427 #define list_for_each_entry_reverse(pos, head, member) \
428 for (pos = list_entry((head)->prev, typeof(*pos), member); \
429 prefetch(pos->member.prev), &pos->member != (head); \
430 pos = list_entry(pos->member.prev, typeof(*pos), member))
433 * list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue()
434 * @pos: the type * to use as a start point
435 * @head: the head of the list
436 * @member: the name of the list_struct within the struct.
438 * Prepares a pos entry for use as a start point in list_for_each_entry_continue().
440 #define list_prepare_entry(pos, head, member) \
441 ((pos) ? : list_entry(head, typeof(*pos), member))
444 * list_for_each_entry_continue - continue iteration over list of given type
445 * @pos: the type * to use as a loop cursor.
446 * @head: the head for your list.
447 * @member: the name of the list_struct within the struct.
449 * Continue to iterate over list of given type, continuing after
450 * the current position.
452 #define list_for_each_entry_continue(pos, head, member) \
453 for (pos = list_entry(pos->member.next, typeof(*pos), member); \
454 prefetch(pos->member.next), &pos->member != (head); \
455 pos = list_entry(pos->member.next, typeof(*pos), member))
458 * list_for_each_entry_continue_reverse - iterate backwards from the given point
459 * @pos: the type * to use as a loop cursor.
460 * @head: the head for your list.
461 * @member: the name of the list_struct within the struct.
463 * Start to iterate over list of given type backwards, continuing after
464 * the current position.
466 #define list_for_each_entry_continue_reverse(pos, head, member) \
467 for (pos = list_entry(pos->member.prev, typeof(*pos), member); \
468 prefetch(pos->member.prev), &pos->member != (head); \
469 pos = list_entry(pos->member.prev, typeof(*pos), member))
472 * list_for_each_entry_from - iterate over list of given type from the current point
473 * @pos: the type * to use as a loop cursor.
474 * @head: the head for your list.
475 * @member: the name of the list_struct within the struct.
477 * Iterate over list of given type, continuing from current position.
479 #define list_for_each_entry_from(pos, head, member) \
480 for (; prefetch(pos->member.next), &pos->member != (head); \
481 pos = list_entry(pos->member.next, typeof(*pos), member))
484 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
485 * @pos: the type * to use as a loop cursor.
486 * @n: another type * to use as temporary storage
487 * @head: the head for your list.
488 * @member: the name of the list_struct within the struct.
490 #define list_for_each_entry_safe(pos, n, head, member) \
491 for (pos = list_entry((head)->next, typeof(*pos), member), \
492 n = list_entry(pos->member.next, typeof(*pos), member); \
493 &pos->member != (head); \
494 pos = n, n = list_entry(n->member.next, typeof(*n), member))
497 * list_for_each_entry_safe_continue - continue list iteration safe against removal
498 * @pos: the type * to use as a loop cursor.
499 * @n: another type * to use as temporary storage
500 * @head: the head for your list.
501 * @member: the name of the list_struct within the struct.
503 * Iterate over list of given type, continuing after current point,
504 * safe against removal of list entry.
506 #define list_for_each_entry_safe_continue(pos, n, head, member) \
507 for (pos = list_entry(pos->member.next, typeof(*pos), member), \
508 n = list_entry(pos->member.next, typeof(*pos), member); \
509 &pos->member != (head); \
510 pos = n, n = list_entry(n->member.next, typeof(*n), member))
513 * list_for_each_entry_safe_from - iterate over list from current point safe against removal
514 * @pos: the type * to use as a loop cursor.
515 * @n: another type * to use as temporary storage
516 * @head: the head for your list.
517 * @member: the name of the list_struct within the struct.
519 * Iterate over list of given type from current point, safe against
520 * removal of list entry.
522 #define list_for_each_entry_safe_from(pos, n, head, member) \
523 for (n = list_entry(pos->member.next, typeof(*pos), member); \
524 &pos->member != (head); \
525 pos = n, n = list_entry(n->member.next, typeof(*n), member))
528 * list_for_each_entry_safe_reverse - iterate backwards over list safe against removal
529 * @pos: the type * to use as a loop cursor.
530 * @n: another type * to use as temporary storage
531 * @head: the head for your list.
532 * @member: the name of the list_struct within the struct.
534 * Iterate backwards over list of given type, safe against removal
537 #define list_for_each_entry_safe_reverse(pos, n, head, member) \
538 for (pos = list_entry((head)->prev, typeof(*pos), member), \
539 n = list_entry(pos->member.prev, typeof(*pos), member); \
540 &pos->member != (head); \
541 pos = n, n = list_entry(n->member.prev, typeof(*n), member))
544 * list_safe_reset_next - reset a stale list_for_each_entry_safe loop
545 * @pos: the loop cursor used in the list_for_each_entry_safe loop
546 * @n: temporary storage used in list_for_each_entry_safe
547 * @member: the name of the list_struct within the struct.
549 * list_safe_reset_next is not safe to use in general if the list may be
550 * modified concurrently (eg. the lock is dropped in the loop body). An
551 * exception to this is if the cursor element (pos) is pinned in the list,
552 * and list_safe_reset_next is called after re-taking the lock and before
553 * completing the current iteration of the loop body.
555 #define list_safe_reset_next(pos, n, member) \
556 n = list_entry(pos->member.next, typeof(*pos), member)
559 * Double linked lists with a single pointer list head.
560 * Mostly useful for hash tables where the two pointer list head is
562 * You lose the ability to access the tail in O(1).
565 #define HLIST_HEAD_INIT { .first = NULL }
566 #define HLIST_HEAD(name) struct hlist_head name = { .first = NULL }
567 #define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
568 static inline void INIT_HLIST_NODE(struct hlist_node *h)
574 static inline int hlist_unhashed(const struct hlist_node *h)
579 static inline int hlist_empty(const struct hlist_head *h)
584 static inline void __hlist_del(struct hlist_node *n)
586 struct hlist_node *next = n->next;
587 struct hlist_node **pprev = n->pprev;
593 static inline void hlist_del(struct hlist_node *n)
596 n->next = LIST_POISON1;
597 n->pprev = LIST_POISON2;
600 static inline void hlist_del_init(struct hlist_node *n)
602 if (!hlist_unhashed(n)) {
608 static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
610 struct hlist_node *first = h->first;
613 first->pprev = &n->next;
615 n->pprev = &h->first;
618 /* next must be != NULL */
619 static inline void hlist_add_before(struct hlist_node *n,
620 struct hlist_node *next)
622 n->pprev = next->pprev;
624 next->pprev = &n->next;
628 static inline void hlist_add_after(struct hlist_node *n,
629 struct hlist_node *next)
631 next->next = n->next;
633 next->pprev = &n->next;
636 next->next->pprev = &next->next;
640 * Move a list from one list head to another. Fixup the pprev
641 * reference of the first entry if it exists.
643 static inline void hlist_move_list(struct hlist_head *old,
644 struct hlist_head *new)
646 new->first = old->first;
648 new->first->pprev = &new->first;
652 #define hlist_entry(ptr, type, member) container_of(ptr,type,member)
654 #define hlist_for_each(pos, head) \
655 for (pos = (head)->first; pos && ({ prefetch(pos->next); 1; }); \
658 #define hlist_for_each_safe(pos, n, head) \
659 for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
663 * hlist_for_each_entry - iterate over list of given type
664 * @tpos: the type * to use as a loop cursor.
665 * @pos: the &struct hlist_node to use as a loop cursor.
666 * @head: the head for your list.
667 * @member: the name of the hlist_node within the struct.
669 #define hlist_for_each_entry(tpos, pos, head, member) \
670 for (pos = (head)->first; \
671 pos && ({ prefetch(pos->next); 1;}) && \
672 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
676 * hlist_for_each_entry_continue - iterate over a hlist continuing after current point
677 * @tpos: the type * to use as a loop cursor.
678 * @pos: the &struct hlist_node to use as a loop cursor.
679 * @member: the name of the hlist_node within the struct.
681 #define hlist_for_each_entry_continue(tpos, pos, member) \
682 for (pos = (pos)->next; \
683 pos && ({ prefetch(pos->next); 1;}) && \
684 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
688 * hlist_for_each_entry_from - iterate over a hlist continuing from current point
689 * @tpos: the type * to use as a loop cursor.
690 * @pos: the &struct hlist_node to use as a loop cursor.
691 * @member: the name of the hlist_node within the struct.
693 #define hlist_for_each_entry_from(tpos, pos, member) \
694 for (; pos && ({ prefetch(pos->next); 1;}) && \
695 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
699 * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
700 * @tpos: the type * to use as a loop cursor.
701 * @pos: the &struct hlist_node to use as a loop cursor.
702 * @n: another &struct hlist_node to use as temporary storage
703 * @head: the head for your list.
704 * @member: the name of the hlist_node within the struct.
706 #define hlist_for_each_entry_safe(tpos, pos, n, head, member) \
707 for (pos = (head)->first; \
708 pos && ({ n = pos->next; 1; }) && \
709 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \