Changeset View
Changeset View
Standalone View
Standalone View
src/memusage.h
// Copyright (c) 2015-2016 The Bitcoin Core developers | // Copyright (c) 2015-2016 The Bitcoin Core developers | ||||
// Distributed under the MIT software license, see the accompanying | // Distributed under the MIT software license, see the accompanying | ||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | // file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
#ifndef BITCOIN_MEMUSAGE_H | #ifndef BITCOIN_MEMUSAGE_H | ||||
#define BITCOIN_MEMUSAGE_H | #define BITCOIN_MEMUSAGE_H | ||||
#include "indirectmap.h" | #include "indirectmap.h" | ||||
#include <cstdlib> | #include <cstdlib> | ||||
#include <map> | #include <map> | ||||
#include <set> | #include <set> | ||||
#include <vector> | #include <vector> | ||||
#include <boost/unordered_map.hpp> | #include <unordered_map> | ||||
#include <boost/unordered_set.hpp> | #include <unordered_set> | ||||
namespace memusage { | 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. */ | /** Dynamic memory usage for built-in types is zero. */ | ||||
static inline size_t DynamicUsage(const int8_t &v) { | static inline size_t DynamicUsage(const int8_t &v) { | ||||
▲ Show 20 Lines • Show All 130 Lines • ▼ Show 20 Lines | static inline size_t DynamicUsage(const std::shared_ptr<X> &p) { | ||||
// counter and the storage (when using std::make_shared), or separate. We | // counter and the storage (when using std::make_shared), or separate. We | ||||
// can't observe the difference, however, so assume the worst. | // can't observe the difference, however, so assume the worst. | ||||
return p ? MallocUsage(sizeof(X)) + MallocUsage(sizeof(stl_shared_counter)) | return p ? MallocUsage(sizeof(X)) + MallocUsage(sizeof(stl_shared_counter)) | ||||
: 0; | : 0; | ||||
} | } | ||||
// Boost data structures | // Boost data structures | ||||
template <typename X> struct boost_unordered_node : private X { | template <typename X> struct unordered_node : private X { | ||||
private: | private: | ||||
void *ptr; | void *ptr; | ||||
}; | }; | ||||
template <typename X, typename Y> | template <typename X, typename Y> | ||||
static inline size_t DynamicUsage(const boost::unordered_set<X, Y> &s) { | static inline size_t DynamicUsage(const std::unordered_set<X, Y> &s) { | ||||
return MallocUsage(sizeof(boost_unordered_node<X>)) * s.size() + | return MallocUsage(sizeof(unordered_node<X>)) * s.size() + | ||||
MallocUsage(sizeof(void *) * s.bucket_count()); | MallocUsage(sizeof(void *) * s.bucket_count()); | ||||
} | } | ||||
template <typename X, typename Y, typename Z> | template <typename X, typename Y, typename Z> | ||||
static inline size_t DynamicUsage(const boost::unordered_map<X, Y, Z> &m) { | static inline size_t DynamicUsage(const std::unordered_map<X, Y, Z> &m) { | ||||
return MallocUsage(sizeof(boost_unordered_node<std::pair<const X, Y>>)) * | return MallocUsage(sizeof(unordered_node<std::pair<const X, Y>>)) * | ||||
m.size() + | m.size() + | ||||
MallocUsage(sizeof(void *) * m.bucket_count()); | MallocUsage(sizeof(void *) * m.bucket_count()); | ||||
} | } | ||||
} | } | ||||
#endif // BITCOIN_MEMUSAGE_H | #endif // BITCOIN_MEMUSAGE_H |