diff options
author | Henning Baldersheim <balder@yahoo-inc.com> | 2021-06-29 10:38:12 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-06-29 10:38:12 +0200 |
commit | 905b03c0e30156c8fcd73998e9ce7fba3880aeba (patch) | |
tree | d6c26c40919e75d689be05799a9bcf4b9e12e3ee /vespamalloc/src/tests | |
parent | e928ee61e47fe9c1cd15585df6dc553eaffa4370 (diff) | |
parent | 918e9f2ba0aa8ac4c1419873bd8dda1ad880b763 (diff) |
Merge pull request #18438 from vespa-engine/balder/implement_malloc_usable_size
Implement _malloc_usable_size
Diffstat (limited to 'vespamalloc/src/tests')
-rw-r--r-- | vespamalloc/src/tests/test1/new_test.cpp | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/vespamalloc/src/tests/test1/new_test.cpp b/vespamalloc/src/tests/test1/new_test.cpp index a8392363f6f..9c291b3842e 100644 --- a/vespamalloc/src/tests/test1/new_test.cpp +++ b/vespamalloc/src/tests/test1/new_test.cpp @@ -1,6 +1,8 @@ // 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> +#include <malloc.h> +#include <dlfcn.h> LOG_SETUP("new_test"); @@ -105,5 +107,35 @@ TEST("verify new with alignment = 64 with single element") { LOG(info, "&s=%p", s.get()); } +void verify_vespamalloc_usable_size() { + struct AllocInfo { size_t requested; size_t usable;}; + AllocInfo allocInfo[] = {{0x7, 0x20}, {0x27, 0x40}, {0x47, 0x80}, {0x87, 0x100}, {0x107, 0x200}, {0x207, 0x400}, + {0x407, 0x800}, {0x807, 0x1000}, {0x1007, 0x2000}, {0x2007, 0x4000}, {0x4007, 0x8000}, + {0x8007, 0x10000}, {0x10007, 0x20000}, {0x20007, 0x40000}, {0x40007, 0x80000}, {0x80007, 0x100000}, + {0x100007, 0x200000}, {0x200007, 0x400000}, {0x400007, 0x600000}}; + for (const AllocInfo & info : allocInfo) { + std::unique_ptr<char[]> buf = std::make_unique<char[]>(info.requested); + size_t usable_size = malloc_usable_size(buf.get()); + EXPECT_EQUAL(info.usable, usable_size); + } +} + +TEST("verify malloc_usable_size is sane") { + constexpr size_t SZ = 33; + std::unique_ptr<char[]> buf = std::make_unique<char[]>(SZ); + size_t usable_size = malloc_usable_size(buf.get()); + if (dlsym(RTLD_NEXT, "is_vespamallocd") != nullptr) { + // Debug variants will never have more memory available as there is pre/postamble for error detection. + EXPECT_EQUAL(SZ, usable_size); + } else if (dlsym(RTLD_NEXT, "is_vespamalloc") != nullptr) { + // Normal production vespamalloc will round up + EXPECT_EQUAL(64u, usable_size); + verify_vespamalloc_usable_size(); + } else { + // Non vespamalloc implementations we can not say anything about + EXPECT_GREATER_EQUAL(usable_size, SZ); + } +} + TEST_MAIN() { TEST_RUN_ALL(); } |