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 LIST_POISON1 ((void *) 0x00100100)
15 #define LIST_POISON2 ((void *) 0x00200200)
16 static inline void prefetch(const void *x
) {;}
19 struct list_head
*next
, *prev
;
22 #define LIST_HEAD_INIT(name) { &(name), &(name) }
24 #define LIST_HEAD(name) \
25 struct list_head name = LIST_HEAD_INIT(name)
27 static inline void INIT_LIST_HEAD(struct list_head
*list
)
34 * Insert a new entry between two known consecutive entries.
36 * This is only for internal list manipulation where we know
37 * the prev/next entries already!
39 #ifndef CONFIG_DEBUG_LIST
40 static inline void __list_add(struct list_head
*new,
41 struct list_head
*prev
,
42 struct list_head
*next
)
50 extern void __list_add(struct list_head
*new,
51 struct list_head
*prev
,
52 struct list_head
*next
);
56 * list_add - add a new entry
57 * @new: new entry to be added
58 * @head: list head to add it after
60 * Insert a new entry after the specified head.
61 * This is good for implementing stacks.
63 #ifndef CONFIG_DEBUG_LIST
64 static inline void list_add(struct list_head
*new, struct list_head
*head
)
66 __list_add(new, head
, head
->next
);
69 extern void list_add(struct list_head
*new, struct list_head
*head
);
74 * list_add_tail - add a new entry
75 * @new: new entry to be added
76 * @head: list head to add it before
78 * Insert a new entry before the specified head.
79 * This is useful for implementing queues.
81 static inline void list_add_tail(struct list_head
*new, struct list_head
*head
)
83 __list_add(new, head
->prev
, head
);
87 * Delete a list entry by making the prev/next entries
88 * point to each other.
90 * This is only for internal list manipulation where we know
91 * the prev/next entries already!
93 static inline void __list_del(struct list_head
* prev
, struct list_head
* next
)
100 * list_del - deletes entry from list.
101 * @entry: the element to delete from the list.
102 * Note: list_empty() on entry does not return true after this, the entry is
103 * in an undefined state.
105 #ifndef CONFIG_DEBUG_LIST
106 static inline void list_del(struct list_head
*entry
)
108 __list_del(entry
->prev
, entry
->next
);
109 entry
->next
= LIST_POISON1
;
110 entry
->prev
= LIST_POISON2
;
113 extern void list_del(struct list_head
*entry
);
117 * list_replace - replace old entry by new one
118 * @old : the element to be replaced
119 * @new : the new element to insert
121 * If @old was empty, it will be overwritten.
123 static inline void list_replace(struct list_head
*old
,
124 struct list_head
*new)
126 new->next
= old
->next
;
127 new->next
->prev
= new;
128 new->prev
= old
->prev
;
129 new->prev
->next
= new;
132 static inline void list_replace_init(struct list_head
*old
,
133 struct list_head
*new)
135 list_replace(old
, new);
140 * list_del_init - deletes entry from list and reinitialize it.
141 * @entry: the element to delete from the list.
143 static inline void list_del_init(struct list_head
*entry
)
145 __list_del(entry
->prev
, entry
->next
);
146 INIT_LIST_HEAD(entry
);
150 * list_move - delete from one list and add as another's head
151 * @list: the entry to move
152 * @head: the head that will precede our entry
154 static inline void list_move(struct list_head
*list
, struct list_head
*head
)
156 __list_del(list
->prev
, list
->next
);
157 list_add(list
, head
);
161 * list_move_tail - delete from one list and add as another's tail
162 * @list: the entry to move
163 * @head: the head that will follow our entry
165 static inline void list_move_tail(struct list_head
*list
,
166 struct list_head
*head
)
168 __list_del(list
->prev
, list
->next
);
169 list_add_tail(list
, head
);
173 * list_is_last - tests whether @list is the last entry in list @head
174 * @list: the entry to test
175 * @head: the head of the list
177 static inline int list_is_last(const struct list_head
*list
,
178 const struct list_head
*head
)
180 return list
->next
== head
;
184 * list_empty - tests whether a list is empty
185 * @head: the list to test.
187 static inline int list_empty(const struct list_head
*head
)
189 return head
->next
== head
;
193 * list_empty_careful - tests whether a list is empty and not being modified
194 * @head: the list to test
197 * tests whether a list is empty _and_ checks that no other CPU might be
198 * in the process of modifying either member (next or prev)
200 * NOTE: using list_empty_careful() without synchronization
201 * can only be safe if the only activity that can happen
202 * to the list entry is list_del_init(). Eg. it cannot be used
203 * if another CPU could re-list_add() it.
205 static inline int list_empty_careful(const struct list_head
*head
)
207 struct list_head
*next
= head
->next
;
208 return (next
== head
) && (next
== head
->prev
);
211 static inline void __list_splice(struct list_head
*list
,
212 struct list_head
*head
)
214 struct list_head
*first
= list
->next
;
215 struct list_head
*last
= list
->prev
;
216 struct list_head
*at
= head
->next
;
226 * list_splice - join two lists
227 * @list: the new list to add.
228 * @head: the place to add it in the first list.
230 static inline void list_splice(struct list_head
*list
, struct list_head
*head
)
232 if (!list_empty(list
))
233 __list_splice(list
, head
);
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_entry - get the struct for this entry
254 * @ptr: the &struct list_head pointer.
255 * @type: the type of the struct this is embedded in.
256 * @member: the name of the list_struct within the struct.
258 #define list_entry(ptr, type, member) \
259 container_of(ptr, type, member)
262 * list_first_entry - get the first element from a list
263 * @ptr: the list head to take the element from.
264 * @type: the type of the struct this is embedded in.
265 * @member: the name of the list_struct within the struct.
267 * Note, that list is expected to be not empty.
269 #define list_first_entry(ptr, type, member) \
270 list_entry((ptr)->next, type, member)
273 * list_for_each - iterate over a list
274 * @pos: the &struct list_head to use as a loop cursor.
275 * @head: the head for your list.
277 #define list_for_each(pos, head) \
278 for (pos = (head)->next; prefetch(pos->next), pos != (head); \
282 * __list_for_each - iterate over a list
283 * @pos: the &struct list_head to use as a loop cursor.
284 * @head: the head for your list.
286 * This variant differs from list_for_each() in that it's the
287 * simplest possible list iteration code, no prefetching is done.
288 * Use this for code that knows the list to be very short (empty
289 * or 1 entry) most of the time.
291 #define __list_for_each(pos, head) \
292 for (pos = (head)->next; pos != (head); pos = pos->next)
295 * list_for_each_prev - iterate over a list backwards
296 * @pos: the &struct list_head to use as a loop cursor.
297 * @head: the head for your list.
299 #define list_for_each_prev(pos, head) \
300 for (pos = (head)->prev; prefetch(pos->prev), pos != (head); \
304 * list_for_each_safe - iterate over a list safe against removal of list entry
305 * @pos: the &struct list_head to use as a loop cursor.
306 * @n: another &struct list_head to use as temporary storage
307 * @head: the head for your list.
309 #define list_for_each_safe(pos, n, head) \
310 for (pos = (head)->next, n = pos->next; pos != (head); \
311 pos = n, n = pos->next)
314 * list_for_each_entry - iterate over list of given type
315 * @pos: the type * to use as a loop cursor.
316 * @head: the head for your list.
317 * @member: the name of the list_struct within the struct.
319 #define list_for_each_entry(pos, head, member) \
320 for (pos = list_entry((head)->next, typeof(*pos), member); \
321 prefetch(pos->member.next), &pos->member != (head); \
322 pos = list_entry(pos->member.next, typeof(*pos), member))
325 * list_for_each_entry_reverse - iterate backwards over list of given type.
326 * @pos: the type * to use as a loop cursor.
327 * @head: the head for your list.
328 * @member: the name of the list_struct within the struct.
330 #define list_for_each_entry_reverse(pos, head, member) \
331 for (pos = list_entry((head)->prev, typeof(*pos), member); \
332 prefetch(pos->member.prev), &pos->member != (head); \
333 pos = list_entry(pos->member.prev, typeof(*pos), member))
336 * list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue()
337 * @pos: the type * to use as a start point
338 * @head: the head of the list
339 * @member: the name of the list_struct within the struct.
341 * Prepares a pos entry for use as a start point in list_for_each_entry_continue().
343 #define list_prepare_entry(pos, head, member) \
344 ((pos) ? : list_entry(head, typeof(*pos), member))
347 * list_for_each_entry_continue - continue iteration over list of given type
348 * @pos: the type * to use as a loop cursor.
349 * @head: the head for your list.
350 * @member: the name of the list_struct within the struct.
352 * Continue to iterate over list of given type, continuing after
353 * the current position.
355 #define list_for_each_entry_continue(pos, head, member) \
356 for (pos = list_entry(pos->member.next, typeof(*pos), member); \
357 prefetch(pos->member.next), &pos->member != (head); \
358 pos = list_entry(pos->member.next, typeof(*pos), member))
361 * list_for_each_entry_from - iterate over list of given type from the current point
362 * @pos: the type * to use as a loop cursor.
363 * @head: the head for your list.
364 * @member: the name of the list_struct within the struct.
366 * Iterate over list of given type, continuing from current position.
368 #define list_for_each_entry_from(pos, head, member) \
369 for (; prefetch(pos->member.next), &pos->member != (head); \
370 pos = list_entry(pos->member.next, typeof(*pos), member))
373 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
374 * @pos: the type * to use as a loop cursor.
375 * @n: another type * to use as temporary storage
376 * @head: the head for your list.
377 * @member: the name of the list_struct within the struct.
379 #define list_for_each_entry_safe(pos, n, head, member) \
380 for (pos = list_entry((head)->next, typeof(*pos), member), \
381 n = list_entry(pos->member.next, typeof(*pos), member); \
382 &pos->member != (head); \
383 pos = n, n = list_entry(n->member.next, typeof(*n), member))
386 * list_for_each_entry_safe_continue
387 * @pos: the type * to use as a loop cursor.
388 * @n: another type * to use as temporary storage
389 * @head: the head for your list.
390 * @member: the name of the list_struct within the struct.
392 * Iterate over list of given type, continuing after current point,
393 * safe against removal of list entry.
395 #define list_for_each_entry_safe_continue(pos, n, head, member) \
396 for (pos = list_entry(pos->member.next, typeof(*pos), member), \
397 n = list_entry(pos->member.next, typeof(*pos), member); \
398 &pos->member != (head); \
399 pos = n, n = list_entry(n->member.next, typeof(*n), member))
402 * list_for_each_entry_safe_from
403 * @pos: the type * to use as a loop cursor.
404 * @n: another type * to use as temporary storage
405 * @head: the head for your list.
406 * @member: the name of the list_struct within the struct.
408 * Iterate over list of given type from current point, safe against
409 * removal of list entry.
411 #define list_for_each_entry_safe_from(pos, n, head, member) \
412 for (n = list_entry(pos->member.next, typeof(*pos), member); \
413 &pos->member != (head); \
414 pos = n, n = list_entry(n->member.next, typeof(*n), member))
417 * list_for_each_entry_safe_reverse
418 * @pos: the type * to use as a loop cursor.
419 * @n: another type * to use as temporary storage
420 * @head: the head for your list.
421 * @member: the name of the list_struct within the struct.
423 * Iterate backwards over list of given type, safe against removal
426 #define list_for_each_entry_safe_reverse(pos, n, head, member) \
427 for (pos = list_entry((head)->prev, typeof(*pos), member), \
428 n = list_entry(pos->member.prev, typeof(*pos), member); \
429 &pos->member != (head); \
430 pos = n, n = list_entry(n->member.prev, typeof(*n), member))
433 * list_add_sort - add a new entry to a sorted list
434 * @new: new entry to be added
435 * @head: list head to add it in
436 * @compare: Compare function to compare two list entries
438 * Insert a new entry before the specified head.
439 * This is useful for implementing queues.
441 static inline void list_add_sort(struct list_head
*new, struct list_head
*head
,
442 int (*compare
)(struct list_head
*a
, struct list_head
*b
))
444 struct list_head
*pos
, *insert
= head
;
446 list_for_each(pos
, head
) {
447 if (compare(pos
, new) < 0)
453 list_add_tail(new, insert
);
457 * Double linked lists with a single pointer list head.
458 * Mostly useful for hash tables where the two pointer list head is
460 * You lose the ability to access the tail in O(1).
464 struct hlist_node
*first
;
468 struct hlist_node
*next
, **pprev
;
471 #define HLIST_HEAD_INIT { .first = NULL }
472 #define HLIST_HEAD(name) struct hlist_head name = { .first = NULL }
473 #define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
474 static inline void INIT_HLIST_NODE(struct hlist_node
*h
)
480 static inline int hlist_unhashed(const struct hlist_node
*h
)
485 static inline int hlist_empty(const struct hlist_head
*h
)
490 static inline void __hlist_del(struct hlist_node
*n
)
492 struct hlist_node
*next
= n
->next
;
493 struct hlist_node
**pprev
= n
->pprev
;
499 static inline void hlist_del(struct hlist_node
*n
)
502 n
->next
= LIST_POISON1
;
503 n
->pprev
= LIST_POISON2
;
506 static inline void hlist_del_init(struct hlist_node
*n
)
508 if (!hlist_unhashed(n
)) {
514 static inline void hlist_add_head(struct hlist_node
*n
, struct hlist_head
*h
)
516 struct hlist_node
*first
= h
->first
;
519 first
->pprev
= &n
->next
;
521 n
->pprev
= &h
->first
;
524 /* next must be != NULL */
525 static inline void hlist_add_before(struct hlist_node
*n
,
526 struct hlist_node
*next
)
528 n
->pprev
= next
->pprev
;
530 next
->pprev
= &n
->next
;
534 static inline void hlist_add_after(struct hlist_node
*n
,
535 struct hlist_node
*next
)
537 next
->next
= n
->next
;
539 next
->pprev
= &n
->next
;
542 next
->next
->pprev
= &next
->next
;
545 #define hlist_entry(ptr, type, member) container_of(ptr,type,member)
547 #define hlist_for_each(pos, head) \
548 for (pos = (head)->first; pos && ({ prefetch(pos->next); 1; }); \
551 #define hlist_for_each_safe(pos, n, head) \
552 for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
556 * hlist_for_each_entry - iterate over list of given type
557 * @tpos: the type * to use as a loop cursor.
558 * @pos: the &struct hlist_node to use as a loop cursor.
559 * @head: the head for your list.
560 * @member: the name of the hlist_node within the struct.
562 #define hlist_for_each_entry(tpos, pos, head, member) \
563 for (pos = (head)->first; \
564 pos && ({ prefetch(pos->next); 1;}) && \
565 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
569 * hlist_for_each_entry_continue - iterate over a hlist continuing after current point
570 * @tpos: the type * to use as a loop cursor.
571 * @pos: the &struct hlist_node to use as a loop cursor.
572 * @member: the name of the hlist_node within the struct.
574 #define hlist_for_each_entry_continue(tpos, pos, member) \
575 for (pos = (pos)->next; \
576 pos && ({ prefetch(pos->next); 1;}) && \
577 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
581 * hlist_for_each_entry_from - iterate over a hlist continuing from current point
582 * @tpos: the type * to use as a loop cursor.
583 * @pos: the &struct hlist_node to use as a loop cursor.
584 * @member: the name of the hlist_node within the struct.
586 #define hlist_for_each_entry_from(tpos, pos, member) \
587 for (; pos && ({ prefetch(pos->next); 1;}) && \
588 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
592 * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
593 * @tpos: the type * to use as a loop cursor.
594 * @pos: the &struct hlist_node to use as a loop cursor.
595 * @n: another &struct hlist_node to use as temporary storage
596 * @head: the head for your list.
597 * @member: the name of the hlist_node within the struct.
599 #define hlist_for_each_entry_safe(tpos, pos, n, head, member) \
600 for (pos = (head)->first; \
601 pos && ({ n = pos->next; 1; }) && \
602 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \