3 * This file is based on include/linux/list.h in the Linux kernel source code.
12 * container_of - cast a member of a structure out to the containing structure
13 * @ptr: the pointer to the member.
14 * @type: the type of the container struct this is embedded in.
15 * @member: the name of the member within the struct.
18 #define container_of(ptr, type, member) ({ \
19 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
20 (type *)( (char *)__mptr - offsetof(type,member) );})
23 # define POISON_POINTER_DELTA 0
25 * These are non-NULL pointers that will result in page faults
26 * under normal circumstances, used to verify that nobody uses
27 * non-initialized list entries.
29 #define LIST_POISON1 ((void *) 0x00100100 + POISON_POINTER_DELTA)
30 #define LIST_POISON2 ((void *) 0x00200200 + POISON_POINTER_DELTA)
32 struct list_head *next, *prev;
35 struct hlist_node *first;
39 struct hlist_node *next, **pprev;
43 * Simple doubly linked list implementation.
45 * Some of the internal functions ("__xxx") are useful when
46 * manipulating whole lists rather than single entries, as
47 * sometimes we already know the next/prev entries and we can
48 * generate better code by using them directly rather than
49 * using the generic single-entry routines.
52 #define LIST_HEAD_INIT(name) { &(name), &(name) }
54 #ifdef LIST_HEAD /* BSD sys/queue.h defines this... */
58 #define LIST_HEAD(name) \
59 struct list_head name = LIST_HEAD_INIT(name)
61 static inline void INIT_LIST_HEAD(struct list_head *list)
68 * Insert a new entry between two known consecutive entries.
70 * This is only for internal list manipulation where we know
71 * the prev/next entries already!
73 #ifndef CONFIG_DEBUG_LIST
74 static inline void __list_add(struct list_head *new,
75 struct list_head *prev,
76 struct list_head *next)
84 extern void __list_add(struct list_head *new,
85 struct list_head *prev,
86 struct list_head *next);
90 * list_add - add a new entry
91 * @new: new entry to be added
92 * @head: list head to add it after
94 * Insert a new entry after the specified head.
95 * This is good for implementing stacks.
97 static inline void list_add(struct list_head *new, struct list_head *head)
99 __list_add(new, head, head->next);
104 * list_add_tail - add a new entry
105 * @new: new entry to be added
106 * @head: list head to add it before
108 * Insert a new entry before the specified head.
109 * This is useful for implementing queues.
111 static inline void list_add_tail(struct list_head *new, struct list_head *head)
113 __list_add(new, head->prev, head);
117 * Delete a list entry by making the prev/next entries
118 * point to each other.
120 * This is only for internal list manipulation where we know
121 * the prev/next entries already!
123 static inline void __list_del(struct list_head * prev, struct list_head * next)
130 * list_del - deletes entry from list.
131 * @entry: the element to delete from the list.
132 * Note: list_empty() on entry does not return true after this, the entry is
133 * in an undefined state.
135 #ifndef CONFIG_DEBUG_LIST
136 static inline void __list_del_entry(struct list_head *entry)
138 __list_del(entry->prev, entry->next);
141 static inline void list_del(struct list_head *entry)
143 __list_del(entry->prev, entry->next);
144 entry->next = LIST_POISON1;
145 entry->prev = LIST_POISON2;
148 extern void __list_del_entry(struct list_head *entry);
149 extern void list_del(struct list_head *entry);
153 * list_replace - replace old entry by new one
154 * @old : the element to be replaced
155 * @new : the new element to insert
157 * If @old was empty, it will be overwritten.
159 static inline void list_replace(struct list_head *old,
160 struct list_head *new)
162 new->next = old->next;
163 new->next->prev = new;
164 new->prev = old->prev;
165 new->prev->next = new;
168 static inline void list_replace_init(struct list_head *old,
169 struct list_head *new)
171 list_replace(old, new);
176 * list_del_init - deletes entry from list and reinitialize it.
177 * @entry: the element to delete from the list.
179 static inline void list_del_init(struct list_head *entry)
181 __list_del_entry(entry);
182 INIT_LIST_HEAD(entry);
186 * list_move - delete from one list and add as another's head
187 * @list: the entry to move
188 * @head: the head that will precede our entry
190 static inline void list_move(struct list_head *list, struct list_head *head)
192 __list_del_entry(list);
193 list_add(list, head);
197 * list_move_tail - delete from one list and add as another's tail
198 * @list: the entry to move
199 * @head: the head that will follow our entry
201 static inline void list_move_tail(struct list_head *list,
202 struct list_head *head)
204 __list_del_entry(list);
205 list_add_tail(list, head);
209 * list_is_last - tests whether @list is the last entry in list @head
210 * @list: the entry to test
211 * @head: the head of the list
213 static inline int list_is_last(const struct list_head *list,
214 const struct list_head *head)
216 return list->next == head;
220 * list_empty - tests whether a list is empty
221 * @head: the list to test.
223 static inline int list_empty(const struct list_head *head)
225 return head->next == head;
229 * list_empty_careful - tests whether a list is empty and not being modified
230 * @head: the list to test
233 * tests whether a list is empty _and_ checks that no other CPU might be
234 * in the process of modifying either member (next or prev)
236 * NOTE: using list_empty_careful() without synchronization
237 * can only be safe if the only activity that can happen
238 * to the list entry is list_del_init(). Eg. it cannot be used
239 * if another CPU could re-list_add() it.
241 static inline int list_empty_careful(const struct list_head *head)
243 struct list_head *next = head->next;
244 return (next == head) && (next == head->prev);
248 * list_rotate_left - rotate the list to the left
249 * @head: the head of the list
251 static inline void list_rotate_left(struct list_head *head)
253 struct list_head *first;
255 if (!list_empty(head)) {
257 list_move_tail(first, head);
262 * list_is_singular - tests whether a list has just one entry.
263 * @head: the list to test.
265 static inline int list_is_singular(const struct list_head *head)
267 return !list_empty(head) && (head->next == head->prev);
270 static inline void __list_cut_position(struct list_head *list,
271 struct list_head *head, struct list_head *entry)
273 struct list_head *new_first = entry->next;
274 list->next = head->next;
275 list->next->prev = list;
278 head->next = new_first;
279 new_first->prev = head;
283 * list_cut_position - cut a list into two
284 * @list: a new list to add all removed entries
285 * @head: a list with entries
286 * @entry: an entry within head, could be the head itself
287 * and if so we won't cut the list
289 * This helper moves the initial part of @head, up to and
290 * including @entry, from @head to @list. You should
291 * pass on @entry an element you know is on @head. @list
292 * should be an empty list or a list you do not care about
296 static inline void list_cut_position(struct list_head *list,
297 struct list_head *head, struct list_head *entry)
299 if (list_empty(head))
301 if (list_is_singular(head) &&
302 (head->next != entry && head != entry))
305 INIT_LIST_HEAD(list);
307 __list_cut_position(list, head, entry);
310 static inline void __list_splice(const struct list_head *list,
311 struct list_head *prev,
312 struct list_head *next)
314 struct list_head *first = list->next;
315 struct list_head *last = list->prev;
325 * list_splice - join two lists, this is designed for stacks
326 * @list: the new list to add.
327 * @head: the place to add it in the first list.
329 static inline void list_splice(const struct list_head *list,
330 struct list_head *head)
332 if (!list_empty(list))
333 __list_splice(list, head, head->next);
337 * list_splice_tail - join two lists, each list being a queue
338 * @list: the new list to add.
339 * @head: the place to add it in the first list.
341 static inline void list_splice_tail(struct list_head *list,
342 struct list_head *head)
344 if (!list_empty(list))
345 __list_splice(list, head->prev, head);
349 * list_splice_init - join two lists and reinitialise the emptied list.
350 * @list: the new list to add.
351 * @head: the place to add it in the first list.
353 * The list at @list is reinitialised
355 static inline void list_splice_init(struct list_head *list,
356 struct list_head *head)
358 if (!list_empty(list)) {
359 __list_splice(list, head, head->next);
360 INIT_LIST_HEAD(list);
365 * list_splice_tail_init - join two lists and reinitialise the emptied list
366 * @list: the new list to add.
367 * @head: the place to add it in the first list.
369 * Each of the lists is a queue.
370 * The list at @list is reinitialised
372 static inline void list_splice_tail_init(struct list_head *list,
373 struct list_head *head)
375 if (!list_empty(list)) {
376 __list_splice(list, head->prev, head);
377 INIT_LIST_HEAD(list);
382 * list_entry - get the struct for this entry
383 * @ptr: the &struct list_head pointer.
384 * @type: the type of the struct this is embedded in.
385 * @member: the name of the list_struct within the struct.
387 #define list_entry(ptr, type, member) \
388 container_of(ptr, type, member)
391 * list_first_entry - get the first element from a list
392 * @ptr: the list head to take the element from.
393 * @type: the type of the struct this is embedded in.
394 * @member: the name of the list_struct within the struct.
396 * Note, that list is expected to be not empty.
398 #define list_first_entry(ptr, type, member) \
399 list_entry((ptr)->next, type, member)
402 * list_for_each - iterate over a list
403 * @pos: the &struct list_head to use as a loop cursor.
404 * @head: the head for your list.
406 #define list_for_each(pos, head) \
407 for (pos = (head)->next; pos != (head); pos = pos->next)
410 * __list_for_each - iterate over a list
411 * @pos: the &struct list_head to use as a loop cursor.
412 * @head: the head for your list.
414 * This variant doesn't differ from list_for_each() any more.
415 * We don't do prefetching in either case.
417 #define __list_for_each(pos, head) \
418 for (pos = (head)->next; pos != (head); pos = pos->next)
421 * list_for_each_prev - iterate over a list backwards
422 * @pos: the &struct list_head to use as a loop cursor.
423 * @head: the head for your list.
425 #define list_for_each_prev(pos, head) \
426 for (pos = (head)->prev; pos != (head); pos = pos->prev)
429 * list_for_each_safe - iterate over a list safe against removal of list entry
430 * @pos: the &struct list_head to use as a loop cursor.
431 * @n: another &struct list_head to use as temporary storage
432 * @head: the head for your list.
434 #define list_for_each_safe(pos, n, head) \
435 for (pos = (head)->next, n = pos->next; pos != (head); \
436 pos = n, n = pos->next)
439 * list_for_each_prev_safe - iterate over a list backwards safe against removal of list entry
440 * @pos: the &struct list_head to use as a loop cursor.
441 * @n: another &struct list_head to use as temporary storage
442 * @head: the head for your list.
444 #define list_for_each_prev_safe(pos, n, head) \
445 for (pos = (head)->prev, n = pos->prev; \
447 pos = n, n = pos->prev)
450 * list_for_each_entry - iterate over list of given type
451 * @pos: the type * to use as a loop cursor.
452 * @head: the head for your list.
453 * @member: the name of the list_struct within the struct.
455 #define list_for_each_entry(pos, head, member) \
456 for (pos = list_entry((head)->next, typeof(*pos), member); \
457 &pos->member != (head); \
458 pos = list_entry(pos->member.next, typeof(*pos), member))
461 * list_for_each_entry_reverse - iterate backwards over list of given type.
462 * @pos: the type * to use as a loop cursor.
463 * @head: the head for your list.
464 * @member: the name of the list_struct within the struct.
466 #define list_for_each_entry_reverse(pos, head, member) \
467 for (pos = list_entry((head)->prev, typeof(*pos), member); \
468 &pos->member != (head); \
469 pos = list_entry(pos->member.prev, typeof(*pos), member))
472 * list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue()
473 * @pos: the type * to use as a start point
474 * @head: the head of the list
475 * @member: the name of the list_struct within the struct.
477 * Prepares a pos entry for use as a start point in list_for_each_entry_continue().
479 #define list_prepare_entry(pos, head, member) \
480 ((pos) ? : list_entry(head, typeof(*pos), member))
483 * list_for_each_entry_continue - continue iteration over list of given type
484 * @pos: the type * to use as a loop cursor.
485 * @head: the head for your list.
486 * @member: the name of the list_struct within the struct.
488 * Continue to iterate over list of given type, continuing after
489 * the current position.
491 #define list_for_each_entry_continue(pos, head, member) \
492 for (pos = list_entry(pos->member.next, typeof(*pos), member); \
493 &pos->member != (head); \
494 pos = list_entry(pos->member.next, typeof(*pos), member))
497 * list_for_each_entry_continue_reverse - iterate backwards from the given point
498 * @pos: the type * to use as a loop cursor.
499 * @head: the head for your list.
500 * @member: the name of the list_struct within the struct.
502 * Start to iterate over list of given type backwards, continuing after
503 * the current position.
505 #define list_for_each_entry_continue_reverse(pos, head, member) \
506 for (pos = list_entry(pos->member.prev, typeof(*pos), member); \
507 &pos->member != (head); \
508 pos = list_entry(pos->member.prev, typeof(*pos), member))
511 * list_for_each_entry_from - iterate over list of given type from the current point
512 * @pos: the type * to use as a loop cursor.
513 * @head: the head for your list.
514 * @member: the name of the list_struct within the struct.
516 * Iterate over list of given type, continuing from current position.
518 #define list_for_each_entry_from(pos, head, member) \
519 for (; &pos->member != (head); \
520 pos = list_entry(pos->member.next, typeof(*pos), member))
523 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
524 * @pos: the type * to use as a loop cursor.
525 * @n: another type * to use as temporary storage
526 * @head: the head for your list.
527 * @member: the name of the list_struct within the struct.
529 #define list_for_each_entry_safe(pos, n, head, member) \
530 for (pos = list_entry((head)->next, typeof(*pos), member), \
531 n = list_entry(pos->member.next, typeof(*pos), member); \
532 &pos->member != (head); \
533 pos = n, n = list_entry(n->member.next, typeof(*n), member))
536 * list_for_each_entry_safe_continue - continue list iteration safe against removal
537 * @pos: the type * to use as a loop cursor.
538 * @n: another type * to use as temporary storage
539 * @head: the head for your list.
540 * @member: the name of the list_struct within the struct.
542 * Iterate over list of given type, continuing after current point,
543 * safe against removal of list entry.
545 #define list_for_each_entry_safe_continue(pos, n, head, member) \
546 for (pos = list_entry(pos->member.next, typeof(*pos), member), \
547 n = list_entry(pos->member.next, typeof(*pos), member); \
548 &pos->member != (head); \
549 pos = n, n = list_entry(n->member.next, typeof(*n), member))
552 * list_for_each_entry_safe_from - iterate over list from current point safe against removal
553 * @pos: the type * to use as a loop cursor.
554 * @n: another type * to use as temporary storage
555 * @head: the head for your list.
556 * @member: the name of the list_struct within the struct.
558 * Iterate over list of given type from current point, safe against
559 * removal of list entry.
561 #define list_for_each_entry_safe_from(pos, n, head, member) \
562 for (n = list_entry(pos->member.next, typeof(*pos), member); \
563 &pos->member != (head); \
564 pos = n, n = list_entry(n->member.next, typeof(*n), member))
567 * list_for_each_entry_safe_reverse - iterate backwards over list safe against removal
568 * @pos: the type * to use as a loop cursor.
569 * @n: another type * to use as temporary storage
570 * @head: the head for your list.
571 * @member: the name of the list_struct within the struct.
573 * Iterate backwards over list of given type, safe against removal
576 #define list_for_each_entry_safe_reverse(pos, n, head, member) \
577 for (pos = list_entry((head)->prev, typeof(*pos), member), \
578 n = list_entry(pos->member.prev, typeof(*pos), member); \
579 &pos->member != (head); \
580 pos = n, n = list_entry(n->member.prev, typeof(*n), member))
583 * list_safe_reset_next - reset a stale list_for_each_entry_safe loop
584 * @pos: the loop cursor used in the list_for_each_entry_safe loop
585 * @n: temporary storage used in list_for_each_entry_safe
586 * @member: the name of the list_struct within the struct.
588 * list_safe_reset_next is not safe to use in general if the list may be
589 * modified concurrently (eg. the lock is dropped in the loop body). An
590 * exception to this is if the cursor element (pos) is pinned in the list,
591 * and list_safe_reset_next is called after re-taking the lock and before
592 * completing the current iteration of the loop body.
594 #define list_safe_reset_next(pos, n, member) \
595 n = list_entry(pos->member.next, typeof(*pos), member)
598 * Double linked lists with a single pointer list head.
599 * Mostly useful for hash tables where the two pointer list head is
601 * You lose the ability to access the tail in O(1).
604 #define HLIST_HEAD_INIT { .first = NULL }
605 #define HLIST_HEAD(name) struct hlist_head name = { .first = NULL }
606 #define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
607 static inline void INIT_HLIST_NODE(struct hlist_node *h)
613 static inline int hlist_unhashed(const struct hlist_node *h)
618 static inline int hlist_empty(const struct hlist_head *h)
623 static inline void __hlist_del(struct hlist_node *n)
625 struct hlist_node *next = n->next;
626 struct hlist_node **pprev = n->pprev;
632 static inline void hlist_del(struct hlist_node *n)
635 n->next = LIST_POISON1;
636 n->pprev = LIST_POISON2;
639 static inline void hlist_del_init(struct hlist_node *n)
641 if (!hlist_unhashed(n)) {
647 static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
649 struct hlist_node *first = h->first;
652 first->pprev = &n->next;
654 n->pprev = &h->first;
657 /* next must be != NULL */
658 static inline void hlist_add_before(struct hlist_node *n,
659 struct hlist_node *next)
661 n->pprev = next->pprev;
663 next->pprev = &n->next;
667 static inline void hlist_add_after(struct hlist_node *n,
668 struct hlist_node *next)
670 next->next = n->next;
672 next->pprev = &n->next;
675 next->next->pprev = &next->next;
678 /* after that we'll appear to be on some hlist and hlist_del will work */
679 static inline void hlist_add_fake(struct hlist_node *n)
685 * Move a list from one list head to another. Fixup the pprev
686 * reference of the first entry if it exists.
688 static inline void hlist_move_list(struct hlist_head *old,
689 struct hlist_head *new)
691 new->first = old->first;
693 new->first->pprev = &new->first;
697 #define hlist_entry(ptr, type, member) container_of(ptr,type,member)
699 #define hlist_for_each(pos, head) \
700 for (pos = (head)->first; pos ; pos = pos->next)
702 #define hlist_for_each_safe(pos, n, head) \
703 for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
707 * hlist_for_each_entry - iterate over list of given type
708 * @tpos: the type * to use as a loop cursor.
709 * @pos: the &struct hlist_node to use as a loop cursor.
710 * @head: the head for your list.
711 * @member: the name of the hlist_node within the struct.
713 #define hlist_for_each_entry(tpos, pos, head, member) \
714 for (pos = (head)->first; \
716 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
720 * hlist_for_each_entry_continue - iterate over a hlist continuing after current point
721 * @tpos: the type * to use as a loop cursor.
722 * @pos: the &struct hlist_node to use as a loop cursor.
723 * @member: the name of the hlist_node within the struct.
725 #define hlist_for_each_entry_continue(tpos, pos, member) \
726 for (pos = (pos)->next; \
728 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
732 * hlist_for_each_entry_from - iterate over a hlist continuing from current point
733 * @tpos: the type * to use as a loop cursor.
734 * @pos: the &struct hlist_node to use as a loop cursor.
735 * @member: the name of the hlist_node within the struct.
737 #define hlist_for_each_entry_from(tpos, pos, member) \
739 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
743 * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
744 * @tpos: the type * to use as a loop cursor.
745 * @pos: the &struct hlist_node to use as a loop cursor.
746 * @n: another &struct hlist_node to use as temporary storage
747 * @head: the head for your list.
748 * @member: the name of the hlist_node within the struct.
750 #define hlist_for_each_entry_safe(tpos, pos, n, head, member) \
751 for (pos = (head)->first; \
752 pos && ({ n = pos->next; 1; }) && \
753 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \