aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/memusage.h13
1 files changed, 13 insertions, 0 deletions
diff --git a/src/memusage.h b/src/memusage.h
index 43407507ed..9d9e549ef2 100644
--- a/src/memusage.h
+++ b/src/memusage.h
@@ -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)
{