Improve SHOW_MEMORY accounting

This commit is contained in:
2024-05-28 16:54:46 -07:00
parent dc587e627b
commit 0de303b6c6
3 changed files with 22 additions and 8 deletions

View File

@@ -27,8 +27,8 @@ inline bool debugVerboseEnabled = true;
// GCOVR_EXCL_START
#if SHOW_MEMORY
inline int64_t mallocBytes = 0;
inline int64_t peakMallocBytes = 0;
inline __attribute__((visibility("default"))) int64_t mallocBytes = 0;
inline __attribute__((visibility("default"))) int64_t peakMallocBytes = 0;
#endif
inline thread_local int64_t mallocBytesDelta = 0;
@@ -108,6 +108,19 @@ __attribute__((always_inline)) inline void safe_free(void *p, size_t s) {
free(p);
}
#if SHOW_MEMORY
inline __attribute__((visibility("default"))) bool showedMemory = false;
static struct __attribute__((visibility("default"))) PeakPrinter {
~PeakPrinter() {
if (!std::exchange(showedMemory, true)) {
printf("--- memory usage ---\n");
printf("malloc bytes: %g\n", double(mallocBytes));
printf("Peak malloc bytes: %g\n", double(peakMallocBytes));
}
}
} peakPrinter;
#endif
// ==================== BEGIN ARENA IMPL ====================
/// Group allocations with similar lifetimes to amortize the cost of malloc/free