mm/mempool: fix bug about size mismatch and binary find

Signed-off-by: dongjiuzhu1 <dongjiuzhu1@xiaomi.com>
This commit is contained in:
dongjiuzhu1 2022-09-16 16:55:09 +08:00 committed by Xiang Xiao
parent 5828e5bb88
commit 30bede7940
2 changed files with 40 additions and 28 deletions

View File

@ -99,19 +99,22 @@ int mempool_init(FAR struct mempool_s *pool, FAR const char *name)
sq_init(&pool->elist); sq_init(&pool->elist);
count = pool->ninitial + pool->ninterrupt; count = pool->ninitial + pool->ninterrupt;
base = mempool_malloc(pool, sizeof(*base) + if (count != 0)
pool->bsize * count);
if (base == NULL)
{ {
return -ENOMEM; base = mempool_malloc(pool, sizeof(*base) +
} pool->bsize * count);
if (base == NULL)
{
return -ENOMEM;
}
sq_addfirst(base, &pool->elist); sq_addfirst(base, &pool->elist);
mempool_add_list(&pool->ilist, base + 1, mempool_add_list(&pool->ilist, base + 1,
pool->ninterrupt, pool->bsize); pool->ninterrupt, pool->bsize);
mempool_add_list(&pool->list, (FAR char *)(base + 1) + mempool_add_list(&pool->list, (FAR char *)(base + 1) +
pool->ninterrupt * pool->bsize, pool->ninterrupt * pool->bsize,
pool->ninitial, pool->bsize); pool->ninitial, pool->bsize);
}
if (pool->wait && pool->nexpand == 0) if (pool->wait && pool->nexpand == 0)
{ {

View File

@ -40,28 +40,29 @@
static inline struct mempool_s * static inline struct mempool_s *
mempool_multiple_find(FAR struct mempool_multiple_s *mpool, size_t size) mempool_multiple_find(FAR struct mempool_multiple_s *mpool, size_t size)
{ {
FAR struct mempool_s *low = mpool->pools; size_t right = mpool->npools;
FAR struct mempool_s *mid; size_t left = 0;
size_t n = mpool->npools; size_t mid;
while (1) while (left < right)
{ {
n >>= 1; mid = (left + right) >> 1;
mid = low + n; if (mpool->pools[mid].bsize > size)
if (size > mid->bsize)
{ {
if (n == 0) right = mid;
{
return NULL;
}
low = ++mid;
} }
else if (size == mid->bsize || n == 0) else
{ {
return mid; left = mid + 1;
} }
} }
if (left == mpool->npools)
{
return NULL;
}
return &mpool->pools[left];
} }
/**************************************************************************** /****************************************************************************
@ -136,7 +137,11 @@ FAR void *mempool_multiple_alloc(FAR struct mempool_multiple_s *mpool,
FAR struct mempool_s *pool; FAR struct mempool_s *pool;
pool = mempool_multiple_find(mpool, size + SIZEOF_HEAD); pool = mempool_multiple_find(mpool, size + SIZEOF_HEAD);
DEBUGASSERT(pool != NULL); if (pool == NULL)
{
return NULL;
}
do do
{ {
FAR void *blk = mempool_alloc(pool); FAR void *blk = mempool_alloc(pool);
@ -266,7 +271,11 @@ FAR void *mempool_multiple_fixed_alloc(FAR struct mempool_multiple_s *mpool,
FAR struct mempool_s *pool; FAR struct mempool_s *pool;
pool = mempool_multiple_find(mpool, size); pool = mempool_multiple_find(mpool, size);
DEBUGASSERT(pool != NULL); if (pool == NULL)
{
return NULL;
}
return mempool_alloc(pool); return mempool_alloc(pool);
} }