7 * container_of - cast a member of a structure out to the containing structure
8 * @ptr: the pointer to the member.
9 * @type: the type of the container struct this is embedded in.
10 * @member: the name of the member within the struct.
13 #define container_of(ptr, type, member) ({ \
14 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
15 (type *)( (char *)__mptr - offsetof(type,member) );})
18 # define POISON_POINTER_DELTA 0
20 * These are non-NULL pointers that will result in page faults
21 * under normal circumstances, used to verify that nobody uses
22 * non-initialized list entries.
24 #define LIST_POISON1 ((void *) 0x00100100 + POISON_POINTER_DELTA)
25 #define LIST_POISON2 ((void *) 0x00200200 + POISON_POINTER_DELTA)
27 struct list_head *next, *prev;
30 struct hlist_node *first;
34 struct hlist_node *next, **pprev;
37 struct stream_list_head {
38 struct list_head list;
40 STREAM_TYPE_NORMAL = 0,
46 * Simple doubly linked list implementation.
48 * Some of the internal functions ("__xxx") are useful when
49 * manipulating whole lists rather than single entries, as
50 * sometimes we already know the next/prev entries and we can
51 * generate better code by using them directly rather than
52 * using the generic single-entry routines.
55 #define LIST_HEAD_INIT(name) { &(name), &(name) }
57 #define LIST_HEAD(name) \
58 struct list_head name = LIST_HEAD_INIT(name)
60 static inline void INIT_LIST_HEAD(struct list_head *list)
67 * Insert a new entry between two known consecutive entries.
69 * This is only for internal list manipulation where we know
70 * the prev/next entries already!
72 #ifndef CONFIG_DEBUG_LIST
73 static inline void __list_add(struct list_head *new,
74 struct list_head *prev,
75 struct list_head *next)
83 extern void __list_add(struct list_head *new,
84 struct list_head *prev,
85 struct list_head *next);
89 * list_add - add a new entry
90 * @new: new entry to be added
91 * @head: list head to add it after
93 * Insert a new entry after the specified head.
94 * This is good for implementing stacks.
96 static inline void list_add(struct list_head *new, struct list_head *head)
98 __list_add(new, head, head->next);
103 * list_add_tail - add a new entry
104 * @new: new entry to be added
105 * @head: list head to add it before
107 * Insert a new entry before the specified head.
108 * This is useful for implementing queues.
110 static inline void list_add_tail(struct list_head *new, struct list_head *head)
112 __list_add(new, head->prev, head);
116 * Delete a list entry by making the prev/next entries
117 * point to each other.
119 * This is only for internal list manipulation where we know
120 * the prev/next entries already!
122 static inline void __list_del(struct list_head * prev, struct list_head * next)
129 * list_del - deletes entry from list.
130 * @entry: the element to delete from the list.
131 * Note: list_empty() on entry does not return true after this, the entry is
132 * in an undefined state.
134 #ifndef CONFIG_DEBUG_LIST
135 static inline void __list_del_entry(struct list_head *entry)
137 __list_del(entry->prev, entry->next);
140 static inline void list_del(struct list_head *entry)
142 __list_del(entry->prev, entry->next);
143 entry->next = LIST_POISON1;
144 entry->prev = LIST_POISON2;
147 extern void __list_del_entry(struct list_head *entry);
148 extern void list_del(struct list_head *entry);
152 * list_replace - replace old entry by new one
153 * @old : the element to be replaced
154 * @new : the new element to insert
156 * If @old was empty, it will be overwritten.
158 static inline void list_replace(struct list_head *old,
159 struct list_head *new)
161 new->next = old->next;
162 new->next->prev = new;
163 new->prev = old->prev;
164 new->prev->next = new;
167 static inline void list_replace_init(struct list_head *old,
168 struct list_head *new)
170 list_replace(old, new);
175 * list_del_init - deletes entry from list and reinitialize it.
176 * @entry: the element to delete from the list.
178 static inline void list_del_init(struct list_head *entry)
180 __list_del_entry(entry);
181 INIT_LIST_HEAD(entry);
185 * list_move - delete from one list and add as another's head
186 * @list: the entry to move
187 * @head: the head that will precede our entry
189 static inline void list_move(struct list_head *list, struct list_head *head)
191 __list_del_entry(list);
192 list_add(list, head);
196 * list_move_tail - delete from one list and add as another's tail
197 * @list: the entry to move
198 * @head: the head that will follow our entry
200 static inline void list_move_tail(struct list_head *list,
201 struct list_head *head)
203 __list_del_entry(list);
204 list_add_tail(list, head);
208 * list_is_last - tests whether @list is the last entry in list @head
209 * @list: the entry to test
210 * @head: the head of the list
212 static inline int list_is_last(const struct list_head *list,
213 const struct list_head *head)
215 return list->next == head;
219 * list_empty - tests whether a list is empty
220 * @head: the list to test.
222 static inline int list_empty(const struct list_head *head)
224 return head->next == head;
228 * list_empty_careful - tests whether a list is empty and not being modified
229 * @head: the list to test
232 * tests whether a list is empty _and_ checks that no other CPU might be
233 * in the process of modifying either member (next or prev)
235 * NOTE: using list_empty_careful() without synchronization
236 * can only be safe if the only activity that can happen
237 * to the list entry is list_del_init(). Eg. it cannot be used
238 * if another CPU could re-list_add() it.
240 static inline int list_empty_careful(const struct list_head *head)
242 struct list_head *next = head->next;
243 return (next == head) && (next == head->prev);
247 * list_rotate_left - rotate the list to the left
248 * @head: the head of the list
250 static inline void list_rotate_left(struct list_head *head)
252 struct list_head *first;
254 if (!list_empty(head)) {
256 list_move_tail(first, head);
261 * list_is_singular - tests whether a list has just one entry.
262 * @head: the list to test.
264 static inline int list_is_singular(const struct list_head *head)
266 return !list_empty(head) && (head->next == head->prev);
269 static inline void __list_cut_position(struct list_head *list,
270 struct list_head *head, struct list_head *entry)
272 struct list_head *new_first = entry->next;
273 list->next = head->next;
274 list->next->prev = list;
277 head->next = new_first;
278 new_first->prev = head;
282 * list_cut_position - cut a list into two
283 * @list: a new list to add all removed entries
284 * @head: a list with entries
285 * @entry: an entry within head, could be the head itself
286 * and if so we won't cut the list
288 * This helper moves the initial part of @head, up to and
289 * including @entry, from @head to @list. You should
290 * pass on @entry an element you know is on @head. @list
291 * should be an empty list or a list you do not care about
295 static inline void list_cut_position(struct list_head *list,
296 struct list_head *head, struct list_head *entry)
298 if (list_empty(head))
300 if (list_is_singular(head) &&
301 (head->next != entry && head != entry))
304 INIT_LIST_HEAD(list);
306 __list_cut_position(list, head, entry);
309 static inline void __list_splice(const struct list_head *list,
310 struct list_head *prev,
311 struct list_head *next)
313 struct list_head *first = list->next;
314 struct list_head *last = list->prev;
324 * list_splice - join two lists, this is designed for stacks
325 * @list: the new list to add.
326 * @head: the place to add it in the first list.
328 static inline void list_splice(const struct list_head *list,
329 struct list_head *head)
331 if (!list_empty(list))
332 __list_splice(list, head, head->next);
336 * list_splice_tail - join two lists, each list being a queue
337 * @list: the new list to add.
338 * @head: the place to add it in the first list.
340 static inline void list_splice_tail(struct list_head *list,
341 struct list_head *head)
343 if (!list_empty(list))
344 __list_splice(list, head->prev, head);
348 * list_splice_init - join two lists and reinitialise the emptied list.
349 * @list: the new list to add.
350 * @head: the place to add it in the first list.
352 * The list at @list is reinitialised
354 static inline void list_splice_init(struct list_head *list,
355 struct list_head *head)
357 if (!list_empty(list)) {
358 __list_splice(list, head, head->next);
359 INIT_LIST_HEAD(list);
364 * list_splice_tail_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 * Each of the lists is a queue.
369 * The list at @list is reinitialised
371 static inline void list_splice_tail_init(struct list_head *list,
372 struct list_head *head)
374 if (!list_empty(list)) {
375 __list_splice(list, head->prev, head);
376 INIT_LIST_HEAD(list);
381 * list_entry - get the struct for this entry
382 * @ptr: the &struct list_head pointer.
383 * @type: the type of the struct this is embedded in.
384 * @member: the name of the list_struct within the struct.
386 #define list_entry(ptr, type, member) \
387 container_of(ptr, type, member)
390 * list_first_entry - get the first element from a list
391 * @ptr: the list head to take the element from.
392 * @type: the type of the struct this is embedded in.
393 * @member: the name of the list_struct within the struct.
395 * Note, that list is expected to be not empty.
397 #define list_first_entry(ptr, type, member) \
398 list_entry((ptr)->next, type, member)
401 * list_for_each - iterate over a list
402 * @pos: the &struct list_head to use as a loop cursor.
403 * @head: the head for your list.
405 #define list_for_each(pos, head) \
406 for (pos = (head)->next; pos != (head); pos = pos->next)
409 * __list_for_each - iterate over a list
410 * @pos: the &struct list_head to use as a loop cursor.
411 * @head: the head for your list.
413 * This variant doesn't differ from list_for_each() any more.
414 * We don't do prefetching in either case.
416 #define __list_for_each(pos, head) \
417 for (pos = (head)->next; pos != (head); pos = pos->next)
420 * list_for_each_prev - iterate over a list backwards
421 * @pos: the &struct list_head to use as a loop cursor.
422 * @head: the head for your list.
424 #define list_for_each_prev(pos, head) \
425 for (pos = (head)->prev; pos != (head); pos = pos->prev)
428 * list_for_each_safe - iterate over a list safe against removal of list entry
429 * @pos: the &struct list_head to use as a loop cursor.
430 * @n: another &struct list_head to use as temporary storage
431 * @head: the head for your list.
433 #define list_for_each_safe(pos, n, head) \
434 for (pos = (head)->next, n = pos->next; pos != (head); \
435 pos = n, n = pos->next)
438 * list_for_each_prev_safe - iterate over a list backwards safe against removal of list entry
439 * @pos: the &struct list_head to use as a loop cursor.
440 * @n: another &struct list_head to use as temporary storage
441 * @head: the head for your list.
443 #define list_for_each_prev_safe(pos, n, head) \
444 for (pos = (head)->prev, n = pos->prev; \
446 pos = n, n = pos->prev)
449 * list_for_each_entry - iterate over list of given type
450 * @pos: the type * to use as a loop cursor.
451 * @head: the head for your list.
452 * @member: the name of the list_struct within the struct.
454 #define list_for_each_entry(pos, head, member) \
455 for (pos = list_entry((head)->next, typeof(*pos), member); \
456 &pos->member != (head); \
457 pos = list_entry(pos->member.next, typeof(*pos), member))
460 * list_for_each_entry_reverse - iterate backwards over list of given type.
461 * @pos: the type * to use as a loop cursor.
462 * @head: the head for your list.
463 * @member: the name of the list_struct within the struct.
465 #define list_for_each_entry_reverse(pos, head, member) \
466 for (pos = list_entry((head)->prev, typeof(*pos), member); \
467 &pos->member != (head); \
468 pos = list_entry(pos->member.prev, typeof(*pos), member))
471 * list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue()
472 * @pos: the type * to use as a start point
473 * @head: the head of the list
474 * @member: the name of the list_struct within the struct.
476 * Prepares a pos entry for use as a start point in list_for_each_entry_continue().
478 #define list_prepare_entry(pos, head, member) \
479 ((pos) ? : list_entry(head, typeof(*pos), member))
482 * list_for_each_entry_continue - continue iteration over list of given type
483 * @pos: the type * to use as a loop cursor.
484 * @head: the head for your list.
485 * @member: the name of the list_struct within the struct.
487 * Continue to iterate over list of given type, continuing after
488 * the current position.
490 #define list_for_each_entry_continue(pos, head, member) \
491 for (pos = list_entry(pos->member.next, typeof(*pos), member); \
492 &pos->member != (head); \
493 pos = list_entry(pos->member.next, typeof(*pos), member))
496 * list_for_each_entry_continue_reverse - iterate backwards from the given point
497 * @pos: the type * to use as a loop cursor.
498 * @head: the head for your list.
499 * @member: the name of the list_struct within the struct.
501 * Start to iterate over list of given type backwards, continuing after
502 * the current position.
504 #define list_for_each_entry_continue_reverse(pos, head, member) \
505 for (pos = list_entry(pos->member.prev, typeof(*pos), member); \
506 &pos->member != (head); \
507 pos = list_entry(pos->member.prev, typeof(*pos), member))
510 * list_for_each_entry_from - iterate over list of given type from the current point
511 * @pos: the type * to use as a loop cursor.
512 * @head: the head for your list.
513 * @member: the name of the list_struct within the struct.
515 * Iterate over list of given type, continuing from current position.
517 #define list_for_each_entry_from(pos, head, member) \
518 for (; &pos->member != (head); \
519 pos = list_entry(pos->member.next, typeof(*pos), member))
522 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
523 * @pos: the type * to use as a loop cursor.
524 * @n: another type * to use as temporary storage
525 * @head: the head for your list.
526 * @member: the name of the list_struct within the struct.
528 #define list_for_each_entry_safe(pos, n, head, member) \
529 for (pos = list_entry((head)->next, typeof(*pos), member), \
530 n = list_entry(pos->member.next, typeof(*pos), member); \
531 &pos->member != (head); \
532 pos = n, n = list_entry(n->member.next, typeof(*n), member))
535 * list_for_each_entry_safe_continue - continue list iteration safe against removal
536 * @pos: the type * to use as a loop cursor.
537 * @n: another type * to use as temporary storage
538 * @head: the head for your list.
539 * @member: the name of the list_struct within the struct.
541 * Iterate over list of given type, continuing after current point,
542 * safe against removal of list entry.
544 #define list_for_each_entry_safe_continue(pos, n, head, member) \
545 for (pos = list_entry(pos->member.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_from - iterate over list from current point 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 from current point, safe against
558 * removal of list entry.
560 #define list_for_each_entry_safe_from(pos, n, head, member) \
561 for (n = list_entry(pos->member.next, typeof(*pos), member); \
562 &pos->member != (head); \
563 pos = n, n = list_entry(n->member.next, typeof(*n), member))
566 * list_for_each_entry_safe_reverse - iterate backwards over list safe against removal
567 * @pos: the type * to use as a loop cursor.
568 * @n: another type * to use as temporary storage
569 * @head: the head for your list.
570 * @member: the name of the list_struct within the struct.
572 * Iterate backwards over list of given type, safe against removal
575 #define list_for_each_entry_safe_reverse(pos, n, head, member) \
576 for (pos = list_entry((head)->prev, typeof(*pos), member), \
577 n = list_entry(pos->member.prev, typeof(*pos), member); \
578 &pos->member != (head); \
579 pos = n, n = list_entry(n->member.prev, typeof(*n), member))
582 * list_safe_reset_next - reset a stale list_for_each_entry_safe loop
583 * @pos: the loop cursor used in the list_for_each_entry_safe loop
584 * @n: temporary storage used in list_for_each_entry_safe
585 * @member: the name of the list_struct within the struct.
587 * list_safe_reset_next is not safe to use in general if the list may be
588 * modified concurrently (eg. the lock is dropped in the loop body). An
589 * exception to this is if the cursor element (pos) is pinned in the list,
590 * and list_safe_reset_next is called after re-taking the lock and before
591 * completing the current iteration of the loop body.
593 #define list_safe_reset_next(pos, n, member) \
594 n = list_entry(pos->member.next, typeof(*pos), member)
597 * Double linked lists with a single pointer list head.
598 * Mostly useful for hash tables where the two pointer list head is
600 * You lose the ability to access the tail in O(1).
603 #define HLIST_HEAD_INIT { .first = NULL }
604 #define HLIST_HEAD(name) struct hlist_head name = { .first = NULL }
605 #define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
606 static inline void INIT_HLIST_NODE(struct hlist_node *h)
612 static inline int hlist_unhashed(const struct hlist_node *h)
617 static inline int hlist_empty(const struct hlist_head *h)
622 static inline void __hlist_del(struct hlist_node *n)
624 struct hlist_node *next = n->next;
625 struct hlist_node **pprev = n->pprev;
631 static inline void hlist_del(struct hlist_node *n)
634 n->next = LIST_POISON1;
635 n->pprev = LIST_POISON2;
638 static inline void hlist_del_init(struct hlist_node *n)
640 if (!hlist_unhashed(n)) {
646 static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
648 struct hlist_node *first = h->first;
651 first->pprev = &n->next;
653 n->pprev = &h->first;
656 /* next must be != NULL */
657 static inline void hlist_add_before(struct hlist_node *n,
658 struct hlist_node *next)
660 n->pprev = next->pprev;
662 next->pprev = &n->next;
666 static inline void hlist_add_after(struct hlist_node *n,
667 struct hlist_node *next)
669 next->next = n->next;
671 next->pprev = &n->next;
674 next->next->pprev = &next->next;
677 /* after that we'll appear to be on some hlist and hlist_del will work */
678 static inline void hlist_add_fake(struct hlist_node *n)
684 * Move a list from one list head to another. Fixup the pprev
685 * reference of the first entry if it exists.
687 static inline void hlist_move_list(struct hlist_head *old,
688 struct hlist_head *new)
690 new->first = old->first;
692 new->first->pprev = &new->first;
696 #define hlist_entry(ptr, type, member) container_of(ptr,type,member)
698 #define hlist_for_each(pos, head) \
699 for (pos = (head)->first; pos ; pos = pos->next)
701 #define hlist_for_each_safe(pos, n, head) \
702 for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
706 * hlist_for_each_entry - iterate over list of given type
707 * @tpos: the type * to use as a loop cursor.
708 * @pos: the &struct hlist_node to use as a loop cursor.
709 * @head: the head for your list.
710 * @member: the name of the hlist_node within the struct.
712 #define hlist_for_each_entry(tpos, pos, head, member) \
713 for (pos = (head)->first; \
715 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
719 * hlist_for_each_entry_continue - iterate over a hlist continuing after current point
720 * @tpos: the type * to use as a loop cursor.
721 * @pos: the &struct hlist_node to use as a loop cursor.
722 * @member: the name of the hlist_node within the struct.
724 #define hlist_for_each_entry_continue(tpos, pos, member) \
725 for (pos = (pos)->next; \
727 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
731 * hlist_for_each_entry_from - iterate over a hlist continuing from 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_from(tpos, pos, member) \
738 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
742 * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
743 * @tpos: the type * to use as a loop cursor.
744 * @pos: the &struct hlist_node to use as a loop cursor.
745 * @n: another &struct hlist_node to use as temporary storage
746 * @head: the head for your list.
747 * @member: the name of the hlist_node within the struct.
749 #define hlist_for_each_entry_safe(tpos, pos, n, head, member) \
750 for (pos = (head)->first; \
751 pos && ({ n = pos->next; 1; }) && \
752 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \