5 #ifndef PIVX_MEMUSAGE_H
6 #define PIVX_MEMUSAGE_H
17 #include <unordered_map>
18 #include <unordered_set>
24 static size_t MallocUsage(
size_t alloc);
27 static inline size_t DynamicUsage(
const int8_t& v) {
return 0; }
28 static inline size_t DynamicUsage(
const uint8_t& v) {
return 0; }
29 static inline size_t DynamicUsage(
const int16_t& v) {
return 0; }
30 static inline size_t DynamicUsage(
const uint16_t& v) {
return 0; }
31 static inline size_t DynamicUsage(
const int32_t& v) {
return 0; }
32 static inline size_t DynamicUsage(
const uint32_t& v) {
return 0; }
33 static inline size_t DynamicUsage(
const int64_t& v) {
return 0; }
34 static inline size_t DynamicUsage(
const uint64_t& v) {
return 0; }
35 static inline size_t DynamicUsage(
const float& v) {
return 0; }
36 static inline size_t DynamicUsage(
const double& v) {
return 0; }
37 template<
typename X>
static inline size_t DynamicUsage(
X *
const &v) {
return 0; }
38 template<
typename X>
static inline size_t DynamicUsage(
const X *
const &v) {
return 0; }
39 template<
typename X,
typename Y>
static inline size_t DynamicUsage(std::pair<X, Y> &p) {
return 0; }
49 template<
typename X>
static size_t DynamicUsage(
const std::vector<X>& v);
50 template<
typename X>
static size_t DynamicUsage(
const std::set<X>& s);
51 template<
typename X,
typename Y>
static size_t DynamicUsage(
const std::map<X, Y>& m);
52 template<
typename X>
static size_t DynamicUsage(
const X& x);
54 template<
typename X>
static size_t RecursiveDynamicUsage(
const std::vector<X>& v);
55 template<
typename X>
static size_t RecursiveDynamicUsage(
const std::set<X>& v);
56 template<
typename X,
typename Y>
static size_t RecursiveDynamicUsage(
const std::map<X, Y>& v);
57 template<
typename X,
typename Y>
static size_t RecursiveDynamicUsage(
const std::pair<X, Y>& v);
58 template<
typename X>
static size_t RecursiveDynamicUsage(
const X& v);
60 static inline size_t MallocUsage(
size_t alloc)
65 }
else if (
sizeof(
void*) == 8) {
66 return ((alloc + 31) >> 4) << 4;
67 }
else if (
sizeof(
void*) == 4) {
68 return ((alloc + 15) >> 3) << 3;
97 static inline size_t DynamicUsage(
const std::vector<X>& v)
99 return MallocUsage(v.capacity() *
sizeof(
X));
103 static inline size_t RecursiveDynamicUsage(
const std::vector<X>& v)
105 size_t usage = DynamicUsage(v);
106 for (
const X& x : v) {
107 usage += RecursiveDynamicUsage(x);
112 template<
unsigned int N,
typename X,
typename S,
typename D>
118 template<
typename X,
typename Y>
119 static inline size_t DynamicUsage(
const std::set<X, Y>& s)
121 return MallocUsage(
sizeof(stl_tree_node<X>)) * s.size();
125 static inline size_t RecursiveDynamicUsage(
const std::set<X>& v)
127 size_t usage = DynamicUsage(v);
128 for (
const X& x : v) {
129 usage += RecursiveDynamicUsage(x);
134 template<
typename X,
typename Y>
135 static inline size_t IncrementalDynamicUsage(
const std::set<X, Y>& s)
137 return MallocUsage(
sizeof(stl_tree_node<X>));
140 template<
typename X,
typename Y,
typename Z>
141 static inline size_t DynamicUsage(
const std::map<X, Y, Z>& m)
143 return MallocUsage(
sizeof(stl_tree_node<std::pair<const X, Y> >)) * m.size();
146 template<
typename X,
typename Y>
147 static inline size_t DynamicUsage(
const std::map<X, Y>& m)
149 return MallocUsage(
sizeof(stl_tree_node<std::pair<const X, Y> >)) * m.size();
152 template<
typename X,
typename Y>
153 static inline size_t RecursiveDynamicUsage(
const std::map<X, Y>& v)
155 size_t usage = DynamicUsage(v);
156 for (
typename std::map<X, Y>::const_iterator it = v.begin(); it != v.end(); it++) {
157 usage += RecursiveDynamicUsage(*it);
162 template<
typename X,
typename Y,
typename Z>
163 static inline size_t IncrementalDynamicUsage(
const std::map<X, Y, Z>& m)
165 return MallocUsage(
sizeof(stl_tree_node<std::pair<const X, Y> >));
168 template<
typename X,
typename Y>
169 static inline size_t RecursiveDynamicUsage(
const std::pair<X, Y>& v)
171 return RecursiveDynamicUsage(v.first) + RecursiveDynamicUsage(v.second);
175 static inline size_t DynamicUsage(
const std::unique_ptr<X>& p)
177 return p ? MallocUsage(
sizeof(
X)) : 0;
181 static inline size_t DynamicUsage(
const std::shared_ptr<X>& p)
186 return p ? MallocUsage(
sizeof(
X)) + MallocUsage(
sizeof(stl_shared_counter)) : 0;
191 template<
typename X,
typename Y>
194 return MallocUsage(
sizeof(stl_tree_node<std::pair<const X*, Y> >)) * m.
size();
197 template<
typename X,
typename Y>
200 return MallocUsage(
sizeof(stl_tree_node<std::pair<const X*, Y> >));
212 template<
typename X,
typename Y>
213 static inline size_t DynamicUsage(
const std::unordered_set<X, Y>& s)
215 return MallocUsage(
sizeof(
unordered_node<X>)) * s.size() + MallocUsage(
sizeof(
void*) * s.bucket_count());
218 template<
typename X,
typename Y,
typename Z>
219 static inline size_t DynamicUsage(
const std::unordered_map<X, Y, Z>& m)
221 return MallocUsage(
sizeof(unordered_node<std::pair<const X, Y> >)) * m.size() + MallocUsage(
sizeof(
void*) * m.bucket_count());
227 static inline size_t DynamicUsage(
const X& x)
229 return x.DynamicMemoryUsage();
233 static inline size_t RecursiveDynamicUsage(
const X& x)
235 return DynamicUsage(x);
239 static inline size_t RecursiveDynamicUsage(
const std::shared_ptr<X>& p)
241 return p ? memusage::DynamicUsage(p) + RecursiveDynamicUsage(*p) : 0;
Implements a drop-in replacement for std::vector<T> which stores up to N elements directly (without h...
size_t allocated_memory() const