diff --git a/include/nuttx/mm/mm.h b/include/nuttx/mm/mm.h index 63a83b9a1d..63fd379adf 100644 --- a/include/nuttx/mm/mm.h +++ b/include/nuttx/mm/mm.h @@ -153,6 +153,16 @@ struct mm_heap_s; /* Forward reference */ +struct mempool_init_s +{ + FAR const size_t *poolsize; + size_t npools; + size_t threshold; + size_t chunksize; + size_t expandsize; + size_t dict_expendsize; +}; + /**************************************************************************** * Public Data ****************************************************************************/ @@ -213,6 +223,18 @@ EXTERN FAR struct mm_heap_s *g_kmmheap; FAR struct mm_heap_s *mm_initialize(FAR const char *name, FAR void *heap_start, size_t heap_size); + +#ifdef CONFIG_MM_HEAP_MEMPOOL +FAR struct mm_heap_s * +mm_initialize_pool(FAR const char *name, + FAR void *heap_start, size_t heap_size, + FAR const struct mempool_init_s *init); + +#else +# define mm_initialize_pool(name, heap_start, heap_size, init) \ + mm_initialize(name, heap_start, heap_size) +#endif + void mm_addregion(FAR struct mm_heap_s *heap, FAR void *heapstart, size_t heapsize); void mm_uninitialize(FAR struct mm_heap_s *heap); diff --git a/mm/kmm_heap/kmm_initialize.c b/mm/kmm_heap/kmm_initialize.c index ac9623f904..6b5d4ac122 100644 --- a/mm/kmm_heap/kmm_initialize.c +++ b/mm/kmm_heap/kmm_initialize.c @@ -58,7 +58,7 @@ FAR struct mm_heap_s *g_kmmheap; void kmm_initialize(FAR void *heap_start, size_t heap_size) { - g_kmmheap = mm_initialize("Kmem", heap_start, heap_size); + g_kmmheap = mm_initialize_pool("Kmem", heap_start, heap_size, NULL); } #endif /* CONFIG_MM_KERNEL_HEAP */ diff --git a/mm/mm_heap/mm_initialize.c b/mm/mm_heap/mm_initialize.c index f868cf5679..86c13c5153 100644 --- a/mm/mm_heap/mm_initialize.c +++ b/mm/mm_heap/mm_initialize.c @@ -210,7 +210,6 @@ void mm_addregion(FAR struct mm_heap_s *heap, FAR void *heapstart, * * Input Parameters: * name - The heap procfs name - * heap - The selected heap * heapstart - Start of the initial heap region * heapsize - Size of the initial heap region * @@ -224,9 +223,6 @@ void mm_addregion(FAR struct mm_heap_s *heap, FAR void *heapstart, FAR struct mm_heap_s *mm_initialize(FAR const char *name, FAR void *heapstart, size_t heapsize) { -#if CONFIG_MM_HEAP_MEMPOOL_THRESHOLD > 0 - size_t poolsize[MEMPOOL_NPOOLS]; -#endif FAR struct mm_heap_s *heap; uintptr_t heap_adj; int i; @@ -286,26 +282,62 @@ FAR struct mm_heap_s *mm_initialize(FAR const char *name, # endif #endif - /* Initialize the multiple mempool in heap */ + return heap; +} + +#ifdef CONFIG_MM_HEAP_MEMPOOL +FAR struct mm_heap_s * +mm_initialize_pool(FAR const char *name, + FAR void *heap_start, size_t heap_size, + FAR const struct mempool_init_s *init) +{ + FAR struct mm_heap_s *heap; #if CONFIG_MM_HEAP_MEMPOOL_THRESHOLD > 0 - for (i = 0; i < MEMPOOL_NPOOLS; i++) - { - poolsize[i] = (i + 1) * MM_MIN_CHUNK; - } + size_t poolsize[MEMPOOL_NPOOLS]; + struct mempool_init_s def; - heap->mm_threshold = CONFIG_MM_HEAP_MEMPOOL_THRESHOLD; - heap->mm_mpool = mempool_multiple_init(name, poolsize, MEMPOOL_NPOOLS, - (mempool_multiple_alloc_t)mempool_memalign, - (mempool_multiple_alloc_size_t)mm_malloc_size, - (mempool_multiple_free_t)mm_free, heap, - CONFIG_MM_HEAP_MEMPOOL_CHUNK_SIZE, - CONFIG_MM_HEAP_MEMPOOL_EXPAND_SIZE, - CONFIG_MM_HEAP_MEMPOOL_DICTIONARY_EXPAND_SIZE); + if (init == NULL) + { + /* Initialize the multiple mempool default parameter */ + + int i; + + for (i = 0; i < MEMPOOL_NPOOLS; i++) + { + poolsize[i] = (i + 1) * MM_MIN_CHUNK; + } + + 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; + + init = &def; + } #endif + heap = mm_initialize(name, heap_start, heap_size); + + /* Initialize the multiple mempool in heap */ + + if (init != NULL && init->poolsize != NULL && init->npools != 0) + { + heap->mm_threshold = CONFIG_MM_HEAP_MEMPOOL_THRESHOLD; + heap->mm_mpool = mempool_multiple_init(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); + } + return heap; } +#endif /**************************************************************************** * Name: mm_uninitialize diff --git a/mm/tlsf/mm_tlsf.c b/mm/tlsf/mm_tlsf.c index 7389c0bcfe..32e9aa14cb 100644 --- a/mm/tlsf/mm_tlsf.c +++ b/mm/tlsf/mm_tlsf.c @@ -865,10 +865,6 @@ FAR struct mm_heap_s *mm_initialize(FAR const char *name, FAR void *heapstart, size_t heapsize) { FAR struct mm_heap_s *heap; -#if CONFIG_MM_HEAP_MEMPOOL_THRESHOLD > 0 - size_t poolsize[MEMPOOL_NPOOLS]; - int i; -#endif minfo("Heap: name=%s start=%p size=%zu\n", name, heapstart, heapsize); @@ -908,24 +904,62 @@ FAR struct mm_heap_s *mm_initialize(FAR const char *name, #endif #endif -#if CONFIG_MM_HEAP_MEMPOOL_THRESHOLD > 0 - for (i = 0; i < MEMPOOL_NPOOLS; i++) - { - poolsize[i] = (i + 1) * tlsf_align_size(); - } + return heap; +} - heap->mm_threshold = CONFIG_MM_HEAP_MEMPOOL_THRESHOLD; - heap->mm_mpool = mempool_multiple_init(name, poolsize, MEMPOOL_NPOOLS, - (mempool_multiple_alloc_t)mempool_memalign, - (mempool_multiple_alloc_size_t)mm_malloc_size, - (mempool_multiple_free_t)mm_free, heap, - CONFIG_MM_HEAP_MEMPOOL_CHUNK_SIZE, - CONFIG_MM_HEAP_MEMPOOL_EXPAND_SIZE, - CONFIG_MM_HEAP_MEMPOOL_DICTIONARY_EXPAND_SIZE); +#ifdef CONFIG_MM_HEAP_MEMPOOL +FAR struct mm_heap_s * +mm_initialize_pool(FAR const char *name, + FAR void *heap_start, size_t heap_size, + FAR const struct mempool_init_s *init) +{ + FAR struct mm_heap_s *heap; + +#if CONFIG_MM_HEAP_MEMPOOL_THRESHOLD > 0 + size_t poolsize[MEMPOOL_NPOOLS]; + struct mempool_init_s def; + + if (init == NULL) + { + /* Initialize the multiple mempool default parameter */ + + int i; + + for (i = 0; i < MEMPOOL_NPOOLS; i++) + { + poolsize[i] = (i + 1) * tlsf_align_size(); + } + + 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; + + init = &def; + } #endif + heap = mm_initialize(name, heap_start, heap_size); + + /* Initialize the multiple mempool in heap */ + + if (init != NULL && init->poolsize != NULL && init->npools != 0) + { + heap->mm_threshold = CONFIG_MM_HEAP_MEMPOOL_THRESHOLD; + heap->mm_mpool = mempool_multiple_init(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); + } + return heap; } +#endif /**************************************************************************** * Name: mm_mallinfo diff --git a/mm/umm_heap/umm_initialize.c b/mm/umm_heap/umm_initialize.c index f5c41d2c4f..cfad97d18f 100644 --- a/mm/umm_heap/umm_initialize.c +++ b/mm/umm_heap/umm_initialize.c @@ -84,9 +84,9 @@ void umm_initialize(FAR void *heap_start, size_t heap_size) { #ifdef CONFIG_BUILD_KERNEL - USR_HEAP = mm_initialize(NULL, heap_start, heap_size); + USR_HEAP = mm_initialize_pool(NULL, heap_start, heap_size, NULL); #else - USR_HEAP = mm_initialize("Umem", heap_start, heap_size); + USR_HEAP = mm_initialize_pool("Umem", heap_start, heap_size, NULL); #endif }