Skip to content

[libcxx] Use aligned_alloc for testing instead of posix_memalign #101748

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

Merged
merged 2 commits into from
Aug 15, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ int main(int, char**) {
test_allocations(64, 64);
// Size being a multiple of alignment also needs to be supported.
test_allocations(64, 32);
// When aligned allocation is implemented using posix_memalign,
// When aligned allocation is implemented using aligned_alloc,
// that function requires a minimum alignment of sizeof(void*).
// Check that we can also create overaligned allocations with
// an alignment argument less than sizeof(void*).
Expand Down
12 changes: 12 additions & 0 deletions libcxx/test/support/count_new.h
Original file line number Diff line number Diff line change
Expand Up @@ -455,11 +455,23 @@ void operator delete[](void* p, std::nothrow_t const&) TEST_NOEXCEPT {
# define USE_ALIGNED_ALLOC
# endif

# if defined(__APPLE__)
# if (defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__) && \
__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ < 101500)
# define TEST_HAS_NO_C11_ALIGNED_ALLOC
# endif
# elif defined(__ANDROID__) && __ANDROID_API__ < 28
# define TEST_HAS_NO_C11_ALIGNED_ALLOC
# endif

inline void* allocate_aligned_impl(std::size_t size, std::align_val_t align) {
const std::size_t alignment = static_cast<std::size_t>(align);
void* ret = nullptr;
# ifdef USE_ALIGNED_ALLOC
ret = _aligned_malloc(size, alignment);
# elif TEST_STD_VER >= 17 && !defined(TEST_HAS_NO_C11_ALIGNED_ALLOC)
size_t rounded_size = (size + alignment - 1) & ~(alignment - 1);
ret = aligned_alloc(alignment, size > rounded_size ? size : rounded_size);
# else
assert(posix_memalign(&ret, std::max(alignment, sizeof(void*)), size) != EINVAL);
# endif
Expand Down
Loading