Inside Bitcoin
Opening the selected file…
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1// Copyright (c) 2015-present The Bitcoin Core developers2// Distributed under the MIT software license, see the accompanying3// file COPYING or http://www.opensource.org/licenses/mit-license.php.5#ifndef BITCOIN_MEMUSAGE_H6#define BITCOIN_MEMUSAGE_H8#include <indirectmap.h>9#include <prevector.h>10#include <support/allocators/pool.h>12#include <cassert>13#include <cstdlib>14#include <list>15#include <map>16#include <memory>17#include <set>18#include <string>19#include <vector>20#include <unordered_map>21#include <unordered_set>24namespace memusage25{27/** Compute the total memory used by allocating alloc bytes. */28static size_t MallocUsage(size_t alloc);30/** Dynamic memory usage for built-in types is zero. */31static inline size_t DynamicUsage(const int8_t& v) { return 0; }32static inline size_t DynamicUsage(const uint8_t& v) { return 0; }33static inline size_t DynamicUsage(const int16_t& v) { return 0; }34static inline size_t DynamicUsage(const uint16_t& v) { return 0; }35static inline size_t DynamicUsage(const int32_t& v) { return 0; }36static inline size_t DynamicUsage(const uint32_t& v) { return 0; }37static inline size_t DynamicUsage(const int64_t& v) { return 0; }38static inline size_t DynamicUsage(const uint64_t& v) { return 0; }39static inline size_t DynamicUsage(const float& v) { return 0; }40static inline size_t DynamicUsage(const double& v) { return 0; }41template<typename X> static inline size_t DynamicUsage(X * const &v) { return 0; }42template<typename X> static inline size_t DynamicUsage(const X * const &v) { return 0; }44/** Compute the memory used for dynamically allocated but owned data structures.45* For generic data types, this is *not* recursive. DynamicUsage(vector<vector<int> >)46* will compute the memory used for the vector<int>'s, but not for the ints inside.47* This is for efficiency reasons, as these functions are intended to be fast. If48* application data structures require more accurate inner accounting, they should49* iterate themselves, or use more efficient caching + updating on modification.50*/52static inline size_t MallocUsage(size_t alloc)53{54// Measured on libc6 2.19 on Linux.55if (alloc == 0) {56return 0;57} else if (sizeof(void*) == 8) {58return ((alloc + 31) >> 4) << 4;59} else if (sizeof(void*) == 4) {60return ((alloc + 15) >> 3) << 3;61} else {62assert(0);63}64}66// STL data structures68template<typename X>69struct stl_tree_node70{71private:72int color;73void* parent;74void* left;75void* right;76X x;77};79struct stl_shared_counter80{81/* Various platforms use different sized counters here.82* Conservatively assume that they won't be larger than size_t. */83void* class_type;84size_t use_count;85size_t weak_count;86};88template<typename T, typename Allocator>89static inline size_t DynamicUsage(const std::vector<T, Allocator>& v)90{91return MallocUsage(v.capacity() * sizeof(T));92}94static inline size_t DynamicUsage(const std::string& s)95{96const char* s_ptr = reinterpret_cast<const char*>(&s);97// Don't count the dynamic memory used for string, if it resides in the98// "small string" optimization area (which stores data inside the object itself, up to some99// size; 15 bytes in modern libstdc++).100if (!std::less{}(s.data(), s_ptr) && !std::greater{}(s.data() + s.size(), s_ptr + sizeof(s))) {101return 0;102}103return MallocUsage(s.capacity());104}106template<unsigned int N, typename X, typename S, typename D>107static inline size_t DynamicUsage(const prevector<N, X, S, D>& v)108{109return MallocUsage(v.allocated_memory());110}112template<typename X, typename Y>113static inline size_t DynamicUsage(const std::set<X, Y>& s)114{115return MallocUsage(sizeof(stl_tree_node<X>)) * s.size();116}118template<typename X, typename Y>119static inline size_t IncrementalDynamicUsage(const std::set<X, Y>& s)120{121return MallocUsage(sizeof(stl_tree_node<X>));122}124template<typename X, typename Y, typename Z>125static inline size_t DynamicUsage(const std::map<X, Y, Z>& m)126{127return MallocUsage(sizeof(stl_tree_node<std::pair<const X, Y> >)) * m.size();128}130template<typename X, typename Y, typename Z>131static inline size_t IncrementalDynamicUsage(const std::map<X, Y, Z>& m)132{133return MallocUsage(sizeof(stl_tree_node<std::pair<const X, Y> >));134}136// indirectmap has underlying map with pointer as key138template<typename X, typename Y>139static inline size_t DynamicUsage(const indirectmap<X, Y>& m)140{141return MallocUsage(sizeof(stl_tree_node<std::pair<const X*, Y> >)) * m.size();142}144template<typename X, typename Y>145static inline size_t IncrementalDynamicUsage(const indirectmap<X, Y>& m)146{147return MallocUsage(sizeof(stl_tree_node<std::pair<const X*, Y> >));148}150template<typename X>151static inline size_t DynamicUsage(const std::unique_ptr<X>& p)152{153return p ? MallocUsage(sizeof(X)) : 0;154}156template<typename X>157static inline size_t DynamicUsage(const std::shared_ptr<X>& p)158{159// A shared_ptr can either use a single continuous memory block for both160// the counter and the storage (when using std::make_shared), or separate.161// We can't observe the difference, however, so assume the worst.162return p ? MallocUsage(sizeof(X)) + MallocUsage(sizeof(stl_shared_counter)) : 0;163}165template<typename X>166struct list_node167{168private:169void* ptr_next;170void* ptr_prev;171X x;172};174template<typename X>175static inline size_t DynamicUsage(const std::list<X>& l)176{177return MallocUsage(sizeof(list_node<X>)) * l.size();178}180template<typename X>181struct unordered_node : private X182{183private:184void* ptr;185};187template<typename X, typename Y>188static inline size_t DynamicUsage(const std::unordered_set<X, Y>& s)189{190return MallocUsage(sizeof(unordered_node<X>)) * s.size() + MallocUsage(sizeof(void*) * s.bucket_count());191}193template<typename X, typename Y, typename Z>194static inline size_t DynamicUsage(const std::unordered_map<X, Y, Z>& m)195{196return MallocUsage(sizeof(unordered_node<std::pair<const X, Y> >)) * m.size() + MallocUsage(sizeof(void*) * m.bucket_count());197}199template <class Key, class T, class Hash, class Pred, std::size_t MAX_BLOCK_SIZE_BYTES, std::size_t ALIGN_BYTES>200static inline size_t DynamicUsage(const std::unordered_map<Key,201T,202Hash,203Pred,204PoolAllocator<std::pair<const Key, T>,205MAX_BLOCK_SIZE_BYTES,206ALIGN_BYTES>>& m)207{208auto* pool_resource = m.get_allocator().resource();210// The allocated chunks are stored in a std::list. Size per node should211// therefore be 3 pointers: next, previous, and a pointer to the chunk.212size_t estimated_list_node_size = MallocUsage(sizeof(void*) * 3);213size_t usage_resource = estimated_list_node_size * pool_resource->NumAllocatedChunks();214size_t usage_chunks = MallocUsage(pool_resource->ChunkSizeBytes()) * pool_resource->NumAllocatedChunks();215return usage_resource + usage_chunks + MallocUsage(sizeof(void*) * m.bucket_count());216}218} // namespace memusage220#endif // BITCOIN_MEMUSAGE_H
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.