0
0
Fork 0
mirror of https://github.com/bitcoin/bitcoin.git synced 2025-03-05 14:06:27 -05: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:
laanwj 2024-11-04 18:19:00 +01:00
parent 7596282a55
commit c6594c0b14

View file

@ -15,6 +15,7 @@
#include <map>
#include <memory>
#include <set>
#include <string>
#include <vector>
#include <unordered_map>
#include <unordered_set>
@ -90,6 +91,18 @@ static inline size_t DynamicUsage(const std::vector<T, Allocator>& v)
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>
static inline size_t DynamicUsage(const prevector<N, X, S, D>& v)
{