diff --git a/xo-alloc2/include/xo/alloc2/AAllocator.hpp b/xo-alloc2/include/xo/alloc2/AAllocator.hpp index 9e77353a..7b0558b1 100644 --- a/xo-alloc2/include/xo/alloc2/AAllocator.hpp +++ b/xo-alloc2/include/xo/alloc2/AAllocator.hpp @@ -59,6 +59,11 @@ namespace xo { **/ virtual bool contains(Copaque d, const void * p) const = 0; + /** expand committed space in arena @p d + * to size at least @p z + * In practice will round up to a multiple of hugepage size (2MB) + **/ + virtual bool expand(Opaque d, std::size_t z) const = 0; /** allocate @p z bytes of memory from allocator @p d. **/ virtual std::byte * alloc(Opaque d, std::size_t z) const = 0; /** reset allocator @p d to empty state **/ diff --git a/xo-alloc2/include/xo/alloc2/IAllocator_Any.hpp b/xo-alloc2/include/xo/alloc2/IAllocator_Any.hpp index 5a57720e..c53495d9 100644 --- a/xo-alloc2/include/xo/alloc2/IAllocator_Any.hpp +++ b/xo-alloc2/include/xo/alloc2/IAllocator_Any.hpp @@ -21,22 +21,30 @@ namespace xo { } namespace mm { + /** @class IAllocator_Any + * @brief Allocator implementation for variant instance. + **/ struct IAllocator_Any : public AAllocator { //using Impl = IAllocator_ImplType; // from AAllocator int32_t _typeseq() const override { return s_typeseq; } - const std::string & name(Copaque) const override { assert(false); static std::string * x; return *x; } - std::size_t reserved(Copaque) const override { assert(false); return 0ul; } - std::size_t size(Copaque) const override { assert(false); return 0ul; } - std::size_t committed(Copaque) const override { assert(false); return 0ul; } - bool contains(Copaque, const void *) const override { assert(false); return false; } + [[noreturn]] const std::string & name(Copaque) const override { _fatal(); } + [[noreturn]] std::size_t reserved(Copaque) const override { _fatal(); } + [[noreturn]] std::size_t size(Copaque) const override { _fatal(); } + [[noreturn]] std::size_t committed(Copaque) const override { _fatal(); } + [[noreturn]] bool contains(Copaque, const void *) const override { _fatal(); } - std::byte * alloc(Opaque, std::size_t) const override { assert(false); return nullptr; } - void clear(Opaque) const override { assert(false); } - void destruct_data(Opaque) const override { assert(false); } + [[noreturn]] bool expand(Opaque, std::size_t) const override { _fatal(); } + [[noreturn]] std::byte * alloc(Opaque, std::size_t) const override { _fatal(); } + [[noreturn]] void clear(Opaque) const override { _fatal(); } + [[noreturn]] void destruct_data(Opaque) const override { _fatal(); } + private: + [[noreturn]] static void _fatal(); + + public: static int32_t s_typeseq; static bool _valid; }; diff --git a/xo-alloc2/include/xo/alloc2/IAllocator_DArena.hpp b/xo-alloc2/include/xo/alloc2/IAllocator_DArena.hpp index 40f57bb6..b0c7782c 100644 --- a/xo-alloc2/include/xo/alloc2/IAllocator_DArena.hpp +++ b/xo-alloc2/include/xo/alloc2/IAllocator_DArena.hpp @@ -21,12 +21,25 @@ namespace xo { } namespace mm { + /* changes here coordinate with: + * AAllocator AAllocator.hpp + * IAllocator_Any IAllocator_Any.hpp + * IAllocator_Xfer IAllocator_Xfer.hpp + * RAllocator RAllocator.hpp + */ struct IAllocator_DArena { static const std::string & name(const DArena &); static std::size_t reserved(const DArena &); static std::size_t size(const DArena &); static std::size_t committed(const DArena &); static bool contains(const DArena &, const void * p); + + /** expand committed space in arena @p d + * to size at least @p z + * In practice will round up to a multiple of @ref page_z_. + **/ + static bool expand(DArena & d, std::size_t z); + static std::byte * alloc(const DArena &, std::size_t z); static void clear(DArena &); static void destruct_data(DArena &); diff --git a/xo-alloc2/include/xo/alloc2/IAllocator_Xfer.hpp b/xo-alloc2/include/xo/alloc2/IAllocator_Xfer.hpp index a9b8888d..872b9975 100644 --- a/xo-alloc2/include/xo/alloc2/IAllocator_Xfer.hpp +++ b/xo-alloc2/include/xo/alloc2/IAllocator_Xfer.hpp @@ -10,6 +10,9 @@ namespace xo { namespace mm { /** @class IAllocator_Xfer + * + * Adapts typed allocator implementation @tparam IAllocator_DRepr + * to type-erased @ref AAllocator interface **/ template struct IAllocator_Xfer : public AAllocator { @@ -17,18 +20,38 @@ namespace xo { using Impl = IAllocator_DRepr; static const DRepr & _dcast(Copaque d) { return *(const DRepr *)d; } + static DRepr & _dcast(Opaque d) { return *(DRepr *)d; } // from AAllocator int32_t _typeseq() const override { return s_typeseq; } - const std::string & name(Copaque d) const override { return Impl::name(_dcast(d)); } - std::size_t reserved(Copaque d) const override { return Impl::reserved(*(DRepr*)d); } - std::size_t size(Copaque d) const override { return Impl::size(*(DRepr*)d); } - std::size_t committed(Copaque d) const override { return Impl::committed(*(DRepr*)d); } - bool contains(Copaque d, const void * p) const override { return Impl::contains(*(DRepr*)d, p); } + const std::string & name(Copaque d) const override { + return Impl::name(_dcast(d)); + } + std::size_t reserved(Copaque d) const override { + return Impl::reserved(_dcast(d)); + } + std::size_t size(Copaque d) const override { + return Impl::size(_dcast(d)); + } + std::size_t committed(Copaque d) const override { + return Impl::committed(_dcast(d)); + } + bool contains(Copaque d, const void * p) const override { + return Impl::contains(_dcast(d), p); + } - std::byte * alloc(Opaque d, std::size_t z) const override { return Impl::alloc(*(DRepr*)d, z); } - void clear(Opaque d) const override { return Impl::clear(*(DRepr*)d); } - void destruct_data(Opaque d) const override { return Impl::destruct_data(*(DRepr*)d); } + bool expand(Opaque d, std::size_t z) const override { + return Impl::expand(_dcast(d), z); + } + std::byte * alloc(Opaque d, std::size_t z) const override { + return Impl::alloc(*(DRepr*)d, z); + } + void clear(Opaque d) const override { + return Impl::clear(*(DRepr*)d); + } + void destruct_data(Opaque d) const override { + return Impl::destruct_data(*(DRepr*)d); + } static int32_t s_typeseq; static bool _valid; diff --git a/xo-alloc2/include/xo/alloc2/RAllocator.hpp b/xo-alloc2/include/xo/alloc2/RAllocator.hpp index 551581d1..a825568e 100644 --- a/xo-alloc2/include/xo/alloc2/RAllocator.hpp +++ b/xo-alloc2/include/xo/alloc2/RAllocator.hpp @@ -28,6 +28,7 @@ namespace xo { size_type size() const { return O::iface()->size(O::data()); } size_type committed() const { return O::iface()->committed(O::data()); } bool contains(const void * p) const { return O::iface()->contains(O::data(), p); } + bool expand(size_type z) { return O::iface()->expand(O::data(), z); } std::byte * alloc(size_type z) { return O::iface()->alloc(O::data(), z); } static bool _valid; diff --git a/xo-alloc2/src/alloc2/IAllocator_Any.cpp b/xo-alloc2/src/alloc2/IAllocator_Any.cpp index 24f90425..414bd02c 100644 --- a/xo-alloc2/src/alloc2/IAllocator_Any.cpp +++ b/xo-alloc2/src/alloc2/IAllocator_Any.cpp @@ -4,6 +4,7 @@ **/ #include "IAllocator_Any.hpp" +#include namespace xo { using xo::facet::DVariantPlaceholder; @@ -12,6 +13,17 @@ namespace xo { namespace mm { + void + IAllocator_Any::_fatal() { + /* control here on uninitialized IAllocator_Any. + * Initialized instance will have specific implementation type + * e.g. IAllocator_Xfer + */ + + std::cerr << "fatal: attempt to call uninitialized IAllocator_Any" << std::endl; + std::terminate(); + } + int32_t IAllocator_Any::s_typeseq = typeseq::id(); diff --git a/xo-alloc2/src/alloc2/IAllocator_DArena.cpp b/xo-alloc2/src/alloc2/IAllocator_DArena.cpp index 06a12d5d..babc0f00 100644 --- a/xo-alloc2/src/alloc2/IAllocator_DArena.cpp +++ b/xo-alloc2/src/alloc2/IAllocator_DArena.cpp @@ -4,8 +4,10 @@ **/ #include "IAllocator_DArena.hpp" +#include "padding.hpp" #include #include +#include namespace xo { namespace mm { @@ -37,6 +39,77 @@ namespace xo { return (s.lo_ <= p) && (p < s.hi_); } + bool + IAllocator_DArena::expand(DArena & s, size_t target_z) + { +// scope log(XO_DEBUG(debug_flag_), +// xtag("offset_z", offset_z), +// xtag("committed_z", committed_z_)); + + if (target_z <= s.committed_z_) { +// log && log("trivial success, offset within committed range", +// xtag("offset_z", offset_z), +// xtag("committed_z", committed_z_)); + return true; + } + + if (s.lo_ + target_z > s.hi_) { +// throw std::runtime_error(tostr("ArenaAlloc::expand: requested size exceeds reserved size", +// xtag("requested", offset_z), xtag("reserved", reserved()))); + return false; + } + + /* + * pre: + * + * _______________................................... + * ^ ^ ^ + * lo limit hi + * + * < committed_z > + * <----------target_z-----------> + * > <- z: 0 <= z < hugepage_z + * <---------aligned_target_z---------> + * <--- add_commit_z --> + * + * post: + * ____________________________________.............. + * ^ ^ ^ + * lo limit hi + * + */ + + std::size_t aligned_target_z = padding::with_padding(target_z, s.config_.hugepage_z_); + std::byte * commit_start = s.limit_; // = s.lo_ + s.committed_z_; + std::size_t add_commit_z = aligned_target_z - s.committed_z_; + + 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)); + + if (::mprotect(commit_start, add_commit_z, PROT_READ | PROT_WRITE) != 0) { + assert(false); +// throw std::runtime_error(tostr("ArenaAlloc::expand: commit failure", +// xtag("committed_z", committed_z_), +// xtag("add_commit_z", add_commit_z))); + return false; + } + + s.committed_z_ = aligned_target_z; + s.limit_ = s.lo_ + s.committed_z_; + + assert(s.committed_z_ % s.config_.hugepage_z_ == 0); + assert(reinterpret_cast(s.limit_) % s.config_.hugepage_z_ == 0); + + return true; + } + std::byte * IAllocator_DArena::alloc(const DArena & s, std::size_t z) diff --git a/xo-alloc2/utest/arena.test.cpp b/xo-alloc2/utest/arena.test.cpp index 42453bc4..8e3cae61 100644 --- a/xo-alloc2/utest/arena.test.cpp +++ b/xo-alloc2/utest/arena.test.cpp @@ -76,13 +76,14 @@ namespace xo { TEST_CASE("allocator-any-1", "[alloc2][AAllocator]") { - /* empty allocator */ + /* empty allocator alloc1 */ obj alloc1; REQUIRE(!alloc1); REQUIRE(alloc1.iface() != nullptr); REQUIRE(alloc1.data() == nullptr); + /* typed allocator a1o */ ArenaConfig cfg { .name_ = "testarena", .size_ = 1 }; DArena arena = DArena::map(cfg); @@ -100,6 +101,19 @@ namespace xo { REQUIRE(a1o.reserved() % cfg.hugepage_z_ == 0); REQUIRE(a1o.size() == 0); REQUIRE(a1o.committed() == 0); + } + + TEST_CASE("allocator-expand-1", "[alloc2][AAllocator]") + { + /* typed allocator a1o */ + ArenaConfig cfg { .name_ = "testarena", + .size_ = 1 }; + DArena arena = DArena::map(cfg); + obj a1o{&arena}; + + a1o.expand(3*1024*1024); + + REQUIRE(a1o.reserved() % cfg.hugepage_z_ == 0); #ifdef NOPE byte * m = a1o.alloc(1);