Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

libc/common: Add memalign #58012

Merged
merged 2 commits into from
Aug 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
keith-packard marked this conversation as resolved.
Show resolved Hide resolved
}
#endif

static int malloc_prepare(void)
{
void *heap_base = NULL;
Expand Down
1 change: 1 addition & 0 deletions tests/lib/cpp/libcxx/prj.conf
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ CONFIG_CPP=y
CONFIG_STD_CPP17=y
CONFIG_ZTEST=y
CONFIG_ZTEST_STACK_SIZE=5120
CONFIG_COMMON_LIBC_MALLOC_ARENA_SIZE=32768
CONFIG_ZTEST_NEW_API=y
12 changes: 12 additions & 0 deletions tests/lib/cpp/libcxx/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <memory>
#include <vector>
#include <zephyr/ztest.h>
#include <zephyr/kernel.h>

BUILD_ASSERT(__cplusplus == 201703);

Expand Down Expand Up @@ -95,6 +96,17 @@ ZTEST(libcxx_tests, test_exception)
}
#endif

struct ThreadStack
{
K_KERNEL_STACK_MEMBER(threadStack, 1024) {};
};

ZTEST(libcxx_tests, test_new_aligned)
{
auto test_aligned = std::make_unique<ThreadStack>();
zassert_not_null(test_aligned, "aligned allocation failed");
}

static void *libcxx_tests_setup(void)
{
TC_PRINT("version %u\n", (uint32_t)__cplusplus);
Expand Down