Browse Source

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 0c15627cc1 ("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 <npitre@baylibre.com>
pull/31896/head
Nicolas Pitre 5 years ago committed by Anas Nashif
parent
commit
4690b8d5ec
  1. 6
      lib/libc/minimal/source/stdlib/malloc.c

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

@ -12,6 +12,7 @@
#include <string.h> #include <string.h>
#include <app_memory/app_memdomain.h> #include <app_memory/app_memdomain.h>
#include <sys/sys_heap.h> #include <sys/sys_heap.h>
#include <zephyr/types.h>
#define LOG_LEVEL CONFIG_KERNEL_LOG_LEVEL #define LOG_LEVEL CONFIG_KERNEL_LOG_LEVEL
#include <logging/log.h> #include <logging/log.h>
@ -34,8 +35,9 @@ Z_GENERIC_SECTION(POOL_SECTION) static char z_malloc_heap_mem[HEAP_BYTES];
void *malloc(size_t size) void *malloc(size_t size)
{ {
void *ret; void *ret = sys_heap_aligned_alloc(&z_malloc_heap,
ret = sys_heap_alloc(&z_malloc_heap, size); __alignof__(z_max_align_t),
size);
if (ret == NULL) { if (ret == NULL) {
errno = ENOMEM; errno = ENOMEM;
} }

Loading…
Cancel
Save