mm: add mm_initialize_pool, make pool more flexible

now allow enable pool for extra heap, and disable umm/kmm pool.

Signed-off-by: buxiasen <buxiasen@xiaomi.com>
This commit is contained in:
buxiasen 2024-06-18 14:56:16 +08:00 committed by Xiang Xiao
parent 50aeea2dc0
commit cf574fa466
5 changed files with 125 additions and 37 deletions

View File

@ -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);

View File

@ -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 */

View File

@ -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

View File

@ -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

View File

@ -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
}