aboutsummaryrefslogtreecommitdiffstats
path: root/vespamalloc/src/tests
diff options
context:
space:
mode:
authorHenning Baldersheim <balder@yahoo-inc.com>2021-06-28 22:10:50 +0000
committerHenning Baldersheim <balder@yahoo-inc.com>2021-06-28 22:10:50 +0000
commit918e9f2ba0aa8ac4c1419873bd8dda1ad880b763 (patch)
treeac538eeb98bfea0806de2a14305648e70412e62b /vespamalloc/src/tests
parentdfa07c1710906646b6474b10e59a71600a162a8b (diff)
Implement _malloc_usable_size
Diffstat (limited to 'vespamalloc/src/tests')
-rw-r--r--vespamalloc/src/tests/test1/new_test.cpp32
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(); }