6 * Simple doubly linked list implementation.
8 * Some of the internal functions ("__xxx") are useful when
9 * manipulating whole lists rather than single entries, as
10 * sometimes we already know the next/prev entries and we can
11 * generate better code by using them directly rather than
12 * using the generic single-entry routines.
15 #define prefetch(a) ((void)a)
18 struct list_head *next, *prev;
21 #define LIST_HEAD_INIT(name) { &(name), &(name) }
23 #define LIST_HEAD(name) \
24 struct list_head name = LIST_HEAD_INIT(name)
26 #define INIT_LIST_HEAD(ptr) do { \
27 (ptr)->next = (ptr); (ptr)->prev = (ptr); \
31 * Insert a new entry between two known consecutive entries.
33 * This is only for internal list manipulation where we know
34 * the prev/next entries already!
36 static inline void __list_add(struct list_head * new,
37 struct list_head * prev,
38 struct list_head * next)
47 * list_add - add a new entry
48 * @new: new entry to be added
49 * @head: list head to add it after
51 * Insert a new entry after the specified head.
52 * This is good for implementing stacks.
54 static inline void list_add(struct list_head *new, struct list_head *head)
56 __list_add(new, head, head->next);
60 * list_add_tail - add a new entry
61 * @new: new entry to be added
62 * @head: list head to add it before
64 * Insert a new entry before the specified head.
65 * This is useful for implementing queues.
67 static inline void list_add_tail(struct list_head *new, struct list_head *head)
69 __list_add(new, head->prev, head);
73 * Delete a list entry by making the prev/next entries
74 * point to each other.
76 * This is only for internal list manipulation where we know
77 * the prev/next entries already!
79 static inline void __list_del(struct list_head * prev, struct list_head * next)
86 * list_del - deletes entry from list.
87 * @entry: the element to delete from the list.
88 * Note: list_empty on entry does not return true after this, the entry is in an undefined state.
90 static inline void list_del(struct list_head *entry)
92 __list_del(entry->prev, entry->next);
96 * list_del_init - deletes entry from list and reinitialize it.
97 * @entry: the element to delete from the list.
99 static inline void list_del_init(struct list_head *entry)
101 __list_del(entry->prev, entry->next);
102 INIT_LIST_HEAD(entry);
106 * list_move - delete from one list and add as another's head
107 * @list: the entry to move
108 * @head: the head that will precede our entry
110 static inline void list_move(struct list_head *list, struct list_head *head)
112 __list_del(list->prev, list->next);
113 list_add(list, head);
117 * list_move_tail - delete from one list and add as another's tail
118 * @list: the entry to move
119 * @head: the head that will follow our entry
121 static inline void list_move_tail(struct list_head *list,
122 struct list_head *head)
124 __list_del(list->prev, list->next);
125 list_add_tail(list, head);
129 * list_empty - tests whether a list is empty
130 * @head: the list to test.
132 static inline int list_empty(struct list_head *head)
134 return head->next == head;
137 static inline void __list_splice(struct list_head *list,
138 struct list_head *head)
140 struct list_head *first = list->next;
141 struct list_head *last = list->prev;
142 struct list_head *at = head->next;
152 * list_splice - join two lists
153 * @list: the new list to add.
154 * @head: the place to add it in the first list.
156 static inline void list_splice(struct list_head *list, struct list_head *head)
158 if (!list_empty(list))
159 __list_splice(list, head);
163 * list_splice_init - join two lists and reinitialise the emptied list.
164 * @list: the new list to add.
165 * @head: the place to add it in the first list.
167 * The list at @list is reinitialised
169 static inline void list_splice_init(struct list_head *list,
170 struct list_head *head)
172 if (!list_empty(list)) {
173 __list_splice(list, head);
174 INIT_LIST_HEAD(list);
179 * list_entry - get the struct for this entry
180 * @ptr: the &struct list_head pointer.
181 * @type: the type of the struct this is embedded in.
182 * @member: the name of the list_struct within the struct.
184 #define list_entry(ptr, type, member) \
185 ((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))
188 * list_for_each - iterate over a list
189 * @pos: the &struct list_head to use as a loop counter.
190 * @head: the head for your list.
192 #define list_for_each(pos, head) \
193 for (pos = (head)->next, prefetch(pos->next); pos != (head); \
194 pos = pos->next, prefetch(pos->next))
197 * list_for_each_prev - iterate over a list in reverse order
198 * @pos: the &struct list_head to use as a loop counter.
199 * @head: the head for your list.
201 #define list_for_each_prev(pos, head) \
202 for (pos = (head)->prev, prefetch(pos->prev); pos != (head); \
203 pos = pos->prev, prefetch(pos->prev))
206 * list_for_each_safe - iterate over a list safe against removal of list entry
207 * @pos: the &struct list_head to use as a loop counter.
208 * @n: another &struct list_head to use as temporary storage
209 * @head: the head for your list.
211 #define list_for_each_safe(pos, n, head) \
212 for (pos = (head)->next, n = pos->next; pos != (head); \
213 pos = n, n = pos->next)
217 #ifndef list_for_each_entry
219 * list_for_each_entry - iterate over list of given type
220 * @pos: the type * to use as a loop counter.
221 * @head: the head for your list.
222 * @member: the name of the list_struct within the struct.
224 #define list_for_each_entry(pos, head, member) \
225 for (pos = list_entry((head)->next, typeof(*pos), member), \
226 prefetch(pos->member.next); \
227 &pos->member != (head); \
228 pos = list_entry(pos->member.next, typeof(*pos), member), \
229 prefetch(pos->member.next))
232 #ifndef list_for_each_entry_safe
234 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
235 * @pos: the type * to use as a loop counter.
236 * @n: another type * to use as temporary storage
237 * @head: the head for your list.
238 * @member: the name of the list_struct within the struct.
240 #define list_for_each_entry_safe(pos, n, head, member) \
241 for (pos = list_entry((head)->next, typeof(*pos), member), \
242 n = list_entry(pos->member.next, typeof(*pos), member); \
243 &pos->member != (head); \
244 pos = n, n = list_entry(n->member.next, typeof(*n), member))