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.
5
#ifndef BITCOIN_MEMUSAGE_H
6
#define BITCOIN_MEMUSAGE_H
14
#include <boost/unordered_set.hpp>
15
#include <boost/unordered_map.hpp>
20
/** Compute the total memory used by allocating alloc bytes. */
21
static size_t MallocUsage(size_t alloc);
23
/** Compute the memory used for dynamically allocated but owned data structures.
24
* For generic data types, this is *not* recursive. DynamicUsage(vector<vector<int> >)
25
* will compute the memory used for the vector<int>'s, but not for the ints inside.
26
* This is for efficiency reasons, as these functions are intended to be fast. If
27
* application data structures require more accurate inner accounting, they should
28
* do the recursion themselves, or use more efficient caching + updating on modification.
30
template<typename X> static size_t DynamicUsage(const std::vector<X>& v);
31
template<typename X> static size_t DynamicUsage(const std::set<X>& s);
32
template<typename X, typename Y> static size_t DynamicUsage(const std::map<X, Y>& m);
33
template<typename X, typename Y> static size_t DynamicUsage(const boost::unordered_set<X, Y>& s);
34
template<typename X, typename Y, typename Z> static size_t DynamicUsage(const boost::unordered_map<X, Y, Z>& s);
35
template<typename X> static size_t DynamicUsage(const X& x);
37
static inline size_t MallocUsage(size_t alloc)
39
// Measured on libc6 2.19 on Linux.
40
if (sizeof(void*) == 8) {
41
return ((alloc + 31) >> 4) << 4;
42
} else if (sizeof(void*) == 4) {
43
return ((alloc + 15) >> 3) << 3;
49
// STL data structures
63
static inline size_t DynamicUsage(const std::vector<X>& v)
65
return MallocUsage(v.capacity() * sizeof(X));
69
static inline size_t DynamicUsage(const std::set<X>& s)
71
return MallocUsage(sizeof(stl_tree_node<X>)) * s.size();
74
template<typename X, typename Y>
75
static inline size_t DynamicUsage(const std::map<X, Y>& m)
77
return MallocUsage(sizeof(stl_tree_node<std::pair<const X, Y> >)) * m.size();
80
// Boost data structures
83
struct boost_unordered_node : private X
89
template<typename X, typename Y>
90
static inline size_t DynamicUsage(const boost::unordered_set<X, Y>& s)
92
return MallocUsage(sizeof(boost_unordered_node<X>)) * s.size() + MallocUsage(sizeof(void*) * s.bucket_count());
95
template<typename X, typename Y, typename Z>
96
static inline size_t DynamicUsage(const boost::unordered_map<X, Y, Z>& m)
98
return MallocUsage(sizeof(boost_unordered_node<std::pair<const X, Y> >)) * m.size() + MallocUsage(sizeof(void*) * m.bucket_count());
101
// Dispatch to class method as fallback
104
static inline size_t DynamicUsage(const X& x)
106
return x.DynamicMemoryUsage();