From 4690b8d5ec3179c94d57c324eea06d412af4d0e0 Mon Sep 17 00:00:00 2001 From: Nicolas Pitre Date: Mon, 25 Jan 2021 18:20:42 -0500 Subject: [PATCH] libc/minimal: fix malloc() allocated memory alignment The definition for malloc() says that it should return a pointer to the allocated memory which is suitably aligned for any built-in type. This requirement was lost in commit 0c15627cc1a7 ("lib: Remove sys_mem_pool implementation") where the entire memory pool used to have an explicit alignment of 16. Fix this by allocating memory with sys_heap_aligned_alloc() using __alignof__(z_max_align_t) which will automatically get the needed alignment on each platform. Signed-off-by: Nicolas Pitre --- lib/libc/minimal/source/stdlib/malloc.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/libc/minimal/source/stdlib/malloc.c b/lib/libc/minimal/source/stdlib/malloc.c index 1524c32b6c2..5dbc03f9427 100644 --- a/lib/libc/minimal/source/stdlib/malloc.c +++ b/lib/libc/minimal/source/stdlib/malloc.c @@ -12,6 +12,7 @@ #include #include #include +#include #define LOG_LEVEL CONFIG_KERNEL_LOG_LEVEL #include @@ -34,8 +35,9 @@ Z_GENERIC_SECTION(POOL_SECTION) static char z_malloc_heap_mem[HEAP_BYTES]; void *malloc(size_t size) { - void *ret; - ret = sys_heap_alloc(&z_malloc_heap, size); + void *ret = sys_heap_aligned_alloc(&z_malloc_heap, + __alignof__(z_max_align_t), + size); if (ret == NULL) { errno = ENOMEM; }