3 * Simple doubly linked list implementation.
5 * Some of the internal functions ("__xxx") are useful when
6 * manipulating whole lists rather than single entries, as
7 * sometimes we already know the next/prev entries and we can
8 * generate better code by using them directly rather than
9 * using the generic single-entry routines.
13 struct list_head *next, *prev;
16 typedef struct list_head list_t;
18 #define LIST_HEAD_INIT(name) { &(name), &(name) }
20 #define LIST_HEAD(name) \
21 struct list_head name = LIST_HEAD_INIT(name)
23 #define INIT_LIST_HEAD(ptr) do { \
24 (ptr)->next = (ptr); (ptr)->prev = (ptr); \
28 * Insert a new entry between two known consecutive entries.
30 * This is only for internal list manipulation where we know
31 * the prev/next entries already!
33 static inline void __list_add(struct list_head * new,
34 struct list_head * prev,
35 struct list_head * next)
44 * list_add - add a new entry
45 * @new: new entry to be added
46 * @head: list head to add it after
48 * Insert a new entry after the specified head.
49 * This is good for implementing stacks.
51 static inline void list_add(struct list_head *new, struct list_head *head)
53 __list_add(new, head, head->next);
57 * list_add_tail - add a new entry
58 * @new: new entry to be added
59 * @head: list head to add it before
61 * Insert a new entry before the specified head.
62 * This is useful for implementing queues.
64 static inline void list_add_tail(struct list_head *new, struct list_head *head)
66 __list_add(new, head->prev, head);
70 * Delete a list entry by making the prev/next entries
71 * point to each other.
73 * This is only for internal list manipulation where we know
74 * the prev/next entries already!
76 static inline void __list_del(struct list_head * prev, struct list_head * next)
83 * list_del - deletes entry from list.
84 * @entry: the element to delete from the list.
85 * Note: list_empty on entry does not return true after this, the entry is in an undefined state.
87 static inline void list_del(struct list_head *entry)
89 __list_del(entry->prev, entry->next);
93 * list_del_init - deletes entry from list and reinitialize it.
94 * @entry: the element to delete from the list.
96 static inline void list_del_init(struct list_head *entry)
98 __list_del(entry->prev, entry->next);
99 INIT_LIST_HEAD(entry);
103 #ifndef list_for_each_entry
105 * list_move - delete from one list and add as another's head
106 * @list: the entry to move
107 * @head: the head that will precede our entry
109 static inline void list_move(struct list_head *list, struct list_head *head)
111 __list_del(list->prev, list->next);
112 list_add(list, head);
116 * list_move_tail - delete from one list and add as another's tail
117 * @list: the entry to move
118 * @head: the head that will follow our entry
120 static inline void list_move_tail(struct list_head *list,
121 struct list_head *head)
123 __list_del(list->prev, list->next);
124 list_add_tail(list, head);
128 #ifndef _LINUX_LIST_H
129 #define _LINUX_LIST_H
131 * list_empty - tests whether a list is empty
132 * @head: the list to test.
134 static inline int list_empty(struct list_head *head)
136 return head->next == head;
139 static inline void __list_splice(struct list_head *list,
140 struct list_head *head)
142 struct list_head *first = list->next;
143 struct list_head *last = list->prev;
144 struct list_head *at = head->next;
154 * list_splice - join two lists
155 * @list: the new list to add.
156 * @head: the place to add it in the first list.
158 static inline void list_splice(struct list_head *list, struct list_head *head)
160 if (!list_empty(list))
161 __list_splice(list, head);
165 * list_splice_init - join two lists and reinitialise the emptied list.
166 * @list: the new list to add.
167 * @head: the place to add it in the first list.
169 * The list at @list is reinitialised
171 static inline void list_splice_init(struct list_head *list,
172 struct list_head *head)
174 if (!list_empty(list)) {
175 __list_splice(list, head);
176 INIT_LIST_HEAD(list);
181 * list_entry - get the struct for this entry
182 * @ptr: the &struct list_head pointer.
183 * @type: the type of the struct this is embedded in.
184 * @member: the name of the list_struct within the struct.
186 #define list_entry(ptr, type, member) \
187 ((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))
190 * list_for_each - iterate over a list
191 * @pos: the &struct list_head to use as a loop counter.
192 * @head: the head for your list.
194 #define list_for_each(pos, head) \
195 for (pos = (head)->next ; pos != (head); pos = pos->next )
198 * list_for_each_prev - iterate over a list in reverse order
199 * @pos: the &struct list_head to use as a loop counter.
200 * @head: the head for your list.
202 #define list_for_each_prev(pos, head) \
203 for (pos = (head)->prev ; pos != (head); pos = 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 &pos->member != (head); \
227 pos = list_entry(pos->member.next, typeof(*pos), member))
230 #ifndef list_for_each_entry_safe
232 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
233 * @pos: the type * to use as a loop counter.
234 * @n: another type * to use as temporary storage
235 * @head: the head for your list.
236 * @member: the name of the list_struct within the struct.
238 #define list_for_each_entry_safe(pos, n, head, member) \
239 for (pos = list_entry((head)->next, typeof(*pos), member), \
240 n = list_entry(pos->member.next, typeof(*pos), member); \
241 &pos->member != (head); \
242 pos = n, n = list_entry(n->member.next, typeof(*n), member))