Skip to content

Commit 4690b8d

Browse files
Nicolas Pitrenashif
authored andcommitted
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 0c15627 ("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 <[email protected]>
1 parent 6055c25 commit 4690b8d

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

lib/libc/minimal/source/stdlib/malloc.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <string.h>
1313
#include <app_memory/app_memdomain.h>
1414
#include <sys/sys_heap.h>
15+
#include <zephyr/types.h>
1516

1617
#define LOG_LEVEL CONFIG_KERNEL_LOG_LEVEL
1718
#include <logging/log.h>
@@ -34,8 +35,9 @@ Z_GENERIC_SECTION(POOL_SECTION) static char z_malloc_heap_mem[HEAP_BYTES];
3435

3536
void *malloc(size_t size)
3637
{
37-
void *ret;
38-
ret = sys_heap_alloc(&z_malloc_heap, size);
38+
void *ret = sys_heap_aligned_alloc(&z_malloc_heap,
39+
__alignof__(z_max_align_t),
40+
size);
3941
if (ret == NULL) {
4042
errno = ENOMEM;
4143
}

0 commit comments

Comments
 (0)