From e863fdaebb545a0595ef22c5114d95ce7baabd19 Mon Sep 17 00:00:00 2001 From: Pieter Wuille Date: Mon, 30 May 2016 16:50:14 +0200 Subject: [PATCH] Add support for unique_ptr and shared_ptr to memusage (cherry picked from commit bitcoin/bitcoin@1b9e6d3c1a0f0e7eeff5ddb2e0386911fe9ab2b6) --- src/memusage.h | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/memusage.h b/src/memusage.h index c88589f22..3899fe61d 100644 --- a/src/memusage.h +++ b/src/memusage.h @@ -69,6 +69,15 @@ private: X x; }; +struct stl_shared_counter +{ + /* Various platforms use different sized counters here. + * Conservatively assume that they won't be larger than size_t. */ + void* class_type; + size_t use_count; + size_t weak_count; +}; + template static inline size_t DynamicUsage(const std::vector& v) { @@ -93,6 +102,21 @@ static inline size_t DynamicUsage(const std::map& m) return MallocUsage(sizeof(stl_tree_node >)) * m.size(); } +template +static inline size_t DynamicUsage(const std::unique_ptr& p) +{ + return p ? MallocUsage(sizeof(X)) : 0; +} + +template +static inline size_t DynamicUsage(const std::shared_ptr& p) +{ + // A shared_ptr can either use a single continuous memory block for both + // the counter and the storage (when using std::make_shared), or separate. + // We can't observe the difference, however, so assume the worst. + return p ? MallocUsage(sizeof(X)) + MallocUsage(sizeof(stl_shared_counter)) : 0; +} + // Boost data structures template