diff options
author | Martin Leitner-Ankerl <martin.ankerl@gmail.com> | 2022-06-11 09:28:13 +0200 |
---|---|---|
committer | Martin Leitner-Ankerl <martin.ankerl@gmail.com> | 2023-03-23 19:38:38 +0100 |
commit | e19943f049ed8aa4f32a1d8440a9fbf160367f0f (patch) | |
tree | ad5c684a7b0188649c620754635db9a630a25c41 /src/memusage.h | |
parent | b8401c3281978beed6198b2f9782b6a8dd35cbd7 (diff) | |
download | bitcoin-e19943f049ed8aa4f32a1d8440a9fbf160367f0f.tar.xz |
Calculate memory usage correctly for unordered_maps that use PoolAllocator
Extracts the resource from a PoolAllocator and uses it for
calculation of the node's memory usage.
Diffstat (limited to 'src/memusage.h')
-rw-r--r-- | src/memusage.h | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/src/memusage.h b/src/memusage.h index 9755be0ff5..bb39066a7d 100644 --- a/src/memusage.h +++ b/src/memusage.h @@ -7,6 +7,7 @@ #include <indirectmap.h> #include <prevector.h> +#include <support/allocators/pool.h> #include <cassert> #include <cstdlib> @@ -166,6 +167,25 @@ static inline size_t DynamicUsage(const std::unordered_map<X, Y, Z>& m) return MallocUsage(sizeof(unordered_node<std::pair<const X, Y> >)) * m.size() + MallocUsage(sizeof(void*) * m.bucket_count()); } +template <class Key, class T, class Hash, class Pred, std::size_t MAX_BLOCK_SIZE_BYTES, std::size_t ALIGN_BYTES> +static inline size_t DynamicUsage(const std::unordered_map<Key, + T, + Hash, + Pred, + PoolAllocator<std::pair<const Key, T>, + MAX_BLOCK_SIZE_BYTES, + ALIGN_BYTES>>& m) +{ + auto* pool_resource = m.get_allocator().resource(); + + // The allocated chunks are stored in a std::list. Size per node should + // therefore be 3 pointers: next, previous, and a pointer to the chunk. + size_t estimated_list_node_size = MallocUsage(sizeof(void*) * 3); + size_t usage_resource = estimated_list_node_size * pool_resource->NumAllocatedChunks(); + size_t usage_chunks = MallocUsage(pool_resource->ChunkSizeBytes()) * pool_resource->NumAllocatedChunks(); + return usage_resource + usage_chunks + MallocUsage(sizeof(void*) * m.bucket_count()); } +} // namespace memusage + #endif // BITCOIN_MEMUSAGE_H |