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.
6 * Ripped out the rcu stuff, as it's not needed.
12 //#include <linux/stddef.h>
14 * container_of - cast a member of a structure out to the containing structure
16 * @ptr: the pointer to the member.
17 * @type: the type of the container struct this is embedded in.
18 * @member: the name of the member within the struct.
21 #define container_of(ptr, type, member) ({ \
22 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
23 (type *)( (char *)__mptr - offsetof(type,member) );})
25 //#include <linux/prefetch.h>
26 static inline void prefetch(const void *x) {;}
28 //#include <asm/system.h>
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(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, prefetch(pos->next); pos != (head); \
228 pos = pos->next, prefetch(pos->next))
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, no prefetching is done.
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, prefetch(pos->prev); pos != (head); \
250 pos = pos->prev, prefetch(pos->prev))
253 * list_for_each_safe - iterate over a list safe against removal of list entry
254 * @pos: the &struct list_head to use as a loop counter.
255 * @n: another &struct list_head to use as temporary storage
256 * @head: the head for your list.
258 #define list_for_each_safe(pos, n, head) \
259 for (pos = (head)->next, n = pos->next; pos != (head); \
260 pos = n, n = pos->next)
263 * list_for_each_entry - iterate over list of given type
264 * @pos: the type * to use as a loop counter.
265 * @head: the head for your list.
266 * @member: the name of the list_struct within the struct.
268 #define list_for_each_entry(pos, head, member) \
269 for (pos = list_entry((head)->next, typeof(*pos), member), \
270 prefetch(pos->member.next); \
271 &pos->member != (head); \
272 pos = list_entry(pos->member.next, typeof(*pos), member), \
273 prefetch(pos->member.next))
276 * list_for_each_entry_reverse - iterate backwards over list of given type.
277 * @pos: the type * to use as a loop counter.
278 * @head: the head for your list.
279 * @member: the name of the list_struct within the struct.
281 #define list_for_each_entry_reverse(pos, head, member) \
282 for (pos = list_entry((head)->prev, typeof(*pos), member), \
283 prefetch(pos->member.prev); \
284 &pos->member != (head); \
285 pos = list_entry(pos->member.prev, typeof(*pos), member), \
286 prefetch(pos->member.prev))
290 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
291 * @pos: the type * to use as a loop counter.
292 * @n: another type * to use as temporary storage
293 * @head: the head for your list.
294 * @member: the name of the list_struct within the struct.
296 #define list_for_each_entry_safe(pos, n, head, member) \
297 for (pos = list_entry((head)->next, typeof(*pos), member), \
298 n = list_entry(pos->member.next, typeof(*pos), member); \
299 &pos->member != (head); \
300 pos = n, n = list_entry(n->member.next, typeof(*n), member))
303 * Double linked lists with a single pointer list head.
304 * Mostly useful for hash tables where the two pointer list head is
306 * You lose the ability to access the tail in O(1).
310 struct hlist_node *first;
314 struct hlist_node *next, **pprev;
317 #define HLIST_HEAD_INIT { .first = NULL }
318 #define HLIST_HEAD(name) struct hlist_head name = { .first = NULL }
319 #define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
320 #define INIT_HLIST_NODE(ptr) ((ptr)->next = NULL, (ptr)->pprev = NULL)
322 static __inline__ int hlist_unhashed(struct hlist_node *h)
327 static __inline__ int hlist_empty(struct hlist_head *h)
332 static __inline__ void __hlist_del(struct hlist_node *n)
334 struct hlist_node *next = n->next;
335 struct hlist_node **pprev = n->pprev;
341 static __inline__ void hlist_del(struct hlist_node *n)
344 n->next = LIST_POISON1;
345 n->pprev = LIST_POISON2;
348 static __inline__ void hlist_del_init(struct hlist_node *n)
356 static __inline__ void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
358 struct hlist_node *first = h->first;
361 first->pprev = &n->next;
363 n->pprev = &h->first;
366 /* next must be != NULL */
367 static __inline__ void hlist_add_before(struct hlist_node *n, struct hlist_node *next)
369 n->pprev = next->pprev;
371 next->pprev = &n->next;
375 static __inline__ void hlist_add_after(struct hlist_node *n,
376 struct hlist_node *next)
378 next->next = n->next;
383 #define hlist_entry(ptr, type, member) container_of(ptr,type,member)
385 /* Cannot easily do prefetch unfortunately */
386 #define hlist_for_each(pos, head) \
387 for (pos = (head)->first; pos && ({ prefetch(pos->next); 1; }); \
390 #define hlist_for_each_safe(pos, n, head) \
391 for (pos = (head)->first; n = pos ? pos->next : 0, pos; \
395 * hlist_for_each_entry - iterate over list of given type
396 * @tpos: the type * to use as a loop counter.
397 * @pos: the &struct hlist_node to use as a loop counter.
398 * @head: the head for your list.
399 * @member: the name of the hlist_node within the struct.
401 #define hlist_for_each_entry(tpos, pos, head, member) \
402 for (pos = (head)->first; \
403 pos && ({ prefetch(pos->next); 1;}) && \
404 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
408 * hlist_for_each_entry_continue - iterate over a hlist continuing after existing point
409 * @tpos: the type * to use as a loop counter.
410 * @pos: the &struct hlist_node to use as a loop counter.
411 * @member: the name of the hlist_node within the struct.
413 #define hlist_for_each_entry_continue(tpos, pos, member) \
414 for (pos = (pos)->next; \
415 pos && ({ prefetch(pos->next); 1;}) && \
416 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
420 * hlist_for_each_entry_from - iterate over a hlist continuing from existing point
421 * @tpos: the type * to use as a loop counter.
422 * @pos: the &struct hlist_node to use as a loop counter.
423 * @member: the name of the hlist_node within the struct.
425 #define hlist_for_each_entry_from(tpos, pos, member) \
426 for (; pos && ({ prefetch(pos->next); 1;}) && \
427 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
431 * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
432 * @tpos: the type * to use as a loop counter.
433 * @pos: the &struct hlist_node to use as a loop counter.
434 * @n: another &struct hlist_node to use as temporary storage
435 * @head: the head for your list.
436 * @member: the name of the hlist_node within the struct.
438 #define hlist_for_each_entry_safe(tpos, pos, n, head, member) \
439 for (pos = (head)->first; \
440 pos && ({ n = pos->next; 1; }) && \
441 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \