From d4978bfba4c6ba93ef83fe4f40aa51ed3392f435 Mon Sep 17 00:00:00 2001 From: wangbowen6 Date: Fri, 8 Apr 2022 18:12:11 +0800 Subject: [PATCH] mm_initialize: malloc() return aligend pointer. malloc() should return aligned (with MM_MIN_CHUNK) pointer, but pr #5906 destroy that, this pr find a better method to solve these questions. Signed-off-by: YAMAMOTO Takashi and Signed-off-by: wangbowen6 --- mm/mm_heap/mm_initialize.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/mm/mm_heap/mm_initialize.c b/mm/mm_heap/mm_initialize.c index e32c63cdac..df28679764 100644 --- a/mm/mm_heap/mm_initialize.c +++ b/mm/mm_heap/mm_initialize.c @@ -93,11 +93,15 @@ void mm_addregion(FAR struct mm_heap_s *heap, FAR void *heapstart, DEBUGVERIFY(mm_takesemaphore(heap)); - /* Adjust the provided heap start and size so that they are both aligned - * with the MM_MIN_CHUNK size. + /* Adjust the provided heap start and size. + * + * Note: (uintptr_t)node + SIZEOF_MM_ALLOCNODE is what's actually + * returned to the malloc user, which should have natural alignment. + * (that is, in this implementation, MM_MIN_CHUNK-alignment.) */ - heapbase = MM_ALIGN_UP((uintptr_t)heapstart); + heapbase = MM_ALIGN_UP((uintptr_t)heapstart + 2 * SIZEOF_MM_ALLOCNODE) - + 2 * SIZEOF_MM_ALLOCNODE; heapend = MM_ALIGN_DOWN((uintptr_t)heapstart + (uintptr_t)heapsize); heapsize = heapend - heapbase;