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 /** \file list.h doubly linked list implementation */
10 #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 * Non-NULL pointers that will result in page faults under normal
18 * circumstances, used to verify that nobody uses non-initialized list entries.
19 * Used for poisoning the \a next pointer of struct list_head.
21 #define LIST_POISON1 ((void *) 0x00100100)
22 /** Non-null pointer, used for poisoning the \a prev pointer of struct
25 #define LIST_POISON2 ((void *) 0x00200200)
28 * Simple doubly linked list implementation.
30 * Some of the internal functions ("__xxx") are useful when
31 * manipulating whole lists rather than single entries, as
32 * sometimes we already know the next/prev entries and we can
33 * generate better code by using them directly rather than
34 * using the generic single-entry routines.
37 struct list_head *next, *prev;
40 #define INIT_LIST_HEAD(ptr) do { \
41 (ptr)->next = (ptr); (ptr)->prev = (ptr); \
45 * Insert a new entry between two known consecutive entries.
47 * This is only for internal list manipulation where we know
48 * the prev/next entries already!
50 static inline void __list_add(struct list_head *new,
51 struct list_head *prev,
52 struct list_head *next)
63 * \param new new entry to be added
64 * \param head list head to add it after
66 * Insert a new entry after the specified head.
67 * This is good for implementing stacks.
69 static inline void para_list_add(struct list_head *new, struct list_head *head)
71 __list_add(new, head, head->next);
77 * \param new new entry to be added
78 * \param head list head to add it before
80 * Insert a new entry before the specified head.
81 * This is useful for implementing queues.
83 static inline void list_add_tail(struct list_head *new, struct list_head *head)
85 __list_add(new, head->prev, head);
89 * Delete a list entry by making the prev/next entries
90 * point to each other.
92 * This is only for internal list manipulation where we know
93 * the prev/next entries already!
95 static inline void __list_del(struct list_head * prev, struct list_head * next)
102 * Delete entry from list.
104 * \param entry the element to delete from the list.
106 * Note: list_empty on entry does not return true after this, the entry is
107 * in an undefined state.
109 static inline void list_del(struct list_head *entry)
111 __list_del(entry->prev, entry->next);
112 entry->next = LIST_POISON1;
113 entry->prev = LIST_POISON2;
117 * delete from one list and add as another's head
119 * \param list: the entry to move
120 * \param head: the head that will precede our entry
122 static inline void list_move(struct list_head *list, struct list_head *head)
124 __list_del(list->prev, list->next);
125 para_list_add(list, head);
129 * test whether a list is empty
131 * \param head the list to test.
133 static inline int list_empty(const struct list_head *head)
135 return head->next == head;
139 * get the struct for this entry
141 * \param ptr the &struct list_head pointer.
142 * \param type the type of the struct this is embedded in.
143 * \param member the name of the list_struct within the struct.
145 #define list_entry(ptr, type, member) \
146 container_of(ptr, type, member)
149 * iterate over a list safe against removal of list entry
151 * \param pos the &struct list_head to use as a loop counter.
152 * \param n another &struct list_head to use as temporary storage
153 * \param head the head for your list.
155 #define list_for_each_safe(pos, n, head) \
156 for (pos = (head)->next, n = pos->next; pos != (head); \
157 pos = n, n = pos->next)
160 * iterate over list of given type
162 * \param pos the type * to use as a loop counter.
163 * \param head the head for your list.
164 * \param member the name of the list_struct within the struct.
166 #define list_for_each_entry(pos, head, member) \
167 for (pos = list_entry((head)->next, typeof(*pos), member); \
168 &pos->member != (head); \
169 pos = list_entry(pos->member.next, typeof(*pos), member))
172 * iterate over list of given type safe against removal of list entry
174 * \param pos the type * to use as a loop counter.
175 * \param n another type * to use as temporary storage
176 * \param head the head for your list.
177 * \param member the name of the list_struct within the struct.
179 #define list_for_each_entry_safe(pos, n, head, member) \
180 for (pos = list_entry((head)->next, typeof(*pos), member), \
181 n = list_entry(pos->member.next, typeof(*pos), member); \
182 &pos->member != (head); \
183 pos = n, n = list_entry(n->member.next, typeof(*n), member))
185 * iterate backwards over list of given type safe against removal of list entry
186 * \param pos the type * to use as a loop counter.
187 * \param n another type * to use as temporary storage
188 * \param head the head for your list.
189 * \param member the name of the list_struct within the struct.
191 #define list_for_each_entry_safe_reverse(pos, n, head, member) \
192 for (pos = list_entry((head)->prev, typeof(*pos), member), \
193 n = list_entry(pos->member.prev, typeof(*pos), member); \
194 &pos->member != (head); \
195 pos = n, n = list_entry(n->member.prev, typeof(*n), member))