This is an automated email from the ASF dual-hosted git repository.

xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-nuttx.git


The following commit(s) were added to refs/heads/master by this push:
     new fccda0c08b mm_heap: add debug assert to check the alignment problem
fccda0c08b is described below

commit fccda0c08bc126077b48904d8e30b0a2063ea5d3
Author: wangbowen6 <wangbow...@xiaomi.com>
AuthorDate: Mon Oct 31 20:09:23 2022 +0800

    mm_heap: add debug assert to check the alignment problem
    
    Signed-off-by: wangbowen6 <wangbow...@xiaomi.com>
    Signed-off-by: YAMAMOTO Takashi <yamam...@midokura.com>
---
 mm/mm_heap/mm_initialize.c | 1 +
 mm/mm_heap/mm_malloc.c     | 1 +
 mm/mm_heap/mm_memalign.c   | 5 ++++-
 3 files changed, 6 insertions(+), 1 deletion(-)

diff --git a/mm/mm_heap/mm_initialize.c b/mm/mm_heap/mm_initialize.c
index d0459175fb..2a7f59960e 100644
--- a/mm/mm_heap/mm_initialize.c
+++ b/mm/mm_heap/mm_initialize.c
@@ -135,6 +135,7 @@ void mm_addregion(FAR struct mm_heap_s *heap, FAR void 
*heapstart,
   heap->mm_heapstart[IDX]->preceding = MM_ALLOC_BIT;
   node                               = (FAR struct mm_freenode_s *)
                                        (heapbase + SIZEOF_MM_ALLOCNODE);
+  DEBUGASSERT((((uintptr_t)node + SIZEOF_MM_ALLOCNODE) % MM_MIN_CHUNK) == 0);
   node->size                         = heapsize - 2*SIZEOF_MM_ALLOCNODE;
   node->preceding                    = SIZEOF_MM_ALLOCNODE;
   heap->mm_heapend[IDX]              = (FAR struct mm_allocnode_s *)
diff --git a/mm/mm_heap/mm_malloc.c b/mm/mm_heap/mm_malloc.c
index 0f1bb57808..8843fed36c 100644
--- a/mm/mm_heap/mm_malloc.c
+++ b/mm/mm_heap/mm_malloc.c
@@ -271,5 +271,6 @@ FAR void *mm_malloc(FAR struct mm_heap_s *heap, size_t size)
     }
 #endif
 
+  DEBUGASSERT(ret == NULL || ((uintptr_t)ret) % MM_MIN_CHUNK == 0);
   return ret;
 }
diff --git a/mm/mm_heap/mm_memalign.c b/mm/mm_heap/mm_memalign.c
index 8e814fdf91..2232c487a5 100644
--- a/mm/mm_heap/mm_memalign.c
+++ b/mm/mm_heap/mm_memalign.c
@@ -79,7 +79,9 @@ FAR void *mm_memalign(FAR struct mm_heap_s *heap, size_t 
alignment,
 
   if (alignment <= MM_MIN_CHUNK)
     {
-      return mm_malloc(heap, size);
+      FAR void *ptr = mm_malloc(heap, size);
+      DEBUGASSERT(ptr == NULL || ((uintptr_t)ptr) % alignment == 0);
+      return ptr;
     }
 
   /* Adjust the size to account for (1) the size of the allocated node, (2)
@@ -230,5 +232,6 @@ FAR void *mm_memalign(FAR struct mm_heap_s *heap, size_t 
alignment,
   kasan_unpoison((FAR void *)alignedchunk,
                  mm_malloc_size((FAR void *)alignedchunk));
 
+  DEBUGASSERT(alignedchunk % alignment == 0);
   return (FAR void *)alignedchunk;
 }

Reply via email to