hc
2023-12-09 b22da3d8526a935aa31e086e63f60ff3246cb61c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
#define ALOG_TAG "rk_list"
 
#include <errno.h>
#include <string.h>
#include <stdlib.h>
 
#include "rk_list.h"
#include "mpp_log.h"
 
//#define _RK_LIST_DEUBG
#define _RK_LIST_ERROR
 
#ifdef _RK_LIST_DEUBG
#define LIST_DEBUG(fmt, ...) mpp_log(fmt, ## __VA_ARGS__)
#else
#define LIST_DEBUG(fmt, ...) /* not debugging: nothing */
#endif
 
#ifdef _RK_LIST_ERROR
#define LIST_ERROR(fmt, ...) mpp_err(fmt, ## __VA_ARGS__)
#else
#define LIST_ERROR(fmt, ...)
#endif
 
typedef struct rk_list_node {
    rk_list_node*   prev;
    rk_list_node*   next;
    RK_U32        key;
    RK_S32         size;
} rk_list_node;
 
static inline void list_node_init(rk_list_node *node)
{
    node->prev = node->next = node;
}
 
static inline void list_node_init_with_key_and_size(rk_list_node *node, RK_U32 key, RK_S32 size)
{
    list_node_init(node);
    node->key   = key;
    node->size  = size;
}
 
static rk_list_node* create_list(void *data, RK_S32 size, RK_U32 key)
{
    rk_list_node *node = (rk_list_node*)malloc(sizeof(rk_list_node) + size);
    if (node) {
        void *dst = (void*)(node + 1);
        list_node_init_with_key_and_size(node, key, size);
        memcpy(dst, data, size);
    } else {
        LIST_ERROR("failed to allocate list node");
    }
    return node;
}
 
static inline void _rk_list_add(rk_list_node * _new, rk_list_node * prev, rk_list_node * next)
{
    next->prev = _new;
    _new->next = next;
    _new->prev = prev;
    prev->next = _new;
}
 
static inline void rk_list_add(rk_list_node *_new, rk_list_node *head)
{
    _rk_list_add(_new, head, head->next);
}
 
static inline void rk_list_add_tail(rk_list_node *_new, rk_list_node *head)
{
    _rk_list_add(_new, head->prev, head);
}
 
RK_S32 rk_list::add_at_head(void *data, RK_S32 size)
{
    RK_S32 ret = -EINVAL;
    pthread_mutex_lock(&mutex);
    if (head) {
        rk_list_node *node = create_list(data, size, 0);
        if (node) {
            rk_list_add(node, head);
            count++;
            ret = 0;
        } else {
            ret = -ENOMEM;
        }
    }
    pthread_mutex_unlock(&mutex);
    return ret;
}
 
RK_S32 rk_list::add_at_tail(void *data, RK_S32 size)
{
    RK_S32 ret = -EINVAL;
    pthread_mutex_lock(&mutex);
    if (head) {
        rk_list_node *node = create_list(data, size, 0);
        if (node) {
            rk_list_add_tail(node, head);
            count++;
            ret = 0;
        } else {
            ret = -ENOMEM;
        }
    }
    pthread_mutex_unlock(&mutex);
    return ret;
}
 
static void release_list(rk_list_node*node, void *data, RK_S32 size)
{
    void *src = (void*)(node + 1);
    if (node->size == size) {
        memcpy(data, src, size);
    } else {
        LIST_ERROR("node size check failed when release_list");
        size = (size < node->size) ? (size) : (node->size);
        memcpy(data, src, size);
    }
    free(node);
}
 
static inline void _rk_list_del(rk_list_node *prev, rk_list_node *next)
{
    next->prev = prev;
    prev->next = next;
}
 
static inline void rk_list_del_init(rk_list_node *node)
{
    _rk_list_del(node->prev, node->next);
    list_node_init(node);
}
 
static inline void _list_del_node_no_lock(rk_list_node *node, void *data, RK_S32 size)
{
    rk_list_del_init(node);
    release_list(node, data, size);
}
 
RK_S32 rk_list::del_at_head(void *data, RK_S32 size)
{
    RK_S32 ret = -EINVAL;
    pthread_mutex_lock(&mutex);
    if (head && count) {
        _list_del_node_no_lock(head->next, data, size);
        count--;
        ret = 0;
    }
    pthread_mutex_unlock(&mutex);
    return ret;
}
 
RK_S32 rk_list::del_at_tail(void *data, RK_S32 size)
{
    RK_S32 ret = -EINVAL;
    pthread_mutex_lock(&mutex);
    if (head && count) {
        _list_del_node_no_lock(head->prev, data, size);
        count--;
        ret = 0;
    }
    pthread_mutex_unlock(&mutex);
    return ret;
}
 
RK_S32 rk_list::list_is_empty()
{
    pthread_mutex_lock(&mutex);
    RK_S32 ret = (count == 0);
    pthread_mutex_unlock(&mutex);
    return ret;
}
 
RK_S32 rk_list::list_size()
{
    pthread_mutex_lock(&mutex);
    RK_S32 ret = count;
    pthread_mutex_unlock(&mutex);
    return ret;
}
 
RK_S32 rk_list::add_by_key(void *data, RK_S32 size, RK_U32 *key)
{
    RK_S32 ret = 0;
    (void)data;
    (void)size;
    (void)key;
    return ret;
}
 
RK_S32 rk_list::del_by_key(void *data, RK_S32 size, RK_U32 key)
{
    RK_S32 ret = 0;
    (void)data;
    (void)size;
    (void)key;
    return ret;
}
 
 
RK_S32 rk_list::show_by_key(void *data, RK_U32 key)
{
    RK_S32 ret = 0;
    (void)data;
    (void)key;
    return ret;
}
 
