xo-alloc2: + Allocator.expand() + streamlining _Any
This commit is contained in:
parent
e21debcda2
commit
38f419f2ea
8 changed files with 167 additions and 18 deletions
|
|
@ -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 **/
|
||||
|
|
|
|||
|
|
@ -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<xo::facet::DVariantPlaceholder>;
|
||||
|
||||
// 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;
|
||||
};
|
||||
|
|
|
|||
|
|
@ -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 &);
|
||||
|
|
|
|||
|
|
@ -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 <typename DRepr, typename IAllocator_DRepr>
|
||||
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;
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@
|
|||
**/
|
||||
|
||||
#include "IAllocator_Any.hpp"
|
||||
#include <iostream>
|
||||
|
||||
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<DArena>
|
||||
*/
|
||||
|
||||
std::cerr << "fatal: attempt to call uninitialized IAllocator_Any" << std::endl;
|
||||
std::terminate();
|
||||
}
|
||||
|
||||
int32_t
|
||||
IAllocator_Any::s_typeseq = typeseq::id<DVariantPlaceholder>();
|
||||
|
||||
|
|
|
|||
|
|
@ -4,8 +4,10 @@
|
|||
**/
|
||||
|
||||
#include "IAllocator_DArena.hpp"
|
||||
#include "padding.hpp"
|
||||
#include <cassert>
|
||||
#include <cstddef>
|
||||
#include <sys/mman.h>
|
||||
|
||||
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<size_t>(s.limit_) % s.config_.hugepage_z_ == 0);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
std::byte *
|
||||
IAllocator_DArena::alloc(const DArena & s,
|
||||
std::size_t z)
|
||||
|
|
|
|||
|
|
@ -76,13 +76,14 @@ namespace xo {
|
|||
|
||||
TEST_CASE("allocator-any-1", "[alloc2][AAllocator]")
|
||||
{
|
||||
/* empty allocator */
|
||||
/* empty allocator alloc1 */
|
||||
obj<AAllocator> 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<AAllocator, DArena> a1o{&arena};
|
||||
|
||||
a1o.expand(3*1024*1024);
|
||||
|
||||
REQUIRE(a1o.reserved() % cfg.hugepage_z_ == 0);
|
||||
|
||||
#ifdef NOPE
|
||||
byte * m = a1o.alloc(1);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue