2 * Copied from the Linux kernel source tree, version 2.6.13.
4 * Licensed under the GPL v2 as per the whole kernel source tree.
8 #include <stddef.h> /* offsetof */
12 #define container_of(ptr, type, member) ({ \
13 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
14 (type *)( (char *)__mptr - offsetof(type,member) );})
17 * These are non-NULL pointers that will result in page faults
18 * under normal circumstances, used to verify that nobody uses
19 * non-initialized list entries.
21 #define LIST_POISON1 ((void *) 0x00100100)
22 #define LIST_POISON2 ((void *) 0x00200200)
25 * Simple doubly linked list implementation.
27 * Some of the internal functions ("__xxx") are useful when
28 * manipulating whole lists rather than single entries, as
29 * sometimes we already know the next/prev entries and we can
30 * generate better code by using them directly rather than
31 * using the generic single-entry routines.
35 struct list_head *next, *prev;
38 #define INIT_LIST_HEAD(ptr) do { \
39 (ptr)->next = (ptr); (ptr)->prev = (ptr); \
43 * Insert a new entry between two known consecutive entries.
45 * This is only for internal list manipulation where we know
46 * the prev/next entries already!
48 static inline void __list_add(struct list_head *new,
49 struct list_head *prev,
50 struct list_head *next)
61 * \param new: new entry to be added
62 * \param head: list head to add it after
64 * Insert a new entry after the specified head.
65 * This is good for implementing stacks.
67 static inline void para_list_add(struct list_head *new, struct list_head *head)
69 __list_add(new, head, head->next);
73 * list_add_tail - add a new entry
74 * @new: new entry to be added
75 * @head: list head to add it before
77 * Insert a new entry before the specified head.
78 * This is useful for implementing queues.
80 static inline void list_add_tail(struct list_head *new, struct list_head *head)
82 __list_add(new, head->prev, head);
86 * Delete a list entry by making the prev/next entries
87 * point to each other.
89 * This is only for internal list manipulation where we know
90 * the prev/next entries already!
92 static inline void __list_del(struct list_head * prev, struct list_head * next)
99 * list_del - deletes entry from list.
100 * @entry: the element to delete from the list.
101 * Note: list_empty on entry does not return true after this, the entry is
102 * in an undefined state.
104 static inline void list_del(struct list_head *entry)
106 __list_del(entry->prev, entry->next);
107 entry->next = LIST_POISON1;
108 entry->prev = LIST_POISON2;
112 * list_move - delete from one list and add as another's head
113 * @list: the entry to move
114 * @head: the head that will precede our entry
116 static inline void list_move(struct list_head *list, struct list_head *head)
118 __list_del(list->prev, list->next);
119 para_list_add(list, head);
123 * list_empty - tests whether a list is empty
124 * @head: the list to test.
126 static inline int list_empty(const struct list_head *head)
128 return head->next == head;
132 * list_entry - get the struct for this entry
133 * @ptr: the &struct list_head pointer.
134 * @type: the type of the struct this is embedded in.
135 * @member: the name of the list_struct within the struct.
137 #define list_entry(ptr, type, member) \
138 container_of(ptr, type, member)
141 * list_for_each_safe - iterate over a list safe against removal of list entry
142 * @pos: the &struct list_head to use as a loop counter.
143 * @n: another &struct list_head to use as temporary storage
144 * @head: the head for your list.
146 #define list_for_each_safe(pos, n, head) \
147 for (pos = (head)->next, n = pos->next; pos != (head); \
148 pos = n, n = pos->next)
151 * list_for_each_entry - iterate over list of given type
152 * @pos: the type * to use as a loop counter.
153 * @head: the head for your list.
154 * @member: the name of the list_struct within the struct.
156 #define list_for_each_entry(pos, head, member) \
157 for (pos = list_entry((head)->next, typeof(*pos), member); \
158 &pos->member != (head); \
159 pos = list_entry(pos->member.next, typeof(*pos), member))
162 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
163 * @pos: the type * to use as a loop counter.
164 * @n: another type * to use as temporary storage
165 * @head: the head for your list.
166 * @member: the name of the list_struct within the struct.
168 #define list_for_each_entry_safe(pos, n, head, member) \
169 for (pos = list_entry((head)->next, typeof(*pos), member), \
170 n = list_entry(pos->member.next, typeof(*pos), member); \
171 &pos->member != (head); \
172 pos = n, n = list_entry(n->member.next, typeof(*n), member))
174 * list_for_each_entry_safe_reverse - iterate backwards over list of given type safe against
175 * removal of list entry
176 * @pos: the type * to use as a loop counter.
177 * @n: another type * to use as temporary storage
178 * @head: the head for your list.
179 * @member: the name of the list_struct within the struct.
181 #define list_for_each_entry_safe_reverse(pos, n, head, member) \
182 for (pos = list_entry((head)->prev, typeof(*pos), member), \
183 n = list_entry(pos->member.prev, typeof(*pos), member); \
184 &pos->member != (head); \
185 pos = n, n = list_entry(n->member.prev, typeof(*n), member))