diff options
Diffstat (limited to 'vespamalloc')
-rw-r--r-- | vespamalloc/src/tests/stacktrace/stacktrace.cpp | 31 | ||||
-rw-r--r-- | vespamalloc/src/tests/test1/new_test.cpp | 14 | ||||
-rw-r--r-- | vespamalloc/src/vespamalloc/malloc/malloc.cpp | 4 | ||||
-rw-r--r-- | vespamalloc/src/vespamalloc/malloc/malloc.h | 1 | ||||
-rw-r--r-- | vespamalloc/src/vespamalloc/malloc/overload.h | 25 | ||||
-rw-r--r-- | vespamalloc/src/vespamalloc/malloc/threadproxy.cpp | 3 |
6 files changed, 58 insertions, 20 deletions
diff --git a/vespamalloc/src/tests/stacktrace/stacktrace.cpp b/vespamalloc/src/tests/stacktrace/stacktrace.cpp index d13fb6bb59b..0fd722e095a 100644 --- a/vespamalloc/src/tests/stacktrace/stacktrace.cpp +++ b/vespamalloc/src/tests/stacktrace/stacktrace.cpp @@ -1,7 +1,10 @@ // Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -#include <stdlib.h> -#include <stdio.h> +#include <cstdlib> +#include <cstdio> #include <pthread.h> +#include <dlfcn.h> +#include <cassert> +#include <malloc.h> void * run(void * arg) { @@ -10,7 +13,23 @@ void * run(void * arg) char * b = new char [1]; // but b should as it not deleted. (void) b; delete [] a; - return NULL; + return nullptr; +} + +void verify_that_vespamalloc_datasegment_size_exists() { + struct mallinfo info = mallinfo(); + printf("Malloc used %dm of memory\n",info.arena); + assert(info.arena >= 10); + assert(info.arena < 10000); + assert(info.fordblks == 0); + assert(info.fsmblks == 0); + assert(info.hblkhd == 0); + assert(info.hblks == 0); + assert(info.keepcost == 0); + assert(info.ordblks == 0); + assert(info.smblks == 0); + assert(info.uordblks == 0); + assert(info.usmblks == 0); } int main(int argc, char *argv[]) @@ -22,14 +41,16 @@ int main(int argc, char *argv[]) (void) b; delete [] a; pthread_t tid; - int retval = pthread_create(&tid, NULL, run, NULL); + int retval = pthread_create(&tid, nullptr, run, nullptr); if (retval != 0) { perror("pthread_create failed"); abort(); } - retval = pthread_join(tid, NULL); + retval = pthread_join(tid, nullptr); if (retval != 0) { perror("pthread_join failed"); abort(); } + + verify_that_vespamalloc_datasegment_size_exists(); } diff --git a/vespamalloc/src/tests/test1/new_test.cpp b/vespamalloc/src/tests/test1/new_test.cpp index 2400e41a1d9..a8392363f6f 100644 --- a/vespamalloc/src/tests/test1/new_test.cpp +++ b/vespamalloc/src/tests/test1/new_test.cpp @@ -12,7 +12,7 @@ void cmp(const void *base, size_t offset, const void *p) { } template <typename S> -void veryfy_aligned(S * p) { +void verify_aligned(S * p) { EXPECT_TRUE((uintptr_t(p) % alignof(S)) == 0); memset(p, 0, sizeof(S)); } @@ -26,7 +26,7 @@ TEST("verify new with normal alignment") { static_assert(sizeof(S) == 24); static_assert(alignof(S) == 8); auto s = std::make_unique<S>(); - veryfy_aligned(s.get()); + verify_aligned(s.get()); cmp(s.get(), &s->a); cmp(s.get(), 8, &s->b); cmp(s.get(), 16, &s->c); @@ -42,7 +42,7 @@ TEST("verify new with alignment = 16") { static_assert(sizeof(S) == 32); static_assert(alignof(S) == 16); auto s = std::make_unique<S>(); - veryfy_aligned(s.get()); + verify_aligned(s.get()); cmp(s.get(), &s->a); cmp(s.get(), 16, &s->b); cmp(s.get(), 24, &s->c); @@ -58,7 +58,7 @@ TEST("verify new with alignment = 32") { static_assert(sizeof(S) == 64); static_assert(alignof(S) == 32); auto s = std::make_unique<S>(); - veryfy_aligned(s.get()); + verify_aligned(s.get()); cmp(s.get(), &s->a); cmp(s.get(), 32, &s->b); cmp(s.get(), 40, &s->c); @@ -74,7 +74,7 @@ TEST("verify new with alignment = 64") { static_assert(sizeof(S) == 128); static_assert(alignof(S) == 64); auto s = std::make_unique<S>(); - veryfy_aligned(s.get()); + verify_aligned(s.get()); cmp(s.get(), &s->a); cmp(s.get(), 64, &s->b); cmp(s.get(), 72, &s->c); @@ -88,7 +88,7 @@ TEST("verify new with alignment = 64 with single element") { static_assert(sizeof(S) == 64); static_assert(alignof(S) == 64); auto s = std::make_unique<S>(); - veryfy_aligned(s.get()); + verify_aligned(s.get()); cmp(s.get(), &s->a); LOG(info, "&s=%p", s.get()); } @@ -100,7 +100,7 @@ TEST("verify new with alignment = 64 with single element") { static_assert(sizeof(S) == 64); static_assert(alignof(S) == 64); auto s = std::make_unique<S>(); - veryfy_aligned(s.get()); + verify_aligned(s.get()); cmp(s.get(), &s->a); LOG(info, "&s=%p", s.get()); } diff --git a/vespamalloc/src/vespamalloc/malloc/malloc.cpp b/vespamalloc/src/vespamalloc/malloc/malloc.cpp index 380e2844f42..4a810c1ac5f 100644 --- a/vespamalloc/src/vespamalloc/malloc/malloc.cpp +++ b/vespamalloc/src/vespamalloc/malloc/malloc.cpp @@ -11,11 +11,11 @@ typedef ThreadListT<MemBlock, NoStat> ThreadList; typedef MemoryWatcher<MemBlock, ThreadList> Allocator; static char _Gmem[sizeof(Allocator)]; -static Allocator *_GmemP = NULL; +static Allocator *_GmemP = nullptr; static Allocator * createAllocator() { - if (_GmemP == NULL) { + if (_GmemP == nullptr) { _GmemP = (Allocator *)1; _GmemP = new (_Gmem) Allocator(-1, 0x7fffffffffffffffl); } diff --git a/vespamalloc/src/vespamalloc/malloc/malloc.h b/vespamalloc/src/vespamalloc/malloc/malloc.h index df40197bbbd..49a7b765cc8 100644 --- a/vespamalloc/src/vespamalloc/malloc/malloc.h +++ b/vespamalloc/src/vespamalloc/malloc/malloc.h @@ -67,6 +67,7 @@ public: _threadList.setParams(alwayReuseLimit, threadCacheLimit); _allocPool.setParams(alwayReuseLimit, threadCacheLimit); } + const DataSegment<MemBlockPtrT> & dataSegment() const { return _segment; } private: void freeSC(void *ptr, SizeClassT sc); void crash() __attribute__((noinline));; diff --git a/vespamalloc/src/vespamalloc/malloc/overload.h b/vespamalloc/src/vespamalloc/malloc/overload.h index 56cd8101731..ca3a76b705b 100644 --- a/vespamalloc/src/vespamalloc/malloc/overload.h +++ b/vespamalloc/src/vespamalloc/malloc/overload.h @@ -5,6 +5,7 @@ #include <errno.h> #include <new> #include <stdlib.h> +#include <malloc.h> class CreateAllocator { @@ -105,6 +106,22 @@ void operator delete[](void* ptr, std::size_t sz, std::align_val_t alignment) no extern "C" { +struct mallinfo mallinfo() __THROW __attribute__((visibility ("default"))); +struct mallinfo mallinfo() __THROW { + struct mallinfo info; + info.arena = (vespamalloc::_GmemP->dataSegment().dataSize() >> 20); // Note reporting in 1M blocks + info.ordblks = 0; + info.smblks = 0; + info.hblks = 0; + info.hblkhd = 0; + info.usmblks = 0; + info.fsmblks = 0; + info.uordblks = 0; + info.fordblks = 0; + info.keepcost = 0; + return info; +} + void * malloc(size_t sz) { return vespamalloc::createAllocator()->malloc(sz); } @@ -119,8 +136,8 @@ void * realloc(void * ptr, size_t sz) return vespamalloc::createAllocator()->realloc(ptr, sz); } -void* memalign(size_t align, size_t sz) __attribute__((visibility ("default"))); -void* memalign(size_t align, size_t sz) +void* memalign(size_t align, size_t sz) __THROW __attribute__((visibility ("default"))); +void* memalign(size_t align, size_t sz) __THROW { void *ptr(nullptr); size_t align_1(align - 1); @@ -132,7 +149,6 @@ void* memalign(size_t align, size_t sz) } int posix_memalign(void** ptr, size_t align, size_t sz) __THROW __nonnull((1)) __attribute__((visibility ("default"))); - int posix_memalign(void** ptr, size_t align, size_t sz) __THROW { int retval(0); @@ -181,7 +197,8 @@ void cfree(void *) __THROW __attribute__((leaf void __libc_free(void* ptr) __THROW __attribute__((leaf)) ALIAS("free"); void __libc_cfree(void* ptr) __THROW __attribute__((leaf)) ALIAS("cfree"); #endif -void* __libc_memalign(size_t align, size_t s) ALIAS("memalign"); +struct mallinfo __libc_mallinfo() __THROW ALIAS("mallinfo"); +void* __libc_memalign(size_t align, size_t s) __THROW __attribute__((leaf, malloc, alloc_size(2))) ALIAS("memalign"); int __posix_memalign(void** r, size_t a, size_t s) __THROW __nonnull((1)) ALIAS("posix_memalign"); #undef ALIAS diff --git a/vespamalloc/src/vespamalloc/malloc/threadproxy.cpp b/vespamalloc/src/vespamalloc/malloc/threadproxy.cpp index fd3a1748b92..1fb2a92c71d 100644 --- a/vespamalloc/src/vespamalloc/malloc/threadproxy.cpp +++ b/vespamalloc/src/vespamalloc/malloc/threadproxy.cpp @@ -3,8 +3,7 @@ #include "threadproxy.h" #include <dlfcn.h> #include <pthread.h> -#include <stdio.h> -#include <errno.h> +#include <cstdio> namespace vespamalloc { |