RK_S32 rk_list::flush()
{
    pthread_mutex_lock(&mutex);
    if (head) {
        while (count) {
            rk_list_node* node = head->next;
            rk_list_del_init(node);
            if (destroy) {
                destroy((void*)(node + 1));
            }
            free(node);
            count--;
        }
    }
    pthread_mutex_unlock(&mutex);
    return 0;
}
 
rk_list::rk_list(node_destructor func)
    : destroy(NULL),
      head(NULL),
      count(0)
{
    pthread_mutexattr_t attr;
    pthread_mutexattr_init(&attr);
    pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
    pthread_mutex_init(&mutex, &attr);
    pthread_mutexattr_destroy(&attr);
    destroy = func;
    head = (rk_list_node*)malloc(sizeof(rk_list_node));
    if (NULL == head) {
        LIST_ERROR("failed to allocate list header");
    } else {
        list_node_init_with_key_and_size(head, 0, 0);
    }
}
 
rk_list::~rk_list()
{
    flush();
    if (head) free(head);
    head = NULL;
    destroy = NULL;
    pthread_mutex_destroy(&mutex);
}
 
#if BUILD_RK_LIST_TEST
#include "vpu_mem.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
#define LOOP_RK_LIST    600
 
#define COUNT_ADD       100
#define COUNT_DEL       99
 
static volatile int err = 0;
 
static int rk_list_fifo_test(rk_list *list_0)
{
    int count;
    VPUMemLinear_t m;
    for (count = 0; count < COUNT_ADD; count++) {
        err |= VPUMallocLinear(&m, 100);
        if (err) {
            printf("VPUMallocLinear in rk_list_fifo_test\n");
            break;
        }
        err |= list_0->add_at_head(&m, sizeof(m));
        if (err) {
            printf("add_at_head in rk_list_fifo_test\n");
            break;
        }
    }
 
    if (!err) {
        for (count = 0; count < COUNT_DEL; count++) {
            err |= list_0->del_at_tail(&m, sizeof(m));
            if (err) {
                printf("del_at_tail in rk_list_fifo_test\n");
                break;
            }
            err |= VPUFreeLinear(&m);
            if (err) {
                printf("VPUFreeLinear in rk_list_fifo_test\n");
                break;
            }
        }
    }
    return err;
}
 
static int rk_list_filo_test(rk_list *list_0)
{
    int count;
    VPUMemLinear_t m;
    for (count = 0; count < COUNT_ADD + COUNT_DEL; count++) {
        if (count & 1) {
            err |= list_0->del_at_head(&m, sizeof(m));
            if (err) {
                printf("del_at_head in rk_list_filo_test\n");
                break;
            }
            err |= VPUFreeLinear(&m);
            if (err) {
                printf("VPUFreeLinear in rk_list_fifo_test\n");
                break;
            }
        } else {
            err |= VPUMallocLinear(&m, 100);
            if (err) {
                printf("VPUMallocLinear in rk_list_filo_test\n");
                break;
            }
            err |= list_0->add_at_head(&m, sizeof(m));
            if (err) {
                printf("add_at_head in rk_list_fifo_test\n");
                break;
            }
        }
    }
 
    return err;
}
 
 
void *rk_list_test_loop_0(void *pdata)
{
    int i;
    rk_list *list_0 = (rk_list *)pdata;
 
    printf("rk_list test 0 loop start\n");
    for (i = 0; i < LOOP_RK_LIST; i++) {
        err |= rk_list_filo_test(list_0);
        if (err) break;
    }
 
    if (err) {
        printf("thread: found vpu mem operation err %d\n", err);
    } else {
        printf("thread: test done and found no err\n");
    }
    return NULL;
}
 
int rk_list_test_0()
{
    int i, err = 0;
    printf("rk_list test 0 FIFO start\n");
 
    rk_list *list_0 = new rk_list((node_destructor)VPUFreeLinear);
 
    pthread_t mThread;
    pthread_attr_t attr;
    pthread_attr_init(&attr);
    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
 
    pthread_create(&mThread, &attr, rk_list_test_loop_0, (void*)list_0);
    pthread_attr_destroy(&attr);
 
    for (i = 0; i < LOOP_RK_LIST; i++) {
        err |= rk_list_fifo_test(list_0);
        if (err) break;
    }
    if (err) {
        printf("main  : found rk_list operation err %d\n", err);
    } else {
        printf("main  : test done and found no err\n");
    }
 
    void *dummy;
    pthread_join(mThread, &dummy);
 
    printf("rk_list test 0 end size %d\n", list_0->list_size());
    delete list_0;
    return err;
}
 
#define TOTAL_RK_LIST_TEST_COUNT    1
 
typedef int (*RK_LIST_TEST_FUNC)(void);
RK_LIST_TEST_FUNC test_func[TOTAL_RK_LIST_TEST_COUNT] = {
    rk_list_test_0,
};
 
int main(int argc, char *argv[])
{
    int i, start = 0, end = 0;
    if (argc < 2) {
        end = TOTAL_RK_LIST_TEST_COUNT;
    } else if (argc == 2) {
        start = atoi(argv[1]);
        end   = start + 1;
    } else if (argc == 3) {
        start = atoi(argv[1]);
        end   = atoi(argv[2]);
    } else {
        printf("too many argc %d\n", argc);
        return -1;
    }
    if (start < 0 || start > TOTAL_RK_LIST_TEST_COUNT || end < 0 || end > TOTAL_RK_LIST_TEST_COUNT) {
        printf("invalid input: start %d end %d\n", start, end);
        return -1;
    }
    for (i = start; i < end; i++) {
        int err = test_func[i]();
        if (err) {
            printf("test case %d return err %d\n", i, err);
            break;
        }
    }
    return 0;
}
#endif