|
|
@ -11,6 +11,7 @@ |
|
|
|
#include <set> |
|
|
|
#include <set> |
|
|
|
#include <vector> |
|
|
|
#include <vector> |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include <boost/foreach.hpp> |
|
|
|
#include <boost/unordered_set.hpp> |
|
|
|
#include <boost/unordered_set.hpp> |
|
|
|
#include <boost/unordered_map.hpp> |
|
|
|
#include <boost/unordered_map.hpp> |
|
|
|
|
|
|
|
|
|
|
@ -20,12 +21,28 @@ namespace memusage |
|
|
|
/** Compute the total memory used by allocating alloc bytes. */ |
|
|
|
/** Compute the total memory used by allocating alloc bytes. */ |
|
|
|
static size_t MallocUsage(size_t alloc); |
|
|
|
static size_t MallocUsage(size_t alloc); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** Dynamic memory usage for built-in types is zero. */ |
|
|
|
|
|
|
|
static inline size_t DynamicUsage(const int8_t& v) { return 0; } |
|
|
|
|
|
|
|
static inline size_t DynamicUsage(const uint8_t& v) { return 0; } |
|
|
|
|
|
|
|
static inline size_t DynamicUsage(const int16_t& v) { return 0; } |
|
|
|
|
|
|
|
static inline size_t DynamicUsage(const uint16_t& v) { return 0; } |
|
|
|
|
|
|
|
static inline size_t DynamicUsage(const int32_t& v) { return 0; } |
|
|
|
|
|
|
|
static inline size_t DynamicUsage(const uint32_t& v) { return 0; } |
|
|
|
|
|
|
|
static inline size_t DynamicUsage(const int64_t& v) { return 0; } |
|
|
|
|
|
|
|
static inline size_t DynamicUsage(const uint64_t& v) { return 0; } |
|
|
|
|
|
|
|
static inline size_t DynamicUsage(const float& v) { return 0; } |
|
|
|
|
|
|
|
static inline size_t DynamicUsage(const double& v) { return 0; } |
|
|
|
|
|
|
|
template<typename X> static inline size_t DynamicUsage(X * const &v) { return 0; } |
|
|
|
|
|
|
|
template<typename X> static inline size_t DynamicUsage(const X * const &v) { return 0; } |
|
|
|
|
|
|
|
template<typename X, typename Y> static inline size_t DynamicUsage(std::pair<X, Y> &p) { return 0; } |
|
|
|
|
|
|
|
|
|
|
|
/** Compute the memory used for dynamically allocated but owned data structures.
|
|
|
|
/** Compute the memory used for dynamically allocated but owned data structures.
|
|
|
|
* For generic data types, this is *not* recursive. DynamicUsage(vector<vector<int> >) |
|
|
|
* For generic data types, this is *not* recursive. DynamicUsage(vector<vector<int> >) |
|
|
|
* will compute the memory used for the vector<int>'s, but not for the ints inside. |
|
|
|
* will compute the memory used for the vector<int>'s, but not for the ints inside. |
|
|
|
* This is for efficiency reasons, as these functions are intended to be fast. If |
|
|
|
* This is for efficiency reasons, as these functions are intended to be fast. If |
|
|
|
* application data structures require more accurate inner accounting, they should |
|
|
|
* application data structures require more accurate inner accounting, they should |
|
|
|
* do the recursion themselves, or use more efficient caching + updating on modification. |
|
|
|
* use RecursiveDynamicUsage, iterate themselves, or use more efficient caching + |
|
|
|
|
|
|
|
* updating on modification. |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
template<typename X> static size_t DynamicUsage(const std::vector<X>& v); |
|
|
|
template<typename X> static size_t DynamicUsage(const std::vector<X>& v); |
|
|
|
template<typename X> static size_t DynamicUsage(const std::set<X>& s); |
|
|
|
template<typename X> static size_t DynamicUsage(const std::set<X>& s); |
|
|
@ -34,6 +51,12 @@ template<typename X, typename Y> static size_t DynamicUsage(const boost::unorder |
|
|
|
template<typename X, typename Y, typename Z> static size_t DynamicUsage(const boost::unordered_map<X, Y, Z>& s); |
|
|
|
template<typename X, typename Y, typename Z> static size_t DynamicUsage(const boost::unordered_map<X, Y, Z>& s); |
|
|
|
template<typename X> static size_t DynamicUsage(const X& x); |
|
|
|
template<typename X> static size_t DynamicUsage(const X& x); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template<typename X> static size_t RecursiveDynamicUsage(const std::vector<X>& v); |
|
|
|
|
|
|
|
template<typename X> static size_t RecursiveDynamicUsage(const std::set<X>& v); |
|
|
|
|
|
|
|
template<typename X, typename Y> static size_t RecursiveDynamicUsage(const std::map<X, Y>& v); |
|
|
|
|
|
|
|
template<typename X, typename Y> static size_t RecursiveDynamicUsage(const std::pair<X, Y>& v); |
|
|
|
|
|
|
|
template<typename X> static size_t RecursiveDynamicUsage(const X& v); |
|
|
|
|
|
|
|
|
|
|
|
static inline size_t MallocUsage(size_t alloc) |
|
|
|
static inline size_t MallocUsage(size_t alloc) |
|
|
|
{ |
|
|
|
{ |
|
|
|
// Measured on libc6 2.19 on Linux.
|
|
|
|
// Measured on libc6 2.19 on Linux.
|
|
|
@ -65,18 +88,54 @@ static inline size_t DynamicUsage(const std::vector<X>& v) |
|
|
|
return MallocUsage(v.capacity() * sizeof(X)); |
|
|
|
return MallocUsage(v.capacity() * sizeof(X)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template<typename X> |
|
|
|
|
|
|
|
static inline size_t RecursiveDynamicUsage(const std::vector<X>& v) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
size_t usage = DynamicUsage(v); |
|
|
|
|
|
|
|
BOOST_FOREACH(const X& x, v) { |
|
|
|
|
|
|
|
usage += RecursiveDynamicUsage(x); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return usage; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
template<typename X> |
|
|
|
template<typename X> |
|
|
|
static inline size_t DynamicUsage(const std::set<X>& s) |
|
|
|
static inline size_t DynamicUsage(const std::set<X>& s) |
|
|
|
{ |
|
|
|
{ |
|
|
|
return MallocUsage(sizeof(stl_tree_node<X>)) * s.size(); |
|
|
|
return MallocUsage(sizeof(stl_tree_node<X>)) * s.size(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template<typename X> |
|
|
|
|
|
|
|
static inline size_t RecursiveDynamicUsage(const std::set<X>& v) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
size_t usage = DynamicUsage(v); |
|
|
|
|
|
|
|
BOOST_FOREACH(const X& x, v) { |
|
|
|
|
|
|
|
usage += RecursiveDynamicUsage(x); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return usage; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
template<typename X, typename Y> |
|
|
|
template<typename X, typename Y> |
|
|
|
static inline size_t DynamicUsage(const std::map<X, Y>& m) |
|
|
|
static inline size_t DynamicUsage(const std::map<X, Y>& m) |
|
|
|
{ |
|
|
|
{ |
|
|
|
return MallocUsage(sizeof(stl_tree_node<std::pair<const X, Y> >)) * m.size(); |
|
|
|
return MallocUsage(sizeof(stl_tree_node<std::pair<const X, Y> >)) * m.size(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template<typename X, typename Y> |
|
|
|
|
|
|
|
static inline size_t RecursiveDynamicUsage(const std::map<X, Y>& v) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
size_t usage = DynamicUsage(v); |
|
|
|
|
|
|
|
for (typename std::map<X, Y>::const_iterator it = v.begin(); it != v.end(); it++) { |
|
|
|
|
|
|
|
usage += RecursiveDynamicUsage(*it); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return usage; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template<typename X, typename Y> |
|
|
|
|
|
|
|
static inline size_t RecursiveDynamicUsage(const std::pair<X, Y>& v) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
return RecursiveDynamicUsage(v.first) + RecursiveDynamicUsage(v.second); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Boost data structures
|
|
|
|
// Boost data structures
|
|
|
|
|
|
|
|
|
|
|
|
template<typename X> |
|
|
|
template<typename X> |
|
|
@ -106,6 +165,12 @@ static inline size_t DynamicUsage(const X& x) |
|
|
|
return x.DynamicMemoryUsage(); |
|
|
|
return x.DynamicMemoryUsage(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template<typename X> |
|
|
|
|
|
|
|
static inline size_t RecursiveDynamicUsage(const X& x) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
return DynamicUsage(x); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
#endif |
|
|
|
#endif |
|
|
|