Fabcoin Core  0.16.2
P2P Digital Currency
memusage.h
Go to the documentation of this file.
1 // Copyright (c) 2015-2017 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 FABCOIN_MEMUSAGE_H
6 #define FABCOIN_MEMUSAGE_H
7 
8 #include <indirectmap.h>
9 
10 #include <stdlib.h>
11 
12 #include <map>
13 #include <set>
14 #include <vector>
15 #include <unordered_map>
16 #include <unordered_set>
17 
18 
19 namespace memusage
20 {
21 
23 static size_t MallocUsage(size_t alloc);
24 
26 static inline size_t DynamicUsage(const int8_t& v) { return 0; }
27 static inline size_t DynamicUsage(const uint8_t& v) { return 0; }
28 static inline size_t DynamicUsage(const int16_t& v) { return 0; }
29 static inline size_t DynamicUsage(const uint16_t& v) { return 0; }
30 static inline size_t DynamicUsage(const int32_t& v) { return 0; }
31 static inline size_t DynamicUsage(const uint32_t& v) { return 0; }
32 static inline size_t DynamicUsage(const int64_t& v) { return 0; }
33 static inline size_t DynamicUsage(const uint64_t& v) { return 0; }
34 static inline size_t DynamicUsage(const float& v) { return 0; }
35 static inline size_t DynamicUsage(const double& v) { return 0; }
36 template<typename X> static inline size_t DynamicUsage(X * const &v) { return 0; }
37 template<typename X> static inline size_t DynamicUsage(const X * const &v) { return 0; }
38 
47 static inline size_t MallocUsage(size_t alloc)
48 {
49  // Measured on libc6 2.19 on Linux.
50  if (alloc == 0) {
51  return 0;
52  } else if (sizeof(void*) == 8) {
53  return ((alloc + 31) >> 4) << 4;
54  } else if (sizeof(void*) == 4) {
55  return ((alloc + 15) >> 3) << 3;
56  } else {
57  assert(0);
58  }
59 }
60 
61 // STL data structures
62 
63 template<typename X>
65 {
66 private:
67  int color;
68  void* parent;
69  void* left;
70  void* right;
71  X x;
72 };
73 
75 {
76  /* Various platforms use different sized counters here.
77  * Conservatively assume that they won't be larger than size_t. */
78  void* class_type;
79  size_t use_count;
80  size_t weak_count;
81 };
82 
83 template<typename X>
84 static inline size_t DynamicUsage(const std::vector<X>& v)
85 {
86  return MallocUsage(v.capacity() * sizeof(X));
87 }
88 
89 template<unsigned int N, typename X, typename S, typename D>
90 static inline size_t DynamicUsage(const prevector<N, X, S, D>& v)
91 {
92  return MallocUsage(v.allocated_memory());
93 }
94 
95 template<typename X, typename Y>
96 static inline size_t DynamicUsage(const std::set<X, Y>& s)
97 {
98  return MallocUsage(sizeof(stl_tree_node<X>)) * s.size();
99 }
100 
101 template<typename X, typename Y>
102 static inline size_t IncrementalDynamicUsage(const std::set<X, Y>& s)
103 {
104  return MallocUsage(sizeof(stl_tree_node<X>));
105 }
106 
107 template<typename X, typename Y, typename Z>
108 static inline size_t DynamicUsage(const std::map<X, Y, Z>& m)
109 {
110  return MallocUsage(sizeof(stl_tree_node<std::pair<const X, Y> >)) * m.size();
111 }
112 
113 template<typename X, typename Y, typename Z>
114 static inline size_t IncrementalDynamicUsage(const std::map<X, Y, Z>& m)
115 {
116  return MallocUsage(sizeof(stl_tree_node<std::pair<const X, Y> >));
117 }
118 
119 // indirectmap has underlying map with pointer as key
120 
121 template<typename X, typename Y>
122 static inline size_t DynamicUsage(const indirectmap<X, Y>& m)
123 {
124  return MallocUsage(sizeof(stl_tree_node<std::pair<const X*, Y> >)) * m.size();
125 }
126 
127 template<typename X, typename Y>
128 static inline size_t IncrementalDynamicUsage(const indirectmap<X, Y>& m)
129 {
130  return MallocUsage(sizeof(stl_tree_node<std::pair<const X*, Y> >));
131 }
132 
133 template<typename X>
134 static inline size_t DynamicUsage(const std::unique_ptr<X>& p)
135 {
136  return p ? MallocUsage(sizeof(X)) : 0;
137 }
138 
139 template<typename X>
140 static inline size_t DynamicUsage(const std::shared_ptr<X>& p)
141 {
142  // A shared_ptr can either use a single continuous memory block for both
143  // the counter and the storage (when using std::make_shared), or separate.
144  // We can't observe the difference, however, so assume the worst.
145  return p ? MallocUsage(sizeof(X)) + MallocUsage(sizeof(stl_shared_counter)) : 0;
146 }
147 
148 template<typename X>
149 struct unordered_node : private X
150 {
151 private:
152  void* ptr;
153 };
154 
155 template<typename X, typename Y>
156 static inline size_t DynamicUsage(const std::unordered_set<X, Y>& s)
157 {
158  return MallocUsage(sizeof(unordered_node<X>)) * s.size() + MallocUsage(sizeof(void*) * s.bucket_count());
159 }
160 
161 template<typename X, typename Y, typename Z>
162 static inline size_t DynamicUsage(const std::unordered_map<X, Y, Z>& m)
163 {
164  return MallocUsage(sizeof(unordered_node<std::pair<const X, Y> >)) * m.size() + MallocUsage(sizeof(void*) * m.bucket_count());
165 }
166 
167 }
168 
169 #endif // FABCOIN_MEMUSAGE_H
assert(len-trim+(2 *lenIndices)<=WIDTH)
Implements a drop-in replacement for std::vector<T> which stores up to N elements directly (without h...
Definition: prevector.h:36
size_t allocated_memory() const
Definition: prevector.h:499
#define X(name)
Definition: net.cpp:642
size_type size() const
Definition: indirectmap.h:45