diff options
Diffstat (limited to 'tests/malloc_test.cpp')
-rw-r--r-- | tests/malloc_test.cpp | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/tests/malloc_test.cpp b/tests/malloc_test.cpp index 8b670f0e5..4a012786d 100644 --- a/tests/malloc_test.cpp +++ b/tests/malloc_test.cpp @@ -560,3 +560,44 @@ TEST(malloc, reallocarray) { GTEST_LOG_(INFO) << "This test requires a C library with reallocarray.\n"; #endif } + +TEST(malloc, mallinfo) { +#if defined(__BIONIC__) + static size_t sizes[] = { + 8, 32, 128, 4096, 32768, 131072, 1024000, 10240000, 20480000, 300000000 + }; + + constexpr static size_t kMaxAllocs = 50; + + for (size_t size : sizes) { + // If some of these allocations are stuck in a thread cache, then keep + // looping until we make an allocation that changes the total size of the + // memory allocated. + // jemalloc implementations counts the thread cache allocations against + // total memory allocated. + void* ptrs[kMaxAllocs] = {}; + bool pass = false; + for (size_t i = 0; i < kMaxAllocs; i++) { + size_t allocated = mallinfo().uordblks; + ptrs[i] = malloc(size); + ASSERT_TRUE(ptrs[i] != nullptr); + size_t new_allocated = mallinfo().uordblks; + if (allocated != new_allocated) { + size_t usable_size = malloc_usable_size(ptrs[i]); + ASSERT_GE(new_allocated, allocated + usable_size) + << "Failed at size " << size << " usable size " << usable_size; + pass = true; + break; + } + } + for (void* ptr : ptrs) { + free(ptr); + } + ASSERT_TRUE(pass) + << "For size " << size << " allocated bytes did not increase after " + << kMaxAllocs << " allocations."; + } +#else + GTEST_LOG_(INFO) << "Host glibc does not pass this test, skipping.\n"; +#endif +} |