mm/mempool: update nexpand/ninitial/ninterrupt to xxxsize

Let's specify size instead of number, so that we can unify the size of
expansion memory in the multiple mempool.

Signed-off-by: dongjiuzhu1 <dongjiuzhu1@xiaomi.com>
This commit is contained in:
dongjiuzhu1
2022-10-28 22:42:14 +08:00
committed by Xiang Xiao
parent adaca6a5ce
commit cd97134c7c
3 changed files with 53 additions and 45 deletions
+12 -12
View File
@@ -54,13 +54,13 @@ struct mempool_procfs_entry_s
struct mempool_s struct mempool_s
{ {
size_t bsize; /* The size for every block in mempool */ size_t blocksize; /* The size for every block in mempool */
size_t ninitial; /* The initialize number of block in normal mempool */ size_t initialsize; /* The initialize size in normal mempool */
size_t ninterrupt; /* The number of block in interrupt mempool */ size_t interruptsize; /* The initialize size in interrupt mempool */
size_t nexpand; /* The number of expand block every time for mempool */ size_t expandsize; /* The size of expand block every time for mempool */
bool wait; /* The flag of need to wait when mempool is empty */ bool wait; /* The flag of need to wait when mempool is empty */
mempool_alloc_t alloc; /* The alloc function for mempool */ mempool_alloc_t alloc; /* The alloc function for mempool */
mempool_free_t free; /* The free function for mempool */ mempool_free_t free; /* The free function for mempool */
/* Private data for memory pool */ /* Private data for memory pool */
@@ -110,7 +110,7 @@ extern "C"
* Description: * Description:
* Initialize a memory pool. * Initialize a memory pool.
* The user needs to specify the initialization information of mempool * The user needs to specify the initialization information of mempool
* including bsize, ninitial, nexpand, ninterrupt. * including blocksize, initialsize, expandsize, interruptsize.
* *
* Input Parameters: * Input Parameters:
* pool - Address of the memory pool to be used. * pool - Address of the memory pool to be used.
@@ -130,7 +130,7 @@ int mempool_init(FAR struct mempool_s *pool, FAR const char *name);
* Allocate an block from a specific memory pool. * Allocate an block from a specific memory pool.
* *
* If there isn't enough memory blocks, This function will expand memory * If there isn't enough memory blocks, This function will expand memory
* pool if nexpand isn't zero. * pool if expandsize isn't zero.
* *
* Input Parameters: * Input Parameters:
* pool - Address of the memory pool to be used. * pool - Address of the memory pool to be used.
@@ -221,9 +221,9 @@ void mempool_procfs_unregister(FAR struct mempool_procfs_entry_s *entry);
* Description: * Description:
* Initialize multiple memory pool, each element represents a memory pool. * Initialize multiple memory pool, each element represents a memory pool.
* The user needs to specify the initialization information of each mempool * The user needs to specify the initialization information of each mempool
* in the array, including bsize, ninitial, nexpand, ninterrupt, wait. * in the array, including blocksize, initialsize, expandsize,
* These mempool will be initialized by mempool_init. The name of all * interruptsize, wait. These mempool will be initialized by mempool_init.
* mempool are "name". * The name of all mempool are "name".
* *
* Input Parameters: * Input Parameters:
* name - The name of memory pool. * name - The name of memory pool.
+35 -27
View File
@@ -35,12 +35,12 @@
static inline void mempool_add_list(FAR struct list_node *list, static inline void mempool_add_list(FAR struct list_node *list,
FAR void *base, size_t nblks, FAR void *base, size_t nblks,
size_t bsize) size_t blocksize)
{ {
while (nblks-- > 0) while (nblks-- > 0)
{ {
list_add_head(list, ((FAR struct list_node *)((FAR char *)base + list_add_head(list, ((FAR struct list_node *)((FAR char *)base +
bsize * nblks))); blocksize * nblks)));
} }
} }
@@ -79,7 +79,7 @@ static inline void mempool_mfree(FAR struct mempool_s *pool, FAR void *addr)
* Description: * Description:
* Initialize a memory pool. * Initialize a memory pool.
* The user needs to specify the initialization information of mempool * The user needs to specify the initialization information of mempool
* including bsize, ninitial, nexpand, ninterrupt. * including blocksize, initialsize, expandsize, interruptsize.
* *
* Input Parameters: * Input Parameters:
* pool - Address of the memory pool to be used. * pool - Address of the memory pool to be used.
@@ -93,20 +93,24 @@ 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) int mempool_init(FAR struct mempool_s *pool, FAR const char *name)
{ {
FAR struct list_node *base; FAR struct list_node *base;
size_t ninterrupt;
size_t ninitial;
size_t count; size_t count;
DEBUGASSERT(pool != NULL && pool->bsize != 0); DEBUGASSERT(pool != NULL && pool->blocksize != 0);
pool->nused = 0; pool->nused = 0;
list_initialize(&pool->list); list_initialize(&pool->list);
list_initialize(&pool->ilist); list_initialize(&pool->ilist);
list_initialize(&pool->elist); list_initialize(&pool->elist);
count = pool->ninitial + pool->ninterrupt; ninitial = pool->initialsize / pool->blocksize;
ninterrupt = pool->interruptsize / pool->blocksize;
count = ninitial + ninterrupt;
if (count != 0) if (count != 0)
{ {
base = mempool_malloc(pool, sizeof(*base) + base = mempool_malloc(pool, sizeof(*base) +
pool->bsize * count); pool->blocksize * count);
if (base == NULL) if (base == NULL)
{ {
return -ENOMEM; return -ENOMEM;
@@ -114,14 +118,14 @@ int mempool_init(FAR struct mempool_s *pool, FAR const char *name)
list_add_head(&pool->elist, base); list_add_head(&pool->elist, base);
mempool_add_list(&pool->ilist, base + 1, mempool_add_list(&pool->ilist, base + 1,
pool->ninterrupt, pool->bsize); ninterrupt, pool->blocksize);
mempool_add_list(&pool->list, (FAR char *)(base + 1) + mempool_add_list(&pool->list, (FAR char *)(base + 1) +
pool->ninterrupt * pool->bsize, ninterrupt * pool->blocksize,
pool->ninitial, pool->bsize); ninitial, pool->blocksize);
kasan_poison(base + 1, pool->bsize * count); kasan_poison(base + 1, pool->blocksize * count);
} }
if (pool->wait && pool->nexpand == 0) if (pool->wait && pool->expandsize == 0)
{ {
nxsem_init(&pool->waitsem, 0, 0); nxsem_init(&pool->waitsem, 0, 0);
} }
@@ -140,7 +144,7 @@ int mempool_init(FAR struct mempool_s *pool, FAR const char *name)
* Allocate an block from a specific memory pool. * Allocate an block from a specific memory pool.
* *
* If there isn't enough memory blocks, This function will expand memory * If there isn't enough memory blocks, This function will expand memory
* pool if nexpand isn't zero. * pool if expandsize isn't zero.
* *
* Input Parameters: * Input Parameters:
* pool - Address of the memory pool to be used. * pool - Address of the memory pool to be used.
@@ -173,20 +177,21 @@ retry:
else else
{ {
spin_unlock_irqrestore(&pool->lock, flags); spin_unlock_irqrestore(&pool->lock, flags);
if (pool->nexpand != 0) if (pool->expandsize != 0)
{ {
blk = mempool_malloc(pool, sizeof(*blk) + pool->bsize * size_t nexpand = pool->expandsize / pool->blocksize;
pool->nexpand); blk = mempool_malloc(pool, sizeof(*blk) + pool->blocksize *
nexpand);
if (blk == NULL) if (blk == NULL)
{ {
return NULL; return NULL;
} }
kasan_poison(blk + 1, pool->bsize * pool->nexpand); kasan_poison(blk + 1, pool->blocksize * nexpand);
flags = spin_lock_irqsave(&pool->lock); flags = spin_lock_irqsave(&pool->lock);
list_add_head(&pool->elist, blk); list_add_head(&pool->elist, blk);
mempool_add_list(&pool->list, blk + 1, pool->nexpand, mempool_add_list(&pool->list, blk + 1, nexpand,
pool->bsize); pool->blocksize);
blk = list_remove_head(&pool->list); blk = list_remove_head(&pool->list);
} }
else if (!pool->wait || else if (!pool->wait ||
@@ -202,7 +207,7 @@ retry:
} }
pool->nused++; pool->nused++;
kasan_unpoison(blk, pool->bsize); kasan_unpoison(blk, pool->blocksize);
out_with_lock: out_with_lock:
spin_unlock_irqrestore(&pool->lock, flags); spin_unlock_irqrestore(&pool->lock, flags);
return blk; return blk;
@@ -226,13 +231,15 @@ void mempool_free(FAR struct mempool_s *pool, FAR void *blk)
DEBUGASSERT(pool != NULL && blk != NULL); DEBUGASSERT(pool != NULL && blk != NULL);
flags = spin_lock_irqsave(&pool->lock); flags = spin_lock_irqsave(&pool->lock);
if (pool->ninterrupt) if (pool->interruptsize != 0)
{ {
FAR char *base; FAR char *base;
size_t ninterrupt;
base = (FAR char *)(list_peek_head(&pool->elist) + 1); base = (FAR char *)(list_peek_head(&pool->elist) + 1);
ninterrupt = pool->interruptsize / pool->blocksize;
if ((FAR char *)blk >= base && if ((FAR char *)blk >= base &&
(FAR char *)blk < base + pool->ninterrupt * pool->bsize) (FAR char *)blk < base + ninterrupt * pool->blocksize)
{ {
list_add_head(&pool->ilist, blk); list_add_head(&pool->ilist, blk);
} }
@@ -247,9 +254,9 @@ void mempool_free(FAR struct mempool_s *pool, FAR void *blk)
} }
pool->nused--; pool->nused--;
kasan_poison(blk, pool->bsize); kasan_poison(blk, pool->blocksize);
spin_unlock_irqrestore(&pool->lock, flags); spin_unlock_irqrestore(&pool->lock, flags);
if (pool->wait && pool->nexpand == 0) if (pool->wait && pool->expandsize == 0)
{ {
int semcount; int semcount;
@@ -285,10 +292,11 @@ int mempool_info(FAR struct mempool_s *pool, FAR struct mempoolinfo_s *info)
info->ordblks = list_length(&pool->list); info->ordblks = list_length(&pool->list);
info->iordblks = list_length(&pool->ilist); info->iordblks = list_length(&pool->ilist);
info->aordblks = pool->nused; info->aordblks = pool->nused;
info->arena = (pool->nused + info->ordblks + info->iordblks) * pool->bsize; info->arena = (pool->nused + info->ordblks + info->iordblks) *
pool->blocksize;
spin_unlock_irqrestore(&pool->lock, flags); spin_unlock_irqrestore(&pool->lock, flags);
info->sizeblks = pool->bsize; info->sizeblks = pool->blocksize;
if (pool->wait && pool->nexpand == 0) if (pool->wait && pool->expandsize == 0)
{ {
int semcount; int semcount;
@@ -334,7 +342,7 @@ int mempool_deinit(FAR struct mempool_s *pool)
mempool_mfree(pool, blk); mempool_mfree(pool, blk);
} }
if (pool->wait && pool->nexpand == 0) if (pool->wait && pool->expandsize == 0)
{ {
nxsem_destroy(&pool->waitsem); nxsem_destroy(&pool->waitsem);
} }
+6 -6
View File
@@ -47,7 +47,7 @@ mempool_multiple_find(FAR struct mempool_multiple_s *mpool, size_t size)
while (left < right) while (left < right)
{ {
mid = (left + right) >> 1; mid = (left + right) >> 1;
if (mpool->pools[mid].bsize > size) if (mpool->pools[mid].blocksize > size)
{ {
right = mid; right = mid;
} }
@@ -75,9 +75,9 @@ mempool_multiple_find(FAR struct mempool_multiple_s *mpool, size_t size)
* Description: * Description:
* Initialize multiple memory pool, each element represents a memory pool. * Initialize multiple memory pool, each element represents a memory pool.
* The user needs to specify the initialization information of each mempool * The user needs to specify the initialization information of each mempool
* in the array, including bsize, ninitial, nexpand, ninterrupt, wait. * in the array, including blocksize, initialsize, expandsize,
* These mempool will be initialized by mempool_init. The name of all * interruptsize, wait. These mempool will be initialized by mempool_init.
* mempool are "name". * The name of all mempool are "name".
* *
* Input Parameters: * Input Parameters:
* name - The name of memory pool. * name - The name of memory pool.
@@ -190,7 +190,7 @@ FAR void *mempool_multiple_realloc(FAR struct mempool_multiple_s *mpool,
oldpool = *(FAR struct mempool_s **) oldpool = *(FAR struct mempool_s **)
((FAR char *)oldblk - SIZEOF_HEAD); ((FAR char *)oldblk - SIZEOF_HEAD);
memcpy(blk, oldblk, MIN(oldpool->bsize, size)); memcpy(blk, oldblk, MIN(oldpool->blocksize, size));
mempool_multiple_free(mpool, oldblk); mempool_multiple_free(mpool, oldblk);
} }
@@ -245,7 +245,7 @@ size_t mempool_multiple_alloc_size(FAR void *blk)
mem = (FAR char *)blk - SIZEOF_HEAD; mem = (FAR char *)blk - SIZEOF_HEAD;
pool = *(FAR struct mempool_s **)mem; pool = *(FAR struct mempool_s **)mem;
return pool->bsize; return pool->blocksize;
} }
/**************************************************************************** /****************************************************************************