From 2dec6dbee89843c5dfbf414b3dbc9054cb9b6715 Mon Sep 17 00:00:00 2001 From: Roland Conybeare Date: Tue, 16 Dec 2025 17:18:38 -0500 Subject: [PATCH] xo-alloc2: rename: AllocatorError -> AllocError --- include/xo/alloc2/alloc/AAllocator.hpp | 4 +- .../{AllocatorError.hpp => AllocError.hpp} | 12 ++-- include/xo/alloc2/alloc/AllocInfo.hpp | 44 ++++++++++++ include/xo/alloc2/alloc/IAllocator_Any.hpp | 2 +- include/xo/alloc2/alloc/IAllocator_Xfer.hpp | 2 +- include/xo/alloc2/alloc/RAllocator.hpp | 2 +- include/xo/alloc2/arena/ArenaConfig.hpp | 2 +- include/xo/alloc2/arena/DArena.hpp | 2 +- include/xo/alloc2/arena/IAllocator_DArena.hpp | 2 +- include/xo/alloc2/gc/DX1Collector.hpp | 2 +- .../xo/alloc2/gc/IAllocator_DX1Collector.hpp | 2 +- src/alloc2/DArena.cpp | 26 +++---- src/alloc2/DX1Collector.cpp | 2 +- src/alloc2/IAllocator_DArena.cpp | 72 +++++++++---------- src/alloc2/IAllocator_DX1Collector.cpp | 2 +- utest/arena.test.cpp | 4 +- 16 files changed, 113 insertions(+), 69 deletions(-) rename include/xo/alloc2/alloc/{AllocatorError.hpp => AllocError.hpp} (91%) create mode 100644 include/xo/alloc2/alloc/AllocInfo.hpp diff --git a/include/xo/alloc2/alloc/AAllocator.hpp b/include/xo/alloc2/alloc/AAllocator.hpp index b6e269a..d2725e9 100644 --- a/include/xo/alloc2/alloc/AAllocator.hpp +++ b/include/xo/alloc2/alloc/AAllocator.hpp @@ -5,7 +5,7 @@ #pragma once -#include "AllocatorError.hpp" +#include "AllocError.hpp" #include "AllocInfo.hpp" #include "xo/facet/facet_implementation.hpp" #include "xo/facet/typeseq.hpp" @@ -74,7 +74,7 @@ namespace xo { **/ virtual bool contains(Copaque d, const void * p) const noexcept = 0; /** report last error **/ - virtual AllocatorError last_error(Copaque d) const noexcept = 0; + virtual AllocError last_error(Copaque d) const noexcept = 0; /** fetch alloc info: given memory @p mem previously obtained * from {@ref alloc, @ref super_alloc}, get {tseq, age, size} details * for that allocation. diff --git a/include/xo/alloc2/alloc/AllocatorError.hpp b/include/xo/alloc2/alloc/AllocError.hpp similarity index 91% rename from include/xo/alloc2/alloc/AllocatorError.hpp rename to include/xo/alloc2/alloc/AllocError.hpp index cb80b48..5264d93 100644 --- a/include/xo/alloc2/alloc/AllocatorError.hpp +++ b/include/xo/alloc2/alloc/AllocError.hpp @@ -1,4 +1,4 @@ -/** @file AllocatorError.hpp +/** @file AllocError.hpp * * @author Roland Conybeare, Dec 2025 **/ @@ -31,15 +31,15 @@ namespace xo { alloc_info_address, }; - struct AllocatorError { + struct AllocError { using size_type = std::size_t; using value_type = std::byte*; - AllocatorError() = default; - explicit AllocatorError(error err, + AllocError() = default; + explicit AllocError(error err, uint32_t seq) : error_{err}, error_seq_{seq} {} - AllocatorError(error err, + AllocError(error err, uint32_t seq, size_type req_z, size_type com_z, @@ -66,4 +66,4 @@ namespace xo { } /*namespace mm*/ } /*namespace xo*/ -/* end AllocatorError.hpp */ +/* end AllocError.hpp */ diff --git a/include/xo/alloc2/alloc/AllocInfo.hpp b/include/xo/alloc2/alloc/AllocInfo.hpp new file mode 100644 index 0000000..6e86446 --- /dev/null +++ b/include/xo/alloc2/alloc/AllocInfo.hpp @@ -0,0 +1,44 @@ +/** @file AllocInfo.hpp + * + * @author Roland Conybeare, Dec 2025 + **/ + +#pragma once + +#include "AllocHeader.hpp" + +namespace xo { + namespace mm { + /** @class AllocInfo + * @brief bookkeeping information for an allocation + * + * AllocInfo instances are 1:1 with sum of calls to + * {@ref AAllocator::alloc, @ref AAllocator::alloc_super} + * + **/ + struct AllocInfo { + using size_type = AllocHeader::size_type; + + AllocInfo(const AllocHeaderConfig * p_cfg, const AllocHeader * p_hdr) + : p_config_{p_cfg}, p_header_{p_hdr} {} + + /** error when alloc-header not configured **/ + static AllocInfo error_not_configured(AllocHeaderConfig * p_cfg) { return AllocInfo(p_cfg, nullptr); } + + /** true for non-sentinel AllocInfo instance **/ + bool is_valid() const { return (p_config_ != nullptr) && (p_header_ != nullptr); } + + /** Type sequence number in garbage collector **/ + std::uint32_t tseq() const noexcept { return p_config_->tseq(*p_header_); } + /** Allocation age in garbage collector **/ + std::uint32_t age() const noexcept { return p_config_->age (*p_header_); } + /** Allocation size (including allocator-supplied padding) **/ + size_type size() const noexcept { return p_config_->size(*p_header_); } + + const AllocHeaderConfig * p_config_ = nullptr; + const AllocHeader * p_header_ = nullptr; + }; + } /*namespace mm*/ +} /*namespace xo*/ + +/* end AllocInfo.hpp */ diff --git a/include/xo/alloc2/alloc/IAllocator_Any.hpp b/include/xo/alloc2/alloc/IAllocator_Any.hpp index dae19cc..ee03fa4 100644 --- a/include/xo/alloc2/alloc/IAllocator_Any.hpp +++ b/include/xo/alloc2/alloc/IAllocator_Any.hpp @@ -39,7 +39,7 @@ namespace xo { [[noreturn]] size_type available(Copaque) const noexcept override { _fatal(); } [[noreturn]] size_type allocated(Copaque) const noexcept override { _fatal(); } [[noreturn]] bool contains(Copaque, const void *) const noexcept override { _fatal(); } - [[noreturn]] AllocatorError last_error(Copaque) const noexcept override { _fatal(); } + [[noreturn]] AllocError last_error(Copaque) const noexcept override { _fatal(); } // non-const methods [[noreturn]] AllocInfo alloc_info(Opaque, value_type) const noexcept override { _fatal(); } diff --git a/include/xo/alloc2/alloc/IAllocator_Xfer.hpp b/include/xo/alloc2/alloc/IAllocator_Xfer.hpp index 7224f6d..65caf85 100644 --- a/include/xo/alloc2/alloc/IAllocator_Xfer.hpp +++ b/include/xo/alloc2/alloc/IAllocator_Xfer.hpp @@ -38,7 +38,7 @@ namespace xo { bool contains(Copaque d, const void * p) const noexcept override { return I::contains(_dcast(d), p); } - AllocatorError last_error(Copaque d) const noexcept override { return I::last_error(_dcast(d)); } + AllocError last_error(Copaque d) const noexcept override { return I::last_error(_dcast(d)); } // non-const methods diff --git a/include/xo/alloc2/alloc/RAllocator.hpp b/include/xo/alloc2/alloc/RAllocator.hpp index cca2f7d..340dc24 100644 --- a/include/xo/alloc2/alloc/RAllocator.hpp +++ b/include/xo/alloc2/alloc/RAllocator.hpp @@ -33,7 +33,7 @@ namespace xo { size_type available() const noexcept { return O::iface()->available(O::data()); } size_type allocated() const noexcept { return O::iface()->allocated(O::data()); } bool contains(const void * p) const noexcept { return O::iface()->contains(O::data(), p); } - AllocatorError last_error() const noexcept { return O::iface()->last_error(O::data()); } + AllocError last_error() const noexcept { return O::iface()->last_error(O::data()); } value_type alloc(size_type z) noexcept { return O::iface()->alloc(O::data(), z); } value_type super_alloc(size_type z) noexcept { return O::iface()->super_alloc(O::data(), z); } diff --git a/include/xo/alloc2/arena/ArenaConfig.hpp b/include/xo/alloc2/arena/ArenaConfig.hpp index 5ba01c4..46311a8 100644 --- a/include/xo/alloc2/arena/ArenaConfig.hpp +++ b/include/xo/alloc2/arena/ArenaConfig.hpp @@ -6,7 +6,7 @@ #pragma once #include "alloc/AllocHeader.hpp" -#include "alloc/AllocatorError.hpp" +#include "alloc/AllocError.hpp" #include #include diff --git a/include/xo/alloc2/arena/DArena.hpp b/include/xo/alloc2/arena/DArena.hpp index 39eb1b5..8be33af 100644 --- a/include/xo/alloc2/arena/DArena.hpp +++ b/include/xo/alloc2/arena/DArena.hpp @@ -152,7 +152,7 @@ namespace xo { uint32_t error_count_ = 0; /** capture some error details if/when error **/ - AllocatorError last_error_; + AllocError last_error_; ///@} }; diff --git a/include/xo/alloc2/arena/IAllocator_DArena.hpp b/include/xo/alloc2/arena/IAllocator_DArena.hpp index 36311bf..2a0d606 100644 --- a/include/xo/alloc2/arena/IAllocator_DArena.hpp +++ b/include/xo/alloc2/arena/IAllocator_DArena.hpp @@ -43,7 +43,7 @@ namespace xo { static size_type available(const DArena &) noexcept; static size_type allocated(const DArena &) noexcept; static bool contains(const DArena &, const void * p) noexcept; - static AllocatorError last_error(const DArena &) noexcept; + static AllocError last_error(const DArena &) noexcept; /** retrieve allocation bookkeeping info for @p mem from arena @p d **/ static AllocInfo alloc_info(DArena &, value_type mem) noexcept; diff --git a/include/xo/alloc2/gc/DX1Collector.hpp b/include/xo/alloc2/gc/DX1Collector.hpp index 5a98c54..79fef9e 100644 --- a/include/xo/alloc2/gc/DX1Collector.hpp +++ b/include/xo/alloc2/gc/DX1Collector.hpp @@ -177,7 +177,7 @@ namespace xo { bool contains(role r, const void * addr) const noexcept; /** return details from last error (will be in gen0 to-space) **/ - AllocatorError last_error() const noexcept; + AllocError last_error() const noexcept; /** get allocation size from header **/ std::size_t header2size(header_type hdr) const noexcept; diff --git a/include/xo/alloc2/gc/IAllocator_DX1Collector.hpp b/include/xo/alloc2/gc/IAllocator_DX1Collector.hpp index b8cf823..c5d2d20 100644 --- a/include/xo/alloc2/gc/IAllocator_DX1Collector.hpp +++ b/include/xo/alloc2/gc/IAllocator_DX1Collector.hpp @@ -48,7 +48,7 @@ namespace xo { /** true iff address @p p comes from collector @p d **/ static bool contains(const DX1Collector & d, const void * p) noexcept; /** report last error, if any, for collector @p d **/ - static AllocatorError last_error(const DX1Collector &) noexcept; + static AllocError last_error(const DX1Collector &) noexcept; /** always alloc in gen0 to-space **/ static value_type alloc(DX1Collector & d, size_type z) noexcept; diff --git a/src/alloc2/DArena.cpp b/src/alloc2/DArena.cpp index 74b404c..2b3e972 100644 --- a/src/alloc2/DArena.cpp +++ b/src/alloc2/DArena.cpp @@ -189,7 +189,7 @@ namespace xo { other.limit_ = nullptr; other.hi_ = nullptr; other.error_count_ = 0; - other.last_error_ = AllocatorError(); + other.last_error_ = AllocError(); } DArena & @@ -212,7 +212,7 @@ namespace xo { other.limit_ = nullptr; other.hi_ = nullptr; other.error_count_ = 0; - other.last_error_ = AllocatorError(); + other.last_error_ = AllocError(); return *this; } @@ -236,7 +236,7 @@ namespace xo { limit_ = nullptr; hi_ = nullptr; error_count_ = 0; - last_error_ = AllocatorError(); + last_error_ = AllocError(); } DArena::header_type * @@ -252,11 +252,11 @@ namespace xo { { if (!config_.store_header_flag_) [[unlikely]] { ++(error_count_); - last_error_ = AllocatorError(error::alloc_info_disabled, - error_count_, - 0 /*add_commit_z*/, - committed_z_, - this->reserved()); + last_error_ = AllocError(error::alloc_info_disabled, + error_count_, + 0 /*add_commit_z*/, + committed_z_, + this->reserved()); return AllocInfo::error_not_configured(&config_.header_); } @@ -265,11 +265,11 @@ namespace xo { if (!this->contains(header_mem)) { ++(error_count_); - last_error_ = AllocatorError(error::alloc_info_address, - error_count_, - 0 /*add_commit_z*/, - committed_z_, - this->reserved()); + last_error_ = AllocError(error::alloc_info_address, + error_count_, + 0 /*add_commit_z*/, + committed_z_, + this->reserved()); } return AllocInfo(&config_.header_, (AllocHeader *)header_mem); diff --git a/src/alloc2/DX1Collector.cpp b/src/alloc2/DX1Collector.cpp index c9f53b0..ab3bab2 100644 --- a/src/alloc2/DX1Collector.cpp +++ b/src/alloc2/DX1Collector.cpp @@ -120,7 +120,7 @@ namespace xo { return false; } - AllocatorError + AllocError DX1Collector::last_error() const noexcept { // TODO: diff --git a/src/alloc2/IAllocator_DArena.cpp b/src/alloc2/IAllocator_DArena.cpp index 04499e0..39b39b2 100644 --- a/src/alloc2/IAllocator_DArena.cpp +++ b/src/alloc2/IAllocator_DArena.cpp @@ -54,7 +54,7 @@ namespace xo { return (s.lo_ <= p) && (p < s.hi_); } - AllocatorError + AllocError IAllocator_DArena::last_error(const DArena & s) noexcept { return s.last_error_; } @@ -66,11 +66,11 @@ namespace xo { if (!s.config_.store_header_flag_) [[unlikely]] { ++(s.error_count_); - s.last_error_ = AllocatorError(error::alloc_info_disabled, - s.error_count_, - 0 /*add_commit_z*/, - s.committed_z_, - reserved(s)); + s.last_error_ = AllocError(error::alloc_info_disabled, + s.error_count_, + 0 /*add_commit_z*/, + s.committed_z_, + reserved(s)); return AllocInfo::error_not_configured(&s.config_.header_); } @@ -79,11 +79,11 @@ namespace xo { if (!s.contains(header_mem)) { ++(s.error_count_); - s.last_error_ = AllocatorError(error::alloc_info_address, - s.error_count_, - 0 /*add_commit_z*/, - s.committed_z_, - reserved(s)); + s.last_error_ = AllocError(error::alloc_info_address, + s.error_count_, + 0 /*add_commit_z*/, + s.committed_z_, + reserved(s)); } return AllocInfo(&s.config_.header_, (AllocHeader*)header_mem); @@ -105,9 +105,9 @@ namespace xo { if (s.lo_ + target_z > s.hi_) [[unlikely]] { ++(s.error_count_); - s.last_error_ = AllocatorError(error::reserve_exhausted, - s.error_count_, - target_z, s.committed_z_, reserved(s)); + s.last_error_ = AllocError(error::reserve_exhausted, + s.error_count_, + target_z, s.committed_z_, reserved(s)); return false; } @@ -137,35 +137,35 @@ namespace xo { assert(s.limit_ == s.lo_ + s.committed_z_); -// log && log(xtag("aligned_offset_z", aligned_offset_z), -// xtag("add_commit_z", add_commit_z)); -// log && log("expand committed range", -// xtag("commit_start", commit_start), -// xtag("add_commit_z", add_commit_z), -// xtag("commit_end", commit_start + add_commit_z)); + // log && log(xtag("aligned_offset_z", aligned_offset_z), + // xtag("add_commit_z", add_commit_z)); + // log && log("expand committed range", + // xtag("commit_start", commit_start), + // xtag("add_commit_z", add_commit_z), + // xtag("commit_end", commit_start + add_commit_z)); if (::mprotect(commit_start, add_commit_z, PROT_READ | PROT_WRITE) != 0) [[unlikely]] - { - ++(s.error_count_); - s.last_error_ = AllocatorError(error::commit_failed, - s.error_count_, - add_commit_z, s.committed_z_, reserved(s)); + { + ++(s.error_count_); + s.last_error_ = AllocError(error::commit_failed, + s.error_count_, + add_commit_z, s.committed_z_, reserved(s)); #ifdef OBSOLETE - throw std::runtime_error(tostr("ArenaAlloc::expand: commit failure", - xtag("committed_z", s.committed_z_), - xtag("add_commit_z", add_commit_z))); + throw std::runtime_error(tostr("ArenaAlloc::expand: commit failure", + xtag("committed_z", s.committed_z_), + xtag("add_commit_z", add_commit_z))); #endif - return false; - } + return false; + } s.committed_z_ = aligned_target_z; s.limit_ = s.lo_ + s.committed_z_; if (commit_start == s.lo_) [[unlikely]] - { - /* first expand() for this allocator - start with guard_z_ bytes */ + { +/* first expand() for this allocator - start with guard_z_ bytes */ ::memset(s.free_, s.config_.guard_byte_, @@ -230,7 +230,7 @@ namespace xo { if (s.config_.store_header_flag_) { if (!s.last_header_) [[unlikely]] { ++(s.error_count_); - s.last_error_ = AllocatorError(error::orphan_sub_alloc, + s.last_error_ = AllocError(error::orphan_sub_alloc, s.error_count_, 0 /*add_commit_z*/, s.committed_z_, reserved(s)); } else { @@ -261,7 +261,7 @@ namespace xo { if (s.config_.store_header_flag_) { if (!s.last_header_) [[unlikely]] { ++(s.error_count_); - s.last_error_ = AllocatorError(error::orphan_sub_alloc, + s.last_error_ = AllocError(error::orphan_sub_alloc, s.error_count_, 0 /*add_commit_z*/, s.committed_z_, reserved(s)); return nullptr; @@ -277,7 +277,7 @@ namespace xo { if ((header & s.config_.header_size_mask_ & z0) != z0) [[unlikely]] { /* cumulative alloc size doesn't fit in configured header_size_mask bits */ ++(s.error_count_); - s.last_error_ = AllocatorError(error::header_size_mask, + s.last_error_ = AllocError(error::header_size_mask, s.error_count_, 0 /*add_commit_z*/, s.committed_z_, reserved(s)); return nullptr; @@ -366,7 +366,7 @@ namespace xo { } else { /* req_z doesn't fit in configured header_size_mask bits */ ++(s.error_count_); - s.last_error_ = AllocatorError(error::header_size_mask, + s.last_error_ = AllocError(error::header_size_mask, s.error_count_, 0 /*add_commit_z*/, s.committed_z_, diff --git a/src/alloc2/IAllocator_DX1Collector.cpp b/src/alloc2/IAllocator_DX1Collector.cpp index f26fc09..89b9c2d 100644 --- a/src/alloc2/IAllocator_DX1Collector.cpp +++ b/src/alloc2/IAllocator_DX1Collector.cpp @@ -55,7 +55,7 @@ namespace xo { return d.contains(role::to_space(), addr); } - AllocatorError + AllocError IAllocator_DX1Collector::last_error(const DX1Collector & d) noexcept { return d.last_error(); diff --git a/utest/arena.test.cpp b/utest/arena.test.cpp index 254bd60..8a01303 100644 --- a/utest/arena.test.cpp +++ b/utest/arena.test.cpp @@ -18,7 +18,7 @@ namespace xo { using xo::mm::AAllocator; using xo::mm::IAllocator_DArena; using xo::mm::IAllocator_Xfer; - using xo::mm::AllocatorError; + using xo::mm::AllocError; using xo::mm::DArena; using xo::mm::AllocHeaderConfig; using xo::mm::ArenaConfig; @@ -290,7 +290,7 @@ namespace xo { REQUIRE(!m0); - AllocatorError err = a1o.last_error(); + AllocError err = a1o.last_error(); REQUIRE(err.error_ == error::reserve_exhausted); REQUIRE(err.error_seq_ == 1);