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 dentry (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 #define LIST_HEAD(name) \
70 struct list_head name = LIST_HEAD_INIT(name)
72 static inline void INIT_LIST_HEAD(struct list_head *list)
79 * Insert a new entry between two known consecutive entries.
81 * This is only for internal list manipulation where we know
82 * the prev/next entries already!
84 #ifndef CONFIG_DEBUG_LIST
85 static inline void __list_add(struct list_head *new,
86 struct list_head *prev,
87 struct list_head *next)
95 extern void __list_add(struct list_head *new,
96 struct list_head *prev,
97 struct list_head *next);
101 * list_add - add a new entry
102 * @new: new entry to be added
103 * @head: list head to add it after
105 * Insert a new entry after the specified head.
106 * This is good for implementing stacks.
108 static inline void list_add(struct list_head *new, struct list_head *head)
110 __list_add(new, head, head->next);
115 * list_add_tail - add a new entry
116 * @new: new entry to be added
117 * @head: list head to add it before
119 * Insert a new entry before the specified head.
120 * This is useful for implementing queues.
122 static inline void list_add_tail(struct list_head *new, struct list_head *head)
124 __list_add(new, head->prev, head);
128 * Delete a list entry by making the prev/next entries
129 * point to each other.
131 * This is only for internal list manipulation where we know
132 * the prev/next entries already!
134 static inline void __list_del(struct list_head * prev, struct list_head * next)
141 * list_del - deletes entry from list.
142 * @entry: the element to delete from the list.
143 * Note: list_empty() on entry does not return true after this, the entry is
144 * in an undefined state.
146 #ifndef CONFIG_DEBUG_LIST
147 static inline void __list_del_entry(struct list_head *entry)
149 __list_del(entry->prev, entry->next);
152 static inline void list_del(struct list_head *entry)
154 __list_del(entry->prev, entry->next);
155 entry->next = LIST_POISON1;
156 entry->prev = LIST_POISON2;
159 extern void __list_del_entry(struct list_head *entry);
160 extern void list_del(struct list_head *entry);
164 * list_replace - replace old entry by new one
165 * @old : the element to be replaced
166 * @new : the new element to insert
168 * If @old was empty, it will be overwritten.
170 static inline void list_replace(struct list_head *old,
171 struct list_head *new)
173 new->next = old->next;
174 new->next->prev = new;
175 new->prev = old->prev;
176 new->prev->next = new;
179 static inline void list_replace_init(struct list_head *old,
180 struct list_head *new)
182 list_replace(old, new);
187 * list_del_init - deletes entry from list and reinitialize it.
188 * @entry: the element to delete from the list.
190 static inline void list_del_init(struct list_head *entry)
192 __list_del_entry(entry);
193 INIT_LIST_HEAD(entry);
197 * list_move - delete from one list and add as another's head
198 * @list: the entry to move
199 * @head: the head that will precede our entry
201 static inline void list_move(struct list_head *list, struct list_head *head)
203 __list_del_entry(list);
204 list_add(list, head);
208 * list_move_tail - delete from one list and add as another's tail
209 * @list: the entry to move
210 * @head: the head that will follow our entry
212 static inline void list_move_tail(struct list_head *list,
213 struct list_head *head)
215 __list_del_entry(list);
216 list_add_tail(list, head);
220 * list_is_last - tests whether @list is the last entry in list @head
221 * @list: the entry to test
222 * @head: the head of the list
224 static inline int list_is_last(const struct list_head *list,
225 const struct list_head *head)
227 return list->next == head;
231 * list_empty - tests whether a list is empty
232 * @head: the list to test.
234 static inline int list_empty(const struct list_head *head)
236 return head->next == head;
240 * list_empty_careful - tests whether a list is empty and not being modified
241 * @head: the list to test
244 * tests whether a list is empty _and_ checks that no other CPU might be
245 * in the process of modifying either member (next or prev)
247 * NOTE: using list_empty_careful() without synchronization
248 * can only be safe if the only activity that can happen
249 * to the list entry is list_del_init(). Eg. it cannot be used
250 * if another CPU could re-list_add() it.
252 static inline int list_empty_careful(const struct list_head *head)
254 struct list_head *next = head->next;
255 return (next == head) && (next == head->prev);
259 * list_rotate_left - rotate the list to the left
260 * @head: the head of the list
262 static inline void list_rotate_left(struct list_head *head)
264 struct list_head *first;
266 if (!list_empty(head)) {
268 list_move_tail(first, head);
273 * list_is_singular - tests whether a list has just one entry.
274 * @head: the list to test.
276 static inline int list_is_singular(const struct list_head *head)
278 return !list_empty(head) && (head->next == head->prev);
281 static inline void __list_cut_position(struct list_head *list,
282 struct list_head *head, struct list_head *entry)
284 struct list_head *new_first = entry->next;
285 list->next = head->next;
286 list->next->prev = list;
289 head->next = new_first;
290 new_first->prev = head;
294 * list_cut_position - cut a list into two
295 * @list: a new list to add all removed entries
296 * @head: a list with entries
297 * @entry: an entry within head, could be the head itself
298 * and if so we won't cut the list
300 * This helper moves the initial part of @head, up to and
301 * including @entry, from @head to @list. You should
302 * pass on @entry an element you know is on @head. @list
303 * should be an empty list or a list you do not care about
307 static inline void list_cut_position(struct list_head *list,
308 struct list_head *head, struct list_head *entry)
310 if (list_empty(head))
312 if (list_is_singular(head) &&
313 (head->next != entry && head != entry))
316 INIT_LIST_HEAD(list);
318 __list_cut_position(list, head, entry);
321 static inline void __list_splice(const struct list_head *list,
322 struct list_head *prev,
323 struct list_head *next)
325 struct list_head *first = list->next;
326 struct list_head *last = list->prev;
336 * list_splice - join two lists, this is designed for stacks
337 * @list: the new list to add.
338 * @head: the place to add it in the first list.
340 static inline void list_splice(const struct list_head *list,
341 struct list_head *head)
343 if (!list_empty(list))
344 __list_splice(list, head, head->next);
348 * list_splice_tail - join two lists, each list being a queue
349 * @list: the new list to add.
350 * @head: the place to add it in the first list.
352 static inline void list_splice_tail(struct list_head *list,
353 struct list_head *head)
355 if (!list_empty(list))
356 __list_splice(list, head->prev, head);
360 * list_splice_init - join two lists and reinitialise the emptied list.
361 * @list: the new list to add.
362 * @head: the place to add it in the first list.
364 * The list at @list is reinitialised
366 static inline void list_splice_init(struct list_head *list,
367 struct list_head *head)
369 if (!list_empty(list)) {
370 __list_splice(list, head, head->next);
371 INIT_LIST_HEAD(list);
376 * list_splice_tail_init - join two lists and reinitialise the emptied list
377 * @list: the new list to add.
378 * @head: the place to add it in the first list.
380 * Each of the lists is a queue.
381 * The list at @list is reinitialised
383 static inline void list_splice_tail_init(struct list_head *list,
384 struct list_head *head)
386 if (!list_empty(list)) {
387 __list_splice(list, head->prev, head);
388 INIT_LIST_HEAD(list);
393 * list_entry - get the struct for this entry
394 * @ptr: the &struct list_head pointer.
395 * @type: the type of the struct this is embedded in.
396 * @member: the name of the list_struct within the struct.
398 #define list_entry(ptr, type, member) \
399 container_of(ptr, type, member)
402 * list_first_entry - get the first element from a list
403 * @ptr: the list head to take the element from.
404 * @type: the type of the struct this is embedded in.
405 * @member: the name of the list_struct within the struct.
407 * Note, that list is expected to be not empty.
409 #define list_first_entry(ptr, type, member) \
410 list_entry((ptr)->next, type, member)
413 * list_for_each - iterate over a list
414 * @pos: the &struct list_head to use as a loop cursor.
415 * @head: the head for your list.
417 #define list_for_each(pos, head) \
418 for (pos = (head)->next; pos != (head); pos = pos->next)
421 * __list_for_each - iterate over a list
422 * @pos: the &struct list_head to use as a loop cursor.
423 * @head: the head for your list.
425 * This variant doesn't differ from list_for_each() any more.
426 * We don't do prefetching in either case.
428 #define __list_for_each(pos, head) \
429 for (pos = (head)->next; pos != (head); pos = pos->next)
432 * list_for_each_prev - iterate over a list backwards
433 * @pos: the &struct list_head to use as a loop cursor.
434 * @head: the head for your list.
436 #define list_for_each_prev(pos, head) \
437 for (pos = (head)->prev; pos != (head); pos = pos->prev)
440 * list_for_each_safe - iterate over a list safe against removal of list entry
441 * @pos: the &struct list_head to use as a loop cursor.
442 * @n: another &struct list_head to use as temporary storage
443 * @head: the head for your list.
445 #define list_for_each_safe(pos, n, head) \
446 for (pos = (head)->next, n = pos->next; pos != (head); \
447 pos = n, n = pos->next)
450 * list_for_each_prev_safe - iterate over a list backwards safe against removal of list entry
451 * @pos: the &struct list_head to use as a loop cursor.
452 * @n: another &struct list_head to use as temporary storage
453 * @head: the head for your list.
455 #define list_for_each_prev_safe(pos, n, head) \
456 for (pos = (head)->prev, n = pos->prev; \
458 pos = n, n = pos->prev)
461 * list_for_each_entry - iterate 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(pos, head, member) \
467 for (pos = list_entry((head)->next, typeof(*pos), member); \
468 &pos->member != (head); \
469 pos = list_entry(pos->member.next, typeof(*pos), member))
472 * list_for_each_entry_reverse - iterate backwards over list of given type.
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 #define list_for_each_entry_reverse(pos, head, member) \
478 for (pos = list_entry((head)->prev, typeof(*pos), member); \
479 &pos->member != (head); \
480 pos = list_entry(pos->member.prev, typeof(*pos), member))
483 * list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue()
484 * @pos: the type * to use as a start point
485 * @head: the head of the list
486 * @member: the name of the list_struct within the struct.
488 * Prepares a pos entry for use as a start point in list_for_each_entry_continue().
490 #define list_prepare_entry(pos, head, member) \
491 ((pos) ? : list_entry(head, typeof(*pos), member))
494 * list_for_each_entry_continue - continue iteration over list of given type
495 * @pos: the type * to use as a loop cursor.
496 * @head: the head for your list.
497 * @member: the name of the list_struct within the struct.
499 * Continue to iterate over list of given type, continuing after
500 * the current position.
502 #define list_for_each_entry_continue(pos, head, member) \
503 for (pos = list_entry(pos->member.next, typeof(*pos), member); \
504 &pos->member != (head); \
505 pos = list_entry(pos->member.next, typeof(*pos), member))
508 * list_for_each_entry_continue_reverse - iterate backwards from the given point
509 * @pos: the type * to use as a loop cursor.
510 * @head: the head for your list.
511 * @member: the name of the list_struct within the struct.
513 * Start to iterate over list of given type backwards, continuing after
514 * the current position.
516 #define list_for_each_entry_continue_reverse(pos, head, member) \
517 for (pos = list_entry(pos->member.prev, typeof(*pos), member); \
518 &pos->member != (head); \
519 pos = list_entry(pos->member.prev, typeof(*pos), member))
522 * list_for_each_entry_from - iterate over list of given type from the current point
523 * @pos: the type * to use as a loop cursor.
524 * @head: the head for your list.
525 * @member: the name of the list_struct within the struct.
527 * Iterate over list of given type, continuing from current position.
529 #define list_for_each_entry_from(pos, head, member) \
530 for (; &pos->member != (head); \
531 pos = list_entry(pos->member.next, typeof(*pos), member))
534 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
535 * @pos: the type * to use as a loop cursor.
536 * @n: another type * to use as temporary storage
537 * @head: the head for your list.
538 * @member: the name of the list_struct within the struct.
540 #define list_for_each_entry_safe(pos, n, head, member) \
541 for (pos = list_entry((head)->next, typeof(*pos), member), \
542 n = list_entry(pos->member.next, typeof(*pos), member); \
543 &pos->member != (head); \
544 pos = n, n = list_entry(n->member.next, typeof(*n), member))
547 * list_for_each_entry_safe_continue - continue list iteration safe against removal
548 * @pos: the type * to use as a loop cursor.
549 * @n: another type * to use as temporary storage
550 * @head: the head for your list.
551 * @member: the name of the list_struct within the struct.
553 * Iterate over list of given type, continuing after current point,
554 * safe against removal of list entry.
556 #define list_for_each_entry_safe_continue(pos, n, head, member) \
557 for (pos = list_entry(pos->member.next, typeof(*pos), member), \
558 n = list_entry(pos->member.next, typeof(*pos), member); \
559 &pos->member != (head); \
560 pos = n, n = list_entry(n->member.next, typeof(*n), member))
563 * list_for_each_entry_safe_from - iterate over list from current point safe against removal
564 * @pos: the type * to use as a loop cursor.
565 * @n: another type * to use as temporary storage
566 * @head: the head for your list.
567 * @member: the name of the list_struct within the struct.
569 * Iterate over list of given type from current point, safe against
570 * removal of list entry.
572 #define list_for_each_entry_safe_from(pos, n, head, member) \
573 for (n = list_entry(pos->member.next, typeof(*pos), member); \
574 &pos->member != (head); \
575 pos = n, n = list_entry(n->member.next, typeof(*n), member))
578 * list_for_each_entry_safe_reverse - iterate backwards over list safe against removal
579 * @pos: the type * to use as a loop cursor.
580 * @n: another type * to use as temporary storage
581 * @head: the head for your list.
582 * @member: the name of the list_struct within the struct.
584 * Iterate backwards over list of given type, safe against removal
587 #define list_for_each_entry_safe_reverse(pos, n, head, member) \
588 for (pos = list_entry((head)->prev, typeof(*pos), member), \
589 n = list_entry(pos->member.prev, typeof(*pos), member); \
590 &pos->member != (head); \
591 pos = n, n = list_entry(n->member.prev, typeof(*n), member))
594 * list_safe_reset_next - reset a stale list_for_each_entry_safe loop
595 * @pos: the loop cursor used in the list_for_each_entry_safe loop
596 * @n: temporary storage used in list_for_each_entry_safe
597 * @member: the name of the list_struct within the struct.
599 * list_safe_reset_next is not safe to use in general if the list may be
600 * modified concurrently (eg. the lock is dropped in the loop body). An
601 * exception to this is if the cursor element (pos) is pinned in the list,
602 * and list_safe_reset_next is called after re-taking the lock and before
603 * completing the current iteration of the loop body.
605 #define list_safe_reset_next(pos, n, member) \
606 n = list_entry(pos->member.next, typeof(*pos), member)
609 * Double linked lists with a single pointer list head.
610 * Mostly useful for hash tables where the two pointer list head is
612 * You lose the ability to access the tail in O(1).
615 #define HLIST_HEAD_INIT { .first = NULL }
616 #define HLIST_HEAD(name) struct hlist_head name = { .first = NULL }
617 #define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
618 static inline void INIT_HLIST_NODE(struct hlist_node *h)
624 static inline int hlist_unhashed(const struct hlist_node *h)
629 static inline int hlist_empty(const struct hlist_head *h)
634 static inline void __hlist_del(struct hlist_node *n)
636 struct hlist_node *next = n->next;
637 struct hlist_node **pprev = n->pprev;
643 static inline void hlist_del(struct hlist_node *n)
646 n->next = LIST_POISON1;
647 n->pprev = LIST_POISON2;
650 static inline void hlist_del_init(struct hlist_node *n)
652 if (!hlist_unhashed(n)) {
658 static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
660 struct hlist_node *first = h->first;
663 first->pprev = &n->next;
665 n->pprev = &h->first;
668 /* next must be != NULL */
669 static inline void hlist_add_before(struct hlist_node *n,
670 struct hlist_node *next)
672 n->pprev = next->pprev;
674 next->pprev = &n->next;
678 static inline void hlist_add_after(struct hlist_node *n,
679 struct hlist_node *next)
681 next->next = n->next;
683 next->pprev = &n->next;
686 next->next->pprev = &next->next;
689 /* after that we'll appear to be on some hlist and hlist_del will work */
690 static inline void hlist_add_fake(struct hlist_node *n)
696 * Move a list from one list head to another. Fixup the pprev
697 * reference of the first entry if it exists.
699 static inline void hlist_move_list(struct hlist_head *old,
700 struct hlist_head *new)
702 new->first = old->first;
704 new->first->pprev = &new->first;
708 #define hlist_entry(ptr, type, member) container_of(ptr,type,member)
710 #define hlist_for_each(pos, head) \
711 for (pos = (head)->first; pos ; pos = pos->next)
713 #define hlist_for_each_safe(pos, n, head) \
714 for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
718 * hlist_for_each_entry - iterate over list of given type
719 * @tpos: the type * to use as a loop cursor.
720 * @pos: the &struct hlist_node to use as a loop cursor.
721 * @head: the head for your list.
722 * @member: the name of the hlist_node within the struct.
724 #define hlist_for_each_entry(tpos, pos, head, member) \
725 for (pos = (head)->first; \
727 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
731 * hlist_for_each_entry_continue - iterate over a hlist continuing after current point
732 * @tpos: the type * to use as a loop cursor.
733 * @pos: the &struct hlist_node to use as a loop cursor.
734 * @member: the name of the hlist_node within the struct.
736 #define hlist_for_each_entry_continue(tpos, pos, member) \
737 for (pos = (pos)->next; \
739 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
743 * hlist_for_each_entry_from - iterate over a hlist continuing from current point
744 * @tpos: the type * to use as a loop cursor.
745 * @pos: the &struct hlist_node to use as a loop cursor.
746 * @member: the name of the hlist_node within the struct.
748 #define hlist_for_each_entry_from(tpos, pos, member) \
750 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
754 * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
755 * @tpos: the type * to use as a loop cursor.
756 * @pos: the &struct hlist_node to use as a loop cursor.
757 * @n: another &struct hlist_node to use as temporary storage
758 * @head: the head for your list.
759 * @member: the name of the hlist_node within the struct.
761 #define hlist_for_each_entry_safe(tpos, pos, n, head, member) \
762 for (pos = (head)->first; \
763 pos && ({ n = pos->next; 1; }) && \
764 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \