Index: compiler-rt/trunk/lib/scudo/standalone/local_cache.h =================================================================== --- compiler-rt/trunk/lib/scudo/standalone/local_cache.h +++ compiler-rt/trunk/lib/scudo/standalone/local_cache.h @@ -83,6 +83,7 @@ // performance. It definitely decreases performance on Android though. // if (!SCUDO_ANDROID) PREFETCH(P); Stats.add(StatAllocated, ClassSize); + Stats.sub(StatFree, ClassSize); return P; } @@ -98,6 +99,7 @@ const uptr ClassSize = C->ClassSize; C->Chunks[C->Count++] = P; Stats.sub(StatAllocated, ClassSize); + Stats.add(StatFree, ClassSize); } void drain() { Index: compiler-rt/trunk/lib/scudo/standalone/mutex.h =================================================================== --- compiler-rt/trunk/lib/scudo/standalone/mutex.h +++ compiler-rt/trunk/lib/scudo/standalone/mutex.h @@ -27,10 +27,10 @@ NOINLINE void lock() { if (LIKELY(tryLock())) return; - // The compiler may try to fully unroll the loop, ending up in a - // NumberOfTries*NumberOfYields block of pauses mixed with tryLocks. This - // is large, ugly and unneeded, a compact loop is better for our purpose - // here. Use a pragma to tell the compiler not to unroll the loop. + // The compiler may try to fully unroll the loop, ending up in a + // NumberOfTries*NumberOfYields block of pauses mixed with tryLocks. This + // is large, ugly and unneeded, a compact loop is better for our purpose + // here. Use a pragma to tell the compiler not to unroll the loop. #ifdef __clang__ #pragma nounroll #endif Index: compiler-rt/trunk/lib/scudo/standalone/primary32.h =================================================================== --- compiler-rt/trunk/lib/scudo/standalone/primary32.h +++ compiler-rt/trunk/lib/scudo/standalone/primary32.h @@ -318,6 +318,8 @@ } DCHECK(B); DCHECK_GT(B->getCount(), 0); + + C->getStats().add(StatFree, AllocatedUser); Sci->AllocatedUser += AllocatedUser; if (Sci->CanRelease) Sci->ReleaseInfo.LastReleaseAtNs = getMonotonicTime(); Index: compiler-rt/trunk/lib/scudo/standalone/primary64.h =================================================================== --- compiler-rt/trunk/lib/scudo/standalone/primary64.h +++ compiler-rt/trunk/lib/scudo/standalone/primary64.h @@ -309,6 +309,7 @@ DCHECK(B); DCHECK_GT(B->getCount(), 0); + C->getStats().add(StatFree, AllocatedUser); Region->AllocatedUser += AllocatedUser; Region->Exhausted = false; if (Region->CanRelease) Index: compiler-rt/trunk/lib/scudo/standalone/stats.h =================================================================== --- compiler-rt/trunk/lib/scudo/standalone/stats.h +++ compiler-rt/trunk/lib/scudo/standalone/stats.h @@ -17,7 +17,7 @@ namespace scudo { // Memory allocator statistics -enum StatType { StatAllocated, StatMapped, StatCount }; +enum StatType { StatAllocated, StatFree, StatMapped, StatCount }; typedef uptr StatCounters[StatCount]; Index: compiler-rt/trunk/lib/scudo/standalone/tests/wrappers_c_test.cpp =================================================================== --- compiler-rt/trunk/lib/scudo/standalone/tests/wrappers_c_test.cpp +++ compiler-rt/trunk/lib/scudo/standalone/tests/wrappers_c_test.cpp @@ -191,7 +191,7 @@ #define M_PURGE -101 #endif -TEST(ScudoWrappersCTest, Mallopt) { +TEST(ScudoWrappersCTest, MallOpt) { errno = 0; EXPECT_EQ(mallopt(-1000, 1), 0); // mallopt doesn't set errno. @@ -223,3 +223,19 @@ EXPECT_EQ(valloc(SIZE_MAX), nullptr); } + +TEST(ScudoWrappersCTest, MallInfo) { + const size_t BypassQuarantineSize = 1024U; + + struct mallinfo MI = mallinfo(); + size_t Allocated = MI.uordblks; + void *P = malloc(BypassQuarantineSize); + EXPECT_NE(P, nullptr); + MI = mallinfo(); + EXPECT_GE(static_cast(MI.uordblks), Allocated + BypassQuarantineSize); + EXPECT_GT(static_cast(MI.hblkhd), 0U); + size_t Free = MI.fordblks; + free(P); + MI = mallinfo(); + EXPECT_GE(static_cast(MI.fordblks), Free + BypassQuarantineSize); +} Index: compiler-rt/trunk/lib/scudo/standalone/wrappers_c.inc =================================================================== --- compiler-rt/trunk/lib/scudo/standalone/wrappers_c.inc +++ compiler-rt/trunk/lib/scudo/standalone/wrappers_c.inc @@ -38,8 +38,17 @@ struct SCUDO_MALLINFO Info = {}; scudo::StatCounters Stats; SCUDO_ALLOCATOR.getStats(Stats); + // Space allocated in mmapped regions (bytes) + Info.hblkhd = static_cast<__scudo_mallinfo_data_t>(Stats[scudo::StatMapped]); + // Maximum total allocated space (bytes) + Info.usmblks = Info.hblkhd; + // Space in freed fastbin blocks (bytes) + Info.fsmblks = static_cast<__scudo_mallinfo_data_t>(Stats[scudo::StatFree]); + // Total allocated space (bytes) Info.uordblks = static_cast<__scudo_mallinfo_data_t>(Stats[scudo::StatAllocated]); + // Total free space (bytes) + Info.fordblks = Info.fsmblks; return Info; }