diff --git a/src/jemalloc_cpp.cpp b/src/jemalloc_cpp.cpp index cf6766c7..0d341845 100644 --- a/src/jemalloc_cpp.cpp +++ b/src/jemalloc_cpp.cpp @@ -268,7 +268,8 @@ alignedSizedDeleteImpl( ptr, size, alignment); if (likely(ptr != nullptr)) { - je_sdallocx_impl(ptr, size, MALLOCX_ALIGN(alignment)); + je_sdallocx_impl( + ptr, size, MALLOCX_LG_ALIGN(lg_floor((size_t)alignment))); } LOG("core.operator_delete.exit", ""); diff --git a/test/integration/cpp/basic.cpp b/test/integration/cpp/basic.cpp index 66c76ae7..370615e2 100644 --- a/test/integration/cpp/basic.cpp +++ b/test/integration/cpp/basic.cpp @@ -1,3 +1,4 @@ +#include #include #include "test/jemalloc_test.h" @@ -32,7 +33,37 @@ TEST_BEGIN(test_basic) { } TEST_END +TEST_BEGIN(test_aligned_sized_delete) { +#if __cpp_aligned_new >= 201606 && __cpp_sized_deallocation >= 201309 + const size_t alignments[] = {1, 64, 256, 4096, ZU(1) << 20}; + const size_t sizes[] = {1, 42, 4096, 65536}; + for (size_t alignment : alignments) { + auto al = static_cast(alignment); + for (size_t size : sizes) { + void *ptr = ::operator new(size, al); + expect_ptr_not_null( + ptr, "Unexpected aligned new failure"); + expect_zu_eq((uintptr_t)ptr & (alignment - 1), 0, + "Unexpected allocation alignment"); + ::operator delete(ptr, size, al); + + ptr = ::operator new[](size, al); + expect_ptr_not_null( + ptr, "Unexpected aligned array new failure"); + expect_zu_eq((uintptr_t)ptr & (alignment - 1), 0, + "Unexpected allocation alignment"); + ::operator delete[](ptr, size, al); + } + ::operator delete(nullptr, 0, al); + ::operator delete[](nullptr, 0, al); + } +#else + test_skip("Aligned sized deallocation is not enabled"); +#endif +} +TEST_END + int main() { - return test(test_basic); + return test(test_basic, test_aligned_sized_delete); }