1 // Copyright (c) 2015 The Bitcoin developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 #ifndef BITCOIN_MEMUSAGE_H
6 #define BITCOIN_MEMUSAGE_H
14 #include <boost/foreach.hpp>
15 #include <boost/unordered_set.hpp>
16 #include <boost/unordered_map.hpp>
21 /** Compute the total memory used by allocating alloc bytes. */
22 static size_t MallocUsage(size_t alloc);
24 /** Dynamic memory usage for built-in types is zero. */
25 static inline size_t DynamicUsage(const int8_t& v) { return 0; }
26 static inline size_t DynamicUsage(const uint8_t& v) { return 0; }
27 static inline size_t DynamicUsage(const int16_t& v) { return 0; }
28 static inline size_t DynamicUsage(const uint16_t& v) { return 0; }
29 static inline size_t DynamicUsage(const int32_t& v) { return 0; }
30 static inline size_t DynamicUsage(const uint32_t& v) { return 0; }
31 static inline size_t DynamicUsage(const int64_t& v) { return 0; }
32 static inline size_t DynamicUsage(const uint64_t& v) { return 0; }
33 static inline size_t DynamicUsage(const float& v) { return 0; }
34 static inline size_t DynamicUsage(const double& v) { return 0; }
35 template<typename X> static inline size_t DynamicUsage(X * const &v) { return 0; }
36 template<typename X> static inline size_t DynamicUsage(const X * const &v) { return 0; }
38 /** Compute the memory used for dynamically allocated but owned data structures.
39 * For generic data types, this is *not* recursive. DynamicUsage(vector<vector<int> >)
40 * will compute the memory used for the vector<int>'s, but not for the ints inside.
41 * This is for efficiency reasons, as these functions are intended to be fast. If
42 * application data structures require more accurate inner accounting, they should
43 * iterate themselves, or use more efficient caching + updating on modification.
46 static inline size_t MallocUsage(size_t alloc)
48 // Measured on libc6 2.19 on Linux.
49 if (sizeof(void*) == 8) {
50 return ((alloc + 31) >> 4) << 4;
51 } else if (sizeof(void*) == 4) {
52 return ((alloc + 15) >> 3) << 3;
58 // STL data structures
72 static inline size_t DynamicUsage(const std::vector<X>& v)
74 return MallocUsage(v.capacity() * sizeof(X));
78 static inline size_t DynamicUsage(const std::set<X>& s)
80 return MallocUsage(sizeof(stl_tree_node<X>)) * s.size();
83 template<typename X, typename Y>
84 static inline size_t DynamicUsage(const std::map<X, Y>& m)
86 return MallocUsage(sizeof(stl_tree_node<std::pair<const X, Y> >)) * m.size();
89 // Boost data structures
92 struct boost_unordered_node : private X
98 template<typename X, typename Y>
99 static inline size_t DynamicUsage(const boost::unordered_set<X, Y>& s)
101 return MallocUsage(sizeof(boost_unordered_node<X>)) * s.size() + MallocUsage(sizeof(void*) * s.bucket_count());
104 template<typename X, typename Y, typename Z>
105 static inline size_t DynamicUsage(const boost::unordered_map<X, Y, Z>& m)
107 return MallocUsage(sizeof(boost_unordered_node<std::pair<const X, Y> >)) * m.size() + MallocUsage(sizeof(void*) * m.bucket_count());