From 4d5aa30778756b369bcccce3513ddf48328850bb Mon Sep 17 00:00:00 2001 From: sakumisu <1203593632@qq.com> Date: Tue, 4 Mar 2025 17:12:41 +0800 Subject: [PATCH] update(cherrymp): remove malloc free Signed-off-by: sakumisu <1203593632@qq.com> --- third_party/cherrymp/chry_mempool.c | 42 +++++++------------ third_party/cherrymp/chry_mempool.h | 8 +++- .../cherrymp/chry_mempool_osal_freertos.c | 21 ++++------ 3 files changed, 29 insertions(+), 42 deletions(-) diff --git a/third_party/cherrymp/chry_mempool.c b/third_party/cherrymp/chry_mempool.c index 9cf078a..149c666 100644 --- a/third_party/cherrymp/chry_mempool.c +++ b/third_party/cherrymp/chry_mempool.c @@ -7,38 +7,26 @@ 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; + uintptr_t *item; - 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); + if (block_count > CONFIG_CHRY_MEMPOOL_MAX_BLOCK_COUNT) { return -1; } - ringbuf2 = chry_mempool_osal_malloc(sizeof(uintptr_t) * block_count); - if (ringbuf2 == NULL) { - chry_mempool_osal_free(ringbuf1); + if (pool->block_size % 4) { 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); + if (chry_ringbuffer_init(&pool->in, pool->in_buf, sizeof(uintptr_t) * block_count) == -1) { + return -1; + } + + if (chry_ringbuffer_init(&pool->out, pool->out_buf, sizeof(uintptr_t) * block_count) == -1) { 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; } @@ -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_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)); + for (uint32_t i = 0; i < pool->block_count; i++) { + item = (uintptr_t *)((uint8_t *)pool->block + i * pool->block_size); + chry_mempool_free(pool, item); } return 0; @@ -59,8 +47,6 @@ 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) @@ -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) { - uintptr_t addr; + uintptr_t *item; 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)); + item = (uintptr_t *)((uint8_t *)pool->block + i * pool->block_size); + chry_mempool_free(pool, item); } } \ No newline at end of file diff --git a/third_party/cherrymp/chry_mempool.h b/third_party/cherrymp/chry_mempool.h index 8a15bb2..06aa545 100644 --- a/third_party/cherrymp/chry_mempool.h +++ b/third_party/cherrymp/chry_mempool.h @@ -14,6 +14,10 @@ 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 { chry_ringbuffer_t in; chry_ringbuffer_t out; @@ -22,6 +26,8 @@ struct chry_mempool { void *block; uint32_t block_size; 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 @@ -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); 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); -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); uintptr_t *chry_mempool_alloc(struct chry_mempool *pool); diff --git a/third_party/cherrymp/chry_mempool_osal_freertos.c b/third_party/cherrymp/chry_mempool_osal_freertos.c index 21e77f5..1db9aa5 100644 --- a/third_party/cherrymp/chry_mempool_osal_freertos.c +++ b/third_party/cherrymp/chry_mempool_osal_freertos.c @@ -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) { - if (timeout == 0xffffffff) { - return (xSemaphoreTake((SemaphoreHandle_t)sem, portMAX_DELAY) == pdPASS) ? 0 : -1; + BaseType_t xHigherPriorityTaskWoken = pdFALSE; + int ret; + + if (xPortIsInsideInterrupt()) { + ret = xSemaphoreTakeFromISR((SemaphoreHandle_t)sem, &xHigherPriorityTaskWoken); + if (ret == pdPASS) { + portYIELD_FROM_ISR(xHigherPriorityTaskWoken); + } + return (ret == pdPASS) ? 0 : -1; } else { 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; -} - -void *chry_mempool_osal_malloc(size_t size) -{ - return pvPortMalloc(size); -} - -void chry_mempool_osal_free(void *ptr) -{ - vPortFree(ptr); } \ No newline at end of file