1 /* SPDX-License-Identifier: Apache-2.0 OR MIT */
2 /*
3 * Copyright (c) 2015 Rockchip Electronics Co., Ltd.
4 */
5
6 #ifndef __MPP_LIST_H__
7 #define __MPP_LIST_H__
8
9 #include "rk_type.h"
10 #include "mpp_err.h"
11
12 #include "mpp_thread.h"
13
14 /*
15 * two list structures are defined here:
16 * 1. MppList : list with key
17 * 2. struct list_head : typical list head
18 */
19
20 #ifdef __cplusplus
21 extern "C" {
22 #endif
23
24 typedef struct MppListNode_t MppListNode;
25 // desctructor of list node
26 typedef void *(*node_destructor)(void *);
27
28 struct MppListNode_t {
29 MppListNode *prev;
30 MppListNode *next;
31 rk_u32 key;
32 rk_s32 size;
33 };
34
35 typedef struct MppList_t {
36 MppListNode *head;
37 int count;
38 node_destructor destroy;
39 MppMutexCond cond_lock;
40 rk_u32 keys;
41 } MppList;
42
43 int mpp_list_add_at_head(MppList *list, void *data, int size);
44 int mpp_list_add_at_tail(MppList *list, void *data, int size);
45 int mpp_list_del_at_head(MppList *list, void *data, int size);
46 int mpp_list_del_at_tail(MppList *list, void *data, int size);
47
48 rk_s32 mpp_list_fifo_wr(MppList *list, void *data, rk_s32 size);
49 rk_s32 mpp_list_fifo_rd(MppList *list, void *data, rk_s32 *size);
50
51 int mpp_list_is_empty(MppList *list);
52 int mpp_list_size(MppList *list);
53
54 rk_s32 mpp_list_add_by_key(MppList *list, void *data, rk_s32 size, rk_u32 *key);
55 rk_s32 mpp_list_del_by_key(MppList *list, void *data, rk_s32 size, rk_u32 key);
56 rk_s32 mpp_list_show_by_key(MppList *list, void *data, rk_u32 key);
57
58 void mpp_list_flush(MppList *list);
59
60 MPP_RET mpp_list_wait(MppList* list);
61 MPP_RET mpp_list_wait_timed(MppList *list, rk_s64 timeout);
62 MPP_RET mpp_list_wait_lt(MppList *list, rk_s64 timeout, rk_s32 val);
63 MPP_RET mpp_list_wait_le(MppList *list, rk_s64 timeout, rk_s32 val);
64 MPP_RET mpp_list_wait_gt(MppList *list, rk_s64 timeout, rk_s32 val);
65 MPP_RET mpp_list_wait_ge(MppList *list, rk_s64 timeout, rk_s32 val);
66
67 void mpp_list_signal(MppList *list);
68 rk_u32 mpp_list_get_key(MppList *list);
69
70 MppList *mpp_list_create(node_destructor func);
71 void mpp_list_destroy(MppList *list);
72
73
74 struct list_head {
75 struct list_head *next, *prev;
76 };
77
78 #define LIST_HEAD_INIT(name) { &(name), &(name) }
79
80 #define LIST_HEAD(name) \
81 struct list_head name = LIST_HEAD_INIT(name)
82
83 #define INIT_LIST_HEAD(ptr) do { \
84 (ptr)->next = (ptr); (ptr)->prev = (ptr); \
85 } while (0)
86
87 #define list_for_each_safe(pos, n, head) \
88 for (pos = (head)->next, n = pos->next; pos != (head); \
89 pos = n, n = pos->next)
90
91 #define list_entry(ptr, type, member) \
92 ((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))
93
94 #define list_first_entry(ptr, type, member) \
95 list_entry((ptr)->next, type, member)
96
97 #define list_last_entry(ptr, type, member) \
98 list_entry((ptr)->prev, type, member)
99
100 #define list_first_entry_or_null(ptr, type, member) ({ \
101 struct list_head *head__ = (ptr); \
102 struct list_head *pos__ = head__->next; \
103 pos__ != head__ ? list_entry(pos__, type, member) : NULL; \
104 })
105
106 #define list_next_entry(pos, type, member) \
107 list_entry((pos)->member.next, type, member)
108
109 #define list_prev_entry(pos, type, member) \
110 list_entry((pos)->member.prev, type, member)
111
112 #define list_for_each_entry(pos, head, type, member) \
113 for (pos = list_entry((head)->next, type, member); \
114 &pos->member != (head); \
115 pos = list_next_entry(pos, type, member))
116
117 #define list_for_each_entry_safe(pos, n, head, type, member) \
118 for (pos = list_first_entry(head, type, member), \
119 n = list_next_entry(pos, type, member); \
120 &pos->member != (head); \
121 pos = n, n = list_next_entry(n, type, member))
122
123 #define list_for_each_entry_reverse(pos, head, type, member) \
124 for (pos = list_last_entry(head, type, member); \
125 &pos->member != (head); \
126 pos = list_prev_entry(pos, type, member))
127
128 #define list_for_each_entry_safe_reverse(pos, n, head, type, member) \
129 for (pos = list_last_entry(head, type, member), \
130 n = list_prev_entry(pos, type, member); \
131 &pos->member != (head); \
132 pos = n, n = list_prev_entry(n, type, member))
133
__list_add(struct list_head * _new,struct list_head * prev,struct list_head * next)134 static __inline void __list_add(struct list_head * _new,
135 struct list_head * prev,
136 struct list_head * next)
137 {
138 next->prev = _new;
139 _new->next = next;
140 _new->prev = prev;
141 prev->next = _new;
142 }
143
list_add(struct list_head * _new,struct list_head * head)144 static __inline void list_add(struct list_head *_new, struct list_head *head)
145 {
146 __list_add(_new, head, head->next);
147 }
148
list_add_tail(struct list_head * _new,struct list_head * head)149 static __inline void list_add_tail(struct list_head *_new, struct list_head *head)
150 {
151 __list_add(_new, head->prev, head);
152 }
153
__list_del(struct list_head * prev,struct list_head * next)154 static __inline void __list_del(struct list_head * prev, struct list_head * next)
155 {
156 next->prev = prev;
157 prev->next = next;
158 }
159
list_del_init(struct list_head * entry)160 static __inline void list_del_init(struct list_head *entry)
161 {
162 __list_del(entry->prev, entry->next);
163
164 INIT_LIST_HEAD(entry);
165 }
166
list_move(struct list_head * list,struct list_head * head)167 static __inline void list_move(struct list_head *list, struct list_head *head)
168 {
169 __list_del(list->prev, list->next);
170 list_add(list, head);
171 }
172
list_move_tail(struct list_head * list,struct list_head * head)173 static __inline void list_move_tail(struct list_head *list, struct list_head *head)
174 {
175 __list_del(list->prev, list->next);
176 list_add_tail(list, head);
177 }
178
list_is_last(const struct list_head * list,const struct list_head * head)179 static __inline int list_is_last(const struct list_head *list, const struct list_head *head)
180 {
181 return list->next == head;
182 }
183
list_empty(struct list_head * head)184 static __inline int list_empty(struct list_head *head)
185 {
186 return head->next == head;
187 }
188
189 typedef rk_s32 (*ListCmpFunc)(void *, const struct list_head *, const struct list_head *);
190
191 void list_sort(void *priv, struct list_head *head, ListCmpFunc cmp);
192
193 #ifdef __cplusplus
194 }
195 #endif
196
197
198 #endif /*__MPP_LIST_H__*/
199