diff --git a/lib/libc/common/source/stdlib/malloc.c b/lib/libc/common/source/stdlib/malloc.c index 02354cd194dc9e9..50c688fa7f21bd0 100644 --- a/lib/libc/common/source/stdlib/malloc.c +++ b/lib/libc/common/source/stdlib/malloc.c @@ -153,6 +153,27 @@ void *aligned_alloc(size_t alignment, size_t size) return ret; } +#ifdef CONFIG_GLIBCXX_LIBCPP + +/* + * GCC's libstdc++ may use this function instead of aligned_alloc due to a + * bug in the configuration for "newlib" environments (which includes picolibc). + * When toolchains including that bug fix can become a dependency for Zephyr, + * this work-around can be removed. + * + * Note that aligned_alloc isn't defined to work as a replacement for + * memalign as it requires that the size be a multiple of the alignment, + * while memalign does not. However, the aligned_alloc implementation here + * is just a wrapper around sys_heap_aligned_alloc which doesn't have that + * requirement and so can be used by memalign. + */ + +void *memalign(size_t alignment, size_t size) +{ + return aligned_alloc(alignment, size); +} +#endif + static int malloc_prepare(void) { void *heap_base = NULL;