diff --git a/include/xo/arena/ArenaConfig.hpp b/include/xo/arena/ArenaConfig.hpp index 4d72b91..9ca2a38 100644 --- a/include/xo/arena/ArenaConfig.hpp +++ b/include/xo/arena/ArenaConfig.hpp @@ -19,13 +19,13 @@ namespace xo { struct ArenaConfig { /** @defgroup mm-arenaconfig-ctors **/ - ArenaConfig with_name(std::string name) { + ArenaConfig with_name(std::string name) const { ArenaConfig copy(*this); copy.name_ = name; return copy; } - ArenaConfig with_size(std::size_t z) { + ArenaConfig with_size(std::size_t z) const { ArenaConfig copy(*this); copy.size_ = z; return copy; diff --git a/include/xo/arena/DArena.hpp b/include/xo/arena/DArena.hpp index 8c7203d..4c12c8c 100644 --- a/include/xo/arena/DArena.hpp +++ b/include/xo/arena/DArena.hpp @@ -138,12 +138,14 @@ namespace xo { **/ AllocHeader * end_header() const noexcept; + /** report memory use for this arena to @p fn. + * For DArena reporting just one pool = arena's memory range + **/ + void visit_pools(const MemorySizeVisitor & fn) const; + /** get header from allocated object address **/ header_type * obj2hdr(void * obj) noexcept; - /** resource ocnsumption in normal form **/ - MemorySizeInfo _store_info() const noexcept; - /** report alloc book-keeping info for allocation at @p mem * * Require: diff --git a/include/xo/arena/DArenaHashMap.hpp b/include/xo/arena/DArenaHashMap.hpp index 042dc90..da0e742 100644 --- a/include/xo/arena/DArenaHashMap.hpp +++ b/include/xo/arena/DArenaHashMap.hpp @@ -45,7 +45,7 @@ namespace xo { using value_type = std::pair; using key_hash = Hash; using key_equal = Equal; - using MemorySizeInfo = xo::mm::MemorySizeInfo; + using MemorySizeVisitor = xo::mm::MemorySizeVisitor; using byte = std::byte; using group_type = detail::ControlGroup; using store_type = detail::HashMapStore; @@ -77,9 +77,8 @@ namespace xo { iterator begin() { return _promote_iterator(_begin_aux()); } iterator end() { return _promote_iterator(_end_aux()); } - std::size_t _n_store() const noexcept { return store_._n_store(); } - MemorySizeInfo _store_info(std::size_t i) const noexcept { - return store_._store_info(i); + void visit_pools(const MemorySizeVisitor & visitor) const { + return store_.visit_pools(visitor); } /** insert @p kv_pair into hash map. diff --git a/include/xo/arena/DArenaVector.hpp b/include/xo/arena/DArenaVector.hpp index b7a0b2a..33230b0 100644 --- a/include/xo/arena/DArenaVector.hpp +++ b/include/xo/arena/DArenaVector.hpp @@ -82,7 +82,7 @@ namespace xo { /** arena used for element storage * (Might prefer obj here; refrain to avoid leveling violation) **/ - MemorySizeInfo _store_info() const { return store_._store_info(); } + void visit_pools(const MemorySizeVisitor & fn) const { store_.visit_pools(fn); } /** reserve space, if possible, for at least @p z elements. * Always limited by ArenaConfig.size_ diff --git a/include/xo/arena/DCircularBuffer.hpp b/include/xo/arena/DCircularBuffer.hpp index 538d272..ba74453 100644 --- a/include/xo/arena/DCircularBuffer.hpp +++ b/include/xo/arena/DCircularBuffer.hpp @@ -83,8 +83,8 @@ namespace xo { const_span_type occupied_range() const noexcept { return occupied_range_; } const_span_type input_range() const noexcept { return input_range_; } - std::size_t _n_store() const noexcept; - MemorySizeInfo _store_info(std::size_t i) const noexcept; + /** report memory-size info for this buffer to @p fn **/ + void visit_pools(const MemorySizeVisitor & fn) const; /** verify DCircularBuffer invariants. * Act on failure according to policy @p p diff --git a/include/xo/arena/MemorySizeInfo.hpp b/include/xo/arena/MemorySizeInfo.hpp index 7cba082..f12dcce 100644 --- a/include/xo/arena/MemorySizeInfo.hpp +++ b/include/xo/arena/MemorySizeInfo.hpp @@ -5,7 +5,8 @@ #pragma once -#include +#include +#include namespace xo { namespace mm { @@ -31,7 +32,12 @@ namespace xo { std::size_t reserved_ = 0; }; - } + /** function that visits MemorySizeInfo for a collection of @p n memory pools. + * Each pool reported with index @p i in [0, n), with associated + * size record @p info. + **/ + using MemorySizeVisitor = std::function; + } } /* end MemorySizeInfo.hpp */ diff --git a/include/xo/arena/hashmap/HashMapStore.hpp b/include/xo/arena/hashmap/HashMapStore.hpp index 74f2f4e..22bfb12 100644 --- a/include/xo/arena/hashmap/HashMapStore.hpp +++ b/include/xo/arena/hashmap/HashMapStore.hpp @@ -19,7 +19,7 @@ namespace xo { using group_type = detail::ControlGroup; using control_vector_type = xo::mm::DArenaVector; using slot_vector_type = xo::mm::DArenaVector; - using MemorySizeInfo = xo::mm::MemorySizeInfo; + using MemorySizeVisitor = xo::mm::MemorySizeVisitor; public: /** group_exp2: number of groups {x, 2^x} **/ @@ -48,16 +48,9 @@ namespace xo { size_type capacity() const noexcept { return n_group_ * c_group_size; } float load_factor() const noexcept { return size_ / static_cast(n_slot_); } - std::size_t _n_store() const noexcept { return 2; } - MemorySizeInfo _store_info(std::size_t i) const noexcept { - switch (i) { - case 0: - return control_._store_info(); - case 1: - return slots_._store_info(); - } - - return MemorySizeInfo::sentinel(); + void visit_pools(const MemorySizeVisitor & visitor) const { + control_.visit_pools(visitor); + slots_.visit_pools(visitor); } void resize_from_empty(const std::pairallocated(), - this->committed(), - this->reserved()); + void + DArena::visit_pools(const MemorySizeVisitor & fn) const { + fn(MemorySizeInfo(config_.name_, + this->allocated(), + this->committed(), + this->reserved())); } AllocInfo diff --git a/src/arena/DCircularBuffer.cpp b/src/arena/DCircularBuffer.cpp index 2a5fb48..667126a 100644 --- a/src/arena/DCircularBuffer.cpp +++ b/src/arena/DCircularBuffer.cpp @@ -79,30 +79,16 @@ namespace xo { { } - std::size_t - DCircularBuffer::_n_store() const noexcept + void + DCircularBuffer::visit_pools(const MemorySizeVisitor & visitor) const { - return 2; - } + visitor(MemorySizeInfo(config_.name_, + occupied_range_.size(), + mapped_range_.size(), + reserved_range_.size())); - MemorySizeInfo - DCircularBuffer::_store_info(std::size_t i) const noexcept - { - switch (i) { - case 0: - return MemorySizeInfo(config_.name_, - occupied_range_.size(), - mapped_range_.size(), - reserved_range_.size()); - case 1: - return pinned_spans_._store_info(); - default: - break; - } - - return MemorySizeInfo::sentinel(); + pinned_spans_.visit_pools(visitor); } - bool DCircularBuffer::verify_ok(verify_policy policy) const