Skip to content

Commit 4287c93

Browse files
committed
libc/common: Add memalign
Memalign is another name for the posix aligned_alloc function, although it has weaker restrictions on the relationship between the alignment and size. memalign() is used internally by the libstdc++ when built for 'newlib' targets (which includes picolibc) instead of aligned_alloc() due to a bug in gcc, so we need to provide an implementation of this when using that library, even though it's not part of the Zephyr C library API. When a fix for the libstdc++ is merged upstream and can be consider a reasonable dependency for Zephyr, this work-around can be removed. Closes: zephyrproject-rtos#57899 Signed-off-by: Keith Packard <[email protected]>
1 parent 0bff695 commit 4287c93

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,27 @@ void *aligned_alloc(size_t alignment, size_t size)
153153
return ret;
154154
}
155155

156+
#ifdef CONFIG_GLIBCXX_LIBCPP
157+
158+
/*
159+
* GCC's libstdc++ may use this function instead of aligned_alloc due to a
160+
* bug in the configuration for "newlib" environments (which includes picolibc).
161+
* When toolchains including that bug fix can become a dependency for Zephyr,
162+
* this work-around can be removed.
163+
*
164+
* Note that aligned_alloc isn't defined to work as a replacement for
165+
* memalign as it requires that the size be a multiple of the alignment,
166+
* while memalign does not. However, the aligned_alloc implementation here
167+
* is just a wrapper around sys_heap_aligned_alloc which doen't have that
168+
* requirement and so can be used by memalign.
169+
*/
170+
171+
void *memalign(size_t alignment, size_t size)
172+
{
173+
return aligned_alloc(alignment, size);
174+
}
175+
#endif
176+
156177
static int malloc_prepare(void)
157178
{
158179
void *heap_base = NULL;

0 commit comments

Comments
 (0)