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 * Structure used to create a linked list of streams that share the same lookup
44 * table entry. This structure may be embedded in either a inode (for the
45 * un-named data stream) or an ads_entry (for an alternate data stream). The
46 * @type field indicates which of these structures the stream_list_head is
49 struct stream_list_head {
50 struct list_head list;
52 STREAM_TYPE_NORMAL = 0,
58 * Simple doubly linked list implementation.
60 * Some of the internal functions ("__xxx") are useful when
61 * manipulating whole lists rather than single entries, as
62 * sometimes we already know the next/prev entries and we can
63 * generate better code by using them directly rather than
64 * using the generic single-entry routines.
67 #define LIST_HEAD_INIT(name) { &(name), &(name) }
69 #ifdef LIST_HEAD /* BSD sys/queue.h defines this... */
73 #define LIST_HEAD(name) \
74 struct list_head name = LIST_HEAD_INIT(name)
76 static inline void INIT_LIST_HEAD(struct list_head *list)
83 * Insert a new entry between two known consecutive entries.
85 * This is only for internal list manipulation where we know
86 * the prev/next entries already!
88 #ifndef CONFIG_DEBUG_LIST
89 static inline void __list_add(struct list_head *new,
90 struct list_head *prev,
91 struct list_head *next)
99 extern void __list_add(struct list_head *new,
100 struct list_head *prev,
101 struct list_head *next);
105 * list_add - add a new entry
106 * @new: new entry to be added
107 * @head: list head to add it after
109 * Insert a new entry after the specified head.
110 * This is good for implementing stacks.
112 static inline void list_add(struct list_head *new, struct list_head *head)
114 __list_add(new, head, head->next);
119 * list_add_tail - add a new entry
120 * @new: new entry to be added
121 * @head: list head to add it before
123 * Insert a new entry before the specified head.
124 * This is useful for implementing queues.
126 static inline void list_add_tail(struct list_head *new, struct list_head *head)
128 __list_add(new, head->prev, head);
132 * Delete a list entry by making the prev/next entries
133 * point to each other.
135 * This is only for internal list manipulation where we know
136 * the prev/next entries already!
138 static inline void __list_del(struct list_head * prev, struct list_head * next)
145 * list_del - deletes entry from list.
146 * @entry: the element to delete from the list.
147 * Note: list_empty() on entry does not return true after this, the entry is
148 * in an undefined state.
150 #ifndef CONFIG_DEBUG_LIST
151 static inline void __list_del_entry(struct list_head *entry)
153 __list_del(entry->prev, entry->next);
156 static inline void list_del(struct list_head *entry)
158 __list_del(entry->prev, entry->next);
159 entry->next = LIST_POISON1;
160 entry->prev = LIST_POISON2;
163 extern void __list_del_entry(struct list_head *entry);
164 extern void list_del(struct list_head *entry);
168 * list_replace - replace old entry by new one
169 * @old : the element to be replaced
170 * @new : the new element to insert
172 * If @old was empty, it will be overwritten.
174 static inline void list_replace(struct list_head *old,
175 struct list_head *new)
177 new->next = old->next;
178 new->next->prev = new;
179 new->prev = old->prev;
180 new->prev->next = new;
183 static inline void list_replace_init(struct list_head *old,
184 struct list_head *new)
186 list_replace(old, new);
191 * list_del_init - deletes entry from list and reinitialize it.
192 * @entry: the element to delete from the list.
194 static inline void list_del_init(struct list_head *entry)
196 __list_del_entry(entry);
197 INIT_LIST_HEAD(entry);
201 * list_move - delete from one list and add as another's head
202 * @list: the entry to move
203 * @head: the head that will precede our entry
205 static inline void list_move(struct list_head *list, struct list_head *head)
207 __list_del_entry(list);
208 list_add(list, head);
212 * list_move_tail - delete from one list and add as another's tail
213 * @list: the entry to move
214 * @head: the head that will follow our entry
216 static inline void list_move_tail(struct list_head *list,
217 struct list_head *head)
219 __list_del_entry(list);
220 list_add_tail(list, head);
224 * list_is_last - tests whether @list is the last entry in list @head
225 * @list: the entry to test
226 * @head: the head of the list
228 static inline int list_is_last(const struct list_head *list,
229 const struct list_head *head)
231 return list->next == head;
235 * list_empty - tests whether a list is empty
236 * @head: the list to test.
238 static inline int list_empty(const struct list_head *head)
240 return head->next == head;
244 * list_empty_careful - tests whether a list is empty and not being modified
245 * @head: the list to test
248 * tests whether a list is empty _and_ checks that no other CPU might be
249 * in the process of modifying either member (next or prev)
251 * NOTE: using list_empty_careful() without synchronization
252 * can only be safe if the only activity that can happen
253 * to the list entry is list_del_init(). Eg. it cannot be used
254 * if another CPU could re-list_add() it.
256 static inline int list_empty_careful(const struct list_head *head)
258 struct list_head *next = head->next;
259 return (next == head) && (next == head->prev);
263 * list_rotate_left - rotate the list to the left
264 * @head: the head of the list
266 static inline void list_rotate_left(struct list_head *head)
268 struct list_head *first;
270 if (!list_empty(head)) {
272 list_move_tail(first, head);
277 * list_is_singular - tests whether a list has just one entry.
278 * @head: the list to test.
280 static inline int list_is_singular(const struct list_head *head)
282 return !list_empty(head) && (head->next == head->prev);
285 static inline void __list_cut_position(struct list_head *list,
286 struct list_head *head, struct list_head *entry)
288 struct list_head *new_first = entry->next;
289 list->next = head->next;
290 list->next->prev = list;
293 head->next = new_first;
294 new_first->prev = head;
298 * list_cut_position - cut a list into two
299 * @list: a new list to add all removed entries
300 * @head: a list with entries
301 * @entry: an entry within head, could be the head itself
302 * and if so we won't cut the list
304 * This helper moves the initial part of @head, up to and
305 * including @entry, from @head to @list. You should
306 * pass on @entry an element you know is on @head. @list
307 * should be an empty list or a list you do not care about
311 static inline void list_cut_position(struct list_head *list,
312 struct list_head *head, struct list_head *entry)
314 if (list_empty(head))
316 if (list_is_singular(head) &&
317 (head->next != entry && head != entry))
320 INIT_LIST_HEAD(list);
322 __list_cut_position(list, head, entry);
325 static inline void __list_splice(const struct list_head *list,
326 struct list_head *prev,
327 struct list_head *next)
329 struct list_head *first = list->next;
330 struct list_head *last = list->prev;
340 * list_splice - join two lists, this is designed for stacks
341 * @list: the new list to add.
342 * @head: the place to add it in the first list.
344 static inline void list_splice(const struct list_head *list,
345 struct list_head *head)
347 if (!list_empty(list))
348 __list_splice(list, head, head->next);
352 * list_splice_tail - join two lists, each list being a queue
353 * @list: the new list to add.
354 * @head: the place to add it in the first list.
356 static inline void list_splice_tail(struct list_head *list,
357 struct list_head *head)
359 if (!list_empty(list))
360 __list_splice(list, head->prev, head);
364 * list_splice_init - join two lists and reinitialise the emptied list.
365 * @list: the new list to add.
366 * @head: the place to add it in the first list.
368 * The list at @list is reinitialised
370 static inline void list_splice_init(struct list_head *list,
371 struct list_head *head)
373 if (!list_empty(list)) {
374 __list_splice(list, head, head->next);
375 INIT_LIST_HEAD(list);
380 * list_splice_tail_init - join two lists and reinitialise the emptied list
381 * @list: the new list to add.
382 * @head: the place to add it in the first list.
384 * Each of the lists is a queue.
385 * The list at @list is reinitialised
387 static inline void list_splice_tail_init(struct list_head *list,
388 struct list_head *head)
390 if (!list_empty(list)) {
391 __list_splice(list, head->prev, head);
392 INIT_LIST_HEAD(list);
397 * list_entry - get the struct for this entry
398 * @ptr: the &struct list_head pointer.
399 * @type: the type of the struct this is embedded in.
400 * @member: the name of the list_struct within the struct.
402 #define list_entry(ptr, type, member) \
403 container_of(ptr, type, member)
406 * list_first_entry - get the first element from a list
407 * @ptr: the list head to take the element from.
408 * @type: the type of the struct this is embedded in.
409 * @member: the name of the list_struct within the struct.
411 * Note, that list is expected to be not empty.
413 #define list_first_entry(ptr, type, member) \
414 list_entry((ptr)->next, type, 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 - iterate over a list
426 * @pos: the &struct list_head to use as a loop cursor.
427 * @head: the head for your list.
429 * This variant doesn't differ from list_for_each() any more.
430 * We don't do prefetching in either case.
432 #define __list_for_each(pos, head) \
433 for (pos = (head)->next; pos != (head); pos = pos->next)
436 * list_for_each_prev - iterate over a list backwards
437 * @pos: the &struct list_head to use as a loop cursor.
438 * @head: the head for your list.
440 #define list_for_each_prev(pos, head) \
441 for (pos = (head)->prev; pos != (head); pos = pos->prev)
444 * list_for_each_safe - iterate over a list safe against removal of list entry
445 * @pos: the &struct list_head to use as a loop cursor.
446 * @n: another &struct list_head to use as temporary storage
447 * @head: the head for your list.
449 #define list_for_each_safe(pos, n, head) \
450 for (pos = (head)->next, n = pos->next; pos != (head); \
451 pos = n, n = pos->next)
454 * list_for_each_prev_safe - iterate over a list backwards safe against removal of list entry
455 * @pos: the &struct list_head to use as a loop cursor.
456 * @n: another &struct list_head to use as temporary storage
457 * @head: the head for your list.
459 #define list_for_each_prev_safe(pos, n, head) \
460 for (pos = (head)->prev, n = pos->prev; \
462 pos = n, n = pos->prev)
465 * list_for_each_entry - iterate 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_struct within the struct.
470 #define list_for_each_entry(pos, head, member) \
471 for (pos = list_entry((head)->next, typeof(*pos), member); \
472 &pos->member != (head); \
473 pos = list_entry(pos->member.next, typeof(*pos), member))
476 * list_for_each_entry_reverse - iterate backwards over list of given type.
477 * @pos: the type * to use as a loop cursor.
478 * @head: the head for your list.
479 * @member: the name of the list_struct within the struct.
481 #define list_for_each_entry_reverse(pos, head, member) \
482 for (pos = list_entry((head)->prev, typeof(*pos), member); \
483 &pos->member != (head); \
484 pos = list_entry(pos->member.prev, typeof(*pos), member))
487 * list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue()
488 * @pos: the type * to use as a start point
489 * @head: the head of the list
490 * @member: the name of the list_struct within the struct.
492 * Prepares a pos entry for use as a start point in list_for_each_entry_continue().
494 #define list_prepare_entry(pos, head, member) \
495 ((pos) ? : list_entry(head, typeof(*pos), member))
498 * list_for_each_entry_continue - continue iteration over list of given type
499 * @pos: the type * to use as a loop cursor.
500 * @head: the head for your list.
501 * @member: the name of the list_struct within the struct.
503 * Continue to iterate over list of given type, continuing after
504 * the current position.
506 #define list_for_each_entry_continue(pos, head, member) \
507 for (pos = list_entry(pos->member.next, typeof(*pos), member); \
508 &pos->member != (head); \
509 pos = list_entry(pos->member.next, typeof(*pos), member))
512 * list_for_each_entry_continue_reverse - iterate backwards from the given point
513 * @pos: the type * to use as a loop cursor.
514 * @head: the head for your list.
515 * @member: the name of the list_struct within the struct.
517 * Start to iterate over list of given type backwards, continuing after
518 * the current position.
520 #define list_for_each_entry_continue_reverse(pos, head, member) \
521 for (pos = list_entry(pos->member.prev, typeof(*pos), member); \
522 &pos->member != (head); \
523 pos = list_entry(pos->member.prev, typeof(*pos), member))
526 * list_for_each_entry_from - iterate over list of given type from the current point
527 * @pos: the type * to use as a loop cursor.
528 * @head: the head for your list.
529 * @member: the name of the list_struct within the struct.
531 * Iterate over list of given type, continuing from current position.
533 #define list_for_each_entry_from(pos, head, member) \
534 for (; &pos->member != (head); \
535 pos = list_entry(pos->member.next, typeof(*pos), member))
538 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
539 * @pos: the type * to use as a loop cursor.
540 * @n: another type * to use as temporary storage
541 * @head: the head for your list.
542 * @member: the name of the list_struct within the struct.
544 #define list_for_each_entry_safe(pos, n, head, member) \
545 for (pos = list_entry((head)->next, typeof(*pos), member), \
546 n = list_entry(pos->member.next, typeof(*pos), member); \
547 &pos->member != (head); \
548 pos = n, n = list_entry(n->member.next, typeof(*n), member))
551 * list_for_each_entry_safe_continue - continue list iteration safe against removal
552 * @pos: the type * to use as a loop cursor.
553 * @n: another type * to use as temporary storage
554 * @head: the head for your list.
555 * @member: the name of the list_struct within the struct.
557 * Iterate over list of given type, continuing after current point,
558 * safe against removal of list entry.
560 #define list_for_each_entry_safe_continue(pos, n, head, member) \
561 for (pos = list_entry(pos->member.next, typeof(*pos), member), \
562 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_from - iterate over list from current point 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 over list of given type from current point, safe against
574 * removal of list entry.
576 #define list_for_each_entry_safe_from(pos, n, head, member) \
577 for (n = list_entry(pos->member.next, typeof(*pos), member); \
578 &pos->member != (head); \
579 pos = n, n = list_entry(n->member.next, typeof(*n), member))
582 * list_for_each_entry_safe_reverse - iterate backwards over list safe against removal
583 * @pos: the type * to use as a loop cursor.
584 * @n: another type * to use as temporary storage
585 * @head: the head for your list.
586 * @member: the name of the list_struct within the struct.
588 * Iterate backwards over list of given type, safe against removal
591 #define list_for_each_entry_safe_reverse(pos, n, head, member) \
592 for (pos = list_entry((head)->prev, typeof(*pos), member), \
593 n = list_entry(pos->member.prev, typeof(*pos), member); \
594 &pos->member != (head); \
595 pos = n, n = list_entry(n->member.prev, typeof(*n), member))
598 * list_safe_reset_next - reset a stale list_for_each_entry_safe loop
599 * @pos: the loop cursor used in the list_for_each_entry_safe loop
600 * @n: temporary storage used in list_for_each_entry_safe
601 * @member: the name of the list_struct within the struct.
603 * list_safe_reset_next is not safe to use in general if the list may be
604 * modified concurrently (eg. the lock is dropped in the loop body). An
605 * exception to this is if the cursor element (pos) is pinned in the list,
606 * and list_safe_reset_next is called after re-taking the lock and before
607 * completing the current iteration of the loop body.
609 #define list_safe_reset_next(pos, n, member) \
610 n = list_entry(pos->member.next, typeof(*pos), member)
613 * Double linked lists with a single pointer list head.
614 * Mostly useful for hash tables where the two pointer list head is
616 * You lose the ability to access the tail in O(1).
619 #define HLIST_HEAD_INIT { .first = NULL }
620 #define HLIST_HEAD(name) struct hlist_head name = { .first = NULL }
621 #define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
622 static inline void INIT_HLIST_NODE(struct hlist_node *h)
628 static inline int hlist_unhashed(const struct hlist_node *h)
633 static inline int hlist_empty(const struct hlist_head *h)
638 static inline void __hlist_del(struct hlist_node *n)
640 struct hlist_node *next = n->next;
641 struct hlist_node **pprev = n->pprev;
647 static inline void hlist_del(struct hlist_node *n)
650 n->next = LIST_POISON1;
651 n->pprev = LIST_POISON2;
654 static inline void hlist_del_init(struct hlist_node *n)
656 if (!hlist_unhashed(n)) {
662 static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
664 struct hlist_node *first = h->first;
667 first->pprev = &n->next;
669 n->pprev = &h->first;
672 /* next must be != NULL */
673 static inline void hlist_add_before(struct hlist_node *n,
674 struct hlist_node *next)
676 n->pprev = next->pprev;
678 next->pprev = &n->next;
682 static inline void hlist_add_after(struct hlist_node *n,
683 struct hlist_node *next)
685 next->next = n->next;
687 next->pprev = &n->next;
690 next->next->pprev = &next->next;
693 /* after that we'll appear to be on some hlist and hlist_del will work */
694 static inline void hlist_add_fake(struct hlist_node *n)
700 * Move a list from one list head to another. Fixup the pprev
701 * reference of the first entry if it exists.
703 static inline void hlist_move_list(struct hlist_head *old,
704 struct hlist_head *new)
706 new->first = old->first;
708 new->first->pprev = &new->first;
712 #define hlist_entry(ptr, type, member) container_of(ptr,type,member)
714 #define hlist_for_each(pos, head) \
715 for (pos = (head)->first; pos ; pos = pos->next)
717 #define hlist_for_each_safe(pos, n, head) \
718 for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
722 * hlist_for_each_entry - iterate over list of given type
723 * @tpos: the type * to use as a loop cursor.
724 * @pos: the &struct hlist_node to use as a loop cursor.
725 * @head: the head for your list.
726 * @member: the name of the hlist_node within the struct.
728 #define hlist_for_each_entry(tpos, pos, head, member) \
729 for (pos = (head)->first; \
731 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
735 * hlist_for_each_entry_continue - iterate over a hlist continuing after current point
736 * @tpos: the type * to use as a loop cursor.
737 * @pos: the &struct hlist_node to use as a loop cursor.
738 * @member: the name of the hlist_node within the struct.
740 #define hlist_for_each_entry_continue(tpos, pos, member) \
741 for (pos = (pos)->next; \
743 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
747 * hlist_for_each_entry_from - iterate over a hlist continuing from current point
748 * @tpos: the type * to use as a loop cursor.
749 * @pos: the &struct hlist_node to use as a loop cursor.
750 * @member: the name of the hlist_node within the struct.
752 #define hlist_for_each_entry_from(tpos, pos, member) \
754 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
758 * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
759 * @tpos: the type * to use as a loop cursor.
760 * @pos: the &struct hlist_node to use as a loop cursor.
761 * @n: another &struct hlist_node to use as temporary storage
762 * @head: the head for your list.
763 * @member: the name of the hlist_node within the struct.
765 #define hlist_for_each_entry_safe(tpos, pos, n, head, member) \
766 for (pos = (head)->first; \
767 pos && ({ n = pos->next; 1; }) && \
768 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \