Bitcoin Core 28.0.0
P2P Digital Currency
Loading...
Searching...
No Matches
memusage.h
Go to the documentation of this file.
1// Copyright (c) 2015-2022 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>
11
12#include <cassert>
13#include <cstdlib>
14#include <list>
15#include <map>
16#include <memory>
17#include <set>
18#include <vector>
19#include <unordered_map>
20#include <unordered_set>
21
22
23namespace memusage
24{
25
27static size_t MallocUsage(size_t alloc);
28
30static inline size_t DynamicUsage(const int8_t& v) { return 0; }
31static inline size_t DynamicUsage(const uint8_t& v) { return 0; }
32static inline size_t DynamicUsage(const int16_t& v) { return 0; }
33static inline size_t DynamicUsage(const uint16_t& v) { return 0; }
34static inline size_t DynamicUsage(const int32_t& v) { return 0; }
35static inline size_t DynamicUsage(const uint32_t& v) { return 0; }
36static inline size_t DynamicUsage(const int64_t& v) { return 0; }
37static inline size_t DynamicUsage(const uint64_t& v) { return 0; }
38static inline size_t DynamicUsage(const float& v) { return 0; }
39static inline size_t DynamicUsage(const double& v) { return 0; }
40template<typename X> static inline size_t DynamicUsage(X * const &v) { return 0; }
41template<typename X> static inline size_t DynamicUsage(const X * const &v) { return 0; }
42
51static inline size_t MallocUsage(size_t alloc)
52{
53 // Measured on libc6 2.19 on Linux.
54 if (alloc == 0) {
55 return 0;
56 } else if (sizeof(void*) == 8) {
57 return ((alloc + 31) >> 4) << 4;
58 } else if (sizeof(void*) == 4) {
59 return ((alloc + 15) >> 3) << 3;
60 } else {
61 assert(0);
62 }
63}
64
65// STL data structures
66
67template<typename X>
69{
70private:
71 int color;
72 void* parent;
73 void* left;
74 void* right;
75 X x;
76};
77
79{
80 /* Various platforms use different sized counters here.
81 * Conservatively assume that they won't be larger than size_t. */
83 size_t use_count;
84 size_t weak_count;
85};
86
87template<typename X>
88static inline size_t DynamicUsage(const std::vector<X>& v)
89{
90 return MallocUsage(v.capacity() * sizeof(X));
91}
92
93template<unsigned int N, typename X, typename S, typename D>
94static inline size_t DynamicUsage(const prevector<N, X, S, D>& v)
95{
96 return MallocUsage(v.allocated_memory());
97}
98
99template<typename X, typename Y>
100static inline size_t DynamicUsage(const std::set<X, Y>& s)
101{
102 return MallocUsage(sizeof(stl_tree_node<X>)) * s.size();
103}
104
105template<typename X, typename Y>
106static inline size_t IncrementalDynamicUsage(const std::set<X, Y>& s)
107{
108 return MallocUsage(sizeof(stl_tree_node<X>));
109}
110
111template<typename X, typename Y, typename Z>
112static inline size_t DynamicUsage(const std::map<X, Y, Z>& m)
113{
114 return MallocUsage(sizeof(stl_tree_node<std::pair<const X, Y> >)) * m.size();
115}
116
117template<typename X, typename Y, typename Z>
118static inline size_t IncrementalDynamicUsage(const std::map<X, Y, Z>& m)
119{
120 return MallocUsage(sizeof(stl_tree_node<std::pair<const X, Y> >));
121}
122
123// indirectmap has underlying map with pointer as key
124
125template<typename X, typename Y>
126static inline size_t DynamicUsage(const indirectmap<X, Y>& m)
127{
128 return MallocUsage(sizeof(stl_tree_node<std::pair<const X*, Y> >)) * m.size();
129}
130
131template<typename X, typename Y>
132static inline size_t IncrementalDynamicUsage(const indirectmap<X, Y>& m)
133{
134 return MallocUsage(sizeof(stl_tree_node<std::pair<const X*, Y> >));
135}
136
137template<typename X>
138static inline size_t DynamicUsage(const std::unique_ptr<X>& p)
139{
140 return p ? MallocUsage(sizeof(X)) : 0;
141}
142
143template<typename X>
144static inline size_t DynamicUsage(const std::shared_ptr<X>& p)
145{
146 // A shared_ptr can either use a single continuous memory block for both
147 // the counter and the storage (when using std::make_shared), or separate.
148 // We can't observe the difference, however, so assume the worst.
149 return p ? MallocUsage(sizeof(X)) + MallocUsage(sizeof(stl_shared_counter)) : 0;
150}
151
152template<typename X>
154{
155private:
156 void* ptr_next;
157 void* ptr_prev;
158 X x;
159};
160
161template<typename X>
162static inline size_t DynamicUsage(const std::list<X>& l)
163{
164 return MallocUsage(sizeof(list_node<X>)) * l.size();
165}
166
167template<typename X>
168struct unordered_node : private X
169{
170private:
171 void* ptr;
172};
173
174template<typename X, typename Y>
175static inline size_t DynamicUsage(const std::unordered_set<X, Y>& s)
176{
177 return MallocUsage(sizeof(unordered_node<X>)) * s.size() + MallocUsage(sizeof(void*) * s.bucket_count());
178}
179
180template<typename X, typename Y, typename Z>
181static inline size_t DynamicUsage(const std::unordered_map<X, Y, Z>& m)
182{
183 return MallocUsage(sizeof(unordered_node<std::pair<const X, Y> >)) * m.size() + MallocUsage(sizeof(void*) * m.bucket_count());
184}
185
186template <class Key, class T, class Hash, class Pred, std::size_t MAX_BLOCK_SIZE_BYTES, std::size_t ALIGN_BYTES>
187static inline size_t DynamicUsage(const std::unordered_map<Key,
188 T,
189 Hash,
190 Pred,
191 PoolAllocator<std::pair<const Key, T>,
192 MAX_BLOCK_SIZE_BYTES,
193 ALIGN_BYTES>>& m)
194{
195 auto* pool_resource = m.get_allocator().resource();
196
197 // The allocated chunks are stored in a std::list. Size per node should
198 // therefore be 3 pointers: next, previous, and a pointer to the chunk.
199 size_t estimated_list_node_size = MallocUsage(sizeof(void*) * 3);
200 size_t usage_resource = estimated_list_node_size * pool_resource->NumAllocatedChunks();
201 size_t usage_chunks = MallocUsage(pool_resource->ChunkSizeBytes()) * pool_resource->NumAllocatedChunks();
202 return usage_resource + usage_chunks + MallocUsage(sizeof(void*) * m.bucket_count());
203}
204
205} // namespace memusage
206
207#endif // BITCOIN_MEMUSAGE_H
Forwards all allocations/deallocations to the PoolResource.
Definition pool.h:277
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:524
uint256 Hash(const T &in1)
Compute the 256-bit hash of an object.
Definition hash.h:75
#define T(expected, seed, data)
static size_t DynamicUsage(const int8_t &v)
Dynamic memory usage for built-in types is zero.
Definition memusage.h:30
static size_t IncrementalDynamicUsage(const std::set< X, Y > &s)
Definition memusage.h:106
static size_t MallocUsage(size_t alloc)
Compute the total memory used by allocating alloc bytes.
Definition memusage.h:51
assert(!tx.IsCoinBase())