c88a591d1c029c8c0db0f16048996d3af1cd40da
[platform/adaptation/renesas_rcar/renesas_kernel.git] / include / linux / list.h
1 #ifndef _LINUX_LIST_H
2 #define _LINUX_LIST_H
3
4 #include <linux/types.h>
5 #include <linux/stddef.h>
6 #include <linux/poison.h>
7 #include <linux/const.h>
8
9 /*
10  * Simple doubly linked list implementation.
11  *
12  * Some of the internal functions ("__xxx") are useful when
13  * manipulating whole lists rather than single entries, as
14  * sometimes we already know the next/prev entries and we can
15  * generate better code by using them directly rather than
16  * using the generic single-entry routines.
17  */
18
19 #define LIST_HEAD_INIT(name) { &(name), &(name) }
20
21 #define LIST_HEAD(name) \
22         struct list_head name = LIST_HEAD_INIT(name)
23
24 static inline void INIT_LIST_HEAD(struct list_head *list)
25 {
26         list->next = list;
27         list->prev = list;
28 }
29
30 /*
31  * Insert a new entry between two known consecutive entries.
32  *
33  * This is only for internal list manipulation where we know
34  * the prev/next entries already!
35  */
36 #ifndef CONFIG_DEBUG_LIST
37 static inline void __list_add(struct list_head *new,
38                               struct list_head *prev,
39                               struct list_head *next)
40 {
41         next->prev = new;
42         new->next = next;
43         new->prev = prev;
44         prev->next = new;
45 }
46 #else
47 extern void __list_add(struct list_head *new,
48                               struct list_head *prev,
49                               struct list_head *next);
50 #endif
51
52 /**
53  * list_add - add a new entry
54  * @new: new entry to be added
55  * @head: list head to add it after
56  *
57  * Insert a new entry after the specified head.
58  * This is good for implementing stacks.
59  */
60 static inline void list_add(struct list_head *new, struct list_head *head)
61 {
62         __list_add(new, head, head->next);
63 }
64
65
66 /**
67  * list_add_tail - add a new entry
68  * @new: new entry to be added
69  * @head: list head to add it before
70  *
71  * Insert a new entry before the specified head.
72  * This is useful for implementing queues.
73  */
74 static inline void list_add_tail(struct list_head *new, struct list_head *head)
75 {
76         __list_add(new, head->prev, head);
77 }
78
79 /*
80  * Delete a list entry by making the prev/next entries
81  * point to each other.
82  *
83  * This is only for internal list manipulation where we know
84  * the prev/next entries already!
85  */
86 static inline void __list_del(struct list_head * prev, struct list_head * next)
87 {
88         next->prev = prev;
89         prev->next = next;
90 }
91
92 /**
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.
97  */
98 #ifndef CONFIG_DEBUG_LIST
99 static inline void __list_del_entry(struct list_head *entry)
100 {
101         __list_del(entry->prev, entry->next);
102 }
103
104 static inline void list_del(struct list_head *entry)
105 {
106         __list_del(entry->prev, entry->next);
107         entry->next = LIST_POISON1;
108         entry->prev = LIST_POISON2;
109 }
110 #else
111 extern void __list_del_entry(struct list_head *entry);
112 extern void list_del(struct list_head *entry);
113 #endif
114
115 /**
116  * list_replace - replace old entry by new one
117  * @old : the element to be replaced
118  * @new : the new element to insert
119  *
120  * If @old was empty, it will be overwritten.
121  */
122 static inline void list_replace(struct list_head *old,
123                                 struct list_head *new)
124 {
125         new->next = old->next;
126         new->next->prev = new;
127         new->prev = old->prev;
128         new->prev->next = new;
129 }
130
131 static inline void list_replace_init(struct list_head *old,
132                                         struct list_head *new)
133 {
134         list_replace(old, new);
135         INIT_LIST_HEAD(old);
136 }
137
138 /**
139  * list_del_init - deletes entry from list and reinitialize it.
140  * @entry: the element to delete from the list.
141  */
142 static inline void list_del_init(struct list_head *entry)
143 {
144         __list_del_entry(entry);
145         INIT_LIST_HEAD(entry);
146 }
147
148 /**
149  * list_move - delete from one list and add as another's head
150  * @list: the entry to move
151  * @head: the head that will precede our entry
152  */
153 static inline void list_move(struct list_head *list, struct list_head *head)
154 {
155         __list_del_entry(list);
156         list_add(list, head);
157 }
158
159 /**
160  * list_move_tail - delete from one list and add as another's tail
161  * @list: the entry to move
162  * @head: the head that will follow our entry
163  */
164 static inline void list_move_tail(struct list_head *list,
165                                   struct list_head *head)
166 {
167         __list_del_entry(list);
168         list_add_tail(list, head);
169 }
170
171 /**
172  * list_is_last - tests whether @list is the last entry in list @head
173  * @list: the entry to test
174  * @head: the head of the list
175  */
176 static inline int list_is_last(const struct list_head *list,
177                                 const struct list_head *head)
178 {
179         return list->next == head;
180 }
181
182 /**
183  * list_empty - tests whether a list is empty
184  * @head: the list to test.
185  */
186 static inline int list_empty(const struct list_head *head)
187 {
188         return head->next == head;
189 }
190
191 /**
192  * list_empty_careful - tests whether a list is empty and not being modified
193  * @head: the list to test
194  *
195  * Description:
196  * tests whether a list is empty _and_ checks that no other CPU might be
197  * in the process of modifying either member (next or prev)
198  *
199  * NOTE: using list_empty_careful() without synchronization
200  * can only be safe if the only activity that can happen
201  * to the list entry is list_del_init(). Eg. it cannot be used
202  * if another CPU could re-list_add() it.
203  */
204 static inline int list_empty_careful(const struct list_head *head)
205 {
206         struct list_head *next = head->next;
207         return (next == head) && (next == head->prev);
208 }
209
210 /**
211  * list_rotate_left - rotate the list to the left
212  * @head: the head of the list
213  */
214 static inline void list_rotate_left(struct list_head *head)
215 {
216         struct list_head *first;
217
218         if (!list_empty(head)) {
219                 first = head->next;
220                 list_move_tail(first, head);
221         }
222 }
223
224 /**
225  * list_is_singular - tests whether a list has just one entry.
226  * @head: the list to test.
227  */
228 static inline int list_is_singular(const struct list_head *head)
229 {
230         return !list_empty(head) && (head->next == head->prev);
231 }
232
233 static inline void __list_cut_position(struct list_head *list,
234                 struct list_head *head, struct list_head *entry)
235 {
236         struct list_head *new_first = entry->next;
237         list->next = head->next;
238         list->next->prev = list;
239         list->prev = entry;
240         entry->next = list;
241         head->next = new_first;
242         new_first->prev = head;
243 }
244
245 /**
246  * list_cut_position - cut a list into two
247  * @list: a new list to add all removed entries
248  * @head: a list with entries
249  * @entry: an entry within head, could be the head itself
250  *      and if so we won't cut the list
251  *
252  * This helper moves the initial part of @head, up to and
253  * including @entry, from @head to @list. You should
254  * pass on @entry an element you know is on @head. @list
255  * should be an empty list or a list you do not care about
256  * losing its data.
257  *
258  */
259 static inline void list_cut_position(struct list_head *list,
260                 struct list_head *head, struct list_head *entry)
261 {
262         if (list_empty(head))
263                 return;
264         if (list_is_singular(head) &&
265                 (head->next != entry && head != entry))
266                 return;
267         if (entry == head)
268                 INIT_LIST_HEAD(list);
269         else
270                 __list_cut_position(list, head, entry);
271 }
272
273 static inline void __list_splice(const struct list_head *list,
274                                  struct list_head *prev,
275                                  struct list_head *next)
276 {
277         struct list_head *first = list->next;
278         struct list_head *last = list->prev;
279
280         first->prev = prev;
281         prev->next = first;
282
283         last->next = next;
284         next->prev = last;
285 }
286
287 /**
288  * list_splice - join two lists, this is designed for stacks
289  * @list: the new list to add.
290  * @head: the place to add it in the first list.
291  */
292 static inline void list_splice(const struct list_head *list,
293                                 struct list_head *head)
294 {
295         if (!list_empty(list))
296                 __list_splice(list, head, head->next);
297 }
298
299 /**
300  * list_splice_tail - join two lists, each list being a queue
301  * @list: the new list to add.
302  * @head: the place to add it in the first list.
303  */
304 static inline void list_splice_tail(struct list_head *list,
305                                 struct list_head *head)
306 {
307         if (!list_empty(list))
308                 __list_splice(list, head->prev, head);
309 }
310
311 /**
312  * list_splice_init - join two lists and reinitialise the emptied list.
313  * @list: the new list to add.
314  * @head: the place to add it in the first list.
315  *
316  * The list at @list is reinitialised
317  */
318 static inline void list_splice_init(struct list_head *list,
319                                     struct list_head *head)
320 {
321         if (!list_empty(list)) {
322                 __list_splice(list, head, head->next);
323                 INIT_LIST_HEAD(list);
324         }
325 }
326
327 /**
328  * list_splice_tail_init - join two lists and reinitialise the emptied list
329  * @list: the new list to add.
330  * @head: the place to add it in the first list.
331  *
332  * Each of the lists is a queue.
333  * The list at @list is reinitialised
334  */
335 static inline void list_splice_tail_init(struct list_head *list,
336                                          struct list_head *head)
337 {
338         if (!list_empty(list)) {
339                 __list_splice(list, head->prev, head);
340                 INIT_LIST_HEAD(list);
341         }
342 }
343
344 /**
345  * list_entry - get the struct for this entry
346  * @ptr:        the &struct list_head pointer.
347  * @type:       the type of the struct this is embedded in.
348  * @member:     the name of the list_struct within the struct.
349  */
350 #define list_entry(ptr, type, member) \
351         container_of(ptr, type, member)
352
353 /**
354  * list_first_entry - get the first element from a list
355  * @ptr:        the list head to take the element from.
356  * @type:       the type of the struct this is embedded in.
357  * @member:     the name of the list_struct within the struct.
358  *
359  * Note, that list is expected to be not empty.
360  */
361 #define list_first_entry(ptr, type, member) \
362         list_entry((ptr)->next, type, member)
363
364 /**
365  * list_first_entry_or_null - get the first element from a list
366  * @ptr:        the list head to take the element from.
367  * @type:       the type of the struct this is embedded in.
368  * @member:     the name of the list_struct within the struct.
369  *
370  * Note that if the list is empty, it returns NULL.
371  */
372 #define list_first_entry_or_null(ptr, type, member) \
373         (!list_empty(ptr) ? list_first_entry(ptr, type, member) : NULL)
374
375 /**
376  * list_next_entry - get the next element in list
377  * @pos:        the type * to cursor
378  * @member:     the name of the list_struct within the struct.
379  */
380 #define list_next_entry(pos, member) \
381         list_entry((pos)->member.next, typeof(*(pos)), member)
382
383 /**
384  * list_prev_entry - get the prev element in list
385  * @pos:        the type * to cursor
386  * @member:     the name of the list_struct within the struct.
387  */
388 #define list_prev_entry(pos, member) \
389         list_entry((pos)->member.prev, typeof(*(pos)), member)
390
391 /**
392  * list_for_each        -       iterate over a list
393  * @pos:        the &struct list_head to use as a loop cursor.
394  * @head:       the head for your list.
395  */
396 #define list_for_each(pos, head) \
397         for (pos = (head)->next; pos != (head); pos = pos->next)
398
399 /**
400  * list_for_each_prev   -       iterate over a list backwards
401  * @pos:        the &struct list_head to use as a loop cursor.
402  * @head:       the head for your list.
403  */
404 #define list_for_each_prev(pos, head) \
405         for (pos = (head)->prev; pos != (head); pos = pos->prev)
406
407 /**
408  * list_for_each_safe - iterate over a list safe against removal of list entry
409  * @pos:        the &struct list_head to use as a loop cursor.
410  * @n:          another &struct list_head to use as temporary storage
411  * @head:       the head for your list.
412  */
413 #define list_for_each_safe(pos, n, head) \
414         for (pos = (head)->next, n = pos->next; pos != (head); \
415                 pos = n, n = pos->next)
416
417 /**
418  * list_for_each_prev_safe - iterate over a list backwards safe against removal of list entry
419  * @pos:        the &struct list_head to use as a loop cursor.
420  * @n:          another &struct list_head to use as temporary storage
421  * @head:       the head for your list.
422  */
423 #define list_for_each_prev_safe(pos, n, head) \
424         for (pos = (head)->prev, n = pos->prev; \
425              pos != (head); \
426              pos = n, n = pos->prev)
427
428 /**
429  * list_for_each_entry  -       iterate over list of given type
430  * @pos:        the type * to use as a loop cursor.
431  * @head:       the head for your list.
432  * @member:     the name of the list_struct within the struct.
433  */
434 #define list_for_each_entry(pos, head, member)                          \
435         for (pos = list_entry((head)->next, typeof(*pos), member);      \
436              &pos->member != (head);                                    \
437              pos = list_next_entry(pos, member))
438
439 /**
440  * list_for_each_entry_reverse - iterate backwards over list of given type.
441  * @pos:        the type * to use as a loop cursor.
442  * @head:       the head for your list.
443  * @member:     the name of the list_struct within the struct.
444  */
445 #define list_for_each_entry_reverse(pos, head, member)                  \
446         for (pos = list_entry((head)->prev, typeof(*pos), member);      \
447              &pos->member != (head);                                    \
448              pos = list_prev_entry(pos, member))
449
450 /**
451  * list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue()
452  * @pos:        the type * to use as a start point
453  * @head:       the head of the list
454  * @member:     the name of the list_struct within the struct.
455  *
456  * Prepares a pos entry for use as a start point in list_for_each_entry_continue().
457  */
458 #define list_prepare_entry(pos, head, member) \
459         ((pos) ? : list_entry(head, typeof(*pos), member))
460
461 /**
462  * list_for_each_entry_continue - continue iteration over list of given type
463  * @pos:        the type * to use as a loop cursor.
464  * @head:       the head for your list.
465  * @member:     the name of the list_struct within the struct.
466  *
467  * Continue to iterate over list of given type, continuing after
468  * the current position.
469  */
470 #define list_for_each_entry_continue(pos, head, member)                 \
471         for (pos = list_next_entry(pos, member);                        \
472              &pos->member != (head);                                    \
473              pos = list_next_entry(pos, member))
474
475 /**
476  * list_for_each_entry_continue_reverse - iterate backwards from the given point
477  * @pos:        the type * to use as a loop cursor.
478  * @head:       the head for your list.
479  * @member:     the name of the list_struct within the struct.
480  *
481  * Start to iterate over list of given type backwards, continuing after
482  * the current position.
483  */
484 #define list_for_each_entry_continue_reverse(pos, head, member)         \
485         for (pos = list_prev_entry(pos, member);                        \
486              &pos->member != (head);                                    \
487              pos = list_prev_entry(pos, member))
488
489 /**
490  * list_for_each_entry_from - iterate over list of given type from the current point
491  * @pos:        the type * to use as a loop cursor.
492  * @head:       the head for your list.
493  * @member:     the name of the list_struct within the struct.
494  *
495  * Iterate over list of given type, continuing from current position.
496  */
497 #define list_for_each_entry_from(pos, head, member)                     \
498         for (; &pos->member != (head);                                  \
499              pos = list_next_entry(pos, member))
500
501 /**
502  * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
503  * @pos:        the type * to use as a loop cursor.
504  * @n:          another type * to use as temporary storage
505  * @head:       the head for your list.
506  * @member:     the name of the list_struct within the struct.
507  */
508 #define list_for_each_entry_safe(pos, n, head, member)                  \
509         for (pos = list_entry((head)->next, typeof(*pos), member),      \
510                 n = list_next_entry(pos, member);                       \
511              &pos->member != (head);                                    \
512              pos = n, n = list_next_entry(n, member))
513
514 /**
515  * list_for_each_entry_safe_continue - continue list iteration safe against removal
516  * @pos:        the type * to use as a loop cursor.
517  * @n:          another type * to use as temporary storage
518  * @head:       the head for your list.
519  * @member:     the name of the list_struct within the struct.
520  *
521  * Iterate over list of given type, continuing after current point,
522  * safe against removal of list entry.
523  */
524 #define list_for_each_entry_safe_continue(pos, n, head, member)                 \
525         for (pos = list_next_entry(pos, member),                                \
526                 n = list_next_entry(pos, member);                               \
527              &pos->member != (head);                                            \
528              pos = n, n = list_next_entry(n, member))
529
530 /**
531  * list_for_each_entry_safe_from - iterate over list from current point safe against removal
532  * @pos:        the type * to use as a loop cursor.
533  * @n:          another type * to use as temporary storage
534  * @head:       the head for your list.
535  * @member:     the name of the list_struct within the struct.
536  *
537  * Iterate over list of given type from current point, safe against
538  * removal of list entry.
539  */
540 #define list_for_each_entry_safe_from(pos, n, head, member)                     \
541         for (n = list_next_entry(pos, member);                                  \
542              &pos->member != (head);                                            \
543              pos = n, n = list_next_entry(n, member))
544
545 /**
546  * list_for_each_entry_safe_reverse - iterate backwards over list safe against removal
547  * @pos:        the type * to use as a loop cursor.
548  * @n:          another type * to use as temporary storage
549  * @head:       the head for your list.
550  * @member:     the name of the list_struct within the struct.
551  *
552  * Iterate backwards over list of given type, safe against removal
553  * of list entry.
554  */
555 #define list_for_each_entry_safe_reverse(pos, n, head, member)          \
556         for (pos = list_entry((head)->prev, typeof(*pos), member),      \
557                 n = list_prev_entry(pos, member);                       \
558              &pos->member != (head);                                    \
559              pos = n, n = list_prev_entry(n, member))
560
561 /**
562  * list_safe_reset_next - reset a stale list_for_each_entry_safe loop
563  * @pos:        the loop cursor used in the list_for_each_entry_safe loop
564  * @n:          temporary storage used in list_for_each_entry_safe
565  * @member:     the name of the list_struct within the struct.
566  *
567  * list_safe_reset_next is not safe to use in general if the list may be
568  * modified concurrently (eg. the lock is dropped in the loop body). An
569  * exception to this is if the cursor element (pos) is pinned in the list,
570  * and list_safe_reset_next is called after re-taking the lock and before
571  * completing the current iteration of the loop body.
572  */
573 #define list_safe_reset_next(pos, n, member)                            \
574         n = list_next_entry(pos, member)
575
576 /*
577  * Double linked lists with a single pointer list head.
578  * Mostly useful for hash tables where the two pointer list head is
579  * too wasteful.
580  * You lose the ability to access the tail in O(1).
581  */
582
583 #define HLIST_HEAD_INIT { .first = NULL }
584 #define HLIST_HEAD(name) struct hlist_head name = {  .first = NULL }
585 #define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
586 static inline void INIT_HLIST_NODE(struct hlist_node *h)
587 {
588         h->next = NULL;
589         h->pprev = NULL;
590 }
591
592 static inline int hlist_unhashed(const struct hlist_node *h)
593 {
594         return !h->pprev;
595 }
596
597 static inline int hlist_empty(const struct hlist_head *h)
598 {
599         return !h->first;
600 }
601
602 static inline void __hlist_del(struct hlist_node *n)
603 {
604         struct hlist_node *next = n->next;
605         struct hlist_node **pprev = n->pprev;
606         *pprev = next;
607         if (next)
608                 next->pprev = pprev;
609 }
610
611 static inline void hlist_del(struct hlist_node *n)
612 {
613         __hlist_del(n);
614         n->next = LIST_POISON1;
615         n->pprev = LIST_POISON2;
616 }
617
618 static inline void hlist_del_init(struct hlist_node *n)
619 {
620         if (!hlist_unhashed(n)) {
621                 __hlist_del(n);
622                 INIT_HLIST_NODE(n);
623         }
624 }
625
626 static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
627 {
628         struct hlist_node *first = h->first;
629         n->next = first;
630         if (first)
631                 first->pprev = &n->next;
632         h->first = n;
633         n->pprev = &h->first;
634 }
635
636 /* next must be != NULL */
637 static inline void hlist_add_before(struct hlist_node *n,
638                                         struct hlist_node *next)
639 {
640         n->pprev = next->pprev;
641         n->next = next;
642         next->pprev = &n->next;
643         *(n->pprev) = n;
644 }
645
646 static inline void hlist_add_after(struct hlist_node *n,
647                                         struct hlist_node *next)
648 {
649         next->next = n->next;
650         n->next = next;
651         next->pprev = &n->next;
652
653         if(next->next)
654                 next->next->pprev  = &next->next;
655 }
656
657 /* after that we'll appear to be on some hlist and hlist_del will work */
658 static inline void hlist_add_fake(struct hlist_node *n)
659 {
660         n->pprev = &n->next;
661 }
662
663 /*
664  * Move a list from one list head to another. Fixup the pprev
665  * reference of the first entry if it exists.
666  */
667 static inline void hlist_move_list(struct hlist_head *old,
668                                    struct hlist_head *new)
669 {
670         new->first = old->first;
671         if (new->first)
672                 new->first->pprev = &new->first;
673         old->first = NULL;
674 }
675
676 #define hlist_entry(ptr, type, member) container_of(ptr,type,member)
677
678 #define hlist_for_each(pos, head) \
679         for (pos = (head)->first; pos ; pos = pos->next)
680
681 #define hlist_for_each_safe(pos, n, head) \
682         for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
683              pos = n)
684
685 #define hlist_entry_safe(ptr, type, member) \
686         ({ typeof(ptr) ____ptr = (ptr); \
687            ____ptr ? hlist_entry(____ptr, type, member) : NULL; \
688         })
689
690 /**
691  * hlist_for_each_entry - iterate over list of given type
692  * @pos:        the type * to use as a loop cursor.
693  * @head:       the head for your list.
694  * @member:     the name of the hlist_node within the struct.
695  */
696 #define hlist_for_each_entry(pos, head, member)                         \
697         for (pos = hlist_entry_safe((head)->first, typeof(*(pos)), member);\
698              pos;                                                       \
699              pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
700
701 /**
702  * hlist_for_each_entry_continue - iterate over a hlist continuing after current point
703  * @pos:        the type * to use as a loop cursor.
704  * @member:     the name of the hlist_node within the struct.
705  */
706 #define hlist_for_each_entry_continue(pos, member)                      \
707         for (pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member);\
708              pos;                                                       \
709              pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
710
711 /**
712  * hlist_for_each_entry_from - iterate over a hlist continuing from current point
713  * @pos:        the type * to use as a loop cursor.
714  * @member:     the name of the hlist_node within the struct.
715  */
716 #define hlist_for_each_entry_from(pos, member)                          \
717         for (; pos;                                                     \
718              pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
719
720 /**
721  * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
722  * @pos:        the type * to use as a loop cursor.
723  * @n:          another &struct hlist_node to use as temporary storage
724  * @head:       the head for your list.
725  * @member:     the name of the hlist_node within the struct.
726  */
727 #define hlist_for_each_entry_safe(pos, n, head, member)                 \
728         for (pos = hlist_entry_safe((head)->first, typeof(*pos), member);\
729              pos && ({ n = pos->member.next; 1; });                     \
730              pos = hlist_entry_safe(n, typeof(*pos), member))
731
732 #endif