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 /** get the struct this entry is embedded in */
13 #define container_of(ptr, type, member) ({ \
14 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
15 (type *)( (char *)__mptr - offsetof(type,member) );})
18 * Non-NULL pointers that will result in page faults under normal
19 * circumstances, used to verify that nobody uses non-initialized list entries.
20 * Used for poisoning the \a next pointer of struct list_head.
22 #define LIST_POISON1 ((void *) 0x00100100)
23 /** Non-null pointer, used for poisoning the \a prev pointer of struct
26 #define LIST_POISON2 ((void *) 0x00200200)
28 /** Simple doubly linked list implementation. */
30 /** pointer to the next list entry */
31 struct list_head *next;
32 /** pointer to the previous list entry */
33 struct list_head *prev;
36 /** Define an initialized list head. */
37 #define INITIALIZED_LIST_HEAD(name) struct list_head name = { &(name), &(name) }
40 /** must be called before using any other list functions */
41 #define INIT_LIST_HEAD(ptr) do { \
42 (ptr)->next = (ptr); (ptr)->prev = (ptr); \
47 * Some of the internal functions ("__xxx") are useful when
48 * manipulating whole lists rather than single entries, as
49 * sometimes we already know the next/prev entries and we can
50 * generate better code by using them directly rather than
51 * using the generic single-entry routines.
56 * Insert a new entry between two known consecutive entries.
58 * This is only for internal list manipulation where we know
59 * the prev/next entries already!
61 static inline void __list_add(struct list_head *new,
62 struct list_head *prev,
63 struct list_head *next)
74 * \param new new entry to be added
75 * \param head list head to add it after
77 * Insert a new entry after the specified head.
78 * This is good for implementing stacks.
80 static inline void para_list_add(struct list_head *new, struct list_head *head)
82 __list_add(new, head, head->next);
88 * \param new new entry to be added
89 * \param head list head to add it before
91 * Insert a new entry before the specified head.
92 * This is useful for implementing queues.
94 static inline void list_add_tail(struct list_head *new, struct list_head *head)
96 __list_add(new, head->prev, head);
100 * Delete a list entry by making the prev/next entries
101 * point to each other.
103 * This is only for internal list manipulation where we know
104 * the prev/next entries already!
106 static inline void __list_del(struct list_head * prev, struct list_head * next)
113 * Delete entry from list.
115 * \param entry the element to delete from the list.
117 * Note: list_empty on entry does not return true after this, the entry is
118 * in an undefined state.
120 static inline void list_del(struct list_head *entry)
122 __list_del(entry->prev, entry->next);
123 entry->next = LIST_POISON1;
124 entry->prev = LIST_POISON2;
128 * delete from one list and add as another's head
130 * \param list: the entry to move
131 * \param head: the head that will precede our entry
133 static inline void list_move(struct list_head *list, struct list_head *head)
135 __list_del(list->prev, list->next);
136 para_list_add(list, head);
140 * test whether a list is empty
142 * \param head the list to test.
144 static inline int list_empty(const struct list_head *head)
146 return head->next == head;
150 * get the struct for this entry
152 * \param ptr the &struct list_head pointer.
153 * \param type the type of the struct this is embedded in.
154 * \param member the name of the list_struct within the struct.
156 #define list_entry(ptr, type, member) \
157 container_of(ptr, type, member)
160 * iterate over a list safe against removal of list entry
162 * \param pos the &struct list_head to use as a loop counter.
163 * \param n another &struct list_head to use as temporary storage
164 * \param head the head for your list.
166 #define list_for_each_safe(pos, n, head) \
167 for (pos = (head)->next, n = pos->next; pos != (head); \
168 pos = n, n = pos->next)
171 * iterate over list of given type
173 * \param pos the type * to use as a loop counter.
174 * \param head the head for your list.
175 * \param member the name of the list_struct within the struct.
177 #define list_for_each_entry(pos, head, member) \
178 for (pos = list_entry((head)->next, typeof(*pos), member); \
179 &pos->member != (head); \
180 pos = list_entry(pos->member.next, typeof(*pos), member))
183 * iterate over list of given type safe against removal of list entry
185 * \param pos the type * to use as a loop counter.
186 * \param n another type * to use as temporary storage
187 * \param head the head for your list.
188 * \param member the name of the list_struct within the struct.
190 #define list_for_each_entry_safe(pos, n, head, member) \
191 for (pos = list_entry((head)->next, typeof(*pos), member), \
192 n = list_entry(pos->member.next, typeof(*pos), member); \
193 &pos->member != (head); \
194 pos = n, n = list_entry(n->member.next, typeof(*n), member))
196 * iterate backwards over list of given type safe against removal of list entry
197 * \param pos the type * to use as a loop counter.
198 * \param n another type * to use as temporary storage
199 * \param head the head for your list.
200 * \param member the name of the list_struct within the struct.
202 #define list_for_each_entry_safe_reverse(pos, n, head, member) \
203 for (pos = list_entry((head)->prev, typeof(*pos), member), \
204 n = list_entry(pos->member.prev, typeof(*pos), member); \
205 &pos->member != (head); \
206 pos = n, n = list_entry(n->member.prev, typeof(*n), member))
209 * Get the first element from a list
210 * \param ptr the list head to take the element from.
211 * \param type The type of the struct this is embedded in.
212 * \param member The name of the list_struct within the struct.
214 * Note that list is expected to be not empty.
216 #define list_first_entry(ptr, type, member) \
217 list_entry((ptr)->next, type, member)
220 * Test whether a list has just one entry.
222 * \param head The list to test.
224 static inline int list_is_singular(const struct list_head *head)
226 return !list_empty(head) && (head->next == head->prev);