Bitcoin Core  24.99.0
P2P Digital Currency
memusage.h
Go to the documentation of this file.
1 // Copyright (c) 2015-2020 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.
4 
5 #ifndef BITCOIN_MEMUSAGE_H
6 #define BITCOIN_MEMUSAGE_H
7 
8 #include <indirectmap.h>
9 #include <prevector.h>
10 
11 #include <cassert>
12 #include <cstdlib>
13 #include <map>
14 #include <memory>
15 #include <set>
16 #include <vector>
17 #include <unordered_map>
18 #include <unordered_set>
19 
20 
21 namespace memusage
22 {
23 
25 static size_t MallocUsage(size_t alloc);
26 
28 static inline size_t DynamicUsage(const int8_t& v) { return 0; }
29 static inline size_t DynamicUsage(const uint8_t& v) { return 0; }
30 static inline size_t DynamicUsage(const int16_t& v) { return 0; }
31 static inline size_t DynamicUsage(const uint16_t& v) { return 0; }
32 static inline size_t DynamicUsage(const int32_t& v) { return 0; }
33 static inline size_t DynamicUsage(const uint32_t& v) { return 0; }
34 static inline size_t DynamicUsage(const int64_t& v) { return 0; }
35 static inline size_t DynamicUsage(const uint64_t& v) { return 0; }
36 static inline size_t DynamicUsage(const float& v) { return 0; }
37 static inline size_t DynamicUsage(const double& v) { return 0; }
38 template<typename X> static inline size_t DynamicUsage(X * const &v) { return 0; }
39 template<typename X> static inline size_t DynamicUsage(const X * const &v) { return 0; }
40 
49 static inline size_t MallocUsage(size_t alloc)
50 {
51  // Measured on libc6 2.19 on Linux.
52  if (alloc == 0) {
53  return 0;
54  } else if (sizeof(void*) == 8) {
55  return ((alloc + 31) >> 4) << 4;
56  } else if (sizeof(void*) == 4) {
57  return ((alloc + 15) >> 3) << 3;
58  } else {
59  assert(0);
60  }
61 }
62 
63 // STL data structures
64 
65 template<typename X>
67 {
68 private:
69  int color;
70  void* parent;
71  void* left;
72  void* right;
73  X x;
74 };
75 
77 {
78  /* Various platforms use different sized counters here.
79  * Conservatively assume that they won't be larger than size_t. */
80  void* class_type;
81  size_t use_count;
82  size_t weak_count;
83 };
84 
85 template<typename X>
86 static inline size_t DynamicUsage(const std::vector<X>& v)
87 {
88  return MallocUsage(v.capacity() * sizeof(X));
89 }
90 
91 template<unsigned int N, typename X, typename S, typename D>
92 static inline size_t DynamicUsage(const prevector<N, X, S, D>& v)
93 {
94  return MallocUsage(v.allocated_memory());
95 }
96 
97 template<typename X, typename Y>
98 static inline size_t DynamicUsage(const std::set<X, Y>& s)
99 {
100  return MallocUsage(sizeof(stl_tree_node<X>)) * s.size();
101 }
102 
103 template<typename X, typename Y>
104 static inline size_t IncrementalDynamicUsage(const std::set<X, Y>& s)
105 {
106  return MallocUsage(sizeof(stl_tree_node<X>));
107 }
108 
109 template<typename X, typename Y, typename Z>
110 static inline size_t DynamicUsage(const std::map<X, Y, Z>& m)
111 {
112  return MallocUsage(sizeof(stl_tree_node<std::pair<const X, Y> >)) * m.size();
113 }
114 
115 template<typename X, typename Y, typename Z>
116 static inline size_t IncrementalDynamicUsage(const std::map<X, Y, Z>& m)
117 {
118  return MallocUsage(sizeof(stl_tree_node<std::pair<const X, Y> >));
119 }
120 
121 // indirectmap has underlying map with pointer as key
122 
123 template<typename X, typename Y>
124 static inline size_t DynamicUsage(const indirectmap<X, Y>& m)
125 {
126  return MallocUsage(sizeof(stl_tree_node<std::pair<const X*, Y> >)) * m.size();
127 }
128 
129 template<typename X, typename Y>
130 static inline size_t IncrementalDynamicUsage(const indirectmap<X, Y>& m)
131 {
132  return MallocUsage(sizeof(stl_tree_node<std::pair<const X*, Y> >));
133 }
134 
135 template<typename X>
136 static inline size_t DynamicUsage(const std::unique_ptr<X>& p)
137 {
138  return p ? MallocUsage(sizeof(X)) : 0;
139 }
140 
141 template<typename X>
142 static inline size_t DynamicUsage(const std::shared_ptr<X>& p)
143 {
144  // A shared_ptr can either use a single continuous memory block for both
145  // the counter and the storage (when using std::make_shared), or separate.
146  // We can't observe the difference, however, so assume the worst.
147  return p ? MallocUsage(sizeof(X)) + MallocUsage(sizeof(stl_shared_counter)) : 0;
148 }
149 
150 template<typename X>
151 struct unordered_node : private X
152 {
153 private:
154  void* ptr;
155 };
156 
157 template<typename X, typename Y>
158 static inline size_t DynamicUsage(const std::unordered_set<X, Y>& s)
159 {
160  return MallocUsage(sizeof(unordered_node<X>)) * s.size() + MallocUsage(sizeof(void*) * s.bucket_count());
161 }
162 
163 template<typename X, typename Y, typename Z>
164 static inline size_t DynamicUsage(const std::unordered_map<X, Y, Z>& m)
165 {
166  return MallocUsage(sizeof(unordered_node<std::pair<const X, Y> >)) * m.size() + MallocUsage(sizeof(void*) * m.bucket_count());
167 }
168 
169 }
170 
171 #endif // BITCOIN_MEMUSAGE_H
Implements a drop-in replacement for std::vector<T> which stores up to N elements directly (without h...
Definition: prevector.h:37
size_t allocated_memory() const
Definition: prevector.h:512
static size_t DynamicUsage(const int8_t &v)
Dynamic memory usage for built-in types is zero.
Definition: memusage.h:28
static size_t IncrementalDynamicUsage(const std::set< X, Y > &s)
Definition: memusage.h:104
static size_t MallocUsage(size_t alloc)
Compute the total memory used by allocating alloc bytes.
Definition: memusage.h:49
#define X(name)
Definition: net.cpp:608
assert(!tx.IsCoinBase())