4 #include <linux/stddef.h>
5 #include <linux/poison.h>
7 #ifndef ARCH_HAS_PREFETCH
8 #define ARCH_HAS_PREFETCH
9 static inline void prefetch(const void *x) {;}
13 * Simple doubly linked list implementation.
15 * Some of the internal functions ("__xxx") are useful when
16 * manipulating whole lists rather than single entries, as
17 * sometimes we already know the next/prev entries and we can
18 * generate better code by using them directly rather than
19 * using the generic single-entry routines.
23 struct list_head *next, *prev;
26 #define LIST_HEAD_INIT(name) { &(name), &(name) }
28 #define LIST_HEAD(name) \
29 struct list_head name = LIST_HEAD_INIT(name)
31 static inline void INIT_LIST_HEAD(struct list_head *list)
38 * Insert a new entry between two known consecutive entries.
40 * This is only for internal list manipulation where we know
41 * the prev/next entries already!
43 static inline void __list_add(struct list_head *new,
44 struct list_head *prev,
45 struct list_head *next)
54 * list_add - add a new entry
55 * @new: new entry to be added
56 * @head: list head to add it after
58 * Insert a new entry after the specified head.
59 * This is good for implementing stacks.
61 static inline void list_add(struct list_head *new, struct list_head *head)
63 __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);
201 * list_is_singular - tests whether a list has just one entry.
202 * @head: the list to test.
204 static inline int list_is_singular(const struct list_head *head)
206 return !list_empty(head) && (head->next == head->prev);
209 static inline void __list_cut_position(struct list_head *list,
210 struct list_head *head, struct list_head *entry)
212 struct list_head *new_first = entry->next;
213 list->next = head->next;
214 list->next->prev = list;
217 head->next = new_first;
218 new_first->prev = head;
222 * list_cut_position - cut a list into two
223 * @list: a new list to add all removed entries
224 * @head: a list with entries
225 * @entry: an entry within head, could be the head itself
226 * and if so we won't cut the list
228 * This helper moves the initial part of @head, up to and
229 * including @entry, from @head to @list. You should
230 * pass on @entry an element you know is on @head. @list
231 * should be an empty list or a list you do not care about
235 static inline void list_cut_position(struct list_head *list,
236 struct list_head *head, struct list_head *entry)
238 if (list_empty(head))
240 if (list_is_singular(head) &&
241 (head->next != entry && head != entry))
244 INIT_LIST_HEAD(list);
246 __list_cut_position(list, head, entry);
249 static inline void __list_splice(const struct list_head *list,
250 struct list_head *prev,
251 struct list_head *next)
253 struct list_head *first = list->next;
254 struct list_head *last = list->prev;
264 * list_splice - join two lists, this is designed for stacks
265 * @list: the new list to add.
266 * @head: the place to add it in the first list.
268 static inline void list_splice(const struct list_head *list,
269 struct list_head *head)
271 if (!list_empty(list))
272 __list_splice(list, head, head->next);
276 * list_splice_tail - join two lists, each list being a queue
277 * @list: the new list to add.
278 * @head: the place to add it in the first list.
280 static inline void list_splice_tail(struct list_head *list,
281 struct list_head *head)
283 if (!list_empty(list))
284 __list_splice(list, head->prev, head);
288 * list_splice_init - join two lists and reinitialise the emptied list.
289 * @list: the new list to add.
290 * @head: the place to add it in the first list.
292 * The list at @list is reinitialised
294 static inline void list_splice_init(struct list_head *list,
295 struct list_head *head)
297 if (!list_empty(list)) {
298 __list_splice(list, head, head->next);
299 INIT_LIST_HEAD(list);
304 * list_splice_tail_init - join two lists and reinitialise the emptied list
305 * @list: the new list to add.
306 * @head: the place to add it in the first list.
308 * Each of the lists is a queue.
309 * The list at @list is reinitialised
311 static inline void list_splice_tail_init(struct list_head *list,
312 struct list_head *head)
314 if (!list_empty(list)) {
315 __list_splice(list, head->prev, head);
316 INIT_LIST_HEAD(list);
321 * list_entry - get the struct for this entry
322 * @ptr: the &struct list_head pointer.
323 * @type: the type of the struct this is embedded in.
324 * @member: the name of the list_struct within the struct.
326 #define list_entry(ptr, type, member) \
327 container_of(ptr, type, member)
330 * list_first_entry - get the first element from a list
331 * @ptr: the list head to take the element from.
332 * @type: the type of the struct this is embedded in.
333 * @member: the name of the list_struct within the struct.
335 * Note, that list is expected to be not empty.
337 #define list_first_entry(ptr, type, member) \
338 list_entry((ptr)->next, type, member)
341 * list_last_entry - get the last element from a list
342 * @ptr: the list head to take the element from.
343 * @type: the type of the struct this is embedded in.
344 * @member: the name of the list_struct within the struct.
346 * Note, that list is expected to be not empty.
348 #define list_last_entry(ptr, type, member) \
349 list_entry((ptr)->prev, type, member)
352 * list_for_each - iterate over a list
353 * @pos: the &struct list_head to use as a loop cursor.
354 * @head: the head for your list.
356 #define list_for_each(pos, head) \
357 for (pos = (head)->next; prefetch(pos->next), pos != (head); \
361 * __list_for_each - iterate over a list
362 * @pos: the &struct list_head to use as a loop cursor.
363 * @head: the head for your list.
365 * This variant differs from list_for_each() in that it's the
366 * simplest possible list iteration code, no prefetching is done.
367 * Use this for code that knows the list to be very short (empty
368 * or 1 entry) most of the time.
370 #define __list_for_each(pos, head) \
371 for (pos = (head)->next; pos != (head); pos = pos->next)
374 * list_for_each_prev - iterate over a list backwards
375 * @pos: the &struct list_head to use as a loop cursor.
376 * @head: the head for your list.
378 #define list_for_each_prev(pos, head) \
379 for (pos = (head)->prev; prefetch(pos->prev), pos != (head); \
383 * list_for_each_safe - iterate over a list safe against removal of list entry
384 * @pos: the &struct list_head to use as a loop cursor.
385 * @n: another &struct list_head to use as temporary storage
386 * @head: the head for your list.
388 #define list_for_each_safe(pos, n, head) \
389 for (pos = (head)->next, n = pos->next; pos != (head); \
390 pos = n, n = pos->next)
393 * list_for_each_prev_safe - iterate over a list backwards safe against removal of list entry
394 * @pos: the &struct list_head to use as a loop cursor.
395 * @n: another &struct list_head to use as temporary storage
396 * @head: the head for your list.
398 #define list_for_each_prev_safe(pos, n, head) \
399 for (pos = (head)->prev, n = pos->prev; \
400 prefetch(pos->prev), pos != (head); \
401 pos = n, n = pos->prev)
404 * list_for_each_entry - iterate over list of given type
405 * @pos: the type * to use as a loop cursor.
406 * @head: the head for your list.
407 * @member: the name of the list_struct within the struct.
409 #define list_for_each_entry(pos, head, member) \
410 for (pos = list_entry((head)->next, typeof(*pos), member); \
411 prefetch(pos->member.next), &pos->member != (head); \
412 pos = list_entry(pos->member.next, typeof(*pos), member))
415 * list_for_each_entry_reverse - iterate backwards over list of given type.
416 * @pos: the type * to use as a loop cursor.
417 * @head: the head for your list.
418 * @member: the name of the list_struct within the struct.
420 #define list_for_each_entry_reverse(pos, head, member) \
421 for (pos = list_entry((head)->prev, typeof(*pos), member); \
422 prefetch(pos->member.prev), &pos->member != (head); \
423 pos = list_entry(pos->member.prev, typeof(*pos), member))
426 * list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue()
427 * @pos: the type * to use as a start point
428 * @head: the head of the list
429 * @member: the name of the list_struct within the struct.
431 * Prepares a pos entry for use as a start point in list_for_each_entry_continue().
433 #define list_prepare_entry(pos, head, member) \
434 ((pos) ? : list_entry(head, typeof(*pos), member))
437 * list_for_each_entry_continue - continue iteration over list of given type
438 * @pos: the type * to use as a loop cursor.
439 * @head: the head for your list.
440 * @member: the name of the list_struct within the struct.
442 * Continue to iterate over list of given type, continuing after
443 * the current position.
445 #define list_for_each_entry_continue(pos, head, member) \
446 for (pos = list_entry(pos->member.next, typeof(*pos), member); \
447 prefetch(pos->member.next), &pos->member != (head); \
448 pos = list_entry(pos->member.next, typeof(*pos), member))
451 * list_for_each_entry_continue_reverse - iterate backwards from the given point
452 * @pos: the type * to use as a loop cursor.
453 * @head: the head for your list.
454 * @member: the name of the list_struct within the struct.
456 * Start to iterate over list of given type backwards, continuing after
457 * the current position.
459 #define list_for_each_entry_continue_reverse(pos, head, member) \
460 for (pos = list_entry(pos->member.prev, typeof(*pos), member); \
461 prefetch(pos->member.prev), &pos->member != (head); \
462 pos = list_entry(pos->member.prev, typeof(*pos), member))
465 * list_for_each_entry_from - iterate over list of given type from the current point
466 * @pos: the type * to use as a loop cursor.
467 * @head: the head for your list.
468 * @member: the name of the list_struct within the struct.
470 * Iterate over list of given type, continuing from current position.
472 #define list_for_each_entry_from(pos, head, member) \
473 for (; prefetch(pos->member.next), &pos->member != (head); \
474 pos = list_entry(pos->member.next, typeof(*pos), member))
477 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
478 * @pos: the type * to use as a loop cursor.
479 * @n: another type * to use as temporary storage
480 * @head: the head for your list.
481 * @member: the name of the list_struct within the struct.
483 #define list_for_each_entry_safe(pos, n, head, member) \
484 for (pos = list_entry((head)->next, typeof(*pos), member), \
485 n = list_entry(pos->member.next, typeof(*pos), member); \
486 &pos->member != (head); \
487 pos = n, n = list_entry(n->member.next, typeof(*n), member))
490 * list_for_each_entry_safe_continue
491 * @pos: the type * to use as a loop cursor.
492 * @n: another type * to use as temporary storage
493 * @head: the head for your list.
494 * @member: the name of the list_struct within the struct.
496 * Iterate over list of given type, continuing after current point,
497 * safe against removal of list entry.
499 #define list_for_each_entry_safe_continue(pos, n, head, member) \
500 for (pos = list_entry(pos->member.next, typeof(*pos), member), \
501 n = list_entry(pos->member.next, typeof(*pos), member); \
502 &pos->member != (head); \
503 pos = n, n = list_entry(n->member.next, typeof(*n), member))
506 * list_for_each_entry_safe_from
507 * @pos: the type * to use as a loop cursor.
508 * @n: another type * to use as temporary storage
509 * @head: the head for your list.
510 * @member: the name of the list_struct within the struct.
512 * Iterate over list of given type from current point, safe against
513 * removal of list entry.
515 #define list_for_each_entry_safe_from(pos, n, head, member) \
516 for (n = list_entry(pos->member.next, typeof(*pos), member); \
517 &pos->member != (head); \
518 pos = n, n = list_entry(n->member.next, typeof(*n), member))
521 * list_for_each_entry_safe_reverse
522 * @pos: the type * to use as a loop cursor.
523 * @n: another type * to use as temporary storage
524 * @head: the head for your list.
525 * @member: the name of the list_struct within the struct.
527 * Iterate backwards over list of given type, safe against removal
530 #define list_for_each_entry_safe_reverse(pos, n, head, member) \
531 for (pos = list_entry((head)->prev, typeof(*pos), member), \
532 n = list_entry(pos->member.prev, typeof(*pos), member); \
533 &pos->member != (head); \
534 pos = n, n = list_entry(n->member.prev, typeof(*n), member))
537 * Double linked lists with a single pointer list head.
538 * Mostly useful for hash tables where the two pointer list head is
540 * You lose the ability to access the tail in O(1).
544 struct hlist_node *first;
548 struct hlist_node *next, **pprev;
551 #define HLIST_HEAD_INIT { .first = NULL }
552 #define HLIST_HEAD(name) struct hlist_head name = { .first = NULL }
553 #define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
554 static inline void INIT_HLIST_NODE(struct hlist_node *h)
560 static inline int hlist_unhashed(const struct hlist_node *h)
565 static inline int hlist_empty(const struct hlist_head *h)
570 static inline void __hlist_del(struct hlist_node *n)
572 struct hlist_node *next = n->next;
573 struct hlist_node **pprev = n->pprev;
579 static inline void hlist_del(struct hlist_node *n)
582 n->next = LIST_POISON1;
583 n->pprev = LIST_POISON2;
586 static inline void hlist_del_init(struct hlist_node *n)
588 if (!hlist_unhashed(n)) {
594 static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
596 struct hlist_node *first = h->first;
599 first->pprev = &n->next;
601 n->pprev = &h->first;
604 /* next must be != NULL */
605 static inline void hlist_add_before(struct hlist_node *n,
606 struct hlist_node *next)
608 n->pprev = next->pprev;
610 next->pprev = &n->next;
614 static inline void hlist_add_after(struct hlist_node *n,
615 struct hlist_node *next)
617 next->next = n->next;
619 next->pprev = &n->next;
622 next->next->pprev = &next->next;
625 #define hlist_entry(ptr, type, member) container_of(ptr,type,member)
627 #define hlist_for_each(pos, head) \
628 for (pos = (head)->first; pos && ({ prefetch(pos->next); 1; }); \
631 #define hlist_for_each_safe(pos, n, head) \
632 for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
636 * hlist_for_each_entry - iterate over list of given type
637 * @tpos: the type * to use as a loop cursor.
638 * @pos: the &struct hlist_node to use as a loop cursor.
639 * @head: the head for your list.
640 * @member: the name of the hlist_node within the struct.
642 #define hlist_for_each_entry(tpos, pos, head, member) \
643 for (pos = (head)->first; \
644 pos && ({ prefetch(pos->next); 1;}) && \
645 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
649 * hlist_for_each_entry_continue - iterate over a hlist continuing after current point
650 * @tpos: the type * to use as a loop cursor.
651 * @pos: the &struct hlist_node to use as a loop cursor.
652 * @member: the name of the hlist_node within the struct.
654 #define hlist_for_each_entry_continue(tpos, pos, member) \
655 for (pos = (pos)->next; \
656 pos && ({ prefetch(pos->next); 1;}) && \
657 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
661 * hlist_for_each_entry_from - iterate over a hlist continuing from current point
662 * @tpos: the type * to use as a loop cursor.
663 * @pos: the &struct hlist_node to use as a loop cursor.
664 * @member: the name of the hlist_node within the struct.
666 #define hlist_for_each_entry_from(tpos, pos, member) \
667 for (; pos && ({ prefetch(pos->next); 1;}) && \
668 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
672 * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
673 * @tpos: the type * to use as a loop cursor.
674 * @pos: the &struct hlist_node to use as a loop cursor.
675 * @n: another &struct hlist_node to use as temporary storage
676 * @head: the head for your list.
677 * @member: the name of the hlist_node within the struct.
679 #define hlist_for_each_entry_safe(tpos, pos, n, head, member) \
680 for (pos = (head)->first; \
681 pos && ({ n = pos->next; 1; }) && \
682 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \