// Copyright (c) 2015 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #ifndef BITCOIN_MEMUSAGE_H #define BITCOIN_MEMUSAGE_H #include "indirectmap.h" #include #include #include #include #include #include #include #include #include namespace memusage { /** Compute the total memory used by allocating alloc bytes. */ static size_t MallocUsage(size_t alloc); /** Dynamic memory usage for built-in types is zero. */ static inline size_t DynamicUsage(const int8_t& v) { return 0; } static inline size_t DynamicUsage(const uint8_t& v) { return 0; } static inline size_t DynamicUsage(const int16_t& v) { return 0; } static inline size_t DynamicUsage(const uint16_t& v) { return 0; } static inline size_t DynamicUsage(const int32_t& v) { return 0; } static inline size_t DynamicUsage(const uint32_t& v) { return 0; } static inline size_t DynamicUsage(const int64_t& v) { return 0; } static inline size_t DynamicUsage(const uint64_t& v) { return 0; } static inline size_t DynamicUsage(const float& v) { return 0; } static inline size_t DynamicUsage(const double& v) { return 0; } template static inline size_t DynamicUsage(X * const &v) { return 0; } template static inline size_t DynamicUsage(const X * const &v) { return 0; } /** Compute the memory used for dynamically allocated but owned data structures. * For generic data types, this is *not* recursive. DynamicUsage(vector >) * will compute the memory used for the vector's, but not for the ints inside. * This is for efficiency reasons, as these functions are intended to be fast. If * application data structures require more accurate inner accounting, they should * iterate themselves, or use more efficient caching + updating on modification. */ static inline size_t MallocUsage(size_t alloc) { // Measured on libc6 2.19 on Linux. if (alloc == 0) { return 0; } else if (sizeof(void*) == 8) { return ((alloc + 31) >> 4) << 4; } else if (sizeof(void*) == 4) { return ((alloc + 15) >> 3) << 3; } else { assert(0); } } // STL data structures template struct stl_tree_node { private: int color; void* parent; void* left; void* right; 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) { return MallocUsage(v.capacity() * sizeof(X)); } template static inline size_t DynamicUsage(const prevector& v) { return MallocUsage(v.allocated_memory()); } template static inline size_t DynamicUsage(const std::set& s) { return MallocUsage(sizeof(stl_tree_node)) * s.size(); } template static inline size_t IncrementalDynamicUsage(const std::set& s) { return MallocUsage(sizeof(stl_tree_node)); } template static inline size_t DynamicUsage(const std::map& m) { return MallocUsage(sizeof(stl_tree_node >)) * m.size(); } template static inline size_t IncrementalDynamicUsage(const std::map& m) { return MallocUsage(sizeof(stl_tree_node >)); } // indirectmap has underlying map with pointer as key template static inline size_t DynamicUsage(const indirectmap& m) { return MallocUsage(sizeof(stl_tree_node >)) * m.size(); } template static inline size_t IncrementalDynamicUsage(const indirectmap& m) { return MallocUsage(sizeof(stl_tree_node >)); } 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 struct unordered_node : private X { private: void* ptr; }; template static inline size_t DynamicUsage(const boost::unordered_set& s) { return MallocUsage(sizeof(unordered_node)) * s.size() + MallocUsage(sizeof(void*) * s.bucket_count()); } template static inline size_t DynamicUsage(const boost::unordered_map& m) { return MallocUsage(sizeof(unordered_node >)) * m.size() + MallocUsage(sizeof(void*) * m.bucket_count()); } template static inline size_t DynamicUsage(const std::unordered_set& s) { return MallocUsage(sizeof(unordered_node)) * s.size() + MallocUsage(sizeof(void*) * s.bucket_count()); } template static inline size_t DynamicUsage(const std::unordered_map& m) { return MallocUsage(sizeof(unordered_node >)) * m.size() + MallocUsage(sizeof(void*) * m.bucket_count()); } } #endif // BITCOIN_MEMUSAGE_H