diff --git a/FdbVersionedMap.cpp b/FdbVersionedMap.cpp index 4bdc391..5ea8940 100644 --- a/FdbVersionedMap.cpp +++ b/FdbVersionedMap.cpp @@ -41,8 +41,10 @@ public: // not sealed. void addref() const { ++referenceCount; } void delref() const { - if (delref_no_destroy()) - delete (Subclass *)this; + if (delref_no_destroy()) { + ((Subclass *)this)->~Subclass(); + safe_free((void *)this, sizeof(Subclass)); + } } bool delref_no_destroy() const { return !--referenceCount; } int32_t debugGetReferenceCount() const { @@ -146,7 +148,8 @@ private: }; template Reference

makeReference(Args &&...args) { - return Reference

(new P(std::forward(args)...)); + return Reference

(new (safe_malloc(sizeof(P))) + P(std::forward(args)...)); } template diff --git a/Internal.h b/Internal.h index 58464bd..a96a9bf 100644 --- a/Internal.h +++ b/Internal.h @@ -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 diff --git a/VersionedMap.cpp b/VersionedMap.cpp index e771efd..ce0e7d9 100644 --- a/VersionedMap.cpp +++ b/VersionedMap.cpp @@ -1354,12 +1354,10 @@ VersionedMap::Impl *cast(const VersionedMap &m) { struct __attribute__((visibility("default"))) PeakPrinter { ~PeakPrinter() { 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("Peak mmap bytes: %g\n", double(peakMmapBytes)); } -} peakPrinter; +} peakPrinter2; #endif } // namespace weaselab