• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     signed int add_at_head(void *data, signed int size);
43     signed int add_at_tail(void *data, signed int 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     signed int del_at_head(void *data, signed int size);
47     signed int del_at_tail(void *data, signed int size);
48 
49     // direct fifo operation
50     signed int fifo_wr(void *data, signed int size);
51     signed int fifo_rd(void *data, signed int *size);
52 
53     // for status check
54     signed int list_is_empty();
55     signed int list_size();
56 
57     // for vector implement - not implemented yet
58     // adding function will return a key
59     signed int add_by_key(void *data, signed int size, unsigned int *key);
60     signed int del_by_key(void *data, signed int size, unsigned int key);
61     signed int show_by_key(void *data, unsigned int key);
62 
63     signed int flush();
64 
65 private:
66     node_destructor destroy;
67     struct mpp_list_node *head;
68     signed int count;
69     static unsigned int keys;
70     static unsigned int get_key();
71 
72     mpp_list(const mpp_list &);
73     mpp_list &operator=(const mpp_list &);
74 };
75 #endif
76 
77 #ifdef __cplusplus
78 extern "C" {
79 #endif
80 
81 struct list_head {
82     struct list_head *next, *prev;
83 };
84 
85 #define LIST_HEAD_INIT(name)                                                                                           \
86     {                                                                                                                  \
87         &(name), &(name)                                                                                               \
88     }
89 
90 #define LIST_HEAD(name) struct list_head name = LIST_HEAD_INIT(name)
91 
92 #define INIT_LIST_HEAD(ptr)                                                                                            \
93     do {                                                                                                               \
94         (ptr)->next = (ptr);                                                                                           \
95         (ptr)->prev = (ptr);                                                                                           \
96     } while (0)
97 
98 #define list_for_each_safe(pos, n, head)                                                                               \
99     do {                                                                                                               \
100         for ((pos) = (head)->next, (n) = (pos)->next; (pos) != (head); (pos) = (n), (n) = (pos)->next)                 \
101     } while (0)
102 
103 #define list_entry(ptr, type, member) ((type *)((char *)(ptr) - (unsigned long)(&((type *)0)->member)))
104 
105 #define list_first_entry(ptr, type, member) list_entry((ptr)->next, type, member)
106 
107 #define list_last_entry(ptr, type, member) list_entry((ptr)->prev, type, member)
108 
109 #define list_first_entry_or_null(ptr, type, member)                                                                    \
110     do {                                                                                                               \
111         struct list_head *head__ = (ptr);                                                                              \
112         struct list_head *pos__ = head__->next;                                                                        \
113         pos__ != head__ ? list_entry(pos__, type, member) : NULL;                                                      \
114     } while (0)
115 
116 #define list_next_entry(pos, type, member) list_entry((pos)->member.next, type, member)
117 
118 #define list_prev_entry(pos, type, member) list_entry((pos)->member.prev, type, member)
119 
120 #define list_for_each_entry(pos, head, type, member)                                                                   \
121     for ((pos) = list_entry((head)->next, type, member); &(pos)->member != (head);                                     \
122          (pos) = list_next_entry((pos), type, member))
123 
124 #define list_for_each_entry_safe(pos, n, head, type, member)                                                           \
125     for ((pos) = list_first_entry(head, type, member), (n) = list_next_entry((pos), type, member);                     \
126          &(pos)->member != (head); (pos) = (n), (n) = list_next_entry(n, type, member))
127 
128 #define list_for_each_entry_reverse(pos, head, type, member)                                                           \
129     for ((pos) = list_last_entry(head, type, member); &(pos)->member != (head);                                        \
130          (pos) = list_prev_entry((pos), type, member))
131 
132 #define list_for_each_entry_safe_reverse(pos, n, head, type, member)                                                   \
133     for ((pos) = list_last_entry(head, type, member), (n) = list_prev_entry((pos), type, member);                      \
134          &(pos)->member != (head); (pos) = (n), (n) = list_prev_entry(n, type, member))
135 
_list_add(struct list_head * _new,struct list_head * prev,struct list_head * next)136 static __inline void _list_add(struct list_head *_new, struct list_head *prev, 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_is_last(const struct list_head * list,const struct list_head * head)167 static __inline int list_is_last(const struct list_head *list, const struct list_head *head)
168 {
169     return list->next == head;
170 }
171 
list_empty(struct list_head * head)172 static __inline int list_empty(struct list_head *head)
173 {
174     return head->next == head;
175 }
176 
177 #ifdef __cplusplus
178 }
179 #endif
180 
181 #endif /* __MPP_LIST_H__ */
182