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 _CS_LIST_H_
16 #define _CS_LIST_H_
17 #include <stddef.h>
18 #include "dbg.h"
19
20 #define _2offsetof(type, member) ((long) &((type *) 0)->member)
21 //#define list_for_each_entry(pos, head, member)
22 //#define list_for_each_entry_safe(pos, n, head, member)
23
24 #if 0//!defined(__ICCARM__)
25 #define container_of(ptr, type, member) ({ \
26 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
27 (type *)( (char *)__mptr - _2offsetof(type,member) );})
28
29 #define list_entry(ptr, type, member) \
30 container_of(ptr, type, member)
31
32 #define list_for_each_entry_safe(pos, n, head, member) \
33 for (pos = list_entry((head)->next, typeof(*pos), member), \
34 n = list_entry(pos->member.next, typeof(*pos), member); \
35 &pos->member != (head); \
36 pos = n, n = list_entry(n->member.next, typeof(*n), member))
37
38 #define list_for_each_entry(pos, head, member) \
39 for (pos = list_entry((head)->next, typeof(*pos), member); \
40 &pos->member != (head); \
41 pos = list_entry(pos->member.next, typeof(*pos), member))
42 //#else /////////////////
43 #endif
44 #define container_of(ptr, type, member) ( \
45 (type *)( (char *)(ptr) - _2offsetof(type,member) ))
46
47 #define list_entry(ptr, type, member) \
48 container_of(ptr, type, member)
49
50 /**
51 * list_first_entry - get the first element from a list
52 * \param ptr: the list head to take the element from.
53 * \param type: the type of the struct this is embedded in.
54 * \param member: the name of the list_struct within the struct.
55 *
56 * Note, that list is expected to be not empty.
57 */
58 #define list_first_entry(ptr, type, member) \
59 list_entry((ptr)->next, type, member)
60
61 #define list_for_each_entry_safe(pos, n, head, member) \
62 for (pos = list_entry((head)->next, rwnx_cmd, member), \
63 n = list_entry(pos->member.next, rwnx_cmd, member); \
64 &pos->member != (head); \
65 pos = n, n = list_entry(n->member.next, rwnx_cmd, member))
66
67 #define list_for_each_entry(pos, head, member) \
68 for (pos = list_entry((head)->next, rwnx_cmd, member); \
69 &pos->member != (head); \
70 pos = list_entry(pos->member.next, rwnx_cmd, member))
71
72 struct list_head {
73 struct list_head *next, *prev;
74 };
75
INIT_LIST_HEAD(struct list_head * list)76 static inline void INIT_LIST_HEAD(struct list_head *list)
77 {
78 list->next = list;
79 list->prev = list;
80 }
81
__list_add(struct list_head * new,struct list_head * prev,struct list_head * next)82 static inline void __list_add(struct list_head *new,
83 struct list_head *prev,
84 struct list_head *next)
85 {
86 next->prev = new;
87 new->next = next;
88 new->prev = prev;
89 prev->next = new;
90 }
91
list_add_tail(struct list_head * new,struct list_head * head)92 static inline void list_add_tail(struct list_head *new, struct list_head *head)
93 {
94 __list_add(new, head->prev, head);
95 }
96
list_add(struct list_head * new,struct list_head * head)97 static inline void list_add(struct list_head *new, struct list_head *head)
98 {
99 __list_add(new, head, head->next);
100 }
101
__list_del(struct list_head * prev,struct list_head * next)102 static inline void __list_del(struct list_head * prev, struct list_head * next)
103 {
104 next->prev = prev;
105 prev->next = next;
106 }
107
list_del(struct list_head * entry)108 static inline void list_del(struct list_head *entry)
109 {
110 __list_del(entry->prev, entry->next);
111 entry->next = NULL;
112 entry->prev = NULL;
113 }
114
list_empty(const struct list_head * head)115 static inline int list_empty(const struct list_head *head)
116 {
117 return head->next == head;
118 }
119
120 #endif /* _CS_LIST_H_ */
121