[tests] Add libFuzzer support.
[bitcoinplatinum.git] / src / memusage.h
blobb69acafffd11a37a9263dacabbc29d4fd2924c17
1 // Copyright (c) 2015-2016 The Bitcoin Core 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 "indirectmap.h"
10 #include <stdlib.h>
12 #include <map>
13 #include <set>
14 #include <vector>
15 #include <unordered_map>
16 #include <unordered_set>
18 #include <boost/foreach.hpp>
19 #include <boost/unordered_set.hpp>
20 #include <boost/unordered_map.hpp>
22 namespace memusage
25 /** Compute the total memory used by allocating alloc bytes. */
26 static size_t MallocUsage(size_t alloc);
28 /** Dynamic memory usage for built-in types is zero. */
29 static inline size_t DynamicUsage(const int8_t& v) { return 0; }
30 static inline size_t DynamicUsage(const uint8_t& v) { return 0; }
31 static inline size_t DynamicUsage(const int16_t& v) { return 0; }
32 static inline size_t DynamicUsage(const uint16_t& v) { return 0; }
33 static inline size_t DynamicUsage(const int32_t& v) { return 0; }
34 static inline size_t DynamicUsage(const uint32_t& v) { return 0; }
35 static inline size_t DynamicUsage(const int64_t& v) { return 0; }
36 static inline size_t DynamicUsage(const uint64_t& v) { return 0; }
37 static inline size_t DynamicUsage(const float& v) { return 0; }
38 static inline size_t DynamicUsage(const double& v) { return 0; }
39 template<typename X> static inline size_t DynamicUsage(X * const &v) { return 0; }
40 template<typename X> static inline size_t DynamicUsage(const X * const &v) { return 0; }
42 /** Compute the memory used for dynamically allocated but owned data structures.
43 * For generic data types, this is *not* recursive. DynamicUsage(vector<vector<int> >)
44 * will compute the memory used for the vector<int>'s, but not for the ints inside.
45 * This is for efficiency reasons, as these functions are intended to be fast. If
46 * application data structures require more accurate inner accounting, they should
47 * iterate themselves, or use more efficient caching + updating on modification.
50 static inline size_t MallocUsage(size_t alloc)
52 // Measured on libc6 2.19 on Linux.
53 if (alloc == 0) {
54 return 0;
55 } else if (sizeof(void*) == 8) {
56 return ((alloc + 31) >> 4) << 4;
57 } else if (sizeof(void*) == 4) {
58 return ((alloc + 15) >> 3) << 3;
59 } else {
60 assert(0);
64 // STL data structures
66 template<typename X>
67 struct stl_tree_node
69 private:
70 int color;
71 void* parent;
72 void* left;
73 void* right;
74 X x;
77 struct stl_shared_counter
79 /* Various platforms use different sized counters here.
80 * Conservatively assume that they won't be larger than size_t. */
81 void* class_type;
82 size_t use_count;
83 size_t weak_count;
86 template<typename X>
87 static inline size_t DynamicUsage(const std::vector<X>& v)
89 return MallocUsage(v.capacity() * sizeof(X));
92 template<unsigned int N, typename X, typename S, typename D>
93 static inline size_t DynamicUsage(const prevector<N, X, S, D>& v)
95 return MallocUsage(v.allocated_memory());
98 template<typename X, typename Y>
99 static inline size_t DynamicUsage(const std::set<X, Y>& s)
101 return MallocUsage(sizeof(stl_tree_node<X>)) * s.size();
104 template<typename X, typename Y>
105 static inline size_t IncrementalDynamicUsage(const std::set<X, Y>& s)
107 return MallocUsage(sizeof(stl_tree_node<X>));
110 template<typename X, typename Y, typename Z>
111 static inline size_t DynamicUsage(const std::map<X, Y, Z>& m)
113 return MallocUsage(sizeof(stl_tree_node<std::pair<const X, Y> >)) * m.size();
116 template<typename X, typename Y, typename Z>
117 static inline size_t IncrementalDynamicUsage(const std::map<X, Y, Z>& m)
119 return MallocUsage(sizeof(stl_tree_node<std::pair<const X, Y> >));
122 // indirectmap has underlying map with pointer as key
124 template<typename X, typename Y>
125 static inline size_t DynamicUsage(const indirectmap<X, Y>& m)
127 return MallocUsage(sizeof(stl_tree_node<std::pair<const X*, Y> >)) * m.size();
130 template<typename X, typename Y>
131 static inline size_t IncrementalDynamicUsage(const indirectmap<X, Y>& m)
133 return MallocUsage(sizeof(stl_tree_node<std::pair<const X*, Y> >));
136 template<typename X>
137 static inline size_t DynamicUsage(const std::unique_ptr<X>& p)
139 return p ? MallocUsage(sizeof(X)) : 0;
142 template<typename X>
143 static inline size_t DynamicUsage(const std::shared_ptr<X>& p)
145 // A shared_ptr can either use a single continuous memory block for both
146 // the counter and the storage (when using std::make_shared), or separate.
147 // We can't observe the difference, however, so assume the worst.
148 return p ? MallocUsage(sizeof(X)) + MallocUsage(sizeof(stl_shared_counter)) : 0;
151 // Boost data structures
153 template<typename X>
154 struct unordered_node : private X
156 private:
157 void* ptr;
160 template<typename X, typename Y>
161 static inline size_t DynamicUsage(const boost::unordered_set<X, Y>& s)
163 return MallocUsage(sizeof(unordered_node<X>)) * s.size() + MallocUsage(sizeof(void*) * s.bucket_count());
166 template<typename X, typename Y, typename Z>
167 static inline size_t DynamicUsage(const boost::unordered_map<X, Y, Z>& m)
169 return MallocUsage(sizeof(unordered_node<std::pair<const X, Y> >)) * m.size() + MallocUsage(sizeof(void*) * m.bucket_count());
172 template<typename X, typename Y>
173 static inline size_t DynamicUsage(const std::unordered_set<X, Y>& s)
175 return MallocUsage(sizeof(unordered_node<X>)) * s.size() + MallocUsage(sizeof(void*) * s.bucket_count());
178 template<typename X, typename Y, typename Z>
179 static inline size_t DynamicUsage(const std::unordered_map<X, Y, Z>& m)
181 return MallocUsage(sizeof(unordered_node<std::pair<const X, Y> >)) * m.size() + MallocUsage(sizeof(void*) * m.bucket_count());
186 #endif // BITCOIN_MEMUSAGE_H