4 #include <linux/types.h>
5 #include <linux/stddef.h>
6 #include <linux/poison.h>
7 #include <linux/prefetch.h>
8 #include <asm/system.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
)
32 * Insert a new entry between two known consecutive entries.
34 * This is only for internal list manipulation where we know
35 * the prev/next entries already!
37 #ifndef CONFIG_DEBUG_LIST
38 static inline void __list_add(struct list_head
*new,
39 struct list_head
*prev
,
40 struct list_head
*next
)
48 extern void __list_add(struct list_head
*new,
49 struct list_head
*prev
,
50 struct list_head
*next
);
54 * list_add - add a new entry
55 * @new: new entry to be added
56 * @head: list head to add it after
58 * Insert a new entry after the specified head.
59 * This is good for implementing stacks.
61 static inline void list_add(struct list_head
*new, struct list_head
*head
)
63 __list_add(new, head
, head
->next
);
68 * list_add_tail - add a new entry
69 * @new: new entry to be added
70 * @head: list head to add it before
72 * Insert a new entry before the specified head.
73 * This is useful for implementing queues.
75 static inline void list_add_tail(struct list_head
*new, struct list_head
*head
)
77 __list_add(new, head
->prev
, head
);
81 * Delete a list entry by making the prev/next entries
82 * point to each other.
84 * This is only for internal list manipulation where we know
85 * the prev/next entries already!
87 static inline void __list_del(struct list_head
* prev
, struct list_head
* next
)
94 * list_del - deletes entry from list.
95 * @entry: the element to delete from the list.
96 * Note: list_empty() on entry does not return true after this, the entry is
97 * in an undefined state.
99 #ifndef CONFIG_DEBUG_LIST
100 static inline void list_del(struct list_head
*entry
)
102 __list_del(entry
->prev
, entry
->next
);
103 entry
->next
= LIST_POISON1
;
104 entry
->prev
= LIST_POISON2
;
107 extern void list_del(struct list_head
*entry
);
111 * list_replace - replace old entry by new one
112 * @old : the element to be replaced
113 * @new : the new element to insert
115 * If @old was empty, it will be overwritten.
117 static inline void list_replace(struct list_head
*old
,
118 struct list_head
*new)
120 new->next
= old
->next
;
121 new->next
->prev
= new;
122 new->prev
= old
->prev
;
123 new->prev
->next
= new;
126 static inline void list_replace_init(struct list_head
*old
,
127 struct list_head
*new)
129 list_replace(old
, new);
134 * list_del_init - deletes entry from list and reinitialize it.
135 * @entry: the element to delete from the list.
137 static inline void list_del_init(struct list_head
*entry
)
139 __list_del(entry
->prev
, entry
->next
);
140 INIT_LIST_HEAD(entry
);
144 * list_move - delete from one list and add as another's head
145 * @list: the entry to move
146 * @head: the head that will precede our entry
148 static inline void list_move(struct list_head
*list
, struct list_head
*head
)
150 __list_del(list
->prev
, list
->next
);
151 list_add(list
, head
);
155 * list_move_tail - delete from one list and add as another's tail
156 * @list: the entry to move
157 * @head: the head that will follow our entry
159 static inline void list_move_tail(struct list_head
*list
,
160 struct list_head
*head
)
162 __list_del(list
->prev
, list
->next
);
163 list_add_tail(list
, head
);
167 * list_is_last - tests whether @list is the last entry in list @head
168 * @list: the entry to test
169 * @head: the head of the list
171 static inline int list_is_last(const struct list_head
*list
,
172 const struct list_head
*head
)
174 return list
->next
== head
;
178 * list_empty - tests whether a list is empty
179 * @head: the list to test.
181 static inline int list_empty(const struct list_head
*head
)
183 return head
->next
== head
;
187 * list_empty_careful - tests whether a list is empty and not being modified
188 * @head: the list to test
191 * tests whether a list is empty _and_ checks that no other CPU might be
192 * in the process of modifying either member (next or prev)
194 * NOTE: using list_empty_careful() without synchronization
195 * can only be safe if the only activity that can happen
196 * to the list entry is list_del_init(). Eg. it cannot be used
197 * if another CPU could re-list_add() it.
199 static inline int list_empty_careful(const struct list_head
*head
)
201 struct list_head
*next
= head
->next
;
202 return (next
== head
) && (next
== head
->prev
);
206 * list_rotate_left - rotate the list to the left
207 * @head: the head of the list
209 static inline void list_rotate_left(struct list_head
*head
)
211 struct list_head
*first
;
213 if (!list_empty(head
)) {
215 list_move_tail(first
, head
);
220 * list_is_singular - tests whether a list has just one entry.
221 * @head: the list to test.
223 static inline int list_is_singular(const struct list_head
*head
)
225 return !list_empty(head
) && (head
->next
== head
->prev
);
228 static inline void __list_cut_position(struct list_head
*list
,
229 struct list_head
*head
, struct list_head
*entry
)
231 struct list_head
*new_first
= entry
->next
;
232 list
->next
= head
->next
;
233 list
->next
->prev
= list
;
236 head
->next
= new_first
;
237 new_first
->prev
= head
;
241 * list_cut_position - cut a list into two
242 * @list: a new list to add all removed entries
243 * @head: a list with entries
244 * @entry: an entry within head, could be the head itself
245 * and if so we won't cut the list
247 * This helper moves the initial part of @head, up to and
248 * including @entry, from @head to @list. You should
249 * pass on @entry an element you know is on @head. @list
250 * should be an empty list or a list you do not care about
254 static inline void list_cut_position(struct list_head
*list
,
255 struct list_head
*head
, struct list_head
*entry
)
257 if (list_empty(head
))
259 if (list_is_singular(head
) &&
260 (head
->next
!= entry
&& head
!= entry
))
263 INIT_LIST_HEAD(list
);
265 __list_cut_position(list
, head
, entry
);
268 static inline void __list_splice(const struct list_head
*list
,
269 struct list_head
*prev
,
270 struct list_head
*next
)
272 struct list_head
*first
= list
->next
;
273 struct list_head
*last
= list
->prev
;
283 * list_splice - join two lists, this is designed for stacks
284 * @list: the new list to add.
285 * @head: the place to add it in the first list.
287 static inline void list_splice(const struct list_head
*list
,
288 struct list_head
*head
)
290 if (!list_empty(list
))
291 __list_splice(list
, head
, head
->next
);
295 * list_splice_tail - join two lists, each list being a queue
296 * @list: the new list to add.
297 * @head: the place to add it in the first list.
299 static inline void list_splice_tail(struct list_head
*list
,
300 struct list_head
*head
)
302 if (!list_empty(list
))
303 __list_splice(list
, head
->prev
, head
);
307 * list_splice_init - join two lists and reinitialise the emptied list.
308 * @list: the new list to add.
309 * @head: the place to add it in the first list.
311 * The list at @list is reinitialised
313 static inline void list_splice_init(struct list_head
*list
,
314 struct list_head
*head
)
316 if (!list_empty(list
)) {
317 __list_splice(list
, head
, head
->next
);
318 INIT_LIST_HEAD(list
);
323 * list_splice_tail_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 * Each of the lists is a queue.
328 * The list at @list is reinitialised
330 static inline void list_splice_tail_init(struct list_head
*list
,
331 struct list_head
*head
)
333 if (!list_empty(list
)) {
334 __list_splice(list
, head
->prev
, head
);
335 INIT_LIST_HEAD(list
);
340 * list_entry - get the struct for this entry
341 * @ptr: the &struct list_head pointer.
342 * @type: the type of the struct this is embedded in.
343 * @member: the name of the list_struct within the struct.
345 #define list_entry(ptr, type, member) \
346 container_of(ptr, type, member)
349 * list_first_entry - get the first element from a list
350 * @ptr: the list head to take the element from.
351 * @type: the type of the struct this is embedded in.
352 * @member: the name of the list_struct within the struct.
354 * Note, that list is expected to be not empty.
356 #define list_first_entry(ptr, type, member) \
357 list_entry((ptr)->next, type, member)
360 * list_for_each - iterate over a list
361 * @pos: the &struct list_head to use as a loop cursor.
362 * @head: the head for your list.
364 #define list_for_each(pos, head) \
365 for (pos = (head)->next; prefetch(pos->next), pos != (head); \
369 * __list_for_each - iterate over a list
370 * @pos: the &struct list_head to use as a loop cursor.
371 * @head: the head for your list.
373 * This variant differs from list_for_each() in that it's the
374 * simplest possible list iteration code, no prefetching is done.
375 * Use this for code that knows the list to be very short (empty
376 * or 1 entry) most of the time.
378 #define __list_for_each(pos, head) \
379 for (pos = (head)->next; pos != (head); pos = pos->next)
382 * list_for_each_prev - iterate over a list backwards
383 * @pos: the &struct list_head to use as a loop cursor.
384 * @head: the head for your list.
386 #define list_for_each_prev(pos, head) \
387 for (pos = (head)->prev; prefetch(pos->prev), pos != (head); \
391 * list_for_each_safe - iterate over a list safe against removal of list entry
392 * @pos: the &struct list_head to use as a loop cursor.
393 * @n: another &struct list_head to use as temporary storage
394 * @head: the head for your list.
396 #define list_for_each_safe(pos, n, head) \
397 for (pos = (head)->next, n = pos->next; pos != (head); \
398 pos = n, n = pos->next)
401 * list_for_each_prev_safe - iterate over a list backwards safe against removal of list entry
402 * @pos: the &struct list_head to use as a loop cursor.
403 * @n: another &struct list_head to use as temporary storage
404 * @head: the head for your list.
406 #define list_for_each_prev_safe(pos, n, head) \
407 for (pos = (head)->prev, n = pos->prev; \
408 prefetch(pos->prev), pos != (head); \
409 pos = n, n = pos->prev)
412 * list_for_each_entry - iterate over list of given type
413 * @pos: the type * to use as a loop cursor.
414 * @head: the head for your list.
415 * @member: the name of the list_struct within the struct.
417 #define list_for_each_entry(pos, head, member) \
418 for (pos = list_entry((head)->next, typeof(*pos), member); \
419 prefetch(pos->member.next), &pos->member != (head); \
420 pos = list_entry(pos->member.next, typeof(*pos), member))
423 * list_for_each_entry_reverse - iterate backwards over list of given type.
424 * @pos: the type * to use as a loop cursor.
425 * @head: the head for your list.
426 * @member: the name of the list_struct within the struct.
428 #define list_for_each_entry_reverse(pos, head, member) \
429 for (pos = list_entry((head)->prev, typeof(*pos), member); \
430 prefetch(pos->member.prev), &pos->member != (head); \
431 pos = list_entry(pos->member.prev, typeof(*pos), member))
434 * list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue()
435 * @pos: the type * to use as a start point
436 * @head: the head of the list
437 * @member: the name of the list_struct within the struct.
439 * Prepares a pos entry for use as a start point in list_for_each_entry_continue().
441 #define list_prepare_entry(pos, head, member) \
442 ((pos) ? : list_entry(head, typeof(*pos), member))
445 * list_for_each_entry_continue - continue iteration over list of given type
446 * @pos: the type * to use as a loop cursor.
447 * @head: the head for your list.
448 * @member: the name of the list_struct within the struct.
450 * Continue to iterate over list of given type, continuing after
451 * the current position.
453 #define list_for_each_entry_continue(pos, head, member) \
454 for (pos = list_entry(pos->member.next, typeof(*pos), member); \
455 prefetch(pos->member.next), &pos->member != (head); \
456 pos = list_entry(pos->member.next, typeof(*pos), member))
459 * list_for_each_entry_continue_reverse - iterate backwards from the given point
460 * @pos: the type * to use as a loop cursor.
461 * @head: the head for your list.
462 * @member: the name of the list_struct within the struct.
464 * Start to iterate over list of given type backwards, continuing after
465 * the current position.
467 #define list_for_each_entry_continue_reverse(pos, head, member) \
468 for (pos = list_entry(pos->member.prev, typeof(*pos), member); \
469 prefetch(pos->member.prev), &pos->member != (head); \
470 pos = list_entry(pos->member.prev, typeof(*pos), member))
473 * list_for_each_entry_from - iterate over list of given type from the current point
474 * @pos: the type * to use as a loop cursor.
475 * @head: the head for your list.
476 * @member: the name of the list_struct within the struct.
478 * Iterate over list of given type, continuing from current position.
480 #define list_for_each_entry_from(pos, head, member) \
481 for (; prefetch(pos->member.next), &pos->member != (head); \
482 pos = list_entry(pos->member.next, typeof(*pos), member))
485 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
486 * @pos: the type * to use as a loop cursor.
487 * @n: another type * to use as temporary storage
488 * @head: the head for your list.
489 * @member: the name of the list_struct within the struct.
491 #define list_for_each_entry_safe(pos, n, head, member) \
492 for (pos = list_entry((head)->next, typeof(*pos), member), \
493 n = list_entry(pos->member.next, typeof(*pos), member); \
494 &pos->member != (head); \
495 pos = n, n = list_entry(n->member.next, typeof(*n), member))
498 * list_for_each_entry_safe_continue - continue list iteration safe against removal
499 * @pos: the type * to use as a loop cursor.
500 * @n: another type * to use as temporary storage
501 * @head: the head for your list.
502 * @member: the name of the list_struct within the struct.
504 * Iterate over list of given type, continuing after current point,
505 * safe against removal of list entry.
507 #define list_for_each_entry_safe_continue(pos, n, head, member) \
508 for (pos = list_entry(pos->member.next, typeof(*pos), member), \
509 n = list_entry(pos->member.next, typeof(*pos), member); \
510 &pos->member != (head); \
511 pos = n, n = list_entry(n->member.next, typeof(*n), member))
514 * list_for_each_entry_safe_from - iterate over list from current point safe against removal
515 * @pos: the type * to use as a loop cursor.
516 * @n: another type * to use as temporary storage
517 * @head: the head for your list.
518 * @member: the name of the list_struct within the struct.
520 * Iterate over list of given type from current point, safe against
521 * removal of list entry.
523 #define list_for_each_entry_safe_from(pos, n, head, member) \
524 for (n = list_entry(pos->member.next, typeof(*pos), member); \
525 &pos->member != (head); \
526 pos = n, n = list_entry(n->member.next, typeof(*n), member))
529 * list_for_each_entry_safe_reverse - iterate backwards over list safe against removal
530 * @pos: the type * to use as a loop cursor.
531 * @n: another type * to use as temporary storage
532 * @head: the head for your list.
533 * @member: the name of the list_struct within the struct.
535 * Iterate backwards over list of given type, safe against removal
538 #define list_for_each_entry_safe_reverse(pos, n, head, member) \
539 for (pos = list_entry((head)->prev, typeof(*pos), member), \
540 n = list_entry(pos->member.prev, typeof(*pos), member); \
541 &pos->member != (head); \
542 pos = n, n = list_entry(n->member.prev, typeof(*n), member))
545 * list_safe_reset_next - reset a stale list_for_each_entry_safe loop
546 * @pos: the loop cursor used in the list_for_each_entry_safe loop
547 * @n: temporary storage used in list_for_each_entry_safe
548 * @member: the name of the list_struct within the struct.
550 * list_safe_reset_next is not safe to use in general if the list may be
551 * modified concurrently (eg. the lock is dropped in the loop body). An
552 * exception to this is if the cursor element (pos) is pinned in the list,
553 * and list_safe_reset_next is called after re-taking the lock and before
554 * completing the current iteration of the loop body.
556 #define list_safe_reset_next(pos, n, member) \
557 n = list_entry(pos->member.next, typeof(*pos), member)
560 * Double linked lists with a single pointer list head.
561 * Mostly useful for hash tables where the two pointer list head is
563 * You lose the ability to access the tail in O(1).
566 #define HLIST_HEAD_INIT { .first = NULL }
567 #define HLIST_HEAD(name) struct hlist_head name = { .first = NULL }
568 #define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
569 static inline void INIT_HLIST_NODE(struct hlist_node
*h
)
575 static inline int hlist_unhashed(const struct hlist_node
*h
)
580 static inline int hlist_empty(const struct hlist_head
*h
)
585 static inline void __hlist_del(struct hlist_node
*n
)
587 struct hlist_node
*next
= n
->next
;
588 struct hlist_node
**pprev
= n
->pprev
;
594 static inline void hlist_del(struct hlist_node
*n
)
597 n
->next
= LIST_POISON1
;
598 n
->pprev
= LIST_POISON2
;
601 static inline void hlist_del_init(struct hlist_node
*n
)
603 if (!hlist_unhashed(n
)) {
609 static inline void hlist_add_head(struct hlist_node
*n
, struct hlist_head
*h
)
611 struct hlist_node
*first
= h
->first
;
614 first
->pprev
= &n
->next
;
616 n
->pprev
= &h
->first
;
619 /* next must be != NULL */
620 static inline void hlist_add_before(struct hlist_node
*n
,
621 struct hlist_node
*next
)
623 n
->pprev
= next
->pprev
;
625 next
->pprev
= &n
->next
;
629 static inline void hlist_add_after(struct hlist_node
*n
,
630 struct hlist_node
*next
)
632 next
->next
= n
->next
;
634 next
->pprev
= &n
->next
;
637 next
->next
->pprev
= &next
->next
;
641 * Move a list from one list head to another. Fixup the pprev
642 * reference of the first entry if it exists.
644 static inline void hlist_move_list(struct hlist_head
*old
,
645 struct hlist_head
*new)
647 new->first
= old
->first
;
649 new->first
->pprev
= &new->first
;
653 #define hlist_entry(ptr, type, member) container_of(ptr,type,member)
655 #define hlist_for_each(pos, head) \
656 for (pos = (head)->first; pos && ({ prefetch(pos->next); 1; }); \
659 #define hlist_for_each_safe(pos, n, head) \
660 for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
664 * hlist_for_each_entry - iterate over list of given type
665 * @tpos: the type * to use as a loop cursor.
666 * @pos: the &struct hlist_node to use as a loop cursor.
667 * @head: the head for your list.
668 * @member: the name of the hlist_node within the struct.
670 #define hlist_for_each_entry(tpos, pos, head, member) \
671 for (pos = (head)->first; \
672 pos && ({ prefetch(pos->next); 1;}) && \
673 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
677 * hlist_for_each_entry_continue - iterate over a hlist continuing after current point
678 * @tpos: the type * to use as a loop cursor.
679 * @pos: the &struct hlist_node to use as a loop cursor.
680 * @member: the name of the hlist_node within the struct.
682 #define hlist_for_each_entry_continue(tpos, pos, member) \
683 for (pos = (pos)->next; \
684 pos && ({ prefetch(pos->next); 1;}) && \
685 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
689 * hlist_for_each_entry_from - iterate over a hlist continuing from current point
690 * @tpos: the type * to use as a loop cursor.
691 * @pos: the &struct hlist_node to use as a loop cursor.
692 * @member: the name of the hlist_node within the struct.
694 #define hlist_for_each_entry_from(tpos, pos, member) \
695 for (; pos && ({ prefetch(pos->next); 1;}) && \
696 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
700 * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
701 * @tpos: the type * to use as a loop cursor.
702 * @pos: the &struct hlist_node to use as a loop cursor.
703 * @n: another &struct hlist_node to use as temporary storage
704 * @head: the head for your list.
705 * @member: the name of the hlist_node within the struct.
707 #define hlist_for_each_entry_safe(tpos, pos, n, head, member) \
708 for (pos = (head)->first; \
709 pos && ({ n = pos->next; 1; }) && \
710 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \