|
@@ -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;
|
|
|
|
|
- uint8_t *ringbuf1;
|
|
|
|
|
- uint8_t *ringbuf2;
|
|
|
|
|
|
|
+ uintptr_t *item;
|
|
|
|
|
|
|
|
- ringbuf1 = chry_mempool_osal_malloc(sizeof(uintptr_t) * block_count);
|
|
|
|
|
- if (ringbuf1 == NULL) {
|
|
|
|
|
|
|
+ if (block_count > CONFIG_CHRY_MEMPOOL_MAX_BLOCK_COUNT) {
|
|
|
return -1;
|
|
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 (pool->block_size % 4) {
|
|
|
return -1;
|
|
return -1;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- ringbuf2 = chry_mempool_osal_malloc(sizeof(uintptr_t) * block_count);
|
|
|
|
|
- if (ringbuf2 == NULL) {
|
|
|
|
|
- chry_mempool_osal_free(ringbuf1);
|
|
|
|
|
|
|
+ if (chry_ringbuffer_init(&pool->in, pool->in_buf, sizeof(uintptr_t) * block_count) == -1) {
|
|
|
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) {
|
|
|
|
|
- chry_mempool_osal_free(ringbuf1);
|
|
|
|
|
- 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++) {
|
|
|
|
|
- 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;
|
|
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);
|
|
|
|
|
- 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);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|