1 #ifndef _AW_FREERTOS_LIST_H
2 #define _AW_FREERTOS_LIST_H
3 
4 #include <aw_common.h>
5 
6 /*
7  * Simple doubly linked list implementation.
8  *
9  * Some of the internal functions ("__xxx") are useful when
10  * manipulating whole lists rather than single entries, as
11  * sometimes we already know the next/prev entries and we can
12  * generate better code by using them directly rather than
13  * using the generic single-entry routines.
14  */
15 
16 struct list_head {
17         struct list_head *next, *prev;
18 };
19 
20 
21 #define LIST_POISON1  ((void *) 0x00100100)
22 #define LIST_POISON2  ((void *) 0x00200)
23 
24 #define LIST_HEAD_INIT(name) { &(name), &(name) }
25 
26 #define LIST_HEAD(name) \
27     struct list_head name = LIST_HEAD_INIT(name)
28 
INIT_LIST_HEAD(struct list_head * list)29 static inline void INIT_LIST_HEAD(struct list_head *list)
30 {
31     list->next = list;
32     list->prev = list;
33 }
34 
35 /*
36  * Insert a new entry between two known consecutive entries.
37  *
38  * This is only for internal list manipulation where we know
39  * the prev/next entries already!
40  */
41 #ifndef CONFIG_DEBUG_LIST
__list_add(struct list_head * new,struct list_head * prev,struct list_head * next)42 static inline void __list_add(struct list_head *new,
43                   struct list_head *prev,
44                   struct list_head *next)
45 {
46     next->prev = new;
47     new->next = next;
48     new->prev = prev;
49     prev->next = new;
50 }
51 #else
52 extern void __list_add(struct list_head *new,
53                   struct list_head *prev,
54                   struct list_head *next);
55 #endif
56 
57 /**
58  * list_add - add a new entry
59  * @new: new entry to be added
60  * @head: list head to add it after
61  *
62  * Insert a new entry after the specified head.
63  * This is good for implementing stacks.
64  */
list_add(struct list_head * new,struct list_head * head)65 static inline void list_add(struct list_head *new, struct list_head *head)
66 {
67     __list_add(new, head, head->next);
68 }
69 
70 
71 /**
72  * list_add_tail - add a new entry
73  * @new: new entry to be added
74  * @head: list head to add it before
75  *
76  * Insert a new entry before the specified head.
77  * This is useful for implementing queues.
78  */
list_add_tail(struct list_head * new,struct list_head * head)79 static inline void list_add_tail(struct list_head *new, struct list_head *head)
80 {
81     __list_add(new, head->prev, head);
82 }
83 
84 /*
85  * Delete a list entry by making the prev/next entries
86  * point to each other.
87  *
88  * This is only for internal list manipulation where we know
89  * the prev/next entries already!
90  */
__list_del(struct list_head * prev,struct list_head * next)91 static inline void __list_del(struct list_head * prev, struct list_head * next)
92 {
93     next->prev = prev;
94     prev->next = next;
95 }
96 
97 /**
98  * list_del - deletes entry from list.
99  * @entry: the element to delete from the list.
100  * Note: list_empty() on entry does not return true after this, the entry is
101  * in an undefined state.
102  */
103 #ifndef CONFIG_DEBUG_LIST
__list_del_entry(struct list_head * entry)104 static inline void __list_del_entry(struct list_head *entry)
105 {
106     __list_del(entry->prev, entry->next);
107 }
108 
list_del(struct list_head * entry)109 static inline void list_del(struct list_head *entry)
110 {
111     __list_del(entry->prev, entry->next);
112     entry->next = LIST_POISON1;
113     entry->prev = LIST_POISON2;
114 }
115 #else
116 extern void __list_del_entry(struct list_head *entry);
117 extern void list_del(struct list_head *entry);
118 #endif
119 
120 /**
121  * list_replace - replace old entry by new one
122  * @old : the element to be replaced
123  * @new : the new element to insert
124  *
125  * If @old was empty, it will be overwritten.
126  */
list_replace(struct list_head * old,struct list_head * new)127 static inline void list_replace(struct list_head *old,
128                 struct list_head *new)
129 {
130     new->next = old->next;
131     new->next->prev = new;
132     new->prev = old->prev;
133     new->prev->next = new;
134 }
135 
list_replace_init(struct list_head * old,struct list_head * new)136 static inline void list_replace_init(struct list_head *old,
137                     struct list_head *new)
138 {
139     list_replace(old, new);
140     INIT_LIST_HEAD(old);
141 }
142 
143 /**
144  * list_del_init - deletes entry from list and reinitialize it.
145  * @entry: the element to delete from the list.
146  */
list_del_init(struct list_head * entry)147 static inline void list_del_init(struct list_head *entry)
148 {
149     __list_del_entry(entry);
150     INIT_LIST_HEAD(entry);
151 }
152 
153 /**
154  * list_move - delete from one list and add as another's head
155  * @list: the entry to move
156  * @head: the head that will precede our entry
157  */
list_move(struct list_head * list,struct list_head * head)158 static inline void list_move(struct list_head *list, struct list_head *head)
159 {
160     __list_del_entry(list);
161     list_add(list, head);
162 }
163 
164 /**
165  * list_move_tail - delete from one list and add as another's tail
166  * @list: the entry to move
167  * @head: the head that will follow our entry
168  */
list_move_tail(struct list_head * list,struct list_head * head)169 static inline void list_move_tail(struct list_head *list,
170                   struct list_head *head)
171 {
172     __list_del_entry(list);
173     list_add_tail(list, head);
174 }
175 
176 /**
177  * list_is_last - tests whether @list is the last entry in list @head
178  * @list: the entry to test
179  * @head: the head of the list
180  */
list_is_last(const struct list_head * list,const struct list_head * head)181 static inline int list_is_last(const struct list_head *list,
182                 const struct list_head *head)
183 {
184     return list->next == head;
185 }
186 
187 /**
188  * list_empty - tests whether a list is empty
189  * @head: the list to test.
190  */
list_empty(const struct list_head * head)191 static inline int list_empty(const struct list_head *head)
192 {
193     return head->next == head;
194 }
195 
196 /**
197  * list_empty_careful - tests whether a list is empty and not being modified
198  * @head: the list to test
199  *
200  * Description:
201  * tests whether a list is empty _and_ checks that no other CPU might be
202  * in the process of modifying either member (next or prev)
203  *
204  * NOTE: using list_empty_careful() without synchronization
205  * can only be safe if the only activity that can happen
206  * to the list entry is list_del_init(). Eg. it cannot be used
207  * if another CPU could re-list_add() it.
208  */
list_empty_careful(const struct list_head * head)209 static inline int list_empty_careful(const struct list_head *head)
210 {
211     struct list_head *next = head->next;
212     return (next == head) && (next == head->prev);
213 }
214 
215 /**
216  * list_rotate_left - rotate the list to the left
217  * @head: the head of the list
218  */
list_rotate_left(struct list_head * head)219 static inline void list_rotate_left(struct list_head *head)
220 {
221     struct list_head *first;
222 
223     if (!list_empty(head)) {
224         first = head->next;
225         list_move_tail(first, head);
226     }
227 }
228 
229 /**
230  * list_is_singular - tests whether a list has just one entry.
231  * @head: the list to test.
232  */
list_is_singular(const struct list_head * head)233 static inline int list_is_singular(const struct list_head *head)
234 {
235     return !list_empty(head) && (head->next == head->prev);
236 }
237 
__list_cut_position(struct list_head * list,struct list_head * head,struct list_head * entry)238 static inline void __list_cut_position(struct list_head *list,
239         struct list_head *head, struct list_head *entry)
240 {
241     struct list_head *new_first = entry->next;
242     list->next = head->next;
243     list->next->prev = list;
244     list->prev = entry;
245     entry->next = list;
246     head->next = new_first;
247     new_first->prev = head;
248 }
249 
250 /**
251  * list_cut_position - cut a list into two
252  * @list: a new list to add all removed entries
253  * @head: a list with entries
254  * @entry: an entry within head, could be the head itself
255  *  and if so we won't cut the list
256  *
257  * This helper moves the initial part of @head, up to and
258  * including @entry, from @head to @list. You should
259  * pass on @entry an element you know is on @head. @list
260  * should be an empty list or a list you do not care about
261  * losing its data.
262  *
263  */
list_cut_position(struct list_head * list,struct list_head * head,struct list_head * entry)264 static inline void list_cut_position(struct list_head *list,
265         struct list_head *head, struct list_head *entry)
266 {
267     if (list_empty(head))
268         return;
269     if (list_is_singular(head) &&
270         (head->next != entry && head != entry))
271         return;
272     if (entry == head)
273         INIT_LIST_HEAD(list);
274     else
275         __list_cut_position(list, head, entry);
276 }
277 
__list_splice(const struct list_head * list,struct list_head * prev,struct list_head * next)278 static inline void __list_splice(const struct list_head *list,
279                  struct list_head *prev,
280                  struct list_head *next)
281 {
282     struct list_head *first = list->next;
283     struct list_head *last = list->prev;
284 
285     first->prev = prev;
286     prev->next = first;
287 
288     last->next = next;
289     next->prev = last;
290 }
291 
292 /**
293  * list_splice - join two lists, this is designed for stacks
294  * @list: the new list to add.
295  * @head: the place to add it in the first list.
296  */
list_splice(const struct list_head * list,struct list_head * head)297 static inline void list_splice(const struct list_head *list,
298                 struct list_head *head)
299 {
300     if (!list_empty(list))
301         __list_splice(list, head, head->next);
302 }
303 
304 /**
305  * list_splice_tail - join two lists, each list being a queue
306  * @list: the new list to add.
307  * @head: the place to add it in the first list.
308  */
list_splice_tail(struct list_head * list,struct list_head * head)309 static inline void list_splice_tail(struct list_head *list,
310                 struct list_head *head)
311 {
312     if (!list_empty(list))
313         __list_splice(list, head->prev, head);
314 }
315 
316 /**
317  * list_splice_init - join two lists and reinitialise the emptied list.
318  * @list: the new list to add.
319  * @head: the place to add it in the first list.
320  *
321  * The list at @list is reinitialised
322  */
list_splice_init(struct list_head * list,struct list_head * head)323 static inline void list_splice_init(struct list_head *list,
324                     struct list_head *head)
325 {
326     if (!list_empty(list)) {
327         __list_splice(list, head, head->next);
328         INIT_LIST_HEAD(list);
329     }
330 }
331 
332 /**
333  * list_splice_tail_init - join two lists and reinitialise the emptied list
334  * @list: the new list to add.
335  * @head: the place to add it in the first list.
336  *
337  * Each of the lists is a queue.
338  * The list at @list is reinitialised
339  */
list_splice_tail_init(struct list_head * list,struct list_head * head)340 static inline void list_splice_tail_init(struct list_head *list,
341                      struct list_head *head)
342 {
343     if (!list_empty(list)) {
344         __list_splice(list, head->prev, head);
345         INIT_LIST_HEAD(list);
346     }
347 }
348 
349 /**
350  * list_entry - get the struct for this entry
351  * @ptr:    the &struct list_head pointer.
352  * @type:   the type of the struct this is embedded in.
353  * @member: the name of the list_struct within the struct.
354  */
355 #define list_entry(ptr, type, member) \
356     container_of(ptr, type, member)
357 
358 /**
359  * list_first_entry - get the first element from a list
360  * @ptr:    the list head to take the element from.
361  * @type:   the type of the struct this is embedded in.
362  * @member: the name of the list_struct within the struct.
363  *
364  * Note, that list is expected to be not empty.
365  */
366 #define list_first_entry(ptr, type, member) \
367     list_entry((ptr)->next, type, member)
368 
369 /**
370  * list_for_each    -   iterate over a list
371  * @pos:    the &struct list_head to use as a loop cursor.
372  * @head:   the head for your list.
373  */
374 #define list_for_each(pos, head) \
375     for (pos = (head)->next; pos != (head); pos = pos->next)
376 
377 /**
378  * __list_for_each  -   iterate over a list
379  * @pos:    the &struct list_head to use as a loop cursor.
380  * @head:   the head for your list.
381  *
382  * This variant doesn't differ from list_for_each() any more.
383  * We don't do prefetching in either case.
384  */
385 #define __list_for_each(pos, head) \
386     for (pos = (head)->next; pos != (head); pos = pos->next)
387 
388 /**
389  * list_for_each_prev   -   iterate over a list backwards
390  * @pos:    the &struct list_head to use as a loop cursor.
391  * @head:   the head for your list.
392  */
393 #define list_for_each_prev(pos, head) \
394     for (pos = (head)->prev; pos != (head); pos = pos->prev)
395 
396 /**
397  * list_for_each_safe - iterate over a list safe against removal of list entry
398  * @pos:    the &struct list_head to use as a loop cursor.
399  * @n:      another &struct list_head to use as temporary storage
400  * @head:   the head for your list.
401  */
402 #define list_for_each_safe(pos, n, head) \
403     for (pos = (head)->next, n = pos->next; pos != (head); \
404         pos = n, n = pos->next)
405 
406 /**
407  * list_for_each_prev_safe - iterate over a list backwards safe against removal of list entry
408  * @pos:    the &struct list_head to use as a loop cursor.
409  * @n:      another &struct list_head to use as temporary storage
410  * @head:   the head for your list.
411  */
412 #define list_for_each_prev_safe(pos, n, head) \
413     for (pos = (head)->prev, n = pos->prev; \
414          pos != (head); \
415          pos = n, n = pos->prev)
416 
417 /**
418  * list_for_each_entry  -   iterate over list of given type
419  * @pos:    the type * to use as a loop cursor.
420  * @head:   the head for your list.
421  * @member: the name of the list_struct within the struct.
422  */
423 #define list_for_each_entry(pos, head, member)              \
424     for (pos = list_entry((head)->next, typeof(*pos), member);  \
425          &pos->member != (head);    \
426          pos = list_entry(pos->member.next, typeof(*pos), member))
427 
428 /**
429  * list_for_each_entry_reverse - iterate backwards 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_reverse(pos, head, member)          \
435     for (pos = list_entry((head)->prev, typeof(*pos), member);  \
436          &pos->member != (head);    \
437          pos = list_entry(pos->member.prev, typeof(*pos), member))
438 
439 /**
440  * list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue()
441  * @pos:    the type * to use as a start point
442  * @head:   the head of the list
443  * @member: the name of the list_struct within the struct.
444  *
445  * Prepares a pos entry for use as a start point in list_for_each_entry_continue().
446  */
447 #define list_prepare_entry(pos, head, member) \
448     ((pos) ? : list_entry(head, typeof(*pos), member))
449 
450 /**
451  * list_for_each_entry_continue - continue iteration 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  * Continue to iterate over list of given type, continuing after
457  * the current position.
458  */
459 #define list_for_each_entry_continue(pos, head, member)         \
460     for (pos = list_entry(pos->member.next, typeof(*pos), member);  \
461          &pos->member != (head);    \
462          pos = list_entry(pos->member.next, typeof(*pos), member))
463 
464 /**
465  * list_for_each_entry_continue_reverse - iterate backwards from the given 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.
469  *
470  * Start to iterate over list of given type backwards, continuing after
471  * the current position.
472  */
473 #define list_for_each_entry_continue_reverse(pos, head, member)     \
474     for (pos = list_entry(pos->member.prev, typeof(*pos), member);  \
475          &pos->member != (head);    \
476          pos = list_entry(pos->member.prev, typeof(*pos), member))
477 
478 /**
479  * list_for_each_entry_from - iterate over list of given type from the current point
480  * @pos:    the type * to use as a loop cursor.
481  * @head:   the head for your list.
482  * @member: the name of the list_struct within the struct.
483  *
484  * Iterate over list of given type, continuing from current position.
485  */
486 #define list_for_each_entry_from(pos, head, member)         \
487     for (; &pos->member != (head);  \
488          pos = list_entry(pos->member.next, typeof(*pos), member))
489 
490 /**
491  * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
492  * @pos:    the type * to use as a loop cursor.
493  * @n:      another type * to use as temporary storage
494  * @head:   the head for your list.
495  * @member: the name of the list_struct within the struct.
496  */
497 #define list_for_each_entry_safe(pos, n, head, member)          \
498     for (pos = list_entry((head)->next, typeof(*pos), member),  \
499         n = list_entry(pos->member.next, typeof(*pos), member); \
500          &pos->member != (head);                    \
501          pos = n, n = list_entry(n->member.next, typeof(*n), member))
502 
503 /**
504  * list_for_each_entry_safe_continue - continue list iteration safe against removal
505  * @pos:    the type * to use as a loop cursor.
506  * @n:      another type * to use as temporary storage
507  * @head:   the head for your list.
508  * @member: the name of the list_struct within the struct.
509  *
510  * Iterate over list of given type, continuing after current point,
511  * safe against removal of list entry.
512  */
513 #define list_for_each_entry_safe_continue(pos, n, head, member)         \
514     for (pos = list_entry(pos->member.next, typeof(*pos), member),      \
515         n = list_entry(pos->member.next, typeof(*pos), member);     \
516          &pos->member != (head);                        \
517          pos = n, n = list_entry(n->member.next, typeof(*n), member))
518 
519 /**
520  * list_for_each_entry_safe_from - iterate over list from current point safe against removal
521  * @pos:    the type * to use as a loop cursor.
522  * @n:      another type * to use as temporary storage
523  * @head:   the head for your list.
524  * @member: the name of the list_struct within the struct.
525  *
526  * Iterate over list of given type from current point, safe against
527  * removal of list entry.
528  */
529 #define list_for_each_entry_safe_from(pos, n, head, member)         \
530     for (n = list_entry(pos->member.next, typeof(*pos), member);        \
531          &pos->member != (head);                        \
532          pos = n, n = list_entry(n->member.next, typeof(*n), member))
533 
534 /**
535  * list_for_each_entry_safe_reverse - iterate backwards over list safe against removal
536  * @pos:    the type * to use as a loop cursor.
537  * @n:      another type * to use as temporary storage
538  * @head:   the head for your list.
539  * @member: the name of the list_struct within the struct.
540  *
541  * Iterate backwards over list of given type, safe against removal
542  * of list entry.
543  */
544 #define list_for_each_entry_safe_reverse(pos, n, head, member)      \
545     for (pos = list_entry((head)->prev, typeof(*pos), member),  \
546         n = list_entry(pos->member.prev, typeof(*pos), member); \
547          &pos->member != (head);                    \
548          pos = n, n = list_entry(n->member.prev, typeof(*n), member))
549 
550 /**
551  * list_safe_reset_next - reset a stale list_for_each_entry_safe loop
552  * @pos:    the loop cursor used in the list_for_each_entry_safe loop
553  * @n:      temporary storage used in list_for_each_entry_safe
554  * @member: the name of the list_struct within the struct.
555  *
556  * list_safe_reset_next is not safe to use in general if the list may be
557  * modified concurrently (eg. the lock is dropped in the loop body). An
558  * exception to this is if the cursor element (pos) is pinned in the list,
559  * and list_safe_reset_next is called after re-taking the lock and before
560  * completing the current iteration of the loop body.
561  */
562 #define list_safe_reset_next(pos, n, member)                \
563     n = list_entry(pos->member.next, typeof(*pos), member)
564 
565 
566 
567 #endif
568