mm/mempool: optimize alloc/free performance

Signed-off-by: dongjiuzhu1 <dongjiuzhu1@xiaomi.com>
This commit is contained in:
dongjiuzhu1 2022-09-09 16:01:49 +08:00 committed by Xiang Xiao
parent 8994c8efa2
commit 4e179e8a85
2 changed files with 54 additions and 80 deletions

View file

@ -88,12 +88,10 @@ static inline void mempool_mfree(FAR struct mempool_s *pool, FAR void *addr)
int mempool_init(FAR struct mempool_s *pool, FAR const char *name)
{
FAR sq_entry_t *base;
size_t count;
if (pool == NULL || pool->bsize == 0)
{
return -EINVAL;
}
DEBUGASSERT(pool != NULL && pool->bsize != 0);
pool->nused = 0;
sq_init(&pool->list);
@ -101,10 +99,6 @@ int mempool_init(FAR struct mempool_s *pool, FAR const char *name)
sq_init(&pool->elist);
count = pool->ninitial + pool->ninterrupt;
if (count != 0)
{
FAR sq_entry_t *base;
base = mempool_malloc(pool, sizeof(*base) +
pool->bsize * count);
if (base == NULL)
@ -118,7 +112,6 @@ int mempool_init(FAR struct mempool_s *pool, FAR const char *name)
mempool_add_list(&pool->list, (FAR char *)(base + 1) +
pool->ninterrupt * pool->bsize,
pool->ninitial, pool->bsize);
}
if (pool->wait && pool->nexpand == 0)
{
@ -154,10 +147,7 @@ FAR void *mempool_alloc(FAR struct mempool_s *pool)
FAR sq_entry_t *blk;
irqstate_t flags;
if (pool == NULL)
{
return NULL;
}
DEBUGASSERT(pool != NULL);
retry:
flags = spin_lock_irqsave(&pool->lock);
@ -223,16 +213,16 @@ out_with_lock:
void mempool_free(FAR struct mempool_s *pool, FAR void *blk)
{
irqstate_t flags;
FAR char *base;
if (blk == NULL || pool == NULL)
{
return;
}
DEBUGASSERT(pool != NULL && blk != NULL);
flags = spin_lock_irqsave(&pool->lock);
if (pool->ninterrupt)
{
FAR char *base;
base = (FAR char *)(sq_peek(&pool->elist) + 1);
if (pool->ninterrupt && (FAR char *)blk >= base &&
if ((FAR char *)blk >= base &&
(FAR char *)blk < base + pool->ninterrupt * pool->bsize)
{
sq_addfirst(blk, &pool->ilist);
@ -241,6 +231,11 @@ void mempool_free(FAR struct mempool_s *pool, FAR void *blk)
{
sq_addfirst(blk, &pool->list);
}
}
else
{
sq_addfirst(blk, &pool->list);
}
pool->nused--;
spin_unlock_irqrestore(&pool->lock, flags);
@ -274,10 +269,7 @@ int mempool_info(FAR struct mempool_s *pool, FAR struct mempoolinfo_s *info)
{
irqstate_t flags;
if (pool == NULL || info == NULL)
{
return -EINVAL;
}
DEBUGASSERT(pool != NULL && info != NULL);
flags = spin_lock_irqsave(&pool->lock);
info->ordblks = sq_count(&pool->list);
@ -315,10 +307,7 @@ int mempool_deinit(FAR struct mempool_s *pool)
{
FAR sq_entry_t *blk;
if (pool == NULL)
{
return -EINVAL;
}
DEBUGASSERT(pool != NULL);
if (pool->nused != 0)
{

View file

@ -32,7 +32,6 @@
#define SIZEOF_HEAD sizeof(FAR struct mempool_s *)
#define MAX(a, b) ((a) > (b) ? (a) : (b))
/****************************************************************************
* Private Functions
****************************************************************************/
@ -57,7 +56,7 @@ mempool_multiple_find(FAR struct mempool_multiple_s *mpool, size_t size)
low = ++mid;
}
else if (n == 0 || size == mid->bsize)
else if (size == mid->bsize || n == 0)
{
return mid;
}
@ -92,10 +91,7 @@ int mempool_multiple_init(FAR struct mempool_multiple_s *mpool,
{
int i;
if (mpool == NULL || mpool->pools == NULL)
{
return -EINVAL;
}
DEBUGASSERT(mpool != NULL && mpool->pools != NULL);
for (i = 0; i < mpool->npools; i++)
{
@ -135,11 +131,11 @@ int mempool_multiple_init(FAR struct mempool_multiple_s *mpool,
FAR void *mempool_multiple_alloc(FAR struct mempool_multiple_s *mpool,
size_t size)
{
FAR struct mempool_s *end = mpool->pools + mpool->npools;
FAR struct mempool_s *pool;
pool = mempool_multiple_find(mpool, size + SIZEOF_HEAD);
if (pool != NULL)
{
DEBUGASSERT(pool != NULL);
do
{
FAR void *blk = mempool_alloc(pool);
@ -149,8 +145,7 @@ FAR void *mempool_multiple_alloc(FAR struct mempool_multiple_s *mpool,
return (FAR char *)blk + SIZEOF_HEAD;
}
}
while (++pool< mpool->pools + mpool->npools);
}
while (++pool< end);
return NULL;
}
@ -173,14 +168,12 @@ void mempool_multiple_free(FAR struct mempool_multiple_s *mpool,
FAR struct mempool_s *pool;
FAR void *mem;
if (blk != NULL)
{
DEBUGASSERT(mpool != NULL && blk != NULL);
mem = (FAR char *)blk - SIZEOF_HEAD;
pool = *(FAR struct mempool_s **)mem;
mempool_free(pool, mem);
}
}
/****************************************************************************
* Name: mempool_multiple_fixed_alloc
@ -205,14 +198,10 @@ FAR void *mempool_multiple_fixed_alloc(FAR struct mempool_multiple_s *mpool,
FAR struct mempool_s *pool;
pool = mempool_multiple_find(mpool, size);
if (pool != NULL)
{
DEBUGASSERT(pool != NULL);
return mempool_alloc(pool);
}
return NULL;
}
/****************************************************************************
* Name: mempool_multiple_fixed_free
*
@ -229,15 +218,14 @@ FAR void *mempool_multiple_fixed_alloc(FAR struct mempool_multiple_s *mpool,
void mempool_multiple_fixed_free(FAR struct mempool_multiple_s *mpool,
FAR void *blk, size_t size)
{
if (blk != NULL)
{
FAR struct mempool_s *pool = mempool_multiple_find(mpool, size);
if (pool != NULL)
{
FAR struct mempool_s *pool;
DEBUGASSERT(mpool != NULL && blk != NULL);
pool = mempool_multiple_find(mpool, size);
DEBUGASSERT(pool != NULL);
mempool_free(pool, blk);
}
}
}
/****************************************************************************
* Name: mempool_multiple_deinit
@ -257,10 +245,7 @@ int mempool_multiple_deinit(FAR struct mempool_multiple_s *mpool)
{
int i;
if (mpool == NULL)
{
return -EINVAL;
}
DEBUGASSERT(mpool != NULL);
for (i = 0; i < mpool->npools; i++)
{