mm/mempool: support specifying alloc and free function

Signed-off-by: dongjiuzhu1 <dongjiuzhu1@xiaomi.com>
This commit is contained in:
Jiuzhu Dong 2022-08-20 23:20:04 +08:00 committed by Xiang Xiao
parent a01f3beefd
commit 8994c8efa2
2 changed files with 37 additions and 3 deletions

View File

@ -36,6 +36,12 @@
* Public Types
****************************************************************************/
struct mempool_s;
typedef CODE void *(*mempool_alloc_t)(FAR struct mempool_s *pool,
size_t size);
typedef CODE void (*mempool_free_t)(FAR struct mempool_s *pool,
FAR void *addr);
#ifndef CONFIG_FS_PROCFS_EXCLUDE_MEMPOOL
struct mempool_procfs_entry_s
{
@ -53,6 +59,8 @@ struct mempool_s
size_t ninterrupt; /* The number of block in interrupt mempool */
size_t nexpand; /* The number of expand block every time for mempool */
bool wait; /* The flag of need to wait when mempool is empty */
mempool_alloc_t alloc; /* The alloc function for mempool */
mempool_free_t free; /* The free function for mempool */
/* Private data for memory pool */

View File

@ -41,6 +41,30 @@ static inline void mempool_add_list(FAR sq_queue_t *list, FAR void *base,
}
}
static inline FAR void *mempool_malloc(FAR struct mempool_s *pool, size_t size)
{
if (pool->alloc != NULL)
{
return pool->alloc(pool, size);
}
else
{
return kmm_malloc(size);
}
}
static inline void mempool_mfree(FAR struct mempool_s *pool, FAR void *addr)
{
if (pool->free != NULL)
{
return pool->free(pool, addr);
}
else
{
return kmm_free(addr);
}
}
/****************************************************************************
* Public Functions
****************************************************************************/
@ -81,7 +105,8 @@ int mempool_init(FAR struct mempool_s *pool, FAR const char *name)
{
FAR sq_entry_t *base;
base = kmm_malloc(sizeof(*base) + pool->bsize * count);
base = mempool_malloc(pool, sizeof(*base) +
pool->bsize * count);
if (base == NULL)
{
return -ENOMEM;
@ -152,7 +177,8 @@ retry:
spin_unlock_irqrestore(&pool->lock, flags);
if (pool->nexpand != 0)
{
blk = kmm_malloc(sizeof(*blk) + pool->bsize * pool->nexpand);
blk = mempool_malloc(pool, sizeof(*blk) + pool->bsize *
pool->nexpand);
if (blk == NULL)
{
return NULL;
@ -305,7 +331,7 @@ int mempool_deinit(FAR struct mempool_s *pool)
while ((blk = sq_remfirst(&pool->elist)) != NULL)
{
kmm_free(blk);
mempool_mfree(pool, blk);
}
if (pool->wait && pool->nexpand == 0)