Dash Core  0.12.2.1
P2P Digital Currency
memusage.h
Go to the documentation of this file.
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.
4 
5 #ifndef BITCOIN_MEMUSAGE_H
6 #define BITCOIN_MEMUSAGE_H
7 
8 #include <stdlib.h>
9 
10 #include <map>
11 #include <set>
12 #include <vector>
13 
14 #include <boost/foreach.hpp>
15 #include <boost/unordered_set.hpp>
16 #include <boost/unordered_map.hpp>
17 
18 namespace memusage
19 {
20 
22 static size_t MallocUsage(size_t alloc);
23 
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; }
37 
46 static inline size_t MallocUsage(size_t alloc)
47 {
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);
57  }
58 }
59 
60 // STL data structures
61 
62 template<typename X>
64 {
65 private:
66  int color;
67  void* parent;
68  void* left;
69  void* right;
70  X x;
71 };
72 
73 template<typename X>
74 static inline size_t DynamicUsage(const std::vector<X>& v)
75 {
76  return MallocUsage(v.capacity() * sizeof(X));
77 }
78 
79 template<unsigned int N, typename X, typename S, typename D>
80 static inline size_t DynamicUsage(const prevector<N, X, S, D>& v)
81 {
82  return MallocUsage(v.allocated_memory());
83 }
84 
85 template<typename X, typename Y>
86 static inline size_t DynamicUsage(const std::set<X, Y>& s)
87 {
88  return MallocUsage(sizeof(stl_tree_node<X>)) * s.size();
89 }
90 
91 template<typename X, typename Y>
92 static inline size_t IncrementalDynamicUsage(const std::set<X, Y>& s)
93 {
94  return MallocUsage(sizeof(stl_tree_node<X>));
95 }
96 
97 template<typename X, typename Y, typename Z>
98 static inline size_t DynamicUsage(const std::map<X, Y, Z>& m)
99 {
100  return MallocUsage(sizeof(stl_tree_node<std::pair<const X, Y> >)) * m.size();
101 }
102 
103 template<typename X, typename Y, typename Z>
104 static inline size_t IncrementalDynamicUsage(const std::map<X, Y, Z>& m)
105 {
106  return MallocUsage(sizeof(stl_tree_node<std::pair<const X, Y> >));
107 }
108 
109 // Boost data structures
110 
111 template<typename X>
112 struct boost_unordered_node : private X
113 {
114 private:
115  void* ptr;
116 };
117 
118 template<typename X, typename Y>
119 static inline size_t DynamicUsage(const boost::unordered_set<X, Y>& s)
120 {
121  return MallocUsage(sizeof(boost_unordered_node<X>)) * s.size() + MallocUsage(sizeof(void*) * s.bucket_count());
122 }
123 
124 template<typename X, typename Y, typename Z>
125 static inline size_t DynamicUsage(const boost::unordered_map<X, Y, Z>& m)
126 {
127  return MallocUsage(sizeof(boost_unordered_node<std::pair<const X, Y> >)) * m.size() + MallocUsage(sizeof(void*) * m.bucket_count());
128 }
129 
130 }
131 
132 #endif // BITCOIN_MEMUSAGE_H
static size_t DynamicUsage(const int8_t &v)
Definition: memusage.h:25
size_t allocated_memory() const
Definition: prevector.h:467
static size_t MallocUsage(size_t alloc)
Definition: memusage.h:46
#define X(name)
Definition: net.cpp:634
static size_t IncrementalDynamicUsage(const std::set< X, Y > &s)
Definition: memusage.h:92