diff options
| author | sakumisu <[email protected]> | 2025-07-18 22:50:48 +0800 |
|---|---|---|
| committer | sakumisu <[email protected]> | 2025-07-18 22:50:48 +0800 |
| commit | a95e06a9514542575f1f3da8e49806d5b864b7c0 (patch) | |
| tree | b59418b047e85e681cd61e82a624466487186e14 | |
| parent | 502c88084c3a3c4a0ad80daea8305e4ab38714ef (diff) | |
update(cherrymp): update local ringbuf
Signed-off-by: sakumisu <[email protected]>
| -rw-r--r-- | third_party/cherrymp/README.md | 2 | ||||
| -rw-r--r-- | third_party/cherrymp/chry_mempool.c | 152 | ||||
| -rw-r--r-- | third_party/cherrymp/chry_mempool.h | 13 | ||||
| -rw-r--r-- | third_party/cherrymp/chry_mempool_osal_nonos.c | 10 | ||||
| -rw-r--r-- | third_party/cherrymp/chry_mempool_osal_rtthread.c | 10 |
5 files changed, 152 insertions, 35 deletions
diff --git a/third_party/cherrymp/README.md b/third_party/cherrymp/README.md index 3e874229..50e983c4 100644 --- a/third_party/cherrymp/README.md +++ b/third_party/cherrymp/README.md @@ -1,3 +1,3 @@ # CherryMempool -CherryMempool is a tiny block memory pool based on CherryRB, support nonos or os(but we suggest you use in os), and only transfer data address not data content. +CherryMempool is a tiny block memory pool based on CherryRB, support nonos or os(but we suggest you use in os). diff --git a/third_party/cherrymp/chry_mempool.c b/third_party/cherrymp/chry_mempool.c index 149c6668..0f3f26aa 100644 --- a/third_party/cherrymp/chry_mempool.c +++ b/third_party/cherrymp/chry_mempool.c @@ -5,6 +5,138 @@ */ #include "chry_mempool.h" +/***************************************************************************** +* @brief init ringbuffer +* +* @param[in] rb ringbuffer instance +* @param[in] pool memory pool address +* @param[in] size memory size in byte, +* must be power of 2 !!! +* +* @retval int 0:Success -1:Error +*****************************************************************************/ +static int __chry_ringbuffer_init(chry_mempool_ringbuffer_t *rb, void *pool, uint32_t size) +{ + if (NULL == rb) { + return -1; + } + + if (NULL == pool) { + return -1; + } + + if ((size < 2) || (size & (size - 1))) { + return -1; + } + + rb->in = 0; + rb->out = 0; + rb->mask = size - 1; + rb->pool = pool; + + return 0; +} + +/***************************************************************************** +* @brief reset ringbuffer, clean all data, +* should be add lock in multithread +* +* @param[in] rb ringbuffer instance +* +*****************************************************************************/ +static void __chry_ringbuffer_reset(chry_mempool_ringbuffer_t *rb) +{ + rb->in = 0; + rb->out = 0; +} + +/***************************************************************************** +* @brief write data to ringbuffer, +* should be add lock in multithread, +* in single write thread not need lock +* +* @param[in] rb ringbuffer instance +* @param[in] data data pointer +* @param[in] size size in byte +* +* @retval uint32_t actual write size in byte +*****************************************************************************/ +static uint32_t __chry_ringbuffer_write(chry_mempool_ringbuffer_t *rb, void *data, uint32_t size) +{ + uint32_t unused; + uint32_t offset; + uint32_t remain; + + unused = (rb->mask + 1) - (rb->in - rb->out); + + if (size > unused) { + size = unused; + } + + offset = rb->in & rb->mask; + + remain = rb->mask + 1 - offset; + remain = remain > size ? size : remain; + + memcpy(((uint8_t *)(rb->pool)) + offset, data, remain); + memcpy(rb->pool, (uint8_t *)data + remain, size - remain); + + rb->in += size; + + return size; +} + +/***************************************************************************** +* @brief peek data from ringbuffer +* should be add lock in multithread, +* in single read thread not need lock +* +* @param[in] rb ringbuffer instance +* @param[in] data data pointer +* @param[in] size size in byte +* +* @retval uint32_t actual peek size in byte +*****************************************************************************/ +static uint32_t __chry_ringbuffer_peek(chry_mempool_ringbuffer_t *rb, void *data, uint32_t size) +{ + uint32_t used; + uint32_t offset; + uint32_t remain; + + used = rb->in - rb->out; + if (size > used) { + size = used; + } + + offset = rb->out & rb->mask; + + remain = rb->mask + 1 - offset; + remain = remain > size ? size : remain; + + memcpy(data, ((uint8_t *)(rb->pool)) + offset, remain); + memcpy((uint8_t *)data + remain, rb->pool, size - remain); + + return size; +} + +/***************************************************************************** +* @brief read data from ringbuffer +* should be add lock in multithread, +* in single read thread not need lock +* +* @param[in] rb ringbuffer instance +* @param[in] data data pointer +* @param[in] size size in byte +* +* @retval uint32_t actual read size in byte +*****************************************************************************/ +static uint32_t __chry_ringbuffer_read(chry_mempool_ringbuffer_t *rb, void *data, uint32_t size) +{ + size = __chry_ringbuffer_peek(rb, data, size); + rb->out += size; + return size; +} + int chry_mempool_create(struct chry_mempool *pool, void *block, uint32_t block_size, uint32_t block_count) { uintptr_t *item; @@ -17,11 +149,11 @@ int chry_mempool_create(struct chry_mempool *pool, void *block, uint32_t block_s return -1; } - if (chry_ringbuffer_init(&pool->in, pool->in_buf, sizeof(uintptr_t) * block_count) == -1) { + 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) { + if (__chry_ringbuffer_init(&pool->out, pool->out_buf, sizeof(uintptr_t) * block_count) == -1) { return -1; } @@ -45,8 +177,8 @@ int chry_mempool_create(struct chry_mempool *pool, void *block, uint32_t block_s 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_ringbuffer_reset(&pool->in); + __chry_ringbuffer_reset(&pool->out); } uintptr_t *chry_mempool_alloc(struct chry_mempool *pool) @@ -54,7 +186,7 @@ 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)); + len = __chry_ringbuffer_read(&pool->in, (uintptr_t *)&addr, sizeof(uintptr_t)); if (len == 0) { return NULL; } else { @@ -67,7 +199,7 @@ 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)); + return __chry_ringbuffer_write(&pool->in, &addr, sizeof(uintptr_t)); } int chry_mempool_send(struct chry_mempool *pool, uintptr_t *item) @@ -75,7 +207,7 @@ 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)); + __chry_ringbuffer_write(&pool->out, &addr, sizeof(uintptr_t)); return chry_mempool_osal_sem_give(pool->out_sem); } @@ -89,7 +221,7 @@ int chry_mempool_recv(struct chry_mempool *pool, uintptr_t **item, uint32_t time return -1; } - len = chry_ringbuffer_read(&pool->out, (uintptr_t *)item, sizeof(uintptr_t)); + len = __chry_ringbuffer_read(&pool->out, (uintptr_t *)item, sizeof(uintptr_t)); if (len == 0) { return -1; } else { @@ -101,8 +233,8 @@ void chry_mempool_reset(struct chry_mempool *pool) { uintptr_t *item; - chry_ringbuffer_reset(&pool->in); - chry_ringbuffer_reset(&pool->out); + __chry_ringbuffer_reset(&pool->in); + __chry_ringbuffer_reset(&pool->out); for (uint32_t i = 0; i < pool->block_count; i++) { item = (uintptr_t *)((uint8_t *)pool->block + i * pool->block_size); diff --git a/third_party/cherrymp/chry_mempool.h b/third_party/cherrymp/chry_mempool.h index 06aa5450..1132454c 100644 --- a/third_party/cherrymp/chry_mempool.h +++ b/third_party/cherrymp/chry_mempool.h @@ -10,17 +10,22 @@ #include <string.h> #include <stdbool.h> -#include "chry_ringbuffer.h" - typedef void *chry_mempool_osal_sem_t; #ifndef CONFIG_CHRY_MEMPOOL_MAX_BLOCK_COUNT #define CONFIG_CHRY_MEMPOOL_MAX_BLOCK_COUNT 128 #endif +typedef struct { + uint32_t in; /*!< Define the write pointer. */ + uint32_t out; /*!< Define the read pointer. */ + uint32_t mask; /*!< Define the write and read pointer mask. */ + void *pool; /*!< Define the memory pointer. */ +} chry_mempool_ringbuffer_t; + struct chry_mempool { - chry_ringbuffer_t in; - chry_ringbuffer_t out; + chry_mempool_ringbuffer_t in; + chry_mempool_ringbuffer_t out; chry_mempool_osal_sem_t out_sem; void *block; diff --git a/third_party/cherrymp/chry_mempool_osal_nonos.c b/third_party/cherrymp/chry_mempool_osal_nonos.c index c61e977a..7bd319b3 100644 --- a/third_party/cherrymp/chry_mempool_osal_nonos.c +++ b/third_party/cherrymp/chry_mempool_osal_nonos.c @@ -23,14 +23,4 @@ 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) { return 0; -} - -void *chry_mempool_osal_malloc(size_t size) -{ - return malloc(size); -} - -void chry_mempool_osal_free(void *ptr) -{ - free(ptr); }
\ No newline at end of file diff --git a/third_party/cherrymp/chry_mempool_osal_rtthread.c b/third_party/cherrymp/chry_mempool_osal_rtthread.c index cfe8a529..86042e29 100644 --- a/third_party/cherrymp/chry_mempool_osal_rtthread.c +++ b/third_party/cherrymp/chry_mempool_osal_rtthread.c @@ -41,14 +41,4 @@ 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) { return (int)rt_sem_release((rt_sem_t)sem); -} - -void *chry_mempool_osal_malloc(size_t size) -{ - return rt_malloc(size); -} - -void chry_mempool_osal_free(void *ptr) -{ - rt_free(ptr); }
\ No newline at end of file |
