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 appears to be used internally by the G++, so we need to provide an
implementation of this when using that compiler, even though it's not part
of the Zephyr C library API.

Closes: zephyrproject-rtos#57899

Signed-off-by: Keith Packard <[email protected]>
  • Loading branch information
keith-packard committed May 18, 2023
1 parent 0bff695 commit a1c2776
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 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,24 @@ void *aligned_alloc(size_t alignment, size_t size)
return ret;
}

#if defined(CONFIG_CPP) && defined(__GNUC__)

/*
* G++ uses this function internally for aligned allocations.
*
* 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, aligned alloc is just a wrapper around
* sys_heap_aligned_alloc which doen'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 a1c2776

Please sign in to comment.