1 /*
2 * Copyright 2015 Rockchip Electronics Co. LTD
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #ifndef __MPP_LIST_H__
18 #define __MPP_LIST_H__
19
20 #include "rk_type.h"
21 #include "mpp_err.h"
22
23 #include "mpp_thread.h"
24
25 /*
26 * two list structures are defined, one for C++, the other for C
27 */
28
29 #ifdef __cplusplus
30
31 // desctructor of list node
32 typedef void *(*node_destructor)(void *);
33
34 struct mpp_list_node;
35
36 class mpp_list : public MppMutexCond
37 {
38 public:
39 mpp_list(node_destructor func = NULL);
40 ~mpp_list();
41
42 // for FIFO or FILO implement
43 // adding functions support simple structure like C struct or C++ class pointer,
44 // do not support C++ object
45 RK_S32 add_at_head(void *data, RK_S32 size);
46 RK_S32 add_at_tail(void *data, RK_S32 size);
47 // deleting function will copy the stored data to input pointer with size as size
48 // if NULL is passed to deleting functions, the node will be delete directly
49 RK_S32 del_at_head(void *data, RK_S32 size);
50 RK_S32 del_at_tail(void *data, RK_S32 size);
51
52 // direct fifo operation
53 RK_S32 fifo_wr(void *data, RK_S32 size);
54 RK_S32 fifo_rd(void *data, RK_S32 *size);
55
56 // for status check
57 RK_S32 list_is_empty();
58 RK_S32 list_size();
59
60 // for vector implement - not implemented yet
61 // adding function will return a key
62 RK_S32 add_by_key(void *data, RK_S32 size, RK_U32 *key);
63 RK_S32 del_by_key(void *data, RK_S32 size, RK_U32 key);
64 RK_S32 show_by_key(void *data, RK_U32 key);
65
66 RK_S32 flush();
67
68 // for list wait
69 MPP_RET wait_lt(RK_S64 timeout, RK_S32 val);
70 MPP_RET wait_le(RK_S64 timeout, RK_S32 val);
71 MPP_RET wait_gt(RK_S64 timeout, RK_S32 val);
72 MPP_RET wait_ge(RK_S64 timeout, RK_S32 val);
73
74 private:
75 node_destructor destroy;
76 struct mpp_list_node *head;
77 RK_S32 count;
78 static RK_U32 keys;
79 static RK_U32 get_key();
80
81 mpp_list(const mpp_list &);
82 mpp_list &operator=(const mpp_list &);
83 };
84 #endif
85
86
87 #ifdef __cplusplus
88 extern "C" {
89 #endif
90
91 struct list_head {
92 struct list_head *next, *prev;
93 };
94
95 #define LIST_HEAD_INIT(name) { &(name), &(name) }
96
97 #define LIST_HEAD(name) \
98 struct list_head name = LIST_HEAD_INIT(name)
99
100 #define INIT_LIST_HEAD(ptr) do { \
101 (ptr)->next = (ptr); (ptr)->prev = (ptr); \
102 } while (0)
103
104 #define list_for_each_safe(pos, n, head) \
105 for (pos = (head)->next, n = pos->next; pos != (head); \
106 pos = n, n = pos->next)
107
108 #define list_entry(ptr, type, member) \
109 ((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))
110
111 #define list_first_entry(ptr, type, member) \
112 list_entry((ptr)->next, type, member)
113
114 #define list_last_entry(ptr, type, member) \
115 list_entry((ptr)->prev, type, member)
116
117 #define list_first_entry_or_null(ptr, type, member) ({ \
118 struct list_head *head__ = (ptr); \
119 struct list_head *pos__ = head__->next; \
120 pos__ != head__ ? list_entry(pos__, type, member) : NULL; \
121 })
122
123 #define list_next_entry(pos, type, member) \
124 list_entry((pos)->member.next, type, member)
125
126 #define list_prev_entry(pos, type, member) \
127 list_entry((pos)->member.prev, type, member)
128
129 #define list_for_each_entry(pos, head, type, member) \
130 for (pos = list_entry((head)->next, type, member); \
131 &pos->member != (head); \
132 pos = list_next_entry(pos, type, member))
133
134 #define list_for_each_entry_safe(pos, n, head, type, member) \
135 for (pos = list_first_entry(head, type, member), \
136 n = list_next_entry(pos, type, member); \
137 &pos->member != (head); \
138 pos = n, n = list_next_entry(n, type, member))
139
140 #define list_for_each_entry_reverse(pos, head, type, member) \
141 for (pos = list_last_entry(head, type, member); \
142 &pos->member != (head); \
143 pos = list_prev_entry(pos, type, member))
144
145 #define list_for_each_entry_safe_reverse(pos, n, head, type, member) \
146 for (pos = list_last_entry(head, type, member), \
147 n = list_prev_entry(pos, type, member); \
148 &pos->member != (head); \
149 pos = n, n = list_prev_entry(n, type, member))
150
__list_add(struct list_head * _new,struct list_head * prev,struct list_head * next)151 static __inline void __list_add(struct list_head * _new,
152 struct list_head * prev,
153 struct list_head * next)
154 {
155 next->prev = _new;
156 _new->next = next;
157 _new->prev = prev;
158 prev->next = _new;
159 }
160
list_add(struct list_head * _new,struct list_head * head)161 static __inline void list_add(struct list_head *_new, struct list_head *head)
162 {
163 __list_add(_new, head, head->next);
164 }
165
list_add_tail(struct list_head * _new,struct list_head * head)166 static __inline void list_add_tail(struct list_head *_new, struct list_head *head)
167 {
168 __list_add(_new, head->prev, head);
169 }
170
__list_del(struct list_head * prev,struct list_head * next)171 static __inline void __list_del(struct list_head * prev, struct list_head * next)
172 {
173 next->prev = prev;
174 prev->next = next;
175 }
176
list_del_init(struct list_head * entry)177 static __inline void list_del_init(struct list_head *entry)
178 {
179 __list_del(entry->prev, entry->next);
180
181 INIT_LIST_HEAD(entry);
182 }
183
list_move(struct list_head * list,struct list_head * head)184 static __inline void list_move(struct list_head *list, struct list_head *head)
185 {
186 __list_del(list->prev, list->next);
187 list_add(list, head);
188 }
189
list_move_tail(struct list_head * list,struct list_head * head)190 static __inline void list_move_tail(struct list_head *list, struct list_head *head)
191 {
192 __list_del(list->prev, list->next);
193 list_add_tail(list, head);
194 }
195
list_is_last(const struct list_head * list,const struct list_head * head)196 static __inline int list_is_last(const struct list_head *list, const struct list_head *head)
197 {
198 return list->next == head;
199 }
200
list_empty(struct list_head * head)201 static __inline int list_empty(struct list_head *head)
202 {
203 return head->next == head;
204 }
205
206 typedef RK_S32 (*list_cmp_func_t)(void *, const struct list_head *, const struct list_head *);
207
208 void list_sort(void *priv, struct list_head *head, list_cmp_func_t cmp);
209
210 #ifdef __cplusplus
211 }
212 #endif
213
214
215 #endif /*__MPP_LIST_H__*/
216