mirror of
https://github.com/sakumisu/CherryUSB.git
synced 2025-05-08 07:59:31 +08:00
update(cherrymp): remove malloc free
Signed-off-by: sakumisu <1203593632@qq.com>
This commit is contained in:
parent
e581097309
commit
4d5aa30778
42
third_party/cherrymp/chry_mempool.c
vendored
42
third_party/cherrymp/chry_mempool.c
vendored
@ -7,38 +7,26 @@
|
|||||||
|
|
||||||
int chry_mempool_create(struct chry_mempool *pool, void *block, uint32_t block_size, uint32_t block_count)
|
int chry_mempool_create(struct chry_mempool *pool, void *block, uint32_t block_size, uint32_t block_count)
|
||||||
{
|
{
|
||||||
uintptr_t addr;
|
uintptr_t *item;
|
||||||
uint8_t *ringbuf1;
|
|
||||||
uint8_t *ringbuf2;
|
|
||||||
|
|
||||||
ringbuf1 = chry_mempool_osal_malloc(sizeof(uintptr_t) * block_count);
|
if (block_count > CONFIG_CHRY_MEMPOOL_MAX_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;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
ringbuf2 = chry_mempool_osal_malloc(sizeof(uintptr_t) * block_count);
|
if (pool->block_size % 4) {
|
||||||
if (ringbuf2 == NULL) {
|
|
||||||
chry_mempool_osal_free(ringbuf1);
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
memset(ringbuf2, 0, sizeof(uintptr_t) * block_count);
|
|
||||||
|
|
||||||
if (chry_ringbuffer_init(&pool->out, ringbuf2, sizeof(uintptr_t) * block_count) == -1) {
|
if (chry_ringbuffer_init(&pool->in, pool->in_buf, sizeof(uintptr_t) * block_count) == -1) {
|
||||||
chry_mempool_osal_free(ringbuf1);
|
return -1;
|
||||||
chry_mempool_osal_free(ringbuf2);
|
}
|
||||||
|
|
||||||
|
if (chry_ringbuffer_init(&pool->out, pool->out_buf, sizeof(uintptr_t) * block_count) == -1) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
pool->out_sem = chry_mempool_osal_sem_create(block_count);
|
pool->out_sem = chry_mempool_osal_sem_create(block_count);
|
||||||
if (pool->out_sem == NULL) {
|
if (pool->out_sem == NULL) {
|
||||||
chry_mempool_osal_free(ringbuf1);
|
|
||||||
chry_mempool_osal_free(ringbuf2);
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -46,9 +34,9 @@ int chry_mempool_create(struct chry_mempool *pool, void *block, uint32_t block_s
|
|||||||
pool->block_size = block_size;
|
pool->block_size = block_size;
|
||||||
pool->block_count = block_count;
|
pool->block_count = block_count;
|
||||||
|
|
||||||
for (uint32_t i = 0; i < block_count; i++) {
|
for (uint32_t i = 0; i < pool->block_count; i++) {
|
||||||
addr = ((uintptr_t)block + i * block_size);
|
item = (uintptr_t *)((uint8_t *)pool->block + i * pool->block_size);
|
||||||
chry_ringbuffer_write(&pool->in, &addr, sizeof(uintptr_t));
|
chry_mempool_free(pool, item);
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
@ -59,8 +47,6 @@ void chry_mempool_delete(struct chry_mempool *pool)
|
|||||||
chry_mempool_osal_sem_delete(pool->out_sem);
|
chry_mempool_osal_sem_delete(pool->out_sem);
|
||||||
chry_ringbuffer_reset(&pool->in);
|
chry_ringbuffer_reset(&pool->in);
|
||||||
chry_ringbuffer_reset(&pool->out);
|
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 *chry_mempool_alloc(struct chry_mempool *pool)
|
||||||
@ -113,13 +99,13 @@ int chry_mempool_recv(struct chry_mempool *pool, uintptr_t **item, uint32_t time
|
|||||||
|
|
||||||
void chry_mempool_reset(struct chry_mempool *pool)
|
void chry_mempool_reset(struct chry_mempool *pool)
|
||||||
{
|
{
|
||||||
uintptr_t addr;
|
uintptr_t *item;
|
||||||
|
|
||||||
chry_ringbuffer_reset(&pool->in);
|
chry_ringbuffer_reset(&pool->in);
|
||||||
chry_ringbuffer_reset(&pool->out);
|
chry_ringbuffer_reset(&pool->out);
|
||||||
|
|
||||||
for (uint32_t i = 0; i < pool->block_count; i++) {
|
for (uint32_t i = 0; i < pool->block_count; i++) {
|
||||||
addr = ((uintptr_t)pool->block + i * pool->block_size);
|
item = (uintptr_t *)((uint8_t *)pool->block + i * pool->block_size);
|
||||||
chry_ringbuffer_write(&pool->in, &addr, sizeof(uintptr_t));
|
chry_mempool_free(pool, item);
|
||||||
}
|
}
|
||||||
}
|
}
|
8
third_party/cherrymp/chry_mempool.h
vendored
8
third_party/cherrymp/chry_mempool.h
vendored
@ -14,6 +14,10 @@
|
|||||||
|
|
||||||
typedef void *chry_mempool_osal_sem_t;
|
typedef void *chry_mempool_osal_sem_t;
|
||||||
|
|
||||||
|
#ifndef CONFIG_CHRY_MEMPOOL_MAX_BLOCK_COUNT
|
||||||
|
#define CONFIG_CHRY_MEMPOOL_MAX_BLOCK_COUNT 128
|
||||||
|
#endif
|
||||||
|
|
||||||
struct chry_mempool {
|
struct chry_mempool {
|
||||||
chry_ringbuffer_t in;
|
chry_ringbuffer_t in;
|
||||||
chry_ringbuffer_t out;
|
chry_ringbuffer_t out;
|
||||||
@ -22,6 +26,8 @@ struct chry_mempool {
|
|||||||
void *block;
|
void *block;
|
||||||
uint32_t block_size;
|
uint32_t block_size;
|
||||||
uint32_t block_count;
|
uint32_t block_count;
|
||||||
|
uint8_t in_buf[sizeof(uintptr_t) * CONFIG_CHRY_MEMPOOL_MAX_BLOCK_COUNT];
|
||||||
|
uint8_t out_buf[sizeof(uintptr_t) * CONFIG_CHRY_MEMPOOL_MAX_BLOCK_COUNT];
|
||||||
};
|
};
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
@ -32,8 +38,6 @@ chry_mempool_osal_sem_t chry_mempool_osal_sem_create(uint32_t max_count);
|
|||||||
void chry_mempool_osal_sem_delete(chry_mempool_osal_sem_t sem);
|
void chry_mempool_osal_sem_delete(chry_mempool_osal_sem_t sem);
|
||||||
int chry_mempool_osal_sem_take(chry_mempool_osal_sem_t sem, uint32_t timeout);
|
int chry_mempool_osal_sem_take(chry_mempool_osal_sem_t sem, uint32_t timeout);
|
||||||
int chry_mempool_osal_sem_give(chry_mempool_osal_sem_t sem);
|
int chry_mempool_osal_sem_give(chry_mempool_osal_sem_t sem);
|
||||||
void *chry_mempool_osal_malloc(size_t size);
|
|
||||||
void chry_mempool_osal_free(void *ptr);
|
|
||||||
|
|
||||||
int chry_mempool_create(struct chry_mempool *pool, void *block, uint32_t block_size, uint32_t block_count);
|
int chry_mempool_create(struct chry_mempool *pool, void *block, uint32_t block_size, uint32_t block_count);
|
||||||
uintptr_t *chry_mempool_alloc(struct chry_mempool *pool);
|
uintptr_t *chry_mempool_alloc(struct chry_mempool *pool);
|
||||||
|
@ -19,8 +19,15 @@ void chry_mempool_osal_sem_delete(chry_mempool_osal_sem_t sem)
|
|||||||
|
|
||||||
int chry_mempool_osal_sem_take(chry_mempool_osal_sem_t sem, uint32_t timeout)
|
int chry_mempool_osal_sem_take(chry_mempool_osal_sem_t sem, uint32_t timeout)
|
||||||
{
|
{
|
||||||
if (timeout == 0xffffffff) {
|
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
|
||||||
return (xSemaphoreTake((SemaphoreHandle_t)sem, portMAX_DELAY) == pdPASS) ? 0 : -1;
|
int ret;
|
||||||
|
|
||||||
|
if (xPortIsInsideInterrupt()) {
|
||||||
|
ret = xSemaphoreTakeFromISR((SemaphoreHandle_t)sem, &xHigherPriorityTaskWoken);
|
||||||
|
if (ret == pdPASS) {
|
||||||
|
portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
|
||||||
|
}
|
||||||
|
return (ret == pdPASS) ? 0 : -1;
|
||||||
} else {
|
} else {
|
||||||
return (xSemaphoreTake((SemaphoreHandle_t)sem, pdMS_TO_TICKS(timeout)) == pdPASS) ? 0 : -1;
|
return (xSemaphoreTake((SemaphoreHandle_t)sem, pdMS_TO_TICKS(timeout)) == pdPASS) ? 0 : -1;
|
||||||
}
|
}
|
||||||
@ -41,14 +48,4 @@ int chry_mempool_osal_sem_give(chry_mempool_osal_sem_t sem)
|
|||||||
}
|
}
|
||||||
|
|
||||||
return (ret == pdPASS) ? 0 : -1;
|
return (ret == pdPASS) ? 0 : -1;
|
||||||
}
|
|
||||||
|
|
||||||
void *chry_mempool_osal_malloc(size_t size)
|
|
||||||
{
|
|
||||||
return pvPortMalloc(size);
|
|
||||||
}
|
|
||||||
|
|
||||||
void chry_mempool_osal_free(void *ptr)
|
|
||||||
{
|
|
||||||
vPortFree(ptr);
|
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user