5 #ifndef BITCOIN_MEMUSAGE_H
6 #define BITCOIN_MEMUSAGE_H
16 #include <boost/foreach.hpp>
17 #include <boost/unordered_set.hpp>
18 #include <boost/unordered_map.hpp>
24 static size_t MallocUsage(
size_t alloc);
27 static inline size_t DynamicUsage(
const int8_t& v) {
return 0; }
28 static inline size_t DynamicUsage(
const uint8_t& v) {
return 0; }
29 static inline size_t DynamicUsage(
const int16_t& v) {
return 0; }
30 static inline size_t DynamicUsage(
const uint16_t& v) {
return 0; }
31 static inline size_t DynamicUsage(
const int32_t& v) {
return 0; }
32 static inline size_t DynamicUsage(
const uint32_t& v) {
return 0; }
33 static inline size_t DynamicUsage(
const int64_t& v) {
return 0; }
34 static inline size_t DynamicUsage(
const uint64_t& v) {
return 0; }
35 static inline size_t DynamicUsage(
const float& v) {
return 0; }
36 static inline size_t DynamicUsage(
const double& v) {
return 0; }
37 template<
typename X>
static inline size_t DynamicUsage(
X *
const &v) {
return 0; }
38 template<
typename X>
static inline size_t DynamicUsage(
const X *
const &v) {
return 0; }
48 static inline size_t MallocUsage(
size_t alloc)
53 }
else if (
sizeof(
void*) == 8) {
54 return ((alloc + 31) >> 4) << 4;
55 }
else if (
sizeof(
void*) == 4) {
56 return ((alloc + 15) >> 3) << 3;
85 static inline size_t DynamicUsage(
const std::vector<X>& v)
87 return MallocUsage(v.capacity() *
sizeof(
X));
90 template<
unsigned int N,
typename X,
typename S,
typename D>
96 template<
typename X,
typename Y>
97 static inline size_t DynamicUsage(
const std::set<X, Y>& s)
99 return MallocUsage(
sizeof(stl_tree_node<X>)) * s.size();
102 template<
typename X,
typename Y>
103 static inline size_t IncrementalDynamicUsage(
const std::set<X, Y>& s)
105 return MallocUsage(
sizeof(stl_tree_node<X>));
108 template<
typename X,
typename Y,
typename Z>
109 static inline size_t DynamicUsage(
const std::map<X, Y, Z>& m)
111 return MallocUsage(
sizeof(stl_tree_node<std::pair<const X, Y> >)) * m.size();
114 template<
typename X,
typename Y,
typename Z>
115 static inline size_t IncrementalDynamicUsage(
const std::map<X, Y, Z>& m)
117 return MallocUsage(
sizeof(stl_tree_node<std::pair<const X, Y> >));
122 template<
typename X,
typename Y>
125 return MallocUsage(
sizeof(stl_tree_node<std::pair<const X*, Y> >)) * m.
size();
128 template<
typename X,
typename Y>
131 return MallocUsage(
sizeof(stl_tree_node<std::pair<const X*, Y> >));
135 static inline size_t DynamicUsage(
const std::unique_ptr<X>& p)
137 return p ? MallocUsage(
sizeof(
X)) : 0;
141 static inline size_t DynamicUsage(
const std::shared_ptr<X>& p)
146 return p ? MallocUsage(
sizeof(
X)) + MallocUsage(
sizeof(stl_shared_counter)) : 0;
158 template<
typename X,
typename Y>
159 static inline size_t DynamicUsage(
const boost::unordered_set<X, Y>& s)
161 return MallocUsage(
sizeof(
boost_unordered_node<X>)) * s.size() + MallocUsage(
sizeof(
void*) * s.bucket_count());
164 template<
typename X,
typename Y,
typename Z>
165 static inline size_t DynamicUsage(
const boost::unordered_map<X, Y, Z>& m)
167 return MallocUsage(
sizeof(boost_unordered_node<std::pair<const X, Y> >)) * m.size() + MallocUsage(
sizeof(
void*) * m.bucket_count());
Implements a drop-in replacement for std::vector<T> which stores up to N elements directly (without h...
size_t allocated_memory() const