diff --git a/Documentation/os/memory/index.rst b/Documentation/os/memory/index.rst index d12cc76a99a4e..31cc037a8cda9 100644 --- a/Documentation/os/memory/index.rst +++ b/Documentation/os/memory/index.rst @@ -74,35 +74,6 @@ parameter. In fact, the standard ``malloc()``, ``realloc()``, ``free()`` use this same mechanism, but with a global heap structure called ``g_mmheap``. -Heap Memory Pool Initial Allocation -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - -``mm_initialize_pool()`` can reserve backing chunks for small-allocation -pools. ``mm_initialize_pool_initial(config, init, initialsize)`` allows the -first backing chunk to differ from later chunks. It accepts the same heap -and pool configuration; passing ``NULL`` for ``init`` keeps the allocator's -default pool sizes and growth policy. The existing interface is equivalent -to passing zero for ``initialsize``. - -A positive initial size is rounded up to pointer alignment and enlarged to -fit the first request if necessary. It excludes the backing chunk header. -Subsequent chunks use the configured normal chunk size. When the normal chunk -size is smaller than the per-pool expansion size, the allocator requests each -allocation directly from the backing heap and ignores the initial size. -The lower-level ``mempool_multiple_init_initial()`` provides the same final -``initialsize`` argument for users of ``mempool_multiple_init()``. - -An independent filesystem heap can select this policy with -``CONFIG_FS_HEAP_MEMPOOL_INIT_CHUNK_SIZE``. Its default, zero, preserves the -normal ``CONFIG_MM_HEAP_MEMPOOL_CHUNK_SIZE`` for the first chunk. The option -requires ``CONFIG_FS_HEAPSIZE`` and heap memory pools with backing chunks. -It applies to both the standard allocator and TLSF, and does not change the -size of the filesystem heap or the policies of the user and kernel heaps. - -A smaller first chunk can leave a larger contiguous region available for -requests that bypass the pools. It can also cause earlier growth; it does -not guarantee lower peak memory use or better allocation throughput. - User/Kernel Heaps ~~~~~~~~~~~~~~~~~ diff --git a/fs/Kconfig b/fs/Kconfig index 58eb61b7cc5ef..4988d8d34fb1a 100644 --- a/fs/Kconfig +++ b/fs/Kconfig @@ -299,18 +299,11 @@ config FS_HEAPSIZE default 0 to use kmm directly. independent heap disabled config FS_HEAP_MEMPOOL_INIT_CHUNK_SIZE - int "FS heap initial mempool chunk size" + int "FS heap mempool init chunk size" default 0 - range 0 2147483647 - depends on FS_HEAPSIZE > 0 && MM_HEAP_MEMPOOL_THRESHOLD > 0 - depends on MM_HEAP_MEMPOOL_CHUNK_SIZE >= MM_HEAP_MEMPOOL_EXPAND_SIZE + depends on FS_HEAPSIZE > 0 ---help--- - Payload bytes in the first backing allocation of the FS heap pool. - Zero preserves the normal chunk size. A positive value is rounded - up to pointer alignment and enlarged for the first request if needed. - Later allocations retain MM_HEAP_MEMPOOL_CHUNK_SIZE. A smaller - initial chunk leaves more of the FS heap available for large requests, - but can cause the pool to grow sooner. This does not resize FS_HEAPSIZE. + Size of the mempool allocated when fs heap init. config FS_HEAPBUF_SECTION string "FS heap use Userheap section" diff --git a/fs/fs_heap.c b/fs/fs_heap.c index f7dd9aaf63d0a..aabac8adfb715 100644 --- a/fs/fs_heap.c +++ b/fs/fs_heap.c @@ -43,6 +43,8 @@ static FAR struct mm_heap_s *g_fs_heap; void fs_heap_initialize(void) { struct mm_heap_config_s config; + struct mm_pool_config_s poolconfig; + #ifdef FS_HEAPBUF_SECTION static uint8_t buf[CONFIG_FS_HEAPSIZE] locate_data(CONFIG_FS_HEAPBUF_SECTION); @@ -55,12 +57,9 @@ void fs_heap_initialize(void) config.name = "heapfs"; config.start = buf; config.size = CONFIG_FS_HEAPSIZE; -#if CONFIG_FS_HEAP_MEMPOOL_INIT_CHUNK_SIZE > 0 - g_fs_heap = mm_initialize_pool_initial(&config, NULL, - CONFIG_FS_HEAP_MEMPOOL_INIT_CHUNK_SIZE); -#else - g_fs_heap = mm_initialize_pool(&config, NULL); -#endif + memset(&poolconfig, 0, sizeof(struct mm_pool_config_s)); + poolconfig.init_chunksize = CONFIG_FS_HEAP_MEMPOOL_INIT_CHUNK_SIZE; + g_fs_heap = mm_initialize_pool(&config, &poolconfig); } FAR void *fs_heap_zalloc(size_t size) diff --git a/include/nuttx/mm/mempool.h b/include/nuttx/mm/mempool.h index 4e3e9bceff023..df12927f55247 100644 --- a/include/nuttx/mm/mempool.h +++ b/include/nuttx/mm/mempool.h @@ -402,6 +402,7 @@ void mempool_procfs_unregister(FAR struct mempool_procfs_entry_s *entry); * free - The free memory function for multiples pool. * arg - The alloc & free memory functions used arg. * chunksize - The multiples pool chunk size. + * init_chunksize - The multiples pool initial chunk size. * expandsize - The expend memory for all pools in multiples pool. * dict_expendsize - The expend size for multiple dictnoary. * Returned Value: @@ -418,30 +419,8 @@ mempool_multiple_init(FAR const char *name, mempool_multiple_alloc_t alloc, mempool_multiple_alloc_size_t alloc_size, mempool_multiple_free_t free, FAR void *arg, - size_t chunksize, size_t expandsize, - size_t dict_expendsize); - -/**************************************************************************** - * Name: mempool_multiple_init_initial - * - * Description: - * A smaller first backing chunk can leave more memory available to other - * users of the backing heap without reducing later growth increments. - * The parameters match mempool_multiple_init, with an additional - * initialsize. Zero selects chunksize. A positive value is rounded up to - * pointer alignment and increased if needed for the first request. - * It is ignored when chunksize is smaller than expandsize. - * - ****************************************************************************/ - -FAR struct mempool_multiple_s * -mempool_multiple_init_initial(FAR const char *name, - FAR const size_t *poolsize, size_t npools, - mempool_multiple_alloc_t alloc, - mempool_multiple_alloc_size_t alloc_size, - mempool_multiple_free_t free, FAR void *arg, - size_t chunksize, size_t expandsize, - size_t dict_expendsize, size_t initialsize); + size_t chunksize, size_t init_chunksize, + size_t expandsize, size_t dict_expendsize); /**************************************************************************** * Name: mempool_multiple_alloc diff --git a/include/nuttx/mm/mm.h b/include/nuttx/mm/mm.h index 4125e17c5e6df..1bd671b97d30b 100644 --- a/include/nuttx/mm/mm.h +++ b/include/nuttx/mm/mm.h @@ -221,12 +221,13 @@ struct mm_heap_config_s bool allocheap; }; -struct mempool_init_s +struct mm_pool_config_s { FAR const size_t *poolsize; size_t npools; size_t threshold; size_t chunksize; + size_t init_chunksize; size_t expandsize; size_t dict_expendsize; }; @@ -307,17 +308,10 @@ mm_initialize(FAR const char *name, FAR void *heapstart, size_t heapsize) #ifdef CONFIG_MM_HEAP_MEMPOOL FAR struct mm_heap_s * mm_initialize_pool(FAR const struct mm_heap_config_s *config, - FAR const struct mempool_init_s *init); - -FAR struct mm_heap_s * -mm_initialize_pool_initial(FAR const struct mm_heap_config_s *config, - FAR const struct mempool_init_s *init, - size_t initialsize); + FAR const struct mm_pool_config_s *poolconfig); #else -# define mm_initialize_pool(config, init) mm_initialize_heap(config) -# define mm_initialize_pool_initial(config, init, initialsize) \ - mm_initialize_heap(config) +# define mm_initialize_pool(config, poolconfig) mm_initialize_heap(config) #endif void mm_addregion(FAR struct mm_heap_s *heap, FAR void *heapstart, diff --git a/mm/Kconfig b/mm/Kconfig index 42ba105ac2e33..e62df7dc74206 100644 --- a/mm/Kconfig +++ b/mm/Kconfig @@ -255,6 +255,14 @@ config MM_HEAP_MEMPOOL_CHUNK_SIZE ---help--- This size describes the multiple mempool chunk size. +config MM_HEAP_MEMPOOL_INIT_CHUNK_SIZE + int "The multiples pool chunk initial size" + default MM_HEAP_MEMPOOL_CHUNK_SIZE + ---help--- + This size describes the multiples pool chunk initial size. + Set the size of the first allocation when mempool is initialized + to avoid fragmentation caused by multiple allocations + config MM_MIN_BLKSIZE int "Minimum memory block size" default 0 diff --git a/mm/mempool/mempool_multiple.c b/mm/mempool/mempool_multiple.c index 172f9cb7c6648..2dbb44b315cdc 100644 --- a/mm/mempool/mempool_multiple.c +++ b/mm/mempool/mempool_multiple.c @@ -25,7 +25,6 @@ ****************************************************************************/ #include -#include #include #include #include @@ -89,8 +88,8 @@ struct mempool_multiple_s rmutex_t lock; sq_queue_t chunk_queue; - size_t chunk_size; - size_t initial_size; + size_t chunksize; + size_t init_chunksize; size_t dict_used; size_t dict_col_num_log2; size_t dict_row_num; @@ -152,11 +151,11 @@ mempool_multiple_alloc_chunk(FAR struct mempool_multiple_s *mpool, size_t align, size_t size) { FAR struct mpool_chunk_s *chunk; + size_t chunksize; FAR char *tmp; FAR void *ret; - size_t chunksize; - if (mpool->chunk_size < mpool->expandsize) + if (mpool->chunksize < mpool->expandsize) { ret = mpool->alloc(mpool->arg, align, size); if (ret) @@ -171,8 +170,8 @@ mempool_multiple_alloc_chunk(FAR struct mempool_multiple_s *mpool, if (chunk == NULL) { retry: - chunksize = mpool->initial_size != 0 ? - MAX(mpool->initial_size, size) : mpool->chunk_size; + chunksize = mpool->init_chunksize != mpool->chunksize ? + mpool->init_chunksize : mpool->chunksize; tmp = mpool->alloc(mpool->arg, mpool->expandsize, chunksize + sizeof(struct mpool_chunk_s)); @@ -181,7 +180,7 @@ mempool_multiple_alloc_chunk(FAR struct mempool_multiple_s *mpool, return NULL; } - mpool->initial_size = 0; + mpool->init_chunksize = mpool->chunksize; mpool->alloced += mpool->alloc_size(mpool->arg, tmp); chunk = (FAR struct mpool_chunk_s *)(tmp + chunksize); chunk->end = tmp + chunksize; @@ -192,11 +191,7 @@ mempool_multiple_alloc_chunk(FAR struct mempool_multiple_s *mpool, } ret = (FAR void *)ALIGN_UP((uintptr_t)chunk->next, align); - - /* A small first chunk may end before the next aligned address. */ - - if ((uintptr_t)ret > (uintptr_t)chunk->end || - (uintptr_t)chunk->end - (uintptr_t)ret < size) + if ((uintptr_t)chunk->end - (uintptr_t)ret < size) { goto retry; } @@ -213,7 +208,7 @@ mempool_multiple_free_chunk(FAR struct mempool_multiple_s *mpool, FAR struct mpool_chunk_s *chunk; FAR sq_entry_t *entry; - if (mpool->chunk_size < mpool->expandsize) + if (mpool->chunksize < mpool->expandsize) { mpool->free(mpool->arg, ptr); return; @@ -405,28 +400,10 @@ FAR struct mempool_multiple_s * mempool_multiple_init(FAR const char *name, FAR const size_t *poolsize, size_t npools, mempool_multiple_alloc_t alloc, - mempool_multiple_alloc_size_t alloc_size, + mempool_multiple_alloc_size_t allocsize, mempool_multiple_free_t free, FAR void *arg, - size_t chunksize, size_t expandsize, - size_t dict_expendsize) -{ - return mempool_multiple_init_initial(name, poolsize, npools, alloc, - alloc_size, free, arg, chunksize, - expandsize, dict_expendsize, 0); -} - -/**************************************************************************** - * Name: mempool_multiple_init_initial - ****************************************************************************/ - -FAR struct mempool_multiple_s * -mempool_multiple_init_initial(FAR const char *name, - FAR const size_t *poolsize, size_t npools, - mempool_multiple_alloc_t alloc, - mempool_multiple_alloc_size_t alloc_size, - mempool_multiple_free_t free, FAR void *arg, - size_t chunksize, size_t expandsize, - size_t dict_expendsize, size_t initialsize) + size_t chunksize, size_t init_chunksize, + size_t expandsize, size_t dict_expendsize) { FAR struct mempool_multiple_s *mpool; FAR struct mempool_s *pools; @@ -441,16 +418,6 @@ mempool_multiple_init_initial(FAR const char *name, return NULL; } - if (chunksize < expandsize) - { - initialsize = 0; - } - else if (initialsize > SIZE_MAX - sizeof(struct mpool_chunk_s) - - (sizeof(uintptr_t) - 1)) - { - return NULL; - } - maxpoolszie = poolsize[0]; minpoolsize = poolsize[0]; for (i = 0; i < npools; i++) @@ -479,14 +446,14 @@ mempool_multiple_init_initial(FAR const char *name, pools = (FAR struct mempool_s *) ((uintptr_t)mpool + sizeof(struct mempool_multiple_s)); - mpool->alloc_size = alloc_size; + mpool->alloc_size = allocsize; mpool->expandsize = expandsize; - mpool->chunk_size = chunksize; - mpool->initial_size = ALIGN_UP(initialsize, sizeof(uintptr_t)); + mpool->chunksize = chunksize; + mpool->init_chunksize = init_chunksize; mpool->alloc = alloc; mpool->free = free; mpool->arg = arg; - mpool->alloced = alloc_size(arg, mpool); + mpool->alloced = allocsize(arg, mpool); sq_init(&mpool->chunk_queue); mpool->pools = pools; mpool->npools = npools; @@ -808,7 +775,7 @@ mempool_multiple_mallinfo(FAR struct mempool_multiple_s *mpool) nxrmutex_lock(&mpool->lock); info.arena = mpool->alloced; - if (mpool->chunk_size >= mpool->expandsize) + if (mpool->chunksize >= mpool->expandsize) { FAR struct mpool_chunk_s *chunk; diff --git a/mm/mm_heap/mm_initialize.c b/mm/mm_heap/mm_initialize.c index 192d5c3271821..0aa91f4085c40 100644 --- a/mm/mm_heap/mm_initialize.c +++ b/mm/mm_heap/mm_initialize.c @@ -90,9 +90,12 @@ static FAR void *mempool_memalign(FAR void *arg, size_t alignment, return ret; } #else -# define mempool_memalign mm_memalign +# define mempool_memalign ((mempool_multiple_alloc_t)mm_memalign) #endif +# define mempool_malloc_size ((mempool_multiple_alloc_size_t)mm_malloc_size) +# define mempool_free ((mempool_multiple_free_t)mm_free) + /**************************************************************************** * Public Functions ****************************************************************************/ @@ -336,25 +339,26 @@ mm_initialize_heap(FAR const struct mm_heap_config_s *config) #ifdef CONFIG_MM_HEAP_MEMPOOL FAR struct mm_heap_s * mm_initialize_pool(FAR const struct mm_heap_config_s *config, - FAR const struct mempool_init_s *init) -{ - return mm_initialize_pool_initial(config, init, 0); -} - -FAR struct mm_heap_s * -mm_initialize_pool_initial(FAR const struct mm_heap_config_s *config, - FAR const struct mempool_init_s *init, - size_t initialsize) + FAR const struct mm_pool_config_s *poolconfig) { FAR struct mm_heap_s *heap; #if CONFIG_MM_HEAP_MEMPOOL_THRESHOLD > 0 size_t poolsize[MEMPOOL_NPOOLS]; - struct mempool_init_s def; +#endif + struct mm_pool_config_s def; - if (init == NULL) + if (poolconfig) + { + memcpy(&def, poolconfig, sizeof(struct mm_pool_config_s)); + } + else { - /* Initialize the multiple mempool default parameter */ + memset(&def, 0, sizeof(struct mm_pool_config_s)); + } +#if CONFIG_MM_HEAP_MEMPOOL_THRESHOLD > 0 + if (def.poolsize == 0 || def.npools == 0) + { int i; for (i = 0; i < MEMPOOL_NPOOLS; i++) @@ -362,14 +366,33 @@ mm_initialize_pool_initial(FAR const struct mm_heap_config_s *config, poolsize[i] = (i + 1 + MEMPOOL_SKIP) * MEMPOOL_STEP; } - def.poolsize = poolsize; - def.npools = MEMPOOL_NPOOLS; - def.threshold = CONFIG_MM_HEAP_MEMPOOL_THRESHOLD; - def.chunksize = CONFIG_MM_HEAP_MEMPOOL_CHUNK_SIZE; - def.expandsize = CONFIG_MM_HEAP_MEMPOOL_EXPAND_SIZE; - def.dict_expendsize = CONFIG_MM_HEAP_MEMPOOL_DICTIONARY_EXPAND_SIZE; + def.poolsize = poolsize; + def.npools = MEMPOOL_NPOOLS; + } - init = &def; + if (def.threshold == 0) + { + def.threshold = CONFIG_MM_HEAP_MEMPOOL_THRESHOLD; + } + + if (def.chunksize == 0) + { + def.chunksize = CONFIG_MM_HEAP_MEMPOOL_CHUNK_SIZE; + } + + if (def.init_chunksize == 0) + { + def.init_chunksize = CONFIG_MM_HEAP_MEMPOOL_INIT_CHUNK_SIZE; + } + + if (def.expandsize == 0) + { + def.expandsize = CONFIG_MM_HEAP_MEMPOOL_EXPAND_SIZE; + } + + if (def.dict_expendsize == 0) + { + def.dict_expendsize = CONFIG_MM_HEAP_MEMPOOL_DICTIONARY_EXPAND_SIZE; } #endif @@ -377,16 +400,20 @@ mm_initialize_pool_initial(FAR const struct mm_heap_config_s *config, /* Initialize the multiple mempool in heap */ - if (init != NULL && init->poolsize != NULL && init->npools != 0) + heap->mm_threshold = def.threshold; + if (def.poolsize != NULL && def.npools != 0) { - heap->mm_threshold = init->threshold; - heap->mm_mpool = mempool_multiple_init_initial(config->name, - init->poolsize, init->npools, - (mempool_multiple_alloc_t)mempool_memalign, - (mempool_multiple_alloc_size_t)mm_malloc_size, - (mempool_multiple_free_t)mm_free, heap, - init->chunksize, init->expandsize, - init->dict_expendsize, initialsize); + heap->mm_mpool = mempool_multiple_init(config->name, + def.poolsize, + def.npools, + mempool_memalign, + mempool_malloc_size, + mempool_free, + heap, + def.chunksize, + def.init_chunksize, + def.expandsize, + def.dict_expendsize); } return heap; diff --git a/mm/tlsf/mm_tlsf.c b/mm/tlsf/mm_tlsf.c index 83de0b8ceb1b9..5a405a727f594 100644 --- a/mm/tlsf/mm_tlsf.c +++ b/mm/tlsf/mm_tlsf.c @@ -547,9 +547,12 @@ static FAR void *mempool_memalign(FAR void *arg, size_t alignment, return ret; } #else -# define mempool_memalign mm_memalign +# define mempool_memalign ((mempool_multiple_alloc_t)mm_memalign) #endif +# define mempool_malloc_size ((mempool_multiple_alloc_size_t)mm_malloc_size) +# define mempool_free ((mempool_multiple_free_t)mm_free) + /**************************************************************************** * Name: mallinfo_handler ****************************************************************************/ @@ -1171,25 +1174,26 @@ mm_initialize_heap(FAR const struct mm_heap_config_s *config) #ifdef CONFIG_MM_HEAP_MEMPOOL FAR struct mm_heap_s * mm_initialize_pool(FAR const struct mm_heap_config_s *config, - FAR const struct mempool_init_s *init) -{ - return mm_initialize_pool_initial(config, init, 0); -} - -FAR struct mm_heap_s * -mm_initialize_pool_initial(FAR const struct mm_heap_config_s *config, - FAR const struct mempool_init_s *init, - size_t initialsize) + FAR const struct mm_pool_config_s *poolconfig) { FAR struct mm_heap_s *heap; #if CONFIG_MM_HEAP_MEMPOOL_THRESHOLD > 0 size_t poolsize[MEMPOOL_NPOOLS]; - struct mempool_init_s def; +#endif + struct mm_pool_config_s def; - if (init == NULL) + if (poolconfig) { - /* Initialize the multiple mempool default parameter */ + memcpy(&def, poolconfig, sizeof(struct mm_pool_config_s)); + } + else + { + memset(&def, 0, sizeof(struct mm_pool_config_s)); + } +#if CONFIG_MM_HEAP_MEMPOOL_THRESHOLD > 0 + if (def.poolsize == 0 || def.npools == 0) + { int i; for (i = 0; i < MEMPOOL_NPOOLS; i++) @@ -1197,14 +1201,33 @@ mm_initialize_pool_initial(FAR const struct mm_heap_config_s *config, poolsize[i] = (i + 1 + MEMPOOL_SKIP) * MEMPOOL_STEP; } - def.poolsize = poolsize; - def.npools = MEMPOOL_NPOOLS; - def.threshold = CONFIG_MM_HEAP_MEMPOOL_THRESHOLD; - def.chunksize = CONFIG_MM_HEAP_MEMPOOL_CHUNK_SIZE; - def.expandsize = CONFIG_MM_HEAP_MEMPOOL_EXPAND_SIZE; - def.dict_expendsize = CONFIG_MM_HEAP_MEMPOOL_DICTIONARY_EXPAND_SIZE; + def.poolsize = poolsize; + def.npools = MEMPOOL_NPOOLS; + } - init = &def; + if (def.threshold == 0) + { + def.threshold = CONFIG_MM_HEAP_MEMPOOL_THRESHOLD; + } + + if (def.chunksize == 0) + { + def.chunksize = CONFIG_MM_HEAP_MEMPOOL_CHUNK_SIZE; + } + + if (def.init_chunksize == 0) + { + def.init_chunksize = CONFIG_MM_HEAP_MEMPOOL_INIT_CHUNK_SIZE; + } + + if (def.expandsize == 0) + { + def.expandsize = CONFIG_MM_HEAP_MEMPOOL_EXPAND_SIZE; + } + + if (def.dict_expendsize == 0) + { + def.dict_expendsize = CONFIG_MM_HEAP_MEMPOOL_DICTIONARY_EXPAND_SIZE; } #endif @@ -1212,16 +1235,20 @@ mm_initialize_pool_initial(FAR const struct mm_heap_config_s *config, /* Initialize the multiple mempool in heap */ - if (init != NULL && init->poolsize != NULL && init->npools != 0) - { - heap->mm_threshold = init->threshold; - heap->mm_mpool = mempool_multiple_init_initial(config->name, - init->poolsize, init->npools, - (mempool_multiple_alloc_t)mempool_memalign, - (mempool_multiple_alloc_size_t)mm_malloc_size, - (mempool_multiple_free_t)mm_free, heap, - init->chunksize, init->expandsize, - init->dict_expendsize, initialsize); + heap->mm_threshold = def.threshold; + if (def.poolsize != NULL && def.npools != 0) + { + heap->mm_mpool = mempool_multiple_init(config->name, + def.poolsize, + def.npools, + mempool_memalign, + mempool_malloc_size, + mempool_free, + heap, + def.chunksize, + def.init_chunksize, + def.expandsize, + def.dict_expendsize); } return heap;