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