From cd97134c7cd58f4cbfef24db51715bd806c01f04 Mon Sep 17 00:00:00 2001 From: dongjiuzhu1 Date: Fri, 28 Oct 2022 22:42:14 +0800 Subject: [PATCH] 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 --- include/nuttx/mm/mempool.h | 24 +++++++------- mm/mempool/mempool.c | 62 ++++++++++++++++++++--------------- mm/mempool/mempool_multiple.c | 12 +++---- 3 files changed, 53 insertions(+), 45 deletions(-) diff --git a/include/nuttx/mm/mempool.h b/include/nuttx/mm/mempool.h index 5c7e04fe48..acfeca8e3c 100644 --- a/include/nuttx/mm/mempool.h +++ b/include/nuttx/mm/mempool.h @@ -54,13 +54,13 @@ struct mempool_procfs_entry_s struct mempool_s { - size_t bsize; /* The size for every block in mempool */ - size_t ninitial; /* The initialize number of block in normal mempool */ - 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 */ + size_t blocksize; /* The size for every block in mempool */ + size_t initialsize; /* The initialize size in normal mempool */ + size_t interruptsize; /* The initialize size in interrupt 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 */ + mempool_alloc_t alloc; /* The alloc function for mempool */ + mempool_free_t free; /* The free function for mempool */ /* Private data for memory pool */ @@ -110,7 +110,7 @@ extern "C" * Description: * Initialize a memory pool. * The user needs to specify the initialization information of mempool - * including bsize, ninitial, nexpand, ninterrupt. + * including blocksize, initialsize, expandsize, interruptsize. * * Input Parameters: * 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. * * 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: * 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: * Initialize multiple memory pool, each element represents a memory pool. * The user needs to specify the initialization information of each mempool - * in the array, including bsize, ninitial, nexpand, ninterrupt, wait. - * These mempool will be initialized by mempool_init. The name of all - * mempool are "name". + * in the array, including blocksize, initialsize, expandsize, + * interruptsize, wait. These mempool will be initialized by mempool_init. + * The name of all mempool are "name". * * Input Parameters: * name - The name of memory pool. diff --git a/mm/mempool/mempool.c b/mm/mempool/mempool.c index 87e8030e84..0870f29f94 100644 --- a/mm/mempool/mempool.c +++ b/mm/mempool/mempool.c @@ -35,12 +35,12 @@ static inline void mempool_add_list(FAR struct list_node *list, FAR void *base, size_t nblks, - size_t bsize) + size_t blocksize) { while (nblks-- > 0) { 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: * Initialize a memory pool. * The user needs to specify the initialization information of mempool - * including bsize, ninitial, nexpand, ninterrupt. + * including blocksize, initialsize, expandsize, interruptsize. * * Input Parameters: * 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) { FAR struct list_node *base; + size_t ninterrupt; + size_t ninitial; size_t count; - DEBUGASSERT(pool != NULL && pool->bsize != 0); + DEBUGASSERT(pool != NULL && pool->blocksize != 0); pool->nused = 0; list_initialize(&pool->list); list_initialize(&pool->ilist); 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) { base = mempool_malloc(pool, sizeof(*base) + - pool->bsize * count); + pool->blocksize * count); if (base == NULL) { return -ENOMEM; @@ -114,14 +118,14 @@ int mempool_init(FAR struct mempool_s *pool, FAR const char *name) list_add_head(&pool->elist, base); mempool_add_list(&pool->ilist, base + 1, - pool->ninterrupt, pool->bsize); + ninterrupt, pool->blocksize); mempool_add_list(&pool->list, (FAR char *)(base + 1) + - pool->ninterrupt * pool->bsize, - pool->ninitial, pool->bsize); - kasan_poison(base + 1, pool->bsize * count); + ninterrupt * pool->blocksize, + ninitial, pool->blocksize); + 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); } @@ -140,7 +144,7 @@ int mempool_init(FAR struct mempool_s *pool, FAR const char *name) * Allocate an block from a specific memory pool. * * 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: * pool - Address of the memory pool to be used. @@ -173,20 +177,21 @@ retry: else { spin_unlock_irqrestore(&pool->lock, flags); - if (pool->nexpand != 0) + if (pool->expandsize != 0) { - blk = mempool_malloc(pool, sizeof(*blk) + pool->bsize * - pool->nexpand); + size_t nexpand = pool->expandsize / pool->blocksize; + blk = mempool_malloc(pool, sizeof(*blk) + pool->blocksize * + nexpand); if (blk == NULL) { return NULL; } - kasan_poison(blk + 1, pool->bsize * pool->nexpand); + kasan_poison(blk + 1, pool->blocksize * nexpand); flags = spin_lock_irqsave(&pool->lock); list_add_head(&pool->elist, blk); - mempool_add_list(&pool->list, blk + 1, pool->nexpand, - pool->bsize); + mempool_add_list(&pool->list, blk + 1, nexpand, + pool->blocksize); blk = list_remove_head(&pool->list); } else if (!pool->wait || @@ -202,7 +207,7 @@ retry: } pool->nused++; - kasan_unpoison(blk, pool->bsize); + kasan_unpoison(blk, pool->blocksize); out_with_lock: spin_unlock_irqrestore(&pool->lock, flags); return blk; @@ -226,13 +231,15 @@ void mempool_free(FAR struct mempool_s *pool, FAR void *blk) DEBUGASSERT(pool != NULL && blk != NULL); flags = spin_lock_irqsave(&pool->lock); - if (pool->ninterrupt) + if (pool->interruptsize != 0) { FAR char *base; + size_t ninterrupt; base = (FAR char *)(list_peek_head(&pool->elist) + 1); + ninterrupt = pool->interruptsize / pool->blocksize; 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); } @@ -247,9 +254,9 @@ void mempool_free(FAR struct mempool_s *pool, FAR void *blk) } pool->nused--; - kasan_poison(blk, pool->bsize); + kasan_poison(blk, pool->blocksize); spin_unlock_irqrestore(&pool->lock, flags); - if (pool->wait && pool->nexpand == 0) + if (pool->wait && pool->expandsize == 0) { 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->iordblks = list_length(&pool->ilist); 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); - info->sizeblks = pool->bsize; - if (pool->wait && pool->nexpand == 0) + info->sizeblks = pool->blocksize; + if (pool->wait && pool->expandsize == 0) { int semcount; @@ -334,7 +342,7 @@ int mempool_deinit(FAR struct mempool_s *pool) mempool_mfree(pool, blk); } - if (pool->wait && pool->nexpand == 0) + if (pool->wait && pool->expandsize == 0) { nxsem_destroy(&pool->waitsem); } diff --git a/mm/mempool/mempool_multiple.c b/mm/mempool/mempool_multiple.c index 62e2ecdea6..220149ea0b 100644 --- a/mm/mempool/mempool_multiple.c +++ b/mm/mempool/mempool_multiple.c @@ -47,7 +47,7 @@ mempool_multiple_find(FAR struct mempool_multiple_s *mpool, size_t size) while (left < right) { mid = (left + right) >> 1; - if (mpool->pools[mid].bsize > size) + if (mpool->pools[mid].blocksize > size) { right = mid; } @@ -75,9 +75,9 @@ mempool_multiple_find(FAR struct mempool_multiple_s *mpool, size_t size) * Description: * Initialize multiple memory pool, each element represents a memory pool. * The user needs to specify the initialization information of each mempool - * in the array, including bsize, ninitial, nexpand, ninterrupt, wait. - * These mempool will be initialized by mempool_init. The name of all - * mempool are "name". + * in the array, including blocksize, initialsize, expandsize, + * interruptsize, wait. These mempool will be initialized by mempool_init. + * The name of all mempool are "name". * * Input Parameters: * 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 **) ((FAR char *)oldblk - SIZEOF_HEAD); - memcpy(blk, oldblk, MIN(oldpool->bsize, size)); + memcpy(blk, oldblk, MIN(oldpool->blocksize, size)); mempool_multiple_free(mpool, oldblk); } @@ -245,7 +245,7 @@ size_t mempool_multiple_alloc_size(FAR void *blk) mem = (FAR char *)blk - SIZEOF_HEAD; pool = *(FAR struct mempool_s **)mem; - return pool->bsize; + return pool->blocksize; } /****************************************************************************