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

@@ -41,8 +41,10 @@ public:
// not sealed. // not sealed.
void addref() const { ++referenceCount; } void addref() const { ++referenceCount; }
void delref() const { void delref() const {
if (delref_no_destroy()) if (delref_no_destroy()) {
delete (Subclass *)this; ((Subclass *)this)->~Subclass();
safe_free((void *)this, sizeof(Subclass));
}
} }
bool delref_no_destroy() const { return !--referenceCount; } bool delref_no_destroy() const { return !--referenceCount; }
int32_t debugGetReferenceCount() const { int32_t debugGetReferenceCount() const {
@@ -146,7 +148,8 @@ private:
}; };
template <class P, class... Args> Reference<P> makeReference(Args &&...args) { template <class P, class... Args> Reference<P> makeReference(Args &&...args) {
return Reference<P>(new P(std::forward<Args>(args)...)); return Reference<P>(new (safe_malloc(sizeof(P)))
P(std::forward<Args>(args)...));
} }
template <class P> template <class P>

View File

@@ -27,8 +27,8 @@ inline bool debugVerboseEnabled = true;
// GCOVR_EXCL_START // GCOVR_EXCL_START
#if SHOW_MEMORY #if SHOW_MEMORY
inline int64_t mallocBytes = 0; inline __attribute__((visibility("default"))) int64_t mallocBytes = 0;
inline int64_t peakMallocBytes = 0; inline __attribute__((visibility("default"))) int64_t peakMallocBytes = 0;
#endif #endif
inline thread_local int64_t mallocBytesDelta = 0; 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); 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 ==================== // ==================== BEGIN ARENA IMPL ====================
/// Group allocations with similar lifetimes to amortize the cost of malloc/free /// Group allocations with similar lifetimes to amortize the cost of malloc/free

View File

@@ -1354,12 +1354,10 @@ VersionedMap::Impl *cast(const VersionedMap &m) {
struct __attribute__((visibility("default"))) PeakPrinter { struct __attribute__((visibility("default"))) PeakPrinter {
~PeakPrinter() { ~PeakPrinter() {
printf("--- versioned_map ---\n"); printf("--- versioned_map ---\n");
printf("malloc bytes: %g\n", double(mallocBytes));
printf("Peak malloc bytes: %g\n", double(peakMallocBytes));
printf("mmap bytes: %g\n", double(mmapBytes)); printf("mmap bytes: %g\n", double(mmapBytes));
printf("Peak mmap bytes: %g\n", double(peakMmapBytes)); printf("Peak mmap bytes: %g\n", double(peakMmapBytes));
} }
} peakPrinter; } peakPrinter2;
#endif #endif
} // namespace weaselab } // namespace weaselab