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 # error "Need GNU GCC"
15 #define typeof __typeof__
16 #define offsetof(type, member) __builtin_offsetof (type, member)
19 * container_of - cast a member of a structure out to the containing structure
21 * @ptr: the pointer to the member.
22 * @type: the type of the container struct this is embedded in.
23 * @member: the name of the member within the struct.
26 #define container_of(ptr, type, member) ({ \
27 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
28 (type *)( (char *)__mptr - offsetof(type,member) );})
31 * These are non-NULL pointers that will result in page faults
32 * under normal circumstances, used to verify that nobody uses
33 * non-initialized list entries.
35 #define LIST_POISON1 ((void *) 0x00100100)
36 #define LIST_POISON2 ((void *) 0x00200200)
39 * Simple doubly linked list implementation.
41 * Some of the internal functions ("__xxx") are useful when
42 * manipulating whole lists rather than single entries, as
43 * sometimes we already know the next/prev entries and we can
44 * generate better code by using them directly rather than
45 * using the generic single-entry routines.
49 struct list_head *next, *prev;
52 #define LIST_HEAD_INIT(name) { &(name), &(name) }
54 #define LIST_HEAD(name) \
55 struct list_head name = LIST_HEAD_INIT(name)
57 #define INIT_LIST_HEAD(ptr) do { \
58 (ptr)->next = (ptr); (ptr)->prev = (ptr); \
62 * Insert a new entry between two known consecutive entries.
64 * This is only for internal list manipulation where we know
65 * the prev/next entries already!
67 static inline void __list_add(struct list_head *new,
68 struct list_head *prev,
69 struct list_head *next)
78 * list_add - add a new entry
79 * @new: new entry to be added
80 * @head: list head to add it after
82 * Insert a new entry after the specified head.
83 * This is good for implementing stacks.
85 static inline void list_add(struct list_head *new, struct list_head *head)
87 __list_add(new, head, head->next);
91 * list_add_tail - add a new entry
92 * @new: new entry to be added
93 * @head: list head to add it before
95 * Insert a new entry before the specified head.
96 * This is useful for implementing queues.
98 static inline void list_add_tail(struct list_head *new, struct list_head *head)
100 __list_add(new, head->prev, head);
104 * Delete a list entry by making the prev/next entries
105 * point to each other.
107 * This is only for internal list manipulation where we know
108 * the prev/next entries already!
110 static inline void __list_del(struct list_head * prev, struct list_head * next)
117 * list_del - deletes entry from list.
118 * @entry: the element to delete from the list.
119 * Note: list_empty on entry does not return true after this, the entry is
120 * in an undefined state.
122 static inline void list_del(struct list_head *entry)
124 __list_del(entry->prev, entry->next);
125 entry->next = LIST_POISON1;
126 entry->prev = LIST_POISON2;
130 * list_del_init - deletes entry from list and reinitialize it.
131 * @entry: the element to delete from the list.
133 static inline void list_del_init(struct list_head *entry)
135 __list_del(entry->prev, entry->next);
136 INIT_LIST_HEAD(entry);
140 * list_move - delete from one list and add as another's head
141 * @list: the entry to move
142 * @head: the head that will precede our entry
144 static inline void list_move(struct list_head *list, struct list_head *head)
146 __list_del(list->prev, list->next);
147 list_add(list, head);
151 * list_move_tail - delete from one list and add as another's tail
152 * @list: the entry to move
153 * @head: the head that will follow our entry
155 static inline void list_move_tail(struct list_head *list,
156 struct list_head *head)
158 __list_del(list->prev, list->next);
159 list_add_tail(list, head);
163 * list_empty - tests whether a list is empty
164 * @head: the list to test.
166 static inline int list_empty(const struct list_head *head)
168 return head->next == head;
171 static inline void __list_splice(struct list_head *list,
172 struct list_head *head)
174 struct list_head *first = list->next;
175 struct list_head *last = list->prev;
176 struct list_head *at = head->next;
186 * list_splice - join two lists
187 * @list: the new list to add.
188 * @head: the place to add it in the first list.
190 static inline void list_splice(struct list_head *list, struct list_head *head)
192 if (!list_empty(list))
193 __list_splice(list, head);
197 * list_splice_init - join two lists and reinitialise the emptied list.
198 * @list: the new list to add.
199 * @head: the place to add it in the first list.
201 * The list at @list is reinitialised
203 static inline void list_splice_init(struct list_head *list,
204 struct list_head *head)
206 if (!list_empty(list)) {
207 __list_splice(list, head);
208 INIT_LIST_HEAD(list);
213 * list_entry - get the struct for this entry
214 * @ptr: the &struct list_head pointer.
215 * @type: the type of the struct this is embedded in.
216 * @member: the name of the list_struct within the struct.
218 #define list_entry(ptr, type, member) \
219 container_of(ptr, type, member)
222 * list_for_each - iterate over a list
223 * @pos: the &struct list_head to use as a loop counter.
224 * @head: the head for your list.
226 #define list_for_each(pos, head) \
227 for (pos = (head)->next; pos != (head); \
231 * __list_for_each - iterate over a list
232 * @pos: the &struct list_head to use as a loop counter.
233 * @head: the head for your list.
235 * This variant differs from list_for_each() in that it's the
236 * simplest possible list iteration code.
237 * Use this for code that knows the list to be very short (empty
238 * or 1 entry) most of the time.
240 #define __list_for_each(pos, head) \
241 for (pos = (head)->next; pos != (head); pos = pos->next)
244 * list_for_each_prev - iterate over a list backwards
245 * @pos: the &struct list_head to use as a loop counter.
246 * @head: the head for your list.
248 #define list_for_each_prev(pos, head) \
249 for (pos = (head)->prev; pos != (head); pos = pos->prev)
252 * list_for_each_safe - iterate over a list safe against removal of list entry
253 * @pos: the &struct list_head to use as a loop counter.
254 * @n: another &struct list_head to use as temporary storage
255 * @head: the head for your list.
257 #define list_for_each_safe(pos, n, head) \
258 for (pos = (head)->next, n = pos->next; pos != (head); \
259 pos = n, n = pos->next)
262 * list_for_each_entry - iterate over list of given type
263 * @pos: the type * to use as a loop counter.
264 * @head: the head for your list.
265 * @member: the name of the list_struct within the struct.
267 #define list_for_each_entry(pos, head, member) \
268 for (pos = list_entry((head)->next, typeof(*pos), member); \
269 &pos->member != (head); \
270 pos = list_entry(pos->member.next, typeof(*pos), member))
273 * list_for_each_entry_reverse - iterate backwards over list of given type.
274 * @pos: the type * to use as a loop counter.
275 * @head: the head for your list.
276 * @member: the name of the list_struct within the struct.
278 #define list_for_each_entry_reverse(pos, head, member) \
279 for (pos = list_entry((head)->prev, typeof(*pos), member); \
280 &pos->member != (head); \
281 pos = list_entry(pos->member.prev, typeof(*pos), member))
284 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
285 * @pos: the type * to use as a loop counter.
286 * @n: another type * to use as temporary storage
287 * @head: the head for your list.
288 * @member: the name of the list_struct within the struct.
290 #define list_for_each_entry_safe(pos, n, head, member) \
291 for (pos = list_entry((head)->next, typeof(*pos), member), \
292 n = list_entry(pos->member.next, typeof(*pos), member); \
293 &pos->member != (head); \
294 pos = n, n = list_entry(n->member.next, typeof(*n), member))