mm: support mm_initialize_heap to specify a specific heap pointer

Support user pass it own heap struct to the mm_initialize_heap() to
avoid the heap struct is reserved from the heap range

Signed-off-by: anjiahao <anjiahao@xiaomi.com>
This commit is contained in:
anjiahao
2025-04-17 16:50:22 +08:00
committed by GUIDINGLI
parent 7c484d6436
commit 6ed4ea63d8
6 changed files with 94 additions and 53 deletions

View File

@@ -198,31 +198,44 @@ static void mm_delayfree(struct mm_heap_s *heap, void *mem, bool delay)
****************************************************************************/
/****************************************************************************
* Name: mm_initialize
* Name: mm_initialize_heap
*
* Description:
* Initialize the selected heap data structures, providing the initial
* heap region.
*
* Input Parameters:
* heap - The selected heap
* heap - If heap is NULL, will use heapstart initialize heap context,
* 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:
* None
* Return the address of a new heap instance.
*
* Assumptions:
*
****************************************************************************/
struct mm_heap_s *mm_initialize(const char *name,
void *heap_start, size_t heap_size)
struct mm_heap_s *
mm_initialize_heap(struct mm_heap_s *heap, const char *name,
void *heapstart, size_t heapsize)
{
struct mm_heap_s *heap;
if (heap == NULL)
{
heap = host_memalign(sizeof(void *), sizeof(*heap));
}
else
{
heap = mm_memalign(heap, MM_ALIGN, sizeof(struct mm_heap_s));
}
heap = host_memalign(sizeof(void *), sizeof(*heap));
DEBUGASSERT(heap);
if (heap == NULL)
{
return NULL;
}
memset(heap, 0, sizeof(struct mm_heap_s));

View File

@@ -133,8 +133,10 @@
#endif
#ifdef CONFIG_MM_KERNEL_HEAP
# define KRN_HEAP g_kmmheap
# define MM_INTERNAL_HEAP(heap) ((heap) == USR_HEAP || (heap) == g_kmmheap)
#else
# define KRN_HEAP USR_HEAP
# define MM_INTERNAL_HEAP(heap) ((heap) == USR_HEAP)
#endif
@@ -250,18 +252,21 @@ EXTERN FAR struct mm_heap_s *g_kmmheap;
/* Functions contained in mm_initialize.c ***********************************/
FAR struct mm_heap_s *mm_initialize(FAR const char *name,
FAR void *heap_start, size_t heap_size);
FAR struct mm_heap_s *
mm_initialize_heap(FAR struct mm_heap_s *heap, FAR const char *name,
FAR void *heapstart, size_t heapsize);
#define mm_initialize(name, heapstart, heapsize) \
mm_initialize_heap(NULL, name, heapstart, heapsize)
#ifdef CONFIG_MM_HEAP_MEMPOOL
FAR struct mm_heap_s *
mm_initialize_pool(FAR const char *name,
mm_initialize_pool(FAR struct mm_heap_s *heap,
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)
# define mm_initialize_pool(heap, name, heap_start, heap_size, init) \
mm_initialize_heap(heap, name, heap_start, heap_size)
#endif
void mm_addregion(FAR struct mm_heap_s *heap, FAR void *heapstart,

View File

@@ -60,7 +60,7 @@ FAR struct mm_heap_s *g_kmmheap;
void kmm_initialize(FAR void *heap_start, size_t heap_size)
{
g_kmmheap = mm_initialize_pool("Kmem", heap_start, heap_size, NULL);
g_kmmheap = mm_initialize_pool(NULL, "Kmem", heap_start, heap_size, NULL);
}
#endif /* CONFIG_MM_KERNEL_HEAP */

View File

@@ -212,13 +212,16 @@ void mm_addregion(FAR struct mm_heap_s *heap, FAR void *heapstart,
}
/****************************************************************************
* Name: mm_initialize
* Name: mm_initialize_heap
*
* Description:
* Initialize the selected heap data structures, providing the initial
* heap region.
*
* Input Parameters:
* heap - If heap is NULL, will use heapstart initialize heap context,
* 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
@@ -230,28 +233,37 @@ 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)
FAR struct mm_heap_s *
mm_initialize_heap(FAR struct mm_heap_s *heap, FAR const char *name,
FAR void *heapstart, size_t heapsize)
{
FAR struct mm_heap_s *heap;
uintptr_t heap_adj;
int i;
int i;
minfo("Heap: name=%s, start=%p size=%zu\n", name, heapstart, heapsize);
if (heap == NULL)
{
/* First ensure the memory to be used is aligned */
/* First ensure the memory to be used is aligned */
uintptr_t heap_adj = MM_ALIGN_UP((uintptr_t)heapstart);
heapsize -= heap_adj - (uintptr_t)heapstart;
heap_adj = MM_ALIGN_UP((uintptr_t)heapstart);
heapsize -= heap_adj - (uintptr_t)heapstart;
/* Reserve a block space for mm_heap_s context */
/* Reserve a block space for mm_heap_s context */
DEBUGASSERT(heapsize > sizeof(struct mm_heap_s));
heap = (FAR struct mm_heap_s *)heap_adj;
heapsize -= sizeof(struct mm_heap_s);
heapstart = (FAR char *)heap_adj + sizeof(struct mm_heap_s);
DEBUGASSERT(heapsize > sizeof(struct mm_heap_s));
heap = (FAR struct mm_heap_s *)heap_adj;
heapsize -= sizeof(struct mm_heap_s);
heapstart = (FAR char *)heap_adj + sizeof(struct mm_heap_s);
DEBUGASSERT(MM_MIN_CHUNK >= MM_SIZEOF_ALLOCNODE);
DEBUGASSERT(MM_MIN_CHUNK >= MM_SIZEOF_ALLOCNODE);
}
else
{
heap = mm_memalign(heap, MM_ALIGN, sizeof(struct mm_heap_s));
if (heap == NULL)
{
return NULL;
}
}
/* Set up global variables */
@@ -297,12 +309,11 @@ FAR struct mm_heap_s *mm_initialize(FAR const char *name,
#ifdef CONFIG_MM_HEAP_MEMPOOL
FAR struct mm_heap_s *
mm_initialize_pool(FAR const char *name,
mm_initialize_pool(FAR struct mm_heap_s *heap,
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;
@@ -333,7 +344,7 @@ mm_initialize_pool(FAR const char *name,
}
#endif
heap = mm_initialize(name, heap_start, heap_size);
heap = mm_initialize_heap(heap, name, heap_start, heap_size);
/* Initialize the multiple mempool in heap */

View File

@@ -991,38 +991,51 @@ bool mm_heapmember(FAR struct mm_heap_s *heap, FAR void *mem)
}
/****************************************************************************
* Name: mm_initialize
* Name: mm_initialize_heap
*
* Description:
* Initialize the selected heap data structures, providing the initial
* heap region.
*
* Input Parameters:
* heap - The selected heap
* heap - If heap is NULL, will use heapstart initialize heap context,
* 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:
* None
* Return the address of a new heap instance.
*
* Assumptions:
*
****************************************************************************/
FAR struct mm_heap_s *mm_initialize(FAR const char *name,
FAR void *heapstart, size_t heapsize)
FAR struct mm_heap_s *
mm_initialize_heap(FAR struct mm_heap_s *heap, FAR const char *name,
FAR void *heapstart, size_t heapsize)
{
FAR struct mm_heap_s *heap;
minfo("Heap: name=%s start=%p size=%zu\n", name, heapstart, heapsize);
if (heap == NULL)
{
/* Reserve a block space for mm_heap_s context */
/* Reserve a block space for mm_heap_s context */
DEBUGASSERT(heapsize > sizeof(struct mm_heap_s));
heap = (FAR struct mm_heap_s *)heapstart;
heapstart += sizeof(struct mm_heap_s);
heapsize -= sizeof(struct mm_heap_s);
}
else
{
heap = mm_memalign(heap, MM_ALIGN, sizeof(struct mm_heap_s));
if (heap == NULL)
{
return NULL;
}
}
DEBUGASSERT(heapsize > sizeof(struct mm_heap_s));
heap = (FAR struct mm_heap_s *)heapstart;
memset(heap, 0, sizeof(struct mm_heap_s));
heapstart += sizeof(struct mm_heap_s);
heapsize -= sizeof(struct mm_heap_s);
/* Allocate and create TLSF context */
@@ -1057,12 +1070,11 @@ FAR struct mm_heap_s *mm_initialize(FAR const char *name,
#ifdef CONFIG_MM_HEAP_MEMPOOL
FAR struct mm_heap_s *
mm_initialize_pool(FAR const char *name,
mm_initialize_pool(FAR struct mm_heap_s *heap,
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;
@@ -1093,7 +1105,7 @@ mm_initialize_pool(FAR const char *name,
}
#endif
heap = mm_initialize(name, heap_start, heap_size);
heap = mm_initialize_heap(heap, name, heap_start, heap_size);
/* Initialize the multiple mempool in heap */

View File

@@ -87,9 +87,9 @@
void umm_initialize(FAR void *heap_start, size_t heap_size)
{
#ifdef CONFIG_BUILD_KERNEL
USR_HEAP = mm_initialize_pool(NULL, heap_start, heap_size, NULL);
USR_HEAP = mm_initialize_pool(NULL, NULL, heap_start, heap_size, NULL);
#else
USR_HEAP = mm_initialize_pool("Umem", heap_start, heap_size, NULL);
USR_HEAP = mm_initialize_pool(NULL, "Umem", heap_start, heap_size, NULL);
#endif
}