PRCYCoin  2.0.0.7rc1
P2P Digital Currency
memusage.h
Go to the documentation of this file.
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/unordered_set.hpp>
15 #include <boost/unordered_map.hpp>
16 
17 namespace memusage
18 {
19 
21 static size_t MallocUsage(size_t alloc);
22 
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);
36 
37 static inline size_t MallocUsage(size_t alloc)
38 {
39  // Measured on libc6 2.19 on Linux.
40  if (alloc == 0) {
41  return 0;
42  } else if (sizeof(void*) == 8) {
43  return ((alloc + 31) >> 4) << 4;
44  } else if (sizeof(void*) == 4) {
45  return ((alloc + 15) >> 3) << 3;
46  } else {
47  assert(0);
48  }
49 }
50 
51 // STL data structures
52 
53 template<typename X>
55 {
56 private:
57  int color;
58  void* parent;
59  void* left;
60  void* right;
61  X x;
62 };
63 
64 template<typename X>
65 static inline size_t DynamicUsage(const std::vector<X>& v)
66 {
67  return MallocUsage(v.capacity() * sizeof(X));
68 }
69 
70 template<unsigned int N, typename X, typename S, typename D>
71 static inline size_t DynamicUsage(const prevector<N, X, S, D>& v)
72 {
73  return MallocUsage(v.allocated_memory());
74 }
75 
76 template<typename X>
77 static inline size_t DynamicUsage(const std::set<X>& s)
78 {
79  return MallocUsage(sizeof(stl_tree_node<X>)) * s.size();
80 }
81 
82 template<typename X, typename Y>
83 static inline size_t DynamicUsage(const std::map<X, Y>& m)
84 {
85  return MallocUsage(sizeof(stl_tree_node<std::pair<const X, Y> >)) * m.size();
86 }
87 
88 // Boost data structures
89 
90 template<typename X>
91 struct boost_unordered_node : private X
92 {
93 private:
94  void* ptr;
95 };
96 
97 template<typename X, typename Y>
98 static inline size_t DynamicUsage(const boost::unordered_set<X, Y>& s)
99 {
100  return MallocUsage(sizeof(boost_unordered_node<X>)) * s.size() + MallocUsage(sizeof(void*) * s.bucket_count());
101 }
102 
103 template<typename X, typename Y, typename Z>
104 static inline size_t DynamicUsage(const boost::unordered_map<X, Y, Z>& m)
105 {
106  return MallocUsage(sizeof(boost_unordered_node<std::pair<const X, Y> >)) * m.size() + MallocUsage(sizeof(void*) * m.bucket_count());
107 }
108 
109 // Dispatch to class method as fallback
110 
111 template<typename X>
112 static inline size_t DynamicUsage(const X& x)
113 {
114  return x.DynamicMemoryUsage();
115 }
116 
117 }
118 
119 #endif
memusage::stl_tree_node::parent
void * parent
Definition: memusage.h:58
memusage::stl_tree_node::color
int color
Definition: memusage.h:57
memusage::boost_unordered_node
Definition: memusage.h:91
memusage::stl_tree_node::left
void * left
Definition: memusage.h:59
memusage::stl_tree_node
Definition: memusage.h:54
memusage::boost_unordered_node::ptr
void * ptr
Definition: memusage.h:94
prevector
Implements a drop-in replacement for std::vector<T> which stores up to N elements directly (without h...
Definition: prevector.h:36
memusage::stl_tree_node::right
void * right
Definition: memusage.h:60
X
#define X(name)
Definition: net.cpp:639
prevector::allocated_memory
size_t allocated_memory() const
Definition: prevector.h:499
memusage
Definition: memusage.h:17
memusage::stl_tree_node::x
X x
Definition: memusage.h:61