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