[libcxx-commits] [libcxx] [libcxx] Use `aligned_alloc` for testing instead of `posix_memalign` (PR #101748)
Joseph Huber via libcxx-commits
libcxx-commits at lists.llvm.org
Mon Aug 5 12:05:08 PDT 2024
https://github.com/jhuber6 updated https://github.com/llvm/llvm-project/pull/101748
>From 899b38f10d79187cd02f4ac5ac9b681e87ee5c21 Mon Sep 17 00:00:00 2001
From: Joseph Huber <huberjn at outlook.com>
Date: Fri, 2 Aug 2024 13:49:58 -0500
Subject: [PATCH 1/2] [libcxx] Use `aligned_alloc` for testing instead of
`posix_memalign`
Summary:
The `aligned_alloc` function is the C11 replacement for
`posix_memalign`. We should favor the C standard over the POSIX standard
so more C library implementations can run the tests.
---
.../support.dynamic/new_faligned_allocation.pass.cpp | 2 +-
libcxx/test/support/count_new.h | 2 ++
2 files changed, 3 insertions(+), 1 deletion(-)
diff --git a/libcxx/test/libcxx/language.support/support.dynamic/new_faligned_allocation.pass.cpp b/libcxx/test/libcxx/language.support/support.dynamic/new_faligned_allocation.pass.cpp
index 69c46f00fb65d..87f4783e12973 100644
--- a/libcxx/test/libcxx/language.support/support.dynamic/new_faligned_allocation.pass.cpp
+++ b/libcxx/test/libcxx/language.support/support.dynamic/new_faligned_allocation.pass.cpp
@@ -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*).
diff --git a/libcxx/test/support/count_new.h b/libcxx/test/support/count_new.h
index 0a95e05b72421..055d269c04824 100644
--- a/libcxx/test/support/count_new.h
+++ b/libcxx/test/support/count_new.h
@@ -460,6 +460,8 @@ inline void* alocate_aligned_impl(std::size_t size, std::align_val_t align) {
void* ret = nullptr;
# ifdef USE_ALIGNED_ALLOC
ret = _aligned_malloc(size, alignment);
+# elif TEST_STD_VER >= 17
+ ret = aligned_alloc(alignment, size);
# else
assert(posix_memalign(&ret, std::max(alignment, sizeof(void*)), size) != EINVAL);
# endif
>From 99272f420df0cb9f99f28874116e4c211abe9cce Mon Sep 17 00:00:00 2001
From: Joseph Huber <huberjn at outlook.com>
Date: Mon, 5 Aug 2024 14:05:00 -0500
Subject: [PATCH 2/2] Update count_new.h
---
libcxx/test/support/count_new.h | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/libcxx/test/support/count_new.h b/libcxx/test/support/count_new.h
index 055d269c04824..b9dc0129bd9e8 100644
--- a/libcxx/test/support/count_new.h
+++ b/libcxx/test/support/count_new.h
@@ -461,7 +461,8 @@ inline void* alocate_aligned_impl(std::size_t size, std::align_val_t align) {
# ifdef USE_ALIGNED_ALLOC
ret = _aligned_malloc(size, alignment);
# elif TEST_STD_VER >= 17
- ret = aligned_alloc(alignment, size);
+ size_t rounded_size = (size + align - 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
More information about the libcxx-commits
mailing list