Skip to content

Commit

Permalink
libc/common: Add memalign
Browse files Browse the repository at this point in the history
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: #57899

Signed-off-by: Keith Packard <[email protected]>
  • Loading branch information
keith-packard authored and nashif committed Aug 10, 2023
1 parent cce530c commit 101cdcd
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions lib/libc/common/source/stdlib/malloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 101cdcd

Please sign in to comment.