diff options
author | Henning Baldersheim <balder@yahoo-inc.com> | 2020-06-09 08:01:03 +0000 |
---|---|---|
committer | Henning Baldersheim <balder@yahoo-inc.com> | 2020-06-09 08:01:03 +0000 |
commit | 6ef440733ef899251be945a1b3c985e76e4a6312 (patch) | |
tree | 28f2ae3d4d0c2de8ce06fc549212e9d540ea88b2 | |
parent | c6120d5e864cabf282495889398621d2fc047f90 (diff) |
Add support for new/delete with alignment that came along in c++17 with gcc 9
-rw-r--r-- | vespamalloc/src/tests/test1/CMakeLists.txt | 22 | ||||
-rw-r--r-- | vespamalloc/src/tests/test1/new_test.cpp | 91 | ||||
-rw-r--r-- | vespamalloc/src/vespamalloc/malloc/overload.h | 38 |
3 files changed, 144 insertions, 7 deletions
diff --git a/vespamalloc/src/tests/test1/CMakeLists.txt b/vespamalloc/src/tests/test1/CMakeLists.txt index cade2e092b4..15680f22595 100644 --- a/vespamalloc/src/tests/test1/CMakeLists.txt +++ b/vespamalloc/src/tests/test1/CMakeLists.txt @@ -6,3 +6,25 @@ vespa_add_executable(vespamalloc_testatomic_app TEST ${VESPA_ATOMIC_LIB} ) vespa_add_test(NAME vespamalloc_testatomic_app NO_VALGRIND COMMAND vespamalloc_testatomic_app) + +vespa_add_executable(vespamalloc_new_test_app TEST + SOURCES + new_test.cpp +) +vespa_add_test(NAME vespamalloc_new_test_app NO_VALGRIND COMMAND vespamalloc_new_test_app) + +vespa_add_executable(vespamalloc_new_test_with_vespamalloc_app TEST + SOURCES + new_test.cpp + DEPENDS + vespamalloc +) +vespa_add_test(NAME vespamalloc_new_test_with_vespamalloc_app NO_VALGRIND COMMAND vespamalloc_new_test_with_vespamalloc_app) + +vespa_add_executable(vespamalloc_new_test_with_vespamallocd_app TEST + SOURCES + new_test.cpp + DEPENDS + vespamallocd +) +vespa_add_test(NAME vespamalloc_new_test_with_vespamallocd_app NO_VALGRIND COMMAND vespamalloc_new_test_with_vespamallocd_app) diff --git a/vespamalloc/src/tests/test1/new_test.cpp b/vespamalloc/src/tests/test1/new_test.cpp new file mode 100644 index 00000000000..784ae16f7ad --- /dev/null +++ b/vespamalloc/src/tests/test1/new_test.cpp @@ -0,0 +1,91 @@ +// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +#include <vespa/vespalib/testkit/testapp.h> +#include <vespa/log/log.h> + +LOG_SETUP("new_test"); + +void cmp(const void *a, const void *b) { + EXPECT_EQUAL(a, b); +} +void cmp(const void *base, size_t offset, const void *p) { + cmp((static_cast<const char *>(base) + offset), p); +} + +TEST("verify new with normal alignment") { + struct S { + int a; + long b; + int c; + }; + static_assert(sizeof(S) == 24, "sizeof(S) == 16"); + auto s = std::make_unique<S>(); + cmp(s.get(), &s->a); + cmp(s.get(), 8, &s->b); + cmp(s.get(), 16, &s->c); + LOG(info, "&s=%p &s.b=%p &s.c=%p", s.get(), &s->b, &s->c); +} + +TEST("verify new with alignment = 16") { + struct S { + int a; + alignas(16) long b; + int c; + }; + static_assert(sizeof(S) == 32, "sizeof(S) == 32"); + auto s = std::make_unique<S>(); + cmp(s.get(), &s->a); + cmp(s.get(), 16, &s->b); + cmp(s.get(), 24, &s->c); + LOG(info, "&s=%p &s.b=%p &s.c=%p", s.get(), &s->b, &s->c); +} + +TEST("verify new with alignment = 32") { + struct S { + int a; + alignas(32) long b; + int c; + }; + static_assert(sizeof(S) == 64, "sizeof(S) == 64"); + auto s = std::make_unique<S>(); + cmp(s.get(), &s->a); + cmp(s.get(), 32, &s->b); + cmp(s.get(), 40, &s->c); + LOG(info, "&s=%p &s.b=%p &s.c=%p", s.get(), &s->b, &s->c); +} + +TEST("verify new with alignment = 64") { + struct S { + int a; + alignas(64) long b; + int c; + }; + static_assert(sizeof(S) == 128, "sizeof(S) == 128"); + auto s = std::make_unique<S>(); + cmp(s.get(), &s->a); + cmp(s.get(), 64, &s->b); + cmp(s.get(), 72, &s->c); + LOG(info, "&s=%p &s.b=%p &s.c=%p", s.get(), &s->b, &s->c); +} + +TEST("verify new with alignment = 64 with single element") { + struct S { + alignas(64) long a; + }; + static_assert(sizeof(S) == 64, "sizeof(S) == 64"); + auto s = std::make_unique<S>(); + cmp(s.get(), &s->a); + LOG(info, "&s=%p", s.get()); +} + +TEST("verify new with alignment = 64 with single element") { + struct alignas(64) S { + long a; + }; + static_assert(sizeof(S) == 64, "sizeof(S) == 64"); + auto s = std::make_unique<S>(); + cmp(s.get(), &s->a); + LOG(info, "&s=%p", s.get()); +} + + +TEST_MAIN() { TEST_RUN_ALL(); } diff --git a/vespamalloc/src/vespamalloc/malloc/overload.h b/vespamalloc/src/vespamalloc/malloc/overload.h index 7883578cc28..50077a371cc 100644 --- a/vespamalloc/src/vespamalloc/malloc/overload.h +++ b/vespamalloc/src/vespamalloc/malloc/overload.h @@ -21,13 +21,6 @@ private: static CreateAllocator _CreateAllocator __attribute__ ((init_priority (543))); -#if 1 // Only until we get on to a new C++14 compiler -void operator delete(void* ptr, std::size_t sz) noexcept __attribute__((visibility ("default"))); -void operator delete[](void* ptr, std::size_t sz) noexcept __attribute__((visibility ("default"))); -void operator delete(void* ptr, std::size_t sz, const std::nothrow_t&) noexcept __attribute__((visibility ("default"))); -void operator delete[](void* ptr, std::size_t sz, const std::nothrow_t&) noexcept __attribute__((visibility ("default"))); -#endif - void* operator new(std::size_t sz) { void * ptr(vespamalloc::createAllocator()->malloc(sz)); @@ -74,6 +67,37 @@ void operator delete[](void* ptr, std::size_t sz, const std::nothrow_t&) noexcep if (ptr) { vespamalloc::_GmemP->free(ptr, sz); } } +void* operator new(std::size_t sz, std::align_val_t) { + return vespamalloc::_GmemP->malloc(sz); +} +void* operator new(std::size_t sz, std::align_val_t, const std::nothrow_t&) noexcept { + return vespamalloc::_GmemP->malloc(sz); +} +void operator delete(void* ptr , std::align_val_t) noexcept { + return vespamalloc::_GmemP->free(ptr); +} +void operator delete(void* ptr, std::align_val_t, const std::nothrow_t&) noexcept { + return vespamalloc::_GmemP->free(ptr); +} +void* operator new[](std::size_t sz, std::align_val_t) { + return vespamalloc::_GmemP->malloc(sz); +} +void* operator new[](std::size_t sz, std::align_val_t, const std::nothrow_t&) noexcept { + return vespamalloc::_GmemP->malloc(sz); +} +void operator delete[](void* ptr, std::align_val_t) noexcept { + return vespamalloc::_GmemP->free(ptr); +} +void operator delete[](void* ptr, std::align_val_t, const std::nothrow_t&) noexcept { + return vespamalloc::_GmemP->free(ptr); +} +void operator delete(void* ptr, std::size_t, std::align_val_t) noexcept { + return vespamalloc::_GmemP->free(ptr); +} +void operator delete[](void* ptr, std::size_t, std::align_val_t) noexcept { + return vespamalloc::_GmemP->free(ptr); +} + extern "C" { void * malloc(size_t sz) { |