4 * Simple doubly linked list implementation.
6 * Some of the internal functions ("__xxx") are useful when
7 * manipulating whole lists rather than single entries, as
8 * sometimes we already know the next/prev entries and we can
9 * generate better code by using them directly rather than
10 * using the generic single-entry routines.
13 #define prefetch(a) ((void)a)
16 struct list_head *next, *prev;
19 #define LIST_HEAD_INIT(name) { &(name), &(name) }
21 #define LIST_HEAD(name) \
22 struct list_head name = LIST_HEAD_INIT(name)
24 #define INIT_LIST_HEAD(ptr) do { \
25 (ptr)->next = (ptr); (ptr)->prev = (ptr); \
29 * Insert a new entry between two known consecutive entries.
31 * This is only for internal list manipulation where we know
32 * the prev/next entries already!
34 static inline void __list_add(struct list_head * new,
35 struct list_head * prev,
36 struct list_head * next)
45 * list_add - add a new entry
46 * @new: new entry to be added
47 * @head: list head to add it after
49 * Insert a new entry after the specified head.
50 * This is good for implementing stacks.
52 static inline void list_add(struct list_head *new, struct list_head *head)
54 __list_add(new, head, head->next);
58 * list_add_tail - add a new entry
59 * @new: new entry to be added
60 * @head: list head to add it before
62 * Insert a new entry before the specified head.
63 * This is useful for implementing queues.
65 static inline void list_add_tail(struct list_head *new, struct list_head *head)
67 __list_add(new, head->prev, head);
71 * Delete a list entry by making the prev/next entries
72 * point to each other.
74 * This is only for internal list manipulation where we know
75 * the prev/next entries already!
77 static inline void __list_del(struct list_head * prev, struct list_head * next)
84 * list_del - deletes entry from list.
85 * @entry: the element to delete from the list.
86 * Note: list_empty on entry does not return true after this, the entry is in an undefined state.
88 static inline void list_del(struct list_head *entry)
90 __list_del(entry->prev, entry->next);
94 * list_del_init - deletes entry from list and reinitialize it.
95 * @entry: the element to delete from the list.
97 static inline void list_del_init(struct list_head *entry)
99 __list_del(entry->prev, entry->next);
100 INIT_LIST_HEAD(entry);
104 #ifndef list_for_each_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 #ifndef _LINUX_LIST_H
130 #define _LINUX_LIST_H
132 * list_empty - tests whether a list is empty
133 * @head: the list to test.
135 static inline int list_empty(struct list_head *head)
137 return head->next == head;
140 static inline void __list_splice(struct list_head *list,
141 struct list_head *head)
143 struct list_head *first = list->next;
144 struct list_head *last = list->prev;
145 struct list_head *at = head->next;
155 * list_splice - join two lists
156 * @list: the new list to add.
157 * @head: the place to add it in the first list.
159 static inline void list_splice(struct list_head *list, struct list_head *head)
161 if (!list_empty(list))
162 __list_splice(list, head);
166 * list_splice_init - join two lists and reinitialise the emptied list.
167 * @list: the new list to add.
168 * @head: the place to add it in the first list.
170 * The list at @list is reinitialised
172 static inline void list_splice_init(struct list_head *list,
173 struct list_head *head)
175 if (!list_empty(list)) {
176 __list_splice(list, head);
177 INIT_LIST_HEAD(list);
182 * list_entry - get the struct for this entry
183 * @ptr: the &struct list_head pointer.
184 * @type: the type of the struct this is embedded in.
185 * @member: the name of the list_struct within the struct.
187 #define list_entry(ptr, type, member) \
188 ((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))
191 * list_for_each - iterate over a list
192 * @pos: the &struct list_head to use as a loop counter.
193 * @head: the head for your list.
195 #define list_for_each(pos, head) \
196 for (pos = (head)->next, prefetch(pos->next); pos != (head); \
197 pos = pos->next, prefetch(pos->next))
200 * list_for_each_prev - iterate over a list in reverse order
201 * @pos: the &struct list_head to use as a loop counter.
202 * @head: the head for your list.
204 #define list_for_each_prev(pos, head) \
205 for (pos = (head)->prev, prefetch(pos->prev); pos != (head); \
206 pos = pos->prev, prefetch(pos->prev))
209 * list_for_each_safe - iterate over a list safe against removal of list entry
210 * @pos: the &struct list_head to use as a loop counter.
211 * @n: another &struct list_head to use as temporary storage
212 * @head: the head for your list.
214 #define list_for_each_safe(pos, n, head) \
215 for (pos = (head)->next, n = pos->next; pos != (head); \
216 pos = n, n = pos->next)
220 #ifndef list_for_each_entry
222 * list_for_each_entry - iterate over list of given type
223 * @pos: the type * to use as a loop counter.
224 * @head: the head for your list.
225 * @member: the name of the list_struct within the struct.
227 #define list_for_each_entry(pos, head, member) \
228 for (pos = list_entry((head)->next, typeof(*pos), member), \
229 prefetch(pos->member.next); \
230 &pos->member != (head); \
231 pos = list_entry(pos->member.next, typeof(*pos), member), \
232 prefetch(pos->member.next))
235 #ifndef list_for_each_entry_safe
237 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
238 * @pos: the type * to use as a loop counter.
239 * @n: another type * to use as temporary storage
240 * @head: the head for your list.
241 * @member: the name of the list_struct within the struct.
243 #define list_for_each_entry_safe(pos, n, head, member) \
244 for (pos = list_entry((head)->next, typeof(*pos), member), \
245 n = list_entry(pos->member.next, typeof(*pos), member); \
246 &pos->member != (head); \
247 pos = n, n = list_entry(n->member.next, typeof(*n), member))