Skip to content

Commit

Permalink
tests/libcxx: Test aligned allocation with C++ new operator
Browse files Browse the repository at this point in the history
Make sure the underlying allocation system can support an allocation
request generated by the new operator which has stricter alignment
requirements than the default. For G++, this ends up using the 'memalign'
function which is not part of any C standard.

Signed-off-by: Keith Packard <[email protected]>
  • Loading branch information
keith-packard committed May 19, 2023
1 parent 4287c93 commit 246c692
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/libc/common/source/stdlib/malloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ void *aligned_alloc(size_t alignment, size_t size)
* 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 doen't have that
* is just a wrapper around sys_heap_aligned_alloc which doesn't have that
* requirement and so can be used by memalign.
*/

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

0 comments on commit 246c692

Please sign in to comment.