Line data Source code
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.
4 :
5 : #ifndef BITCOIN_MEMUSAGE_H
6 : #define BITCOIN_MEMUSAGE_H
7 :
8 : #include <stdlib.h>
9 :
10 : #include <map>
11 : #include <set>
12 : #include <vector>
13 :
14 : #include <boost/foreach.hpp>
15 : #include <boost/unordered_set.hpp>
16 : #include <boost/unordered_map.hpp>
17 :
18 : namespace memusage
19 : {
20 :
21 : /** Compute the total memory used by allocating alloc bytes. */
22 : static size_t MallocUsage(size_t alloc);
23 :
24 : /** Dynamic memory usage for built-in types is zero. */
25 : static inline size_t DynamicUsage(const int8_t& v) { return 0; }
26 : static inline size_t DynamicUsage(const uint8_t& v) { return 0; }
27 : static inline size_t DynamicUsage(const int16_t& v) { return 0; }
28 : static inline size_t DynamicUsage(const uint16_t& v) { return 0; }
29 : static inline size_t DynamicUsage(const int32_t& v) { return 0; }
30 : static inline size_t DynamicUsage(const uint32_t& v) { return 0; }
31 : static inline size_t DynamicUsage(const int64_t& v) { return 0; }
32 : static inline size_t DynamicUsage(const uint64_t& v) { return 0; }
33 : static inline size_t DynamicUsage(const float& v) { return 0; }
34 : static inline size_t DynamicUsage(const double& v) { return 0; }
35 : template<typename X> static inline size_t DynamicUsage(X * const &v) { return 0; }
36 : template<typename X> static inline size_t DynamicUsage(const X * const &v) { return 0; }
37 :
38 : /** Compute the memory used for dynamically allocated but owned data structures.
39 : * For generic data types, this is *not* recursive. DynamicUsage(vector<vector<int> >)
40 : * will compute the memory used for the vector<int>'s, but not for the ints inside.
41 : * This is for efficiency reasons, as these functions are intended to be fast. If
42 : * application data structures require more accurate inner accounting, they should
43 : * iterate themselves, or use more efficient caching + updating on modification.
44 : */
45 :
46 : static inline size_t MallocUsage(size_t alloc)
47 : {
48 : // Measured on libc6 2.19 on Linux.
49 : if (sizeof(void*) == 8) {
50 1438748 : return ((alloc + 31) >> 4) << 4;
51 : } else if (sizeof(void*) == 4) {
52 : return ((alloc + 15) >> 3) << 3;
53 : } else {
54 : assert(0);
55 : }
56 : }
57 :
58 : // STL data structures
59 :
60 : template<typename X>
61 : struct stl_tree_node
62 : {
63 : private:
64 : int color;
65 : void* parent;
66 : void* left;
67 : void* right;
68 : X x;
69 : };
70 :
71 : template<typename X>
72 : static inline size_t DynamicUsage(const std::vector<X>& v)
73 : {
74 2801222 : return MallocUsage(v.capacity() * sizeof(X));
75 : }
76 :
77 : template<typename X, typename Y>
78 : static inline size_t DynamicUsage(const std::set<X, Y>& s)
79 : {
80 74544 : return MallocUsage(sizeof(stl_tree_node<X>)) * s.size();
81 : }
82 :
83 : template<typename X, typename Y>
84 0 : static inline size_t IncrementalDynamicUsage(const std::set<X, Y>& s)
85 : {
86 0 : return MallocUsage(sizeof(stl_tree_node<X>));
87 : }
88 :
89 : template<typename X, typename Y, typename Z>
90 : static inline size_t DynamicUsage(const std::map<X, Y, Z>& m)
91 : {
92 6 : return MallocUsage(sizeof(stl_tree_node<std::pair<const X, Y> >)) * m.size();
93 : }
94 :
95 : template<typename X, typename Y, typename Z>
96 : static inline size_t IncrementalDynamicUsage(const std::map<X, Y, Z>& m)
97 : {
98 : return MallocUsage(sizeof(stl_tree_node<std::pair<const X, Y> >));
99 : }
100 :
101 : // Boost data structures
102 :
103 : template<typename X>
104 : struct boost_unordered_node : private X
105 : {
106 : private:
107 : void* ptr;
108 : };
109 :
110 : template<typename X, typename Y>
111 : static inline size_t DynamicUsage(const boost::unordered_set<X, Y>& s)
112 : {
113 : return MallocUsage(sizeof(boost_unordered_node<X>)) * s.size() + MallocUsage(sizeof(void*) * s.bucket_count());
114 : }
115 :
116 : template<typename X, typename Y, typename Z>
117 : static inline size_t DynamicUsage(const boost::unordered_map<X, Y, Z>& m)
118 : {
119 76274 : return MallocUsage(sizeof(boost_unordered_node<std::pair<const X, Y> >)) * m.size() + MallocUsage(sizeof(void*) * m.bucket_count());
120 : }
121 :
122 : }
123 :
124 : #endif
|