-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchry_mempool.c
125 lines (103 loc) · 3.18 KB
/
chry_mempool.c
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
/*
* Copyright (c) 2024, sakumisu
*
* SPDX-License-Identifier: Apache-2.0
*/
#include "chry_mempool.h"
int chry_mempool_create(struct chry_mempool *pool, void *block, uint32_t block_size, uint32_t block_count)
{
uintptr_t addr;
uint8_t *ringbuf1;
uint8_t *ringbuf2;
ringbuf1 = chry_mempool_osal_malloc(sizeof(uintptr_t) * block_count);
if (ringbuf1 == NULL) {
return -1;
}
memset(ringbuf1, 0, sizeof(uintptr_t) * block_count);
if (chry_ringbuffer_init(&pool->in, ringbuf1, sizeof(uintptr_t) * block_count) == -1) {
chry_mempool_osal_free(ringbuf1);
return -1;
}
ringbuf2 = chry_mempool_osal_malloc(sizeof(uintptr_t) * block_count);
if (ringbuf2 == NULL) {
chry_mempool_osal_free(ringbuf1);
return -1;
}
memset(ringbuf2, 0, sizeof(uintptr_t) * block_count);
if (chry_ringbuffer_init(&pool->out, ringbuf2, sizeof(uintptr_t) * block_count) == -1) {
chry_mempool_osal_free(ringbuf1);
chry_mempool_osal_free(ringbuf2);
return -1;
}
pool->out_sem = chry_mempool_osal_sem_create(block_count);
if (pool->out_sem == NULL) {
chry_mempool_osal_free(ringbuf1);
chry_mempool_osal_free(ringbuf2);
return -1;
}
pool->block = block;
pool->block_size = block_size;
pool->block_count = block_count;
for (uint32_t i = 0; i < block_count; i++) {
addr = ((uintptr_t)block + i * block_size);
chry_ringbuffer_write(&pool->in, &addr, sizeof(uintptr_t));
}
return 0;
}
void chry_mempool_delete(struct chry_mempool *pool)
{
chry_mempool_osal_sem_delete(pool->out_sem);
chry_ringbuffer_reset(&pool->in);
chry_ringbuffer_reset(&pool->out);
chry_mempool_osal_free(pool->in.pool);
chry_mempool_osal_free(pool->out.pool);
}
uintptr_t *chry_mempool_alloc(struct chry_mempool *pool)
{
uintptr_t *addr;
uint32_t len;
len = chry_ringbuffer_read(&pool->in, (uintptr_t *)&addr, sizeof(uintptr_t));
if (len == 0) {
return NULL;
} else {
return addr;
}
}
int chry_mempool_free(struct chry_mempool *pool, uintptr_t *item)
{
uintptr_t addr;
addr = (uintptr_t)item;
return chry_ringbuffer_write(&pool->in, &addr, sizeof(uintptr_t));
}
int chry_mempool_send(struct chry_mempool *pool, uintptr_t *item)
{
uintptr_t addr;
addr = (uintptr_t)item;
chry_ringbuffer_write(&pool->out, &addr, sizeof(uintptr_t));
return chry_mempool_osal_sem_give(pool->out_sem);
}
int chry_mempool_recv(struct chry_mempool *pool, uintptr_t **item, uint32_t timeout)
{
uint32_t len;
int ret;
ret = chry_mempool_osal_sem_take(pool->out_sem, timeout);
if (ret < 0) {
return -1;
}
len = chry_ringbuffer_read(&pool->out, (uintptr_t *)item, sizeof(uintptr_t));
if (len == 0) {
return -1;
} else {
return 0;
}
}
void chry_mempool_reset(struct chry_mempool *pool)
{
uintptr_t addr;
chry_ringbuffer_reset(&pool->in);
chry_ringbuffer_reset(&pool->out);
for (uint32_t i = 0; i < pool->block_count; i++) {
addr = ((uintptr_t)pool->block + i * pool->block_size);
chry_ringbuffer_write(&pool->in, &addr, sizeof(uintptr_t));
}
}