1 /*
2 * Copyright (c) 2021 Rockchip Electronics Co., Ltd.
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
16 #ifndef __MPP_LIST_H__
17 #define __MPP_LIST_H__
18
19 #include "rk_type.h"
20
21 #include "mpp_thread.h"
22
23 /*
24 * two list structures are defined, one for C++, the other for C
25 */
26
27 #ifdef __cplusplus
28
29 // desctructor of list node
30 typedef void *(*node_destructor)(void *);
31
32 struct mpp_list_node;
33
34 class mpp_list : public MppMutexCond {
35 public:
36 mpp_list(node_destructor func = NULL);
37 ~mpp_list();
38
39 // for FIFO or FILO implement
40 // adding functions support simple structure like C struct or C++ class pointer,
41 // do not support C++ object
42 RK_S32 add_at_head(void *data, RK_S32 size);
43 RK_S32 add_at_tail(void *data, RK_S32 size);
44 // deleting function will copy the stored data to input pointer with size as size
45 // if NULL is passed to deleting functions, the node will be delete directly
46 RK_S32 del_at_head(void *data, RK_S32 size);
47 RK_S32 del_at_tail(void *data, RK_S32 size);
48
49 // direct fifo operation
50 RK_S32 fifo_wr(void *data, RK_S32 size);
51 RK_S32 fifo_rd(void *data, RK_S32 *size);
52
53 // for status check
54 RK_S32 list_is_empty();
55 RK_S32 list_size();
56
57 // for vector implement - not implemented yet
58 // adding function will return a key
59 RK_S32 add_by_key(void *data, RK_S32 size, RK_U32 *key);
60 RK_S32 del_by_key(void *data, RK_S32 size, RK_U32 key);
61 RK_S32 show_by_key(void *data, RK_U32 key);
62
63 RK_S32 flush();
64
65 private:
66 node_destructor destroy;
67 struct mpp_list_node *head;
68 RK_S32 count;
69 static RK_U32 keys;
70 static RK_U32 get_key();
71
72 mpp_list(const mpp_list &);
73 mpp_list &operator=(const mpp_list &);
74 };
75 #endif
76
77
78 #ifdef __cplusplus
79 extern "C" {
80 #endif
81
82 struct list_head {
83 struct list_head *next, *prev;
84 };
85
86 #define LIST_HEAD_INIT(name) { &(name), &(name) }
87
88 #define LIST_HEAD(name) \
89 struct list_head name = LIST_HEAD_INIT(name)
90
91 #define INIT_LIST_HEAD(ptr) do { \
92 (ptr)->next = (ptr); (ptr)->prev = (ptr); \
93 } while (0)
94
95
96 #define list_for_each_safe(pos, n, head) do { \
97 for ((pos) = (head)->next, (n) = (pos)->next; (pos) != (head); \
98 (pos) = (n), (n) = (pos)->next) \
99 } while (0)
100
101
102 #define list_entry(ptr, type, member) \
103 ((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))
104
105 #define list_first_entry(ptr, type, member) \
106 list_entry((ptr)->next, type, member)
107
108 #define list_last_entry(ptr, type, member) \
109 list_entry((ptr)->prev, type, member)
110
111 #define list_first_entry_or_null(ptr, type, member) do { \
112 struct list_head *head__ = (ptr); \
113 struct list_head *pos__ = head__->next; \
114 pos__ != head__ ? list_entry(pos__, type, member) : NULL; \
115 } while (0)
116
117 #define list_next_entry(pos, type, member) \
118 list_entry((pos)->member.next, type, member)
119
120 #define list_prev_entry(pos, type, member) \
121 list_entry((pos)->member.prev, type, member)
122
123 #define list_for_each_entry(pos, head, type, member) \
124 for ((pos) = list_entry((head)->next, type, member); \
125 &(pos)->member != (head); \
126 (pos) = list_next_entry((pos), type, member))
127
128 #define list_for_each_entry_safe(pos, n, head, type, member) \
129 for ((pos) = list_first_entry(head, type, member), \
130 (n) = list_next_entry((pos), type, member); \
131 &(pos)->member != (head); \
132 (pos) = (n), (n) = list_next_entry(n, type, member))
133
134 #define list_for_each_entry_reverse(pos, head, type, member) \
135 for ((pos) = list_last_entry(head, type, member); \
136 &(pos)->member != (head); \
137 (pos) = list_prev_entry((pos), type, member))
138
139 #define list_for_each_entry_safe_reverse(pos, n, head, type, member) \
140 for ((pos) = list_last_entry(head, type, member), \
141 (n) = list_prev_entry((pos), type, member); \
142 &(pos)->member != (head); \
143 (pos) = (n), (n) = list_prev_entry(n, type, member))
144
_list_add(struct list_head * _new,struct list_head * prev,struct list_head * next)145 static __inline void _list_add(struct list_head * _new,
146 struct list_head * prev,
147 struct list_head * next)
148 {
149 next->prev = _new;
150 _new->next = next;
151 _new->prev = prev;
152 prev->next = _new;
153 }
154
list_add(struct list_head * _new,struct list_head * head)155 static __inline void list_add(struct list_head *_new, struct list_head *head)
156 {
157 _list_add(_new, head, head->next);
158 }
159
list_add_tail(struct list_head * _new,struct list_head * head)160 static __inline void list_add_tail(struct list_head *_new, struct list_head *head)
161 {
162 _list_add(_new, head->prev, head);
163 }
164
_list_del(struct list_head * prev,struct list_head * next)165 static __inline void _list_del(struct list_head * prev, struct list_head * next)
166 {
167 next->prev = prev;
168 prev->next = next;
169 }
170
list_del_init(struct list_head * entry)171 static __inline void list_del_init(struct list_head *entry)
172 {
173 _list_del(entry->prev, entry->next);
174
175 INIT_LIST_HEAD(entry);
176 }
177
list_is_last(const struct list_head * list,const struct list_head * head)178 static __inline int list_is_last(const struct list_head *list, const struct list_head *head)
179 {
180 return list->next == head;
181 }
182
list_empty(struct list_head * head)183 static __inline int list_empty(struct list_head *head)
184 {
185 return head->next == head;
186 }
187
188 #ifdef __cplusplus
189 }
190 #endif
191
192
193 #endif /* __MPP_LIST_H__ */
194