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);
count = pool->ninitial + pool->ninterrupt;
base = mempool_malloc(pool, sizeof(*base) +
pool->bsize * count);
if (base == NULL)
if (count != 0)
{
return -ENOMEM;
}
base = mempool_malloc(pool, sizeof(*base) +
pool->bsize * count);
if (base == NULL)
{
return -ENOMEM;
}
sq_addfirst(base, &pool->elist);
mempool_add_list(&pool->ilist, base + 1,
pool->ninterrupt, pool->bsize);
mempool_add_list(&pool->list, (FAR char *)(base + 1) +
pool->ninterrupt * pool->bsize,
pool->ninitial, pool->bsize);
sq_addfirst(base, &pool->elist);
mempool_add_list(&pool->ilist, base + 1,
pool->ninterrupt, pool->bsize);
mempool_add_list(&pool->list, (FAR char *)(base + 1) +
pool->ninterrupt * pool->bsize,
pool->ninitial, pool->bsize);
}
if (pool->wait && pool->nexpand == 0)
{

View File

@ -40,28 +40,29 @@
static inline struct mempool_s *
mempool_multiple_find(FAR struct mempool_multiple_s *mpool, size_t size)
{
FAR struct mempool_s *low = mpool->pools;
FAR struct mempool_s *mid;
size_t n = mpool->npools;
size_t right = mpool->npools;
size_t left = 0;
size_t mid;
while (1)
while (left < right)
{
n >>= 1;
mid = low + n;
if (size > mid->bsize)
mid = (left + right) >> 1;
if (mpool->pools[mid].bsize > size)
{
if (n == 0)
{
return NULL;
}
low = ++mid;
right = 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;
pool = mempool_multiple_find(mpool, size + SIZEOF_HEAD);
DEBUGASSERT(pool != NULL);
if (pool == NULL)
{
return NULL;
}
do
{
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;
pool = mempool_multiple_find(mpool, size);
DEBUGASSERT(pool != NULL);
if (pool == NULL)
{
return NULL;
}
return mempool_alloc(pool);
}