depends: use c++11
[bitcoinplatinum.git] / src / memusage.h
blob49760e64c78cd293a7720f036778a101aed042f9
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
8 #include <stdlib.h>
10 #include <map>
11 #include <set>
12 #include <vector>
14 #include <boost/foreach.hpp>
15 #include <boost/unordered_set.hpp>
16 #include <boost/unordered_map.hpp>
18 namespace memusage
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 (alloc == 0) {
50 return 0;
51 } else if (sizeof(void*) == 8) {
52 return ((alloc + 31) >> 4) << 4;
53 } else if (sizeof(void*) == 4) {
54 return ((alloc + 15) >> 3) << 3;
55 } else {
56 assert(0);
60 // STL data structures
62 template<typename X>
63 struct stl_tree_node
65 private:
66 int color;
67 void* parent;
68 void* left;
69 void* right;
70 X x;
73 template<typename X>
74 static inline size_t DynamicUsage(const std::vector<X>& v)
76 return MallocUsage(v.capacity() * sizeof(X));
79 template<unsigned int N, typename X, typename S, typename D>
80 static inline size_t DynamicUsage(const prevector<N, X, S, D>& v)
82 return MallocUsage(v.allocated_memory());
85 template<typename X, typename Y>
86 static inline size_t DynamicUsage(const std::set<X, Y>& s)
88 return MallocUsage(sizeof(stl_tree_node<X>)) * s.size();
91 template<typename X, typename Y>
92 static inline size_t IncrementalDynamicUsage(const std::set<X, Y>& s)
94 return MallocUsage(sizeof(stl_tree_node<X>));
97 template<typename X, typename Y, typename Z>
98 static inline size_t DynamicUsage(const std::map<X, Y, Z>& m)
100 return MallocUsage(sizeof(stl_tree_node<std::pair<const X, Y> >)) * m.size();
103 template<typename X, typename Y, typename Z>
104 static inline size_t IncrementalDynamicUsage(const std::map<X, Y, Z>& m)
106 return MallocUsage(sizeof(stl_tree_node<std::pair<const X, Y> >));
109 // Boost data structures
111 template<typename X>
112 struct boost_unordered_node : private X
114 private:
115 void* ptr;
118 template<typename X, typename Y>
119 static inline size_t DynamicUsage(const boost::unordered_set<X, Y>& s)
121 return MallocUsage(sizeof(boost_unordered_node<X>)) * s.size() + MallocUsage(sizeof(void*) * s.bucket_count());
124 template<typename X, typename Y, typename Z>
125 static inline size_t DynamicUsage(const boost::unordered_map<X, Y, Z>& m)
127 return MallocUsage(sizeof(boost_unordered_node<std::pair<const X, Y> >)) * m.size() + MallocUsage(sizeof(void*) * m.bucket_count());
132 #endif // BITCOIN_MEMUSAGE_H