2 * Based on list.h from Linux Kernel
8 #define LIST_POISON1 ((void*) 0x8585858585858585ULL)
9 #define LIST_POISON2 ((void*) 0x8686868686868686ULL)
12 * Simple doubly linked list implementation.
14 * Some of the internal functions ("__xxx") are useful when
15 * manipulating whole lists rather than single entries, as
16 * sometimes we already know the next/prev entries and we can
17 * generate better code by using them directly rather than
18 * using the generic single-entry routines.
22 struct list_head
*next
, *prev
;
25 #define LIST_HEAD_INIT(name) { &(name), &(name) }
27 #define LIST_HEAD(name) \
28 struct list_head name = LIST_HEAD_INIT(name)
30 static inline void INIT_LIST_HEAD(struct list_head
*list
)
37 * Insert a new entry between two known consecutive entries.
39 * This is only for internal list manipulation where we know
40 * the prev/next entries already!
42 static inline void __list_add(struct list_head
*new,
43 struct list_head
*prev
,
44 struct list_head
*next
)
53 * list_add - add a new entry
54 * @new: new entry to be added
55 * @head: list head to add it after
57 * Insert a new entry after the specified head.
58 * This is good for implementing stacks.
60 static inline void list_add(struct list_head
*new, struct list_head
*head
)
62 __list_add(new, head
, head
->next
);
67 * list_add_tail - add a new entry
68 * @new: new entry to be added
69 * @head: list head to add it before
71 * Insert a new entry before the specified head.
72 * This is useful for implementing queues.
74 static inline void list_add_tail(struct list_head
*new, struct list_head
*head
)
76 __list_add(new, head
->prev
, head
);
80 * Delete a list entry by making the prev/next entries
81 * point to each other.
83 * This is only for internal list manipulation where we know
84 * the prev/next entries already!
86 static inline void __list_del(struct list_head
* prev
, struct list_head
* next
)
93 * list_del - deletes entry from list.
94 * @entry: the element to delete from the list.
95 * Note: list_empty() on entry does not return true after this, the entry is
96 * in an undefined state.
98 static inline void list_del(struct list_head
*entry
)
100 __list_del(entry
->prev
, entry
->next
);
101 entry
->next
= LIST_POISON1
;
102 entry
->prev
= LIST_POISON2
;
106 * list_replace - replace old entry by new one
107 * @old : the element to be replaced
108 * @new : the new element to insert
110 * If @old was empty, it will be overwritten.
112 static inline void list_replace(struct list_head
*old
,
113 struct list_head
*new)
115 new->next
= old
->next
;
116 new->next
->prev
= new;
117 new->prev
= old
->prev
;
118 new->prev
->next
= new;
121 static inline void list_replace_init(struct list_head
*old
,
122 struct list_head
*new)
124 list_replace(old
, new);
129 * list_del_init - deletes entry from list and reinitialize it.
130 * @entry: the element to delete from the list.
132 static inline void list_del_init(struct list_head
*entry
)
134 __list_del(entry
->prev
, entry
->next
);
135 INIT_LIST_HEAD(entry
);
139 * list_move - delete from one list and add as another's head
140 * @list: the entry to move
141 * @head: the head that will precede our entry
143 static inline void list_move(struct list_head
*list
, struct list_head
*head
)
145 __list_del(list
->prev
, list
->next
);
146 list_add(list
, head
);
150 * list_move_tail - delete from one list and add as another's tail
151 * @list: the entry to move
152 * @head: the head that will follow our entry
154 static inline void list_move_tail(struct list_head
*list
,
155 struct list_head
*head
)
157 __list_del(list
->prev
, list
->next
);
158 list_add_tail(list
, head
);
162 * list_is_last - tests whether @list is the last entry in list @head
163 * @list: the entry to test
164 * @head: the head of the list
166 static inline int list_is_last(const struct list_head
*list
,
167 const struct list_head
*head
)
169 return list
->next
== head
;
173 * list_empty - tests whether a list is empty
174 * @head: the list to test.
176 static inline int list_empty(const struct list_head
*head
)
178 return head
->next
== head
;
182 * list_empty_careful - tests whether a list is empty and not being modified
183 * @head: the list to test
186 * tests whether a list is empty _and_ checks that no other CPU might be
187 * in the process of modifying either member (next or prev)
189 * NOTE: using list_empty_careful() without synchronization
190 * can only be safe if the only activity that can happen
191 * to the list entry is list_del_init(). Eg. it cannot be used
192 * if another CPU could re-list_add() it.
194 static inline int list_empty_careful(const struct list_head
*head
)
196 struct list_head
*next
= head
->next
;
197 return (next
== head
) && (next
== head
->prev
);
200 static inline void __list_splice(struct list_head
*list
,
201 struct list_head
*head
)
203 struct list_head
*first
= list
->next
;
204 struct list_head
*last
= list
->prev
;
205 struct list_head
*at
= head
->next
;
215 * list_splice - join two lists
216 * @list: the new list to add.
217 * @head: the place to add it in the first list.
219 static inline void list_splice(struct list_head
*list
, struct list_head
*head
)
221 if (!list_empty(list
))
222 __list_splice(list
, head
);
226 * list_splice_tail - join two lists
227 * @list: the new list to add.
228 * @head: the place to add it before in the first list.
230 static inline void list_splice_tail(struct list_head
*list
, struct list_head
*head
)
232 if (!list_empty(list
))
233 __list_splice(list
, head
->prev
);
237 * list_splice_init - join two lists and reinitialise the emptied list.
238 * @list: the new list to add.
239 * @head: the place to add it in the first list.
241 * The list at @list is reinitialised
243 static inline void list_splice_init(struct list_head
*list
,
244 struct list_head
*head
)
246 if (!list_empty(list
)) {
247 __list_splice(list
, head
);
248 INIT_LIST_HEAD(list
);
253 * list_splice_tail_init - join two lists and reinitialise the emptied list.
254 * @list: the new list to add.
255 * @head: the place to add it before in the first list.
257 * The list at @list is reinitialised
259 static inline void list_splice_tail_init(struct list_head
*list
,
260 struct list_head
*head
)
262 if (!list_empty(list
)) {
263 __list_splice(list
, head
->prev
);
264 INIT_LIST_HEAD(list
);
269 * list_entry - get the struct for this entry
270 * @ptr: the &struct list_head pointer.
271 * @type: the type of the struct this is embedded in.
272 * @member: the name of the list_struct within the struct.
274 #define list_entry(ptr, type, member) \
275 container_of(ptr, type, member)
278 * list_first_entry - get the first element from a list
279 * @ptr: the list head to take the element from.
280 * @type: the type of the struct this is embedded in.
281 * @member: the name of the list_struct within the struct.
283 * Note, that list is expected to be not empty.
285 #define list_first_entry(ptr, type, member) \
286 list_entry((ptr)->next, type, member)
289 * list_last_entry - get the last element from a list
290 * @ptr: the list head to take the element from.
291 * @type: the type of the struct this is embedded in.
292 * @member: the name of the list_struct within the struct.
294 * Note, that list is expected to be not empty.
296 #define list_last_entry(ptr, type, member) \
297 list_entry((ptr)->prev, type, member)
300 * list_for_each - iterate over a list
301 * @pos: the &struct list_head to use as a loop cursor.
302 * @head: the head for your list.
304 #define list_for_each(pos, head) \
305 for (pos = (head)->next; prefetch(pos->next), pos != (head); \
309 * __list_for_each - iterate over a list
310 * @pos: the &struct list_head to use as a loop cursor.
311 * @head: the head for your list.
313 * This variant differs from list_for_each() in that it's the
314 * simplest possible list iteration code, no prefetching is done.
315 * Use this for code that knows the list to be very short (empty
316 * or 1 entry) most of the time.
318 #define __list_for_each(pos, head) \
319 for (pos = (head)->next; pos != (head); pos = pos->next)
322 * list_for_each_prev - iterate over a list backwards
323 * @pos: the &struct list_head to use as a loop cursor.
324 * @head: the head for your list.
326 #define list_for_each_prev(pos, head) \
327 for (pos = (head)->prev; prefetch(pos->prev), pos != (head); \
331 * list_for_each_safe - iterate over a list safe against removal of list entry
332 * @pos: the &struct list_head to use as a loop cursor.
333 * @n: another &struct list_head to use as temporary storage
334 * @head: the head for your list.
336 #define list_for_each_safe(pos, n, head) \
337 for (pos = (head)->next, n = pos->next; pos != (head); \
338 pos = n, n = pos->next)
341 * list_for_each_entry - iterate over list of given type
342 * @pos: the type * to use as a loop cursor.
343 * @head: the head for your list.
344 * @member: the name of the list_struct within the struct.
346 #define list_for_each_entry(pos, head, member) \
347 for (pos = list_entry((head)->next, typeof(*pos), member); \
348 &pos->member != (head); \
349 pos = list_entry(pos->member.next, typeof(*pos), member))
352 * list_for_each_entry_reverse - iterate backwards over list of given type.
353 * @pos: the type * to use as a loop cursor.
354 * @head: the head for your list.
355 * @member: the name of the list_struct within the struct.
357 #define list_for_each_entry_reverse(pos, head, member) \
358 for (pos = list_entry((head)->prev, typeof(*pos), member); \
359 prefetch(pos->member.prev), &pos->member != (head); \
360 pos = list_entry(pos->member.prev, typeof(*pos), member))
363 * list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue()
364 * @pos: the type * to use as a start point
365 * @head: the head of the list
366 * @member: the name of the list_struct within the struct.
368 * Prepares a pos entry for use as a start point in list_for_each_entry_continue().
370 #define list_prepare_entry(pos, head, member) \
371 ((pos) ? : list_entry(head, typeof(*pos), member))
374 * list_for_each_entry_continue - continue iteration over list of given type
375 * @pos: the type * to use as a loop cursor.
376 * @head: the head for your list.
377 * @member: the name of the list_struct within the struct.
379 * Continue to iterate over list of given type, continuing after
380 * the current position.
382 #define list_for_each_entry_continue(pos, head, member) \
383 for (pos = list_entry(pos->member.next, typeof(*pos), member); \
384 prefetch(pos->member.next), &pos->member != (head); \
385 pos = list_entry(pos->member.next, typeof(*pos), member))
388 * list_for_each_entry_from - iterate over list of given type from the current point
389 * @pos: the type * to use as a loop cursor.
390 * @head: the head for your list.
391 * @member: the name of the list_struct within the struct.
393 * Iterate over list of given type, continuing from current position.
395 #define list_for_each_entry_from(pos, head, member) \
396 for (; prefetch(pos->member.next), &pos->member != (head); \
397 pos = list_entry(pos->member.next, typeof(*pos), member))
400 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
401 * @pos: the type * to use as a loop cursor.
402 * @n: another type * to use as temporary storage
403 * @head: the head for your list.
404 * @member: the name of the list_struct within the struct.
406 #define list_for_each_entry_safe(pos, n, head, member) \
407 for (pos = list_entry((head)->next, typeof(*pos), member), \
408 n = list_entry(pos->member.next, typeof(*pos), member); \
409 &pos->member != (head); \
410 pos = n, n = list_entry(n->member.next, typeof(*n), member))
413 * list_for_each_entry_safe_continue
414 * @pos: the type * to use as a loop cursor.
415 * @n: another type * to use as temporary storage
416 * @head: the head for your list.
417 * @member: the name of the list_struct within the struct.
419 * Iterate over list of given type, continuing after current point,
420 * safe against removal of list entry.
422 #define list_for_each_entry_safe_continue(pos, n, head, member) \
423 for (pos = list_entry(pos->member.next, typeof(*pos), member), \
424 n = list_entry(pos->member.next, typeof(*pos), member); \
425 &pos->member != (head); \
426 pos = n, n = list_entry(n->member.next, typeof(*n), member))
429 * list_for_each_entry_safe_from
430 * @pos: the type * to use as a loop cursor.
431 * @n: another type * to use as temporary storage
432 * @head: the head for your list.
433 * @member: the name of the list_struct within the struct.
435 * Iterate over list of given type from current point, safe against
436 * removal of list entry.
438 #define list_for_each_entry_safe_from(pos, n, head, member) \
439 for (n = list_entry(pos->member.next, typeof(*pos), member); \
440 &pos->member != (head); \
441 pos = n, n = list_entry(n->member.next, typeof(*n), member))
444 * list_for_each_entry_safe_reverse
445 * @pos: the type * to use as a loop cursor.
446 * @n: another type * to use as temporary storage
447 * @head: the head for your list.
448 * @member: the name of the list_struct within the struct.
450 * Iterate backwards over list of given type, safe against removal
453 #define list_for_each_entry_safe_reverse(pos, n, head, member) \
454 for (pos = list_entry((head)->prev, typeof(*pos), member), \
455 n = list_entry(pos->member.prev, typeof(*pos), member); \
456 &pos->member != (head); \
457 pos = n, n = list_entry(n->member.prev, typeof(*n), member))