mm/mm_heap: use struct mm_heap_config_s to init the memory heap

To avoid add new parameters to the mm_initialize_heap() and
mm_initialize_pool()

Signed-off-by: Bowen Wang <wangbowen6@xiaomi.com>
This commit is contained in:
Bowen Wang
2025-05-24 11:47:36 +08:00
committed by GUIDINGLI
parent 32f504c8f5
commit e686a3ac42
8 changed files with 90 additions and 52 deletions
+9 -9
View File
@@ -205,12 +205,7 @@ static void mm_delayfree(struct mm_heap_s *heap, void *mem, bool delay)
* heap region. * heap region.
* *
* Input Parameters: * Input Parameters:
* heap - If heap is NULL, will use heapstart initialize heap context, * config - The heap config structure
* otherwise, will use heap alloc a heap context, caller need
* free it after mm_uninitialize.
* name - The heap procfs name
* heapstart - Start of the initial heap region
* heapsize - Size of the initial heap region
* *
* Returned Value: * Returned Value:
* Return the address of a new heap instance. * Return the address of a new heap instance.
@@ -219,10 +214,13 @@ static void mm_delayfree(struct mm_heap_s *heap, void *mem, bool delay)
* *
****************************************************************************/ ****************************************************************************/
struct mm_heap_s * struct mm_heap_s *mm_initialize_heap(const struct mm_heap_config_s *config)
mm_initialize_heap(struct mm_heap_s *heap, const char *name,
void *heapstart, size_t heapsize)
{ {
struct mm_heap_s *heap = config->heap;
const char *name = config->name;
void *heap_start = config->start;
size_t heap_size = config->size;
if (heap == NULL) if (heap == NULL)
{ {
heap = host_memalign(sizeof(void *), sizeof(*heap)); heap = host_memalign(sizeof(void *), sizeof(*heap));
@@ -246,6 +244,8 @@ mm_initialize_heap(struct mm_heap_s *heap, const char *name,
#endif #endif
sched_note_heap(NOTE_HEAP_ADD, heap, heap_start, heap_size, 0); sched_note_heap(NOTE_HEAP_ADD, heap, heap_start, heap_size, 0);
UNUSED(heap_start);
UNUSED(heap_size);
return heap; return heap;
} }
+7 -1
View File
@@ -297,6 +297,7 @@ static int rptun_init_carveout(FAR struct rptun_priv_s *priv,
FAR void *shmbase, size_t shmlen) FAR void *shmbase, size_t shmlen)
{ {
FAR struct rptun_carveout_s *carveout; FAR struct rptun_carveout_s *carveout;
struct mm_heap_config_s config;
if (vdev->role == VIRTIO_DEV_DEVICE) if (vdev->role == VIRTIO_DEV_DEVICE)
{ {
@@ -309,9 +310,14 @@ static int rptun_init_carveout(FAR struct rptun_priv_s *priv,
return -ENOMEM; return -ENOMEM;
} }
memset(&config, 0, sizeof(config));
config.name = shmname;
config.start = shmbase;
config.size = shmlen;
carveout->base = shmbase; carveout->base = shmbase;
carveout->size = shmlen; carveout->size = shmlen;
carveout->heap = mm_initialize_heap(KRN_HEAP, shmname, shmbase, shmlen); carveout->heap = mm_initialize_heap(&config);
if (carveout->heap == NULL) if (carveout->heap == NULL)
{ {
rptunerr("ERROR: Failed to initialize heap\n"); rptunerr("ERROR: Failed to initialize heap\n");
+6 -2
View File
@@ -42,6 +42,7 @@ static FAR struct mm_heap_s *g_fs_heap;
void fs_heap_initialize(void) void fs_heap_initialize(void)
{ {
struct mm_heap_config_s config;
#ifdef FS_HEAPBUF_SECTION #ifdef FS_HEAPBUF_SECTION
static uint8_t buf[CONFIG_FS_HEAPSIZE] locate_data(FS_HEAPBUF_SECTION); static uint8_t buf[CONFIG_FS_HEAPSIZE] locate_data(FS_HEAPBUF_SECTION);
#else #else
@@ -49,8 +50,11 @@ void fs_heap_initialize(void)
#endif #endif
DEBUGASSERT(buf != NULL); DEBUGASSERT(buf != NULL);
g_fs_heap = mm_initialize_pool(NULL, "heapfs", buf, memset(&config, 0, sizeof(config));
CONFIG_FS_HEAPSIZE, NULL); config.name = "heapfs";
config.start = buf;
config.size = CONFIG_FS_HEAPSIZE;
g_fs_heap = mm_initialize_pool(&config, NULL);
} }
FAR void *fs_heap_zalloc(size_t size) FAR void *fs_heap_zalloc(size_t size)
+30 -9
View File
@@ -184,6 +184,19 @@
struct mm_heap_s; /* Forward reference */ struct mm_heap_s; /* Forward reference */
struct mm_heap_config_s
{
/* If heap == NULL, means use the heap memory ([start, start + size])
* to construct the heap struct.
* If heap != NULL, means malloc struct mm_heap_s from this heap.
*/
FAR struct mm_heap_s *heap;
FAR const char *name;
FAR void *start;
size_t size;
};
struct mempool_init_s struct mempool_init_s
{ {
FAR const size_t *poolsize; FAR const size_t *poolsize;
@@ -253,20 +266,28 @@ EXTERN FAR struct mm_heap_s *g_kmmheap;
/* Functions contained in mm_initialize.c ***********************************/ /* Functions contained in mm_initialize.c ***********************************/
FAR struct mm_heap_s * FAR struct mm_heap_s *
mm_initialize_heap(FAR struct mm_heap_s *heap, FAR const char *name, mm_initialize_heap(FAR const struct mm_heap_config_s *config);
FAR void *heapstart, size_t heapsize);
#define mm_initialize(name, heapstart, heapsize) \ static inline_function FAR struct mm_heap_s *
mm_initialize_heap(NULL, name, heapstart, heapsize) mm_initialize(FAR const char *name, FAR void *heapstart, size_t heapsize)
{
struct mm_heap_config_s config;
memset(&config, 0, sizeof(config));
config.name = name;
config.start = heapstart;
config.size = heapsize;
return mm_initialize_heap(&config);
}
#ifdef CONFIG_MM_HEAP_MEMPOOL #ifdef CONFIG_MM_HEAP_MEMPOOL
FAR struct mm_heap_s * FAR struct mm_heap_s *
mm_initialize_pool(FAR struct mm_heap_s *heap, mm_initialize_pool(FAR const struct mm_heap_config_s *config,
FAR const char *name,
FAR void *heap_start, size_t heap_size,
FAR const struct mempool_init_s *init); FAR const struct mempool_init_s *init);
#else #else
# define mm_initialize_pool(heap, name, heap_start, heap_size, init) \ # define mm_initialize_pool(config, init) mm_initialize_heap(config)
mm_initialize_heap(heap, name, heap_start, heap_size)
#endif #endif
void mm_addregion(FAR struct mm_heap_s *heap, FAR void *heapstart, void mm_addregion(FAR struct mm_heap_s *heap, FAR void *heapstart,
+7 -1
View File
@@ -60,7 +60,13 @@ FAR struct mm_heap_s *g_kmmheap;
void kmm_initialize(FAR void *heap_start, size_t heap_size) void kmm_initialize(FAR void *heap_start, size_t heap_size)
{ {
g_kmmheap = mm_initialize_pool(NULL, "Kmem", heap_start, heap_size, NULL); struct mm_heap_config_s config;
memset(&config, 0, sizeof(config));
config.name = "Kmem";
config.start = heap_start;
config.size = heap_size;
g_kmmheap = mm_initialize_pool(&config, NULL);
} }
#endif /* CONFIG_MM_KERNEL_HEAP */ #endif /* CONFIG_MM_KERNEL_HEAP */
+11 -14
View File
@@ -219,12 +219,7 @@ void mm_addregion(FAR struct mm_heap_s *heap, FAR void *heapstart,
* heap region. * heap region.
* *
* Input Parameters: * Input Parameters:
* heap - If heap is NULL, will use heapstart initialize heap context, * config - The heap config structure
* otherwise, will use heap alloc a heap context, caller need
* free it after mm_uninitialize.
* name - The heap procfs name
* heapstart - Start of the initial heap region
* heapsize - Size of the initial heap region
* *
* Returned Value: * Returned Value:
* Return the address of a new heap instance. * Return the address of a new heap instance.
@@ -234,9 +229,12 @@ void mm_addregion(FAR struct mm_heap_s *heap, FAR void *heapstart,
****************************************************************************/ ****************************************************************************/
FAR struct mm_heap_s * FAR struct mm_heap_s *
mm_initialize_heap(FAR struct mm_heap_s *heap, FAR const char *name, mm_initialize_heap(FAR const struct mm_heap_config_s *config)
FAR void *heapstart, size_t heapsize)
{ {
FAR struct mm_heap_s *heap = config->heap;
FAR const char *name = config->name;
FAR void *heapstart = config->start;
size_t heapsize = config->size;
int i; int i;
minfo("Heap: name=%s, start=%p size=%zu\n", name, heapstart, heapsize); minfo("Heap: name=%s, start=%p size=%zu\n", name, heapstart, heapsize);
@@ -309,11 +307,10 @@ mm_initialize_heap(FAR struct mm_heap_s *heap, FAR const char *name,
#ifdef CONFIG_MM_HEAP_MEMPOOL #ifdef CONFIG_MM_HEAP_MEMPOOL
FAR struct mm_heap_s * FAR struct mm_heap_s *
mm_initialize_pool(FAR struct mm_heap_s *heap, mm_initialize_pool(FAR const struct mm_heap_config_s *config,
FAR const char *name,
FAR void *heap_start, size_t heap_size,
FAR const struct mempool_init_s *init) FAR const struct mempool_init_s *init)
{ {
FAR struct mm_heap_s *heap;
#if CONFIG_MM_HEAP_MEMPOOL_THRESHOLD > 0 #if CONFIG_MM_HEAP_MEMPOOL_THRESHOLD > 0
size_t poolsize[MEMPOOL_NPOOLS]; size_t poolsize[MEMPOOL_NPOOLS];
struct mempool_init_s def; struct mempool_init_s def;
@@ -344,15 +341,15 @@ mm_initialize_pool(FAR struct mm_heap_s *heap,
} }
#endif #endif
heap = mm_initialize_heap(heap, name, heap_start, heap_size); heap = mm_initialize_heap(config);
/* Initialize the multiple mempool in heap */ /* Initialize the multiple mempool in heap */
if (init != NULL && init->poolsize != NULL && init->npools != 0) if (init != NULL && init->poolsize != NULL && init->npools != 0)
{ {
heap->mm_threshold = init->threshold; heap->mm_threshold = init->threshold;
heap->mm_mpool = mempool_multiple_init(name, init->poolsize, heap->mm_mpool = mempool_multiple_init(config->name,
init->npools, init->poolsize, init->npools,
(mempool_multiple_alloc_t)mempool_memalign, (mempool_multiple_alloc_t)mempool_memalign,
(mempool_multiple_alloc_size_t)mm_malloc_size, (mempool_multiple_alloc_size_t)mm_malloc_size,
(mempool_multiple_free_t)mm_free, heap, (mempool_multiple_free_t)mm_free, heap,
+12 -14
View File
@@ -998,12 +998,7 @@ bool mm_heapmember(FAR struct mm_heap_s *heap, FAR void *mem)
* heap region. * heap region.
* *
* Input Parameters: * Input Parameters:
* heap - If heap is NULL, will use heapstart initialize heap context, * config - The heap config structure
* otherwise, will use heap alloc a heap context, caller need
* free it after mm_uninitialize.
* name - The heap procfs name
* heapstart - Start of the initial heap region
* heapsize - Size of the initial heap region
* *
* Returned Value: * Returned Value:
* Return the address of a new heap instance. * Return the address of a new heap instance.
@@ -1013,9 +1008,13 @@ bool mm_heapmember(FAR struct mm_heap_s *heap, FAR void *mem)
****************************************************************************/ ****************************************************************************/
FAR struct mm_heap_s * FAR struct mm_heap_s *
mm_initialize_heap(FAR struct mm_heap_s *heap, FAR const char *name, mm_initialize_heap(FAR const struct mm_heap_config_s *config)
FAR void *heapstart, size_t heapsize)
{ {
FAR struct mm_heap_s *heap = config->heap;
FAR const char *name = config->name;
FAR void *heapstart = config->start;
size_t heapsize = config->size;
minfo("Heap: name=%s start=%p size=%zu\n", name, heapstart, heapsize); minfo("Heap: name=%s start=%p size=%zu\n", name, heapstart, heapsize);
if (heap == NULL) if (heap == NULL)
{ {
@@ -1070,11 +1069,10 @@ mm_initialize_heap(FAR struct mm_heap_s *heap, FAR const char *name,
#ifdef CONFIG_MM_HEAP_MEMPOOL #ifdef CONFIG_MM_HEAP_MEMPOOL
FAR struct mm_heap_s * FAR struct mm_heap_s *
mm_initialize_pool(FAR struct mm_heap_s *heap, mm_initialize_pool(FAR const struct mm_heap_config_s *config,
FAR const char *name,
FAR void *heap_start, size_t heap_size,
FAR const struct mempool_init_s *init) FAR const struct mempool_init_s *init)
{ {
FAR struct mm_heap_s *heap;
#if CONFIG_MM_HEAP_MEMPOOL_THRESHOLD > 0 #if CONFIG_MM_HEAP_MEMPOOL_THRESHOLD > 0
size_t poolsize[MEMPOOL_NPOOLS]; size_t poolsize[MEMPOOL_NPOOLS];
struct mempool_init_s def; struct mempool_init_s def;
@@ -1105,15 +1103,15 @@ mm_initialize_pool(FAR struct mm_heap_s *heap,
} }
#endif #endif
heap = mm_initialize_heap(heap, name, heap_start, heap_size); heap = mm_initialize_heap(config);
/* Initialize the multiple mempool in heap */ /* Initialize the multiple mempool in heap */
if (init != NULL && init->poolsize != NULL && init->npools != 0) if (init != NULL && init->poolsize != NULL && init->npools != 0)
{ {
heap->mm_threshold = init->threshold; heap->mm_threshold = init->threshold;
heap->mm_mpool = mempool_multiple_init(name, init->poolsize, heap->mm_mpool = mempool_multiple_init(config->name,
init->npools, init->poolsize, init->npools,
(mempool_multiple_alloc_t)mempool_memalign, (mempool_multiple_alloc_t)mempool_memalign,
(mempool_multiple_alloc_size_t)mm_malloc_size, (mempool_multiple_alloc_size_t)mm_malloc_size,
(mempool_multiple_free_t)mm_free, heap, (mempool_multiple_free_t)mm_free, heap,
+8 -2
View File
@@ -86,10 +86,16 @@
void umm_initialize(FAR void *heap_start, size_t heap_size) void umm_initialize(FAR void *heap_start, size_t heap_size)
{ {
struct mm_heap_config_s config;
memset(&config, 0, sizeof(config));
config.start = heap_start;
config.size = heap_size;
#ifdef CONFIG_BUILD_KERNEL #ifdef CONFIG_BUILD_KERNEL
USR_HEAP = mm_initialize_pool(NULL, NULL, heap_start, heap_size, NULL); USR_HEAP = mm_initialize_pool(&config, NULL);
#else #else
USR_HEAP = mm_initialize_pool(NULL, "Umem", heap_start, heap_size, NULL); config.name = "Umem";
USR_HEAP = mm_initialize_pool(&config, NULL);
#endif #endif
} }