Bitcoin Core 22.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 <stdlib.h>
12
13#include <cassert>
14#include <map>
15#include <memory>
16#include <set>
17#include <vector>
18#include <unordered_map>
19#include <unordered_set>
20
21
22namespace memusage
23{
24
26static size_t MallocUsage(size_t alloc);
27
29static inline size_t DynamicUsage(const int8_t& v) { return 0; }
30static inline size_t DynamicUsage(const uint8_t& v) { return 0; }
31static inline size_t DynamicUsage(const int16_t& v) { return 0; }
32static inline size_t DynamicUsage(const uint16_t& v) { return 0; }
33static inline size_t DynamicUsage(const int32_t& v) { return 0; }
34static inline size_t DynamicUsage(const uint32_t& v) { return 0; }
35static inline size_t DynamicUsage(const int64_t& v) { return 0; }
36static inline size_t DynamicUsage(const uint64_t& v) { return 0; }
37static inline size_t DynamicUsage(const float& v) { return 0; }
38static inline size_t DynamicUsage(const double& v) { return 0; }
39template<typename X> static inline size_t DynamicUsage(X * const &v) { return 0; }
40template<typename X> static inline size_t DynamicUsage(const X * const &v) { return 0; }
41
50static inline size_t MallocUsage(size_t alloc)
51{
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);
61 }
62}
63
64// STL data structures
65
66template<typename X>
68{
69private:
70 int color;
71 void* parent;
72 void* left;
73 void* right;
75};
76
78{
79 /* Various platforms use different sized counters here.
80 * Conservatively assume that they won't be larger than size_t. */
82 size_t use_count;
83 size_t weak_count;
84};
85
86template<typename X>
87static inline size_t DynamicUsage(const std::vector<X>& v)
88{
89 return MallocUsage(v.capacity() * sizeof(X));
90}
91
92template<unsigned int N, typename X, typename S, typename D>
93static inline size_t DynamicUsage(const prevector<N, X, S, D>& v)
94{
95 return MallocUsage(v.allocated_memory());
96}
97
98template<typename X, typename Y>
99static inline size_t DynamicUsage(const std::set<X, Y>& s)
100{
101 return MallocUsage(sizeof(stl_tree_node<X>)) * s.size();
102}
103
104template<typename X, typename Y>
105static inline size_t IncrementalDynamicUsage(const std::set<X, Y>& s)
106{
107 return MallocUsage(sizeof(stl_tree_node<X>));
108}
109
110template<typename X, typename Y, typename Z>
111static inline size_t DynamicUsage(const std::map<X, Y, Z>& m)
112{
113 return MallocUsage(sizeof(stl_tree_node<std::pair<const X, Y> >)) * m.size();
114}
115
116template<typename X, typename Y, typename Z>
117static inline size_t IncrementalDynamicUsage(const std::map<X, Y, Z>& m)
118{
119 return MallocUsage(sizeof(stl_tree_node<std::pair<const X, Y> >));
120}
121
122// indirectmap has underlying map with pointer as key
123
124template<typename X, typename Y>
125static inline size_t DynamicUsage(const indirectmap<X, Y>& m)
126{
127 return MallocUsage(sizeof(stl_tree_node<std::pair<const X*, Y> >)) * m.size();
128}
129
130template<typename X, typename Y>
131static inline size_t IncrementalDynamicUsage(const indirectmap<X, Y>& m)
132{
133 return MallocUsage(sizeof(stl_tree_node<std::pair<const X*, Y> >));
134}
135
136template<typename X>
137static inline size_t DynamicUsage(const std::unique_ptr<X>& p)
138{
139 return p ? MallocUsage(sizeof(X)) : 0;
140}
141
142template<typename X>
143static inline size_t DynamicUsage(const std::shared_ptr<X>& p)
144{
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;
149}
150
151template<typename X>
152struct unordered_node : private X
153{
154private:
155 void* ptr;
156};
157
158template<typename X, typename Y>
159static inline size_t DynamicUsage(const std::unordered_set<X, Y>& s)
160{
161 return MallocUsage(sizeof(unordered_node<X>)) * s.size() + MallocUsage(sizeof(void*) * s.bucket_count());
162}
163
164template<typename X, typename Y, typename Z>
165static inline size_t DynamicUsage(const std::unordered_map<X, Y, Z>& m)
166{
167 return MallocUsage(sizeof(unordered_node<std::pair<const X, Y> >)) * m.size() + MallocUsage(sizeof(void*) * m.bucket_count());
168}
169
170}
171
172#endif // BITCOIN_MEMUSAGE_H
size_type size() const
Definition: indirectmap.h:47
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:520
static size_t DynamicUsage(const int8_t &v)
Dynamic memory usage for built-in types is zero.
Definition: memusage.h:29
static size_t IncrementalDynamicUsage(const std::set< X, Y > &s)
Definition: memusage.h:105
static size_t MallocUsage(size_t alloc)
Compute the total memory used by allocating alloc bytes.
Definition: memusage.h:50
#define X(name)
Definition: net.cpp:574
assert(!tx.IsCoinBase())