1 #ifndef __LINUX_LIST_H__
2 #define __LINUX_LIST_H__
5 * Simple doubly linked list implementation.
7 * Some of the internal functions ("__xxx") are useful when
8 * manipulating whole lists rather than single entries, as
9 * sometimes we already know the next/prev entries and we can
10 * generate better code by using them directly rather than
11 * using the generic single-entry routines.
14 #define prefetch(a) ((void *)a)
17 struct list_head
*next
, *prev
;
20 #define LIST_HEAD_INIT(name) { &(name), &(name) }
22 #define LIST_HEAD(name) \
23 struct list_head name = LIST_HEAD_INIT(name)
25 static inline void INIT_LIST_HEAD(struct list_head
*list
)
32 * Insert a new entry between two known consecutive entries.
34 * This is only for internal list manipulation where we know
35 * the prev/next entries already!
37 static inline void __list_add(struct list_head
* new,
38 struct list_head
* prev
,
39 struct list_head
* next
)
48 * list_add - add a new entry
49 * @new: new entry to be added
50 * @head: list head to add it after
52 * Insert a new entry after the specified head.
53 * This is good for implementing stacks.
55 static inline void list_add(struct list_head
*new, struct list_head
*head
)
57 __list_add(new, head
, head
->next
);
61 * list_add_tail - add a new entry
62 * @new: new entry to be added
63 * @head: list head to add it before
65 * Insert a new entry before the specified head.
66 * This is useful for implementing queues.
68 static inline void list_add_tail(struct list_head
*new, struct list_head
*head
)
70 __list_add(new, head
->prev
, head
);
74 * Delete a list entry by making the prev/next entries
75 * point to each other.
77 * This is only for internal list manipulation where we know
78 * the prev/next entries already!
80 static inline void __list_del(struct list_head
* prev
, struct list_head
* next
)
87 * list_del - deletes entry from list.
88 * @entry: the element to delete from the list.
89 * Note: list_empty on entry does not return true after this, the entry is in an undefined state.
91 static inline void list_del(struct list_head
*entry
)
93 __list_del(entry
->prev
, entry
->next
);
97 * list_del_init - deletes entry from list and reinitialize it.
98 * @entry: the element to delete from the list.
100 static inline void list_del_init(struct list_head
*entry
)
102 __list_del(entry
->prev
, entry
->next
);
103 INIT_LIST_HEAD(entry
);
107 * list_move - delete from one list and add as another's head
108 * @list: the entry to move
109 * @head: the head that will precede our entry
111 static inline void list_move(struct list_head
*list
, struct list_head
*head
)
113 __list_del(list
->prev
, list
->next
);
114 list_add(list
, head
);
118 * list_move_tail - delete from one list and add as another's tail
119 * @list: the entry to move
120 * @head: the head that will follow our entry
122 static inline void list_move_tail(struct list_head
*list
,
123 struct list_head
*head
)
125 __list_del(list
->prev
, list
->next
);
126 list_add_tail(list
, head
);
130 * list_empty - tests whether a list is empty
131 * @head: the list to test.
133 static inline int list_empty(struct list_head
*head
)
135 return head
->next
== head
;
138 static inline int list_empty_careful(const struct list_head
*head
)
140 struct list_head
*next
= head
->next
;
141 return (next
== head
) && (next
== head
->prev
);
144 static inline void __list_splice(struct list_head
*list
,
145 struct list_head
*head
)
147 struct list_head
*first
= list
->next
;
148 struct list_head
*last
= list
->prev
;
149 struct list_head
*at
= head
->next
;
159 * list_splice - join two lists
160 * @list: the new list to add.
161 * @head: the place to add it in the first list.
163 static inline void list_splice(struct list_head
*list
, struct list_head
*head
)
165 if (!list_empty(list
))
166 __list_splice(list
, head
);
170 * list_splice_init - join two lists and reinitialise the emptied list.
171 * @list: the new list to add.
172 * @head: the place to add it in the first list.
174 * The list at @list is reinitialised
176 static inline void list_splice_init(struct list_head
*list
,
177 struct list_head
*head
)
179 if (!list_empty(list
)) {
180 __list_splice(list
, head
);
181 INIT_LIST_HEAD(list
);
186 * list_entry - get the struct for this entry
187 * @ptr: the &struct list_head pointer.
188 * @type: the type of the struct this is embedded in.
189 * @member: the name of the list_struct within the struct.
191 #define list_entry(ptr, type, member) \
192 ((type *)((char *)(ptr)-(char *)(&((type *)0)->member)))
195 * list_for_each - iterate over a list
196 * @pos: the &struct list_head to use as a loop counter.
197 * @head: the head for your list.
199 #define list_for_each(pos, head) \
200 for (pos = (head)->next, prefetch(pos->next); pos != (head); \
201 pos = pos->next, prefetch(pos->next))
204 * list_for_each_safe - iterate over a list safe against removal of list entry
205 * @pos: the &struct list_head to use as a loop counter.
206 * @n: another &struct list_head to use as temporary storage
207 * @head: the head for your list.
209 #define list_for_each_safe(pos, n, head) \
210 for (pos = (head)->next, n = pos->next; pos != (head); \
211 pos = n, n = pos->next)
213 #ifndef list_for_each_prev
215 * list_for_each_prev - iterate over a list in reverse order
216 * @pos: the &struct list_head to use as a loop counter.
217 * @head: the head for your list.
219 #define list_for_each_prev(pos, head) \
220 for (pos = (head)->prev, prefetch(pos->prev); pos != (head); \
221 pos = pos->prev, prefetch(pos->prev))
223 #endif /* list_for_each_prev */
225 #ifndef list_for_each_entry
227 * list_for_each_entry - iterate over list of given type
228 * @pos: the type * to use as a loop counter.
229 * @head: the head for your list.
230 * @member: the name of the list_struct within the struct.
232 #define list_for_each_entry(pos, head, type, member) \
233 for (pos = list_entry((head)->next, type, member), \
234 prefetch(pos->member.next); \
235 &pos->member != (head); \
236 pos = list_entry(pos->member.next, type, member), \
237 prefetch(pos->member.next))
238 #endif /* list_for_each_entry */
240 #ifndef list_for_each_entry_safe
242 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
243 * @pos: the type * to use as a loop counter.
244 * @n: another type * to use as temporary storage
245 * @head: the head for your list.
246 * @member: the name of the list_struct within the struct.
248 #define list_for_each_entry_safe(pos, n, head, type, member) \
249 for (pos = list_entry((head)->next, type, member), \
250 n = list_entry(pos->member.next, type, member); \
251 &pos->member != (head); \
252 pos = n, n = list_entry(n->member.next, type, member))
253 #endif /* list_for_each_entry_safe */
255 #endif /* __LINUX_LIST_H__ */