• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Chipsea Technologies (Shenzhen) Corp., Ltd. All rights reserved.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #ifndef _LNX_LIST_H_
16 #define _LNX_LIST_H_
17 
18 /**
19  * *** Porting from linux kernel. ***
20  * NOTES:
21  * 1. armcc do not support typeof to retrieve the data type, direct use type.
22  * 2. prefetch is not support in current platform, remove.
23  *
24  * Simple doubly linked list implementation.
25  *
26  * Some of the internal functions ("__xxx") are useful when
27  * manipulating whole lists rather than single entries, as
28  * sometimes we already know the next/prev entries and we can
29  * generate better code by using them directly rather than
30  * using the generic single-entry routines.
31  */
32 
33 /*****************************************************************************/
34 /* File Includes                                                             */
35 /*****************************************************************************/
36 
37 #include "cmsis_compiler.h"
38 
39 /*****************************************************************************/
40 /* Porting Modification                                                      */
41 /*****************************************************************************/
42 
43 #ifndef size_t
44 typedef unsigned int        size_t;
45 #endif /* size_t */
46 
47 #ifndef offsetof
48 /**
49  * expands to an integral constant expression that has type size_t, the
50  * value of which is the offset in bytes, from the beginning of a structure
51  * designated by type, of the member designated by the identifier (if the
52  * specified member is a bit-field, the behaviour is undefined).
53  */
54 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
55 #endif
56 
57 #ifndef container_of
58 /**
59  * container_of - cast a member of a structure out to the containing structure
60  * \param    ptr:    the pointer to the member.
61  * \param    type:   the type of the container struct this is embedded in.
62  * \param    member: the name of the member within the struct.
63  *
64  */
65 #define container_of(ptr, type, member) \
66     (type *)((char *)ptr - offsetof(type,member))
67 #endif
68 
69 /*****************************************************************************/
70 /* Constants Definitions                                                     */
71 /*****************************************************************************/
72 
73 /*
74  * These are non-NULL pointers that will result in page faults
75  * under normal circumstances, used to verify that nobody uses
76  * non-initialized list entries.
77  */
78 //#define LIST_POISON1    ((void *) 0x00100200)
79 //#define LIST_POISON2    ((void *) 0x00200200)
80 #define LIST_POISON1    NULL
81 #define LIST_POISON2    NULL
82 
83 struct list_head
84 {
85     struct list_head *next, *prev;
86 };
87 
88 #define LIST_HEAD_INIT(name) { &(name), &(name) }
89 
90 #define LIST_HEAD(name) \
91     struct list_head name = LIST_HEAD_INIT(name)
92 
INIT_LIST_HEAD(struct list_head * list)93 __STATIC_INLINE void INIT_LIST_HEAD(struct list_head *list)
94 {
95     list->next = list;
96     list->prev = list;
97 }
98 
99 /*
100  * Insert a new entry between two known consecutive entries.
101  *
102  * This is only for internal list manipulation where we know
103  * the prev/next entries already!
104  */
__list_add(struct list_head * new,struct list_head * prev,struct list_head * next)105 __STATIC_INLINE void __list_add(struct list_head *new,
106     struct list_head *prev, struct list_head *next)
107 {
108     next->prev = new;
109     new->next = next;
110     new->prev = prev;
111     prev->next = new;
112 }
113 
114 /**
115  * list_add - add a new entry
116  * \param    new: new entry to be added
117  * \param    head: list head to add it after
118  *
119  * Insert a new entry after the specified head.
120  * This is good for implementing stacks.
121  */
list_add(struct list_head * new,struct list_head * head)122 __STATIC_INLINE void list_add(struct list_head *new, struct list_head *head)
123 {
124     __list_add(new, head, head->next);
125 }
126 
127 /**
128  * list_add_tail - add a new entry
129  * \param    new: new entry to be added
130  * \param    head: list head to add it before
131  *
132  * Insert a new entry before the specified head.
133  * This is useful for implementing queues.
134  */
list_add_tail(struct list_head * new,struct list_head * head)135 __STATIC_INLINE void list_add_tail(struct list_head *new, struct list_head *head)
136 {
137     __list_add(new, head->prev, head);
138 }
139 
140 /*
141  * Delete a list entry by making the prev/next entries
142  * point to each other.
143  *
144  * This is only for internal list manipulation where we know
145  * the prev/next entries already!
146  */
__list_del(struct list_head * prev,struct list_head * next)147 __STATIC_INLINE void __list_del(struct list_head * prev, struct list_head * next)
148 {
149     next->prev = prev;
150     prev->next = next;
151 }
152 
153 /**
154  * list_del - deletes entry from list.
155  * \param    entry: the element to delete from the list.
156  * Note: list_empty() on entry does not return true after this, the entry is
157  * in an undefined state.
158  */
__list_del_entry(struct list_head * entry)159 __STATIC_INLINE void __list_del_entry(struct list_head *entry)
160 {
161     __list_del(entry->prev, entry->next);
162 }
163 
list_del(struct list_head * entry)164 __STATIC_INLINE void list_del(struct list_head *entry)
165 {
166     __list_del(entry->prev, entry->next);
167     entry->next = LIST_POISON1;
168     entry->prev = LIST_POISON2;
169 }
170 
171 /**
172  * list_replace - replace old entry by new one
173  * \param    old : the element to be replaced
174  * \param    new : the new element to insert
175  *
176  * If old was empty, it will be overwritten.
177  */
list_replace(struct list_head * old,struct list_head * new)178 __STATIC_INLINE void list_replace(struct list_head *old,
179                              struct list_head *new)
180 {
181     new->next = old->next;
182     new->next->prev = new;
183     new->prev = old->prev;
184     new->prev->next = new;
185 }
186 
list_replace_init(struct list_head * old,struct list_head * new)187 __STATIC_INLINE void list_replace_init(struct list_head *old,
188                                 struct list_head *new)
189 {
190     list_replace(old, new);
191     INIT_LIST_HEAD(old);
192 }
193 
194 /**
195  * list_del_init - deletes entry from list and reinitialize it.
196  * \param    entry: the element to delete from the list.
197  */
list_del_init(struct list_head * entry)198 __STATIC_INLINE void list_del_init(struct list_head *entry)
199 {
200     __list_del_entry(entry);
201     INIT_LIST_HEAD(entry);
202 }
203 
204 /**
205  * list_move - delete from one list and add as another's head
206  * \param    list: the entry to move
207  * \param    head: the head that will precede our entry
208  */
list_move(struct list_head * list,struct list_head * head)209 __STATIC_INLINE void list_move(struct list_head *list, struct list_head *head)
210 {
211     __list_del_entry(list);
212     list_add(list, head);
213 }
214 
215 /**
216  * list_move_tail - delete from one list and add as another's tail
217  * \param    list: the entry to move
218  * \param    head: the head that will follow our entry
219  */
list_move_tail(struct list_head * list,struct list_head * head)220 __STATIC_INLINE void list_move_tail(struct list_head *list,
221                                 struct list_head *head)
222 {
223     __list_del_entry(list);
224     list_add_tail(list, head);
225 }
226 
227 /**
228  * list_is_last - tests whether \param    list is the last entry in list \param    head
229  * \param    list: the entry to test
230  * \param    head: the head of the list
231  */
list_is_last(const struct list_head * list,const struct list_head * head)232 __STATIC_INLINE int list_is_last(const struct list_head *list,
233                             const struct list_head *head)
234 {
235     return list->next == head;
236 }
237 
238 /**
239  * list_empty - tests whether a list is empty
240  * \param    head: the list to test.
241  */
list_empty(const struct list_head * head)242 __STATIC_INLINE int list_empty(const struct list_head *head)
243 {
244     return head->next == head;
245 }
246 
247 /**
248  * list_empty_careful - tests whether a list is empty and not being modified
249  * \param    head: the list to test
250  *
251  * Description:
252  * tests whether a list is empty _and_ checks that no other CPU might be
253  * in the process of modifying either member (next or prev)
254  *
255  * NOTE: using list_empty_careful() without synchronization
256  * can only be safe if the only activity that can happen
257  * to the list entry is list_del_init(). Eg. it cannot be used
258  * if another CPU could re-list_add() it.
259  */
list_empty_careful(const struct list_head * head)260 __STATIC_INLINE int list_empty_careful(const struct list_head *head)
261 {
262     struct list_head *next = head->next;
263     return (next == head) && (next == head->prev);
264 }
265 
266 /**
267  * list_rotate_left - rotate the list to the left
268  * \param    head: the head of the list
269  */
list_rotate_left(struct list_head * head)270 __STATIC_INLINE void list_rotate_left(struct list_head *head)
271 {
272     struct list_head *first;
273 
274     if (!list_empty(head))
275     {
276         first = head->next;
277         list_move_tail(first, head);
278     }
279 }
280 
281 /**
282  * list_is_singular - tests whether a list has just one entry.
283  * \param    head: the list to test.
284  */
list_is_singular(const struct list_head * head)285 __STATIC_INLINE int list_is_singular(const struct list_head *head)
286 {
287     return !list_empty(head) && (head->next == head->prev);
288 }
289 
__list_cut_position(struct list_head * list,struct list_head * head,struct list_head * entry)290 __STATIC_INLINE void __list_cut_position(struct list_head *list,
291                     struct list_head *head, struct list_head *entry)
292 {
293     struct list_head *new_first = entry->next;
294     list->next = head->next;
295     list->next->prev = list;
296     list->prev = entry;
297     entry->next = list;
298     head->next = new_first;
299     new_first->prev = head;
300 }
301 
302 /**
303  * list_cut_position - cut a list into two
304  * \param    list: a new list to add all removed entries
305  * \param    head: a list with entries
306  * \param    entry: an entry within head, could be the head itself
307  *  and if so we won't cut the list
308  *
309  * This helper moves the initial part of head, up to and
310  * including entry, from head to list. You should
311  * pass on entry an element you know is on head. list
312  * should be an empty list or a list you do not care about
313  * losing its data.
314  *
315  */
list_cut_position(struct list_head * list,struct list_head * head,struct list_head * entry)316 __STATIC_INLINE void list_cut_position(struct list_head *list,
317                     struct list_head *head, struct list_head *entry)
318 {
319     if (list_empty(head))
320         return;
321     if (list_is_singular(head) &&
322     (head->next != entry && head != entry))
323         return;
324     if (entry == head)
325         INIT_LIST_HEAD(list);
326     else
327         __list_cut_position(list, head, entry);
328 }
329 
__list_splice(const struct list_head * list,struct list_head * prev,struct list_head * next)330 __STATIC_INLINE void __list_splice(const struct list_head *list,
331                               struct list_head *prev,
332                               struct list_head *next)
333 {
334     struct list_head *first = list->next;
335     struct list_head *last = list->prev;
336 
337     first->prev = prev;
338     prev->next = first;
339 
340     last->next = next;
341     next->prev = last;
342 }
343 
344 /**
345  * list_splice - join two lists, this is designed for stacks
346  * \param    list: the new list to add.
347  * \param    head: the place to add it in the first list.
348  */
list_splice(const struct list_head * list,struct list_head * head)349 __STATIC_INLINE void list_splice(const struct list_head *list,
350                                   struct list_head *head)
351 {
352     if (!list_empty(list))
353         __list_splice(list, head, head->next);
354 }
355 
356 /**
357  * list_splice_tail - join two lists, each list being a queue
358  * \param    list: the new list to add.
359  * \param    head: the place to add it in the first list.
360  */
list_splice_tail(struct list_head * list,struct list_head * head)361 __STATIC_INLINE void list_splice_tail(struct list_head *list,
362                                struct list_head *head)
363 {
364     if (!list_empty(list))
365         __list_splice(list, head->prev, head);
366 }
367 
368 /**
369  * list_splice_init - join two lists and reinitialise the emptied list.
370  * \param    list: the new list to add.
371  * \param    head: the place to add it in the first list.
372  *
373  * The list at 'list' is reinitialised
374  */
list_splice_init(struct list_head * list,struct list_head * head)375 __STATIC_INLINE void list_splice_init(struct list_head *list,
376                                struct list_head *head)
377 {
378     if (!list_empty(list))
379     {
380         __list_splice(list, head, head->next);
381         INIT_LIST_HEAD(list);
382     }
383 }
384 
385 /**
386  * list_splice_tail_init - join two lists and reinitialise the emptied list
387  * \param    list: the new list to add.
388  * \param    head: the place to add it in the first list.
389  *
390  * Each of the lists is a queue.
391  * The list at 'list' is reinitialised
392  */
list_splice_tail_init(struct list_head * list,struct list_head * head)393 __STATIC_INLINE void list_splice_tail_init(struct list_head *list,
394                                   struct list_head *head)
395 {
396     if (!list_empty(list))
397     {
398         __list_splice(list, head->prev, head);
399         INIT_LIST_HEAD(list);
400     }
401 }
402 
403 /**
404  * list_entry - get the struct for this entry
405  * \param    ptr:    the &struct list_head pointer.
406  * \param    type:   the type of the struct this is embedded in.
407  * \param    member: the name of the list_struct within the struct.
408  */
409 #define list_entry(ptr, type, member) \
410     container_of(ptr, type, member)
411 
412 /**
413  * list_first_entry - get the first element from a list
414  * \param    ptr:    the list head to take the element from.
415  * \param    type:   the type of the struct this is embedded in.
416  * \param    member: the name of the list_struct within the struct.
417  *
418  * Note, that list is expected to be not empty.
419  */
420 #define list_first_entry(ptr, type, member) \
421     list_entry((ptr)->next, type, member)
422 
423 /**
424  * list_for_each    -   iterate over a list
425  * \param    pos:    the &struct list_head to use as a loop cursor.
426  * \param    head:   the head for your list.
427  */
428 #define list_for_each(pos, head) \
429     for (pos = (head)->next; pos != (head); pos = pos->next)
430 
431 /**
432  * __list_for_each  -   iterate over a list
433  * \param    pos:    the &struct list_head to use as a loop cursor.
434  * \param    head:   the head for your list.
435  *
436  * This variant differs from list_for_each() in that it's the
437  * simplest possible list iteration code, no prefetching is done.
438  * Use this for code that knows the list to be very short (empty
439  * or 1 entry) most of the time.
440  */
441 #define __list_for_each(pos, head) \
442     for (pos = (head)->next; pos != (head); pos = pos->next)
443 
444 /**
445  * list_for_each_prev   -   iterate over a list backwards
446  * \param    pos:    the &struct list_head to use as a loop cursor.
447  * \param    head:   the head for your list.
448  */
449 #define list_for_each_prev(pos, head) \
450     for (pos = (head)->prev; pos != (head); pos = pos->prev)
451 
452 /**
453  * list_for_each_safe - iterate over a list safe against removal of list entry
454  * \param    pos:    the &struct list_head to use as a loop cursor.
455  * \param    n:      another &struct list_head to use as temporary storage
456  * \param    head:   the head for your list.
457  */
458 #define list_for_each_safe(pos, n, head)                    \
459     for (pos = (head)->next, n = pos->next; pos != (head);  \
460         pos = n, n = pos->next)
461 
462 /**
463  * list_for_each_prev_safe - iterate over a list backwards safe against removal of list entry
464  * \param    pos:    the &struct list_head to use as a loop cursor.
465  * \param    n:      another &struct list_head to use as temporary storage
466  * \param    head:   the head for your list.
467  */
468 #define list_for_each_prev_safe(pos, n, head)   \
469     for (pos = (head)->prev, n = pos->prev;     \
470          pos != (head);                         \
471          pos = n, n = pos->prev)
472 
473 /**
474  * list_for_each_entry  -   iterate over list of given type
475  * \param    pos:    the type * to use as a loop cursor.
476  * \param    head:   the head for your list.
477  * \param    member: the name of the list_struct within the struct.
478  */
479 #define list_for_each_entry(pos, head, type, member)         \
480     for (pos = list_entry((head)->next, type, member);      \
481          &pos->member != (head);                            \
482          pos = list_entry(pos->member.next, type, member))
483 
484 /**
485  * list_for_each_entry_reverse - iterate backwards over list of given type.
486  * \param    pos:    the type * to use as a loop cursor.
487  * \param    head:   the head for your list.
488  * \param    type:   the type of the container struct this is embedded in.
489  * \param    member: the name of the list_struct within the struct.
490  */
491 #define list_for_each_entry_reverse(pos, head, type, member)    \
492     for (pos = list_entry((head)->prev, type, member);          \
493          &pos->member != (head);                                \
494          pos = list_entry(pos->member.prev, type, member))
495 
496 /**
497  * list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue()
498  * \param    pos:    the type * to use as a start point
499  * \param    head:   the head of the list
500  * \param    type:   the type of the container struct this is embedded in.
501  * \param    member: the name of the list_struct within the struct.
502  *
503  * Prepares a pos entry for use as a start point in list_for_each_entry_continue().
504  */
505 #define list_prepare_entry(pos, head, type, member) \
506     ((pos) ? : list_entry(head, type, member))
507 
508 /**
509  * list_for_each_entry_continue - continue iteration over list of given type
510  * \param    pos:    the type * to use as a loop cursor.
511  * \param    head:   the head for your list.
512  * \param    type:   the type of the container struct this is embedded in.
513  * \param    member: the name of the list_struct within the struct.
514  *
515  * Continue to iterate over list of given type, continuing after
516  * the current position.
517  */
518 #define list_for_each_entry_continue(pos, head, type, member)   \
519     for (pos = list_entry(pos->member.next, type, member);      \
520          &pos->member != (head);                                \
521          pos = list_entry(pos->member.next, type, member))
522 
523 /**
524  * list_for_each_entry_continue_reverse - iterate backwards from the given point
525  * \param    pos:    the type * to use as a loop cursor.
526  * \param    head:   the head for your list.
527  * \param    type:   the type of the container struct this is embedded in.
528  * \param    member: the name of the list_struct within the struct.
529  *
530  * Start to iterate over list of given type backwards, continuing after
531  * the current position.
532  */
533 #define list_for_each_entry_continue_reverse(pos, head, type, member)   \
534     for (pos = list_entry(pos->member.prev, type, member);              \
535          &pos->member != (head);                                        \
536          pos = list_entry(pos->member.prev, type, member))
537 
538 /**
539  * list_for_each_entry_from - iterate over list of given type from the current point
540  * \param    pos:    the type * to use as a loop cursor.
541  * \param    head:   the head for your list.
542  * \param    type:   the type of the container struct this is embedded in.
543  * \param    member: the name of the list_struct within the struct.
544  *
545  * Iterate over list of given type, continuing from current position.
546  */
547 #define list_for_each_entry_from(pos, head, type, member)   \
548     for (; &pos->member != (head);                          \
549          pos = list_entry(pos->member.next, type, member))
550 
551 /**
552  * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
553  * \param    pos:    the type * to use as a loop cursor.
554  * \param    n:      another type * to use as temporary storage
555  * \param    head:   the head for your list.
556  * \param    type:   the type of the container struct this is embedded in.
557  * \param    member: the name of the list_struct within the struct.
558  */
559 #define list_for_each_entry_safe(pos, n, head, type, member)    \
560     for (pos = list_entry((head)->next, type, member),          \
561         n = list_entry(pos->member.next, type, member);         \
562          &pos->member != (head);                                \
563          pos = n, n = list_entry(n->member.next, type, member))
564 
565 /**
566  * list_for_each_entry_safe_continue - continue list iteration safe against removal
567  * \param    pos:    the type * to use as a loop cursor.
568  * \param    n:      another type * to use as temporary storage
569  * \param    head:   the head for your list.
570  * \param    type:   the type of the container struct this is embedded in.
571  * \param    member: the name of the list_struct within the struct.
572  *
573  * Iterate over list of given type, continuing after current point,
574  * safe against removal of list entry.
575  */
576 #define list_for_each_entry_safe_continue(pos, n, head, type, member)   \
577     for (pos = list_entry(pos->member.next, type), member),             \
578         n = list_entry(pos->member.next, type, member);                 \
579          &pos->member != (head);                                        \
580          pos = n, n = list_entry(n->member.next, type, member))
581 
582 /**
583  * list_for_each_entry_safe_from - iterate over list from current point safe against removal
584  * \param    pos:    the type * to use as a loop cursor.
585  * \param    n:      another type * to use as temporary storage
586  * \param    head:   the head for your list.
587  * \param    type:   the type of the container struct this is embedded in.
588  * \param    member: the name of the list_struct within the struct.
589  *
590  * Iterate over list of given type from current point, safe against
591  * removal of list entry.
592  */
593 #define list_for_each_entry_safe_from(pos, n, head, type, member)   \
594     for (n = list_entry(pos->member.next, type, member);            \
595          &pos->member != (head);                                    \
596          pos = n, n = list_entry(n->member.next, type, member))
597 
598 /**
599  * list_for_each_entry_safe_reverse - iterate backwards over list safe against removal
600  * \param    pos:    the type * to use as a loop cursor.
601  * \param    n:      another type * to use as temporary storage
602  * \param    head:   the head for your list.
603  * \param    type:   the type of the container struct this is embedded in.
604  * \param    member: the name of the list_struct within the struct.
605  *
606  * Iterate backwards over list of given type, safe against removal
607  * of list entry.
608  */
609 #define list_for_each_entry_safe_reverse(pos, n, head, type, member) \
610     for (pos = list_entry((head)->prev, type, member),              \
611         n = list_entry(pos->member.prev, type, member);             \
612          &pos->member != (head);                                    \
613          pos = n, n = list_entry(n->member.prev, type, member))
614 
615 /**
616  * list_safe_reset_next - reset a stale list_for_each_entry_safe loop
617  * \param    pos:    the loop cursor used in the list_for_each_entry_safe loop
618  * \param    n:      temporary storage used in list_for_each_entry_safe
619  * \param    type:   the type of the container struct this is embedded in.
620  * \param    member: the name of the list_struct within the struct.
621  *
622  * list_safe_reset_next is not safe to use in general if the list may be
623  * modified concurrently (eg. the lock is dropped in the loop body). An
624  * exception to this is if the cursor element (pos) is pinned in the list,
625  * and list_safe_reset_next is called after re-taking the lock and before
626  * completing the current iteration of the loop body.
627  */
628 #define list_safe_reset_next(pos, n, type, member)  \
629     n = list_entry(pos->member.next, type, member)
630 
631 #endif /* _LNX_LIST_H_ */
632 
633