2 * Copied from the Linux kernel source tree, version 2.6.0-test1.
4 * Licensed under the GPL v2 as per the whole kernel source tree.
12 * container_of - cast a member of a structure out to the containing structure
14 * @ptr: the pointer to the member.
15 * @type: the type of the container struct this is embedded in.
16 * @member: the name of the member within the struct.
19 #define container_of(ptr, type, member) ({ \
20 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
21 (type *)( (char *)__mptr - offsetof(type,member) );})
24 * These are non-NULL pointers that will result in page faults
25 * under normal circumstances, used to verify that nobody uses
26 * non-initialized list entries.
28 #define LIST_POISON1 ((void *) 0x00100100)
29 #define LIST_POISON2 ((void *) 0x00200200)
32 * Simple doubly linked list implementation.
34 * Some of the internal functions ("__xxx") are useful when
35 * manipulating whole lists rather than single entries, as
36 * sometimes we already know the next/prev entries and we can
37 * generate better code by using them directly rather than
38 * using the generic single-entry routines.
42 struct list_head
*next
, *prev
;
45 #define LIST_HEAD_INIT(name) { &(name), &(name) }
47 #define LIST_HEAD(name) \
48 struct list_head name = LIST_HEAD_INIT(name)
50 #define INIT_LIST_HEAD(ptr) do { \
51 (ptr)->next = (ptr); (ptr)->prev = (ptr); \
55 * Insert a new entry between two known consecutive entries.
57 * This is only for internal list manipulation where we know
58 * the prev/next entries already!
60 static inline void __list_add(struct list_head
*new,
61 struct list_head
*prev
,
62 struct list_head
*next
)
71 * list_add - add a new entry
72 * @new: new entry to be added
73 * @head: list head to add it after
75 * Insert a new entry after the specified head.
76 * This is good for implementing stacks.
78 static inline void list_add(struct list_head
*new, struct list_head
*head
)
80 __list_add(new, head
, head
->next
);
84 * list_add_tail - add a new entry
85 * @new: new entry to be added
86 * @head: list head to add it before
88 * Insert a new entry before the specified head.
89 * This is useful for implementing queues.
91 static inline void list_add_tail(struct list_head
*new, struct list_head
*head
)
93 __list_add(new, head
->prev
, head
);
97 * Delete a list entry by making the prev/next entries
98 * point to each other.
100 * This is only for internal list manipulation where we know
101 * the prev/next entries already!
103 static inline void __list_del(struct list_head
* prev
, struct list_head
* next
)
110 * list_del - deletes entry from list.
111 * @entry: the element to delete from the list.
112 * Note: list_empty on entry does not return true after this, the entry is
113 * in an undefined state.
115 static inline void list_del(struct list_head
*entry
)
117 __list_del(entry
->prev
, entry
->next
);
118 entry
->next
= LIST_POISON1
;
119 entry
->prev
= LIST_POISON2
;
123 * list_del_init - deletes entry from list and reinitialize it.
124 * @entry: the element to delete from the list.
126 static inline void list_del_init(struct list_head
*entry
)
128 __list_del(entry
->prev
, entry
->next
);
129 INIT_LIST_HEAD(entry
);
133 * list_move - delete from one list and add as another's head
134 * @list: the entry to move
135 * @head: the head that will precede our entry
137 static inline void list_move(struct list_head
*list
, struct list_head
*head
)
139 __list_del(list
->prev
, list
->next
);
140 list_add(list
, head
);
144 * list_move_tail - delete from one list and add as another's tail
145 * @list: the entry to move
146 * @head: the head that will follow our entry
148 static inline void list_move_tail(struct list_head
*list
,
149 struct list_head
*head
)
151 __list_del(list
->prev
, list
->next
);
152 list_add_tail(list
, head
);
156 * list_empty - tests whether a list is empty
157 * @head: the list to test.
159 static inline int list_empty(struct list_head
*head
)
161 return head
->next
== head
;
164 static inline void __list_splice(struct list_head
*list
,
165 struct list_head
*head
)
167 struct list_head
*first
= list
->next
;
168 struct list_head
*last
= list
->prev
;
169 struct list_head
*at
= head
->next
;
179 * list_splice - join two lists
180 * @list: the new list to add.
181 * @head: the place to add it in the first list.
183 static inline void list_splice(struct list_head
*list
, struct list_head
*head
)
185 if (!list_empty(list
))
186 __list_splice(list
, head
);
190 * list_splice_init - join two lists and reinitialise the emptied list.
191 * @list: the new list to add.
192 * @head: the place to add it in the first list.
194 * The list at @list is reinitialised
196 static inline void list_splice_init(struct list_head
*list
,
197 struct list_head
*head
)
199 if (!list_empty(list
)) {
200 __list_splice(list
, head
);
201 INIT_LIST_HEAD(list
);
206 * list_entry - get the struct for this entry
207 * @ptr: the &struct list_head pointer.
208 * @type: the type of the struct this is embedded in.
209 * @member: the name of the list_struct within the struct.
211 #define list_entry(ptr, type, member) \
212 container_of(ptr, type, member)
215 * list_for_each - iterate over a list
216 * @pos: the &struct list_head to use as a loop counter.
217 * @head: the head for your list.
219 #define list_for_each(pos, head) \
220 for (pos = (head)->next; pos != (head); \
224 * __list_for_each - iterate over a list
225 * @pos: the &struct list_head to use as a loop counter.
226 * @head: the head for your list.
228 * This variant differs from list_for_each() in that it's the
229 * simplest possible list iteration code.
230 * Use this for code that knows the list to be very short (empty
231 * or 1 entry) most of the time.
233 #define __list_for_each(pos, head) \
234 for (pos = (head)->next; pos != (head); pos = pos->next)
237 * list_for_each_prev - iterate over a list backwards
238 * @pos: the &struct list_head to use as a loop counter.
239 * @head: the head for your list.
241 #define list_for_each_prev(pos, head) \
242 for (pos = (head)->prev; pos != (head); pos = pos->prev)
245 * list_for_each_safe - iterate over a list safe against removal of list entry
246 * @pos: the &struct list_head to use as a loop counter.
247 * @n: another &struct list_head to use as temporary storage
248 * @head: the head for your list.
250 #define list_for_each_safe(pos, n, head) \
251 for (pos = (head)->next, n = pos->next; pos != (head); \
252 pos = n, n = pos->next)
255 * list_for_each_entry - iterate over list of given type
256 * @pos: the type * to use as a loop counter.
257 * @head: the head for your list.
258 * @member: the name of the list_struct within the struct.
260 #define list_for_each_entry(pos, head, member) \
261 for (pos = list_entry((head)->next, typeof(*pos), member); \
262 &pos->member != (head); \
263 pos = list_entry(pos->member.next, typeof(*pos), member))
266 * list_for_each_entry_reverse - iterate backwards over list of given type.
267 * @pos: the type * to use as a loop counter.
268 * @head: the head for your list.
269 * @member: the name of the list_struct within the struct.
271 #define list_for_each_entry_reverse(pos, head, member) \
272 for (pos = list_entry((head)->prev, typeof(*pos), member); \
273 &pos->member != (head); \
274 pos = list_entry(pos->member.prev, typeof(*pos), member))
277 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
278 * @pos: the type * to use as a loop counter.
279 * @n: another type * to use as temporary storage
280 * @head: the head for your list.
281 * @member: the name of the list_struct within the struct.
283 #define list_for_each_entry_safe(pos, n, head, member) \
284 for (pos = list_entry((head)->next, typeof(*pos), member), \
285 n = list_entry(pos->member.next, typeof(*pos), member); \
286 &pos->member != (head); \
287 pos = n, n = list_entry(n->member.next, typeof(*n), member))