mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-06-28 01:41:57 +02:00
memusage: Add DynamicUsage for std::string
Add DynamicUsage(std::string) which Returns the dynamic allocation of a std::string, or 0 if none (in case of small string optimization).
This commit is contained in:
parent
7596282a55
commit
c6594c0b14
@ -15,6 +15,7 @@
|
|||||||
#include <map>
|
#include <map>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <set>
|
#include <set>
|
||||||
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
#include <unordered_set>
|
#include <unordered_set>
|
||||||
@ -90,6 +91,18 @@ static inline size_t DynamicUsage(const std::vector<T, Allocator>& v)
|
|||||||
return MallocUsage(v.capacity() * sizeof(T));
|
return MallocUsage(v.capacity() * sizeof(T));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static inline size_t DynamicUsage(const std::string& s)
|
||||||
|
{
|
||||||
|
const char* s_ptr = reinterpret_cast<const char*>(&s);
|
||||||
|
// Don't count the dynamic memory used for string, if it resides in the
|
||||||
|
// "small string" optimization area (which stores data inside the object itself, up to some
|
||||||
|
// size; 15 bytes in modern libstdc++).
|
||||||
|
if (!std::less{}(s.data(), s_ptr) && !std::greater{}(s.data() + s.size(), s_ptr + sizeof(s))) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
return MallocUsage(s.capacity());
|
||||||
|
}
|
||||||
|
|
||||||
template<unsigned int N, typename X, typename S, typename D>
|
template<unsigned int N, typename X, typename S, typename D>
|
||||||
static inline size_t DynamicUsage(const prevector<N, X, S, D>& v)
|
static inline size_t DynamicUsage(const prevector<N, X, S, D>& v)
|
||||||
{
|
{
|
||||||
|
Loading…
x
Reference in New Issue
Block a user