xo-alloc2: header reorg redux

This commit is contained in:
Roland Conybeare 2025-12-14 17:16:05 -05:00
commit fa32a95be5
14 changed files with 18 additions and 13 deletions

View file

@ -0,0 +1,130 @@
/** @file AAllocator.hpp
*
* @author Roland Conybeare, Dec 2025
**/
#pragma once
#include "AllocatorError.hpp"
#include "xo/facet/facet_implementation.hpp"
#include "xo/facet/typeseq.hpp"
#include <string>
namespace xo {
namespace mm {
using Copaque = const void *;
using Opaque = void *;
/** @class AAllocator
* @brief Abstract facet for allocation
*
**/
struct AAllocator {
/** @defgroup mm-allocator-type-traits allocator type traits **/
///@{
/** @brief type used for allocation amounts **/
using size_type = std::size_t;
/** @brief type used for allocation responses **/
using value_type = std::byte *;
/** object header, if configured **/
using header_type = std::uint64_t;
///@}
/*
* <----------------------------size-------------------------->
* <------------committed-----------><-------uncommitted------>
* <--allocated-->
*
* XXXXXXXXXXXXXXX___________________..........................
*
* allocated: in use
* committed: physical memory obtained
* uncommitted: mapped in virtual memory, not backed by memory
*/
/** @defgroup mm-allocator-methods Allocator methods **/
///@{
/** RTTI: unique id# for actual runtime data representation **/
virtual int32_t _typeseq() const noexcept = 0;
/** optional name for allocator @p d
* Labeling, for diagnostics.
**/
virtual std::string_view name(Copaque d) const noexcept = 0;
/** reserved size in bytes for allocator @p d.
* Includes committed + uncommitted memory.
* Cannot be increased.
**/
virtual size_type reserved(Copaque d) const noexcept = 0;
/** Synonym for @ref committed.
* Can increase on @ref alloc
**/
virtual size_type size(Copaque d) const noexcept = 0;
/** committed size (physical addresses obtained)
* for allocator @p d.
* @ref alloc may auto-increase this
**/
virtual size_type committed(Copaque d) const noexcept = 0;
/** unallocated (but committed) size in bytes for allocator @p d **/
virtual size_type available(Copaque d) const noexcept = 0;
/** allocated (i.e. in-use) amount in bytes for allocator @p d **/
virtual size_type allocated(Copaque d) const noexcept = 0;
/** true iff allocator @p d is responsible for memory at address @p p.
**/
virtual bool contains(Copaque d, const void * p) const noexcept = 0;
/** report last error **/
virtual AllocatorError last_error(Copaque d) const noexcept = 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 noexcept = 0;
/** allocate @p z bytes of memory from allocator @p d. **/
virtual value_type alloc(Opaque d, size_type z) const = 0;
/** like @ref alloc, but follow with one or more consecutive
* @ref sub_alloc() calls. This sequence of allocs will share
* the initial allocation header.
**/
virtual value_type super_alloc(Opaque d, size_type z) const = 0;
/** follow a preceding @ref super_alloc call with additional
* subsidiary allocs that share the same object header.
* Must finish sequence with exactly one sub_alloc call
* with @p complete_flag set. This sub_alloc call may have
* zero @p z
**/
virtual value_type sub_alloc(Opaque d, size_type z, bool complete_flag) const = 0;
/** reset allocator @p d to empty state **/
virtual void clear(Opaque d) const = 0;
/** destruct allocator @p d **/
virtual void destruct_data(Opaque d) const = 0;
///@}
}; /*AAllocator*/
// implementation IAllocator_DRepr of AAllocator for state DRepr
// should provide a specialization:
//
// template <>
// struct xo::facet::FacetImplementation<AAllocator, DRepr> {
// using ImplType = IAllocator_DRepr;
// };
//
// then IAllocator_ImplType<DRepr> --> IAllocator_DRepr
//
template <typename DRepr>
using IAllocator_ImplType = xo::facet::FacetImplType<AAllocator, DRepr>;
// can't we do this with FacetImplementation<AAllocator, DRepr>
//
// template <typename DRepr>
// struct IAllocator_Impl {};
// template <typename DRepr>
// using IAllocator_ImplType = IAllocator_Impl<DRepr>::ImplType;
} /*namespace mm*/
} /*namespace xo*/
/* end AAllocator.hpp */

View file

@ -0,0 +1,63 @@
/** @file AllocatorError.hpp
*
* @author Roland Conybeare, Dec 2025
**/
#pragma once
#include <cstdint>
#include <cstddef>
namespace xo {
namespace mm {
enum class error : int32_t {
/** sentinel **/
invalid = -1,
/** not an error **/
none,
/** reserved size exhauged **/
reserve_exhausted,
/** unable to commit (i.e. mprotect failure) **/
commit_failed,
/** allocation size too big (See @ref ArenaConfig::header_size_mask_) **/
header_size_mask,
/** sub_alloc not preceded by super alloc (or another sub_alloc) **/
orphan_sub_alloc,
};
struct AllocatorError {
using size_type = std::size_t;
using value_type = std::byte*;
AllocatorError() = default;
explicit AllocatorError(error err,
uint32_t seq) : error_{err},
error_seq_{seq} {}
AllocatorError(error err,
uint32_t seq,
size_type req_z,
size_type com_z,
size_type rsv_z) : error_{err},
error_seq_{seq},
request_z_{req_z},
committed_z_{com_z},
reserved_z_{rsv_z} {}
/** error code **/
error error_ = error::none;
/** sequence# of this error.
* Each error event within an allocator gets next sequence number
**/
uint32_t error_seq_ = 0;
/** reqeust size assoc'd with errror **/
size_type request_z_ = 0;
/** committed allocator memory at time of error **/
size_type committed_z_ = 0;
/** reserved allocator memory at time of error **/
size_type reserved_z_ = 0;
};
} /*namespace mm*/
} /*namespace xo*/
/* end AllocatorError.hpp */

View file

@ -0,0 +1,63 @@
/** @file IAllocator_Any.hpp
*
* @author Roland Conybeare, Dec 2025
**/
#pragma once
#include "AAllocator.hpp"
#include <cassert>
namespace xo {
namespace mm { struct IAllocator_Any; }
namespace facet {
template <>
struct FacetImplementation<xo::mm::AAllocator, DVariantPlaceholder> {
using ImplType = xo::mm::IAllocator_Any;
};
}
namespace mm {
/** @class IAllocator_Any
* @brief Allocator implementation for empty variant instance.
**/
struct IAllocator_Any : public AAllocator {
//using Impl = IAllocator_ImplType<xo::facet::DVariantPlaceholder>;
using size_type = std::size_t;
const AAllocator * iface() const { return std::launder(this); }
// from AAllocator
int32_t _typeseq() const noexcept override { return s_typeseq; }
// const methods
[[noreturn]] std::string_view name(Copaque) const noexcept override { _fatal(); }
[[noreturn]] size_type reserved(Copaque) const noexcept override { _fatal(); }
[[noreturn]] size_type size(Copaque) const noexcept override { _fatal(); }
[[noreturn]] size_type committed(Copaque) const noexcept override { _fatal(); }
[[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(); }
// non-const methods
[[noreturn]] bool expand(Opaque, std::size_t) const noexcept override { _fatal(); }
[[noreturn]] value_type alloc(Opaque, std::size_t) const override { _fatal(); }
[[noreturn]] value_type super_alloc(Opaque, std::size_t) const override { _fatal(); }
[[noreturn]] value_type sub_alloc(Opaque, std::size_t, bool) 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;
};
}
}
/* end IAllocator_Any.hpp */

View file

@ -0,0 +1,77 @@
/** @file IAllocator_Xfer.hpp
*
* @author Roland Conybeare, Dec 2025
**/
#pragma once
#include "AAllocator.hpp"
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 {
// parallel interface to AAllocator, with specific data type
using Impl = IAllocator_DRepr;
using size_type = AAllocator::size_type;
using value_type = AAllocator::value_type;
static const DRepr & _dcast(Copaque d) { return *(const DRepr *)d; }
static DRepr & _dcast(Opaque d) { return *(DRepr *)d; }
// from AAllocator
// const methods
int32_t _typeseq() const noexcept override { return s_typeseq; }
std::string_view name(Copaque d) const noexcept override { return I::name(_dcast(d)); }
size_type reserved(Copaque d) const noexcept override { return I::reserved(_dcast(d)); }
size_type size(Copaque d) const noexcept override { return I::size(_dcast(d)); }
size_type committed(Copaque d) const noexcept override { return I::committed(_dcast(d)); }
size_type available(Copaque d) const noexcept override { return I::available(_dcast(d)); }
size_type allocated(Copaque d) const noexcept override { return I::allocated(_dcast(d)); }
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)); }
// non-const methods
bool expand(Opaque d,
std::size_t z) const noexcept override { return I::expand(_dcast(d), z); }
value_type alloc(Opaque d,
std::size_t z) const override { return I::alloc(_dcast(d), z); }
value_type super_alloc(Opaque d,
std::size_t z) const override { return I::super_alloc(_dcast(d), z); }
value_type sub_alloc(Opaque d,
std::size_t z,
bool complete_flag) const override {
return I::sub_alloc(_dcast(d), z, complete_flag);
}
void clear(Opaque d) const override { return I::clear(_dcast(d)); }
void destruct_data(Opaque d) const override { return I::destruct_data(_dcast(d)); }
private:
using I = Impl;
public:
static int32_t s_typeseq;
static bool _valid;
};
template <typename DRepr, typename IAllocator_DRepr>
int32_t
IAllocator_Xfer<DRepr, IAllocator_DRepr>::s_typeseq = facet::typeseq::id<DRepr>();
template <typename DRepr, typename IAllocator_DRepr>
bool
IAllocator_Xfer<DRepr, IAllocator_DRepr>::_valid = facet::valid_facet_implementation<AAllocator, IAllocator_Xfer>();
} /*namespace mm*/
} /*namespace xo*/
/* end IAllocator_Xfer.hpp */

View file

@ -0,0 +1,61 @@
/** @file RAllocator.hpp
*
* @author Roland Conybeare, Dec 2025
**/
#pragma once
#include "AAllocator.hpp"
#include <xo/facet/RRouter.hpp>
#include <string>
namespace xo {
namespace mm {
/** @class RAllocator **/
template <typename Object>
struct RAllocator : public Object {
private:
using O = Object;
public:
using ObjectType = Object;
using DataPtr = Object::DataPtr;
using size_type = std::size_t;
using value_type = std::byte *;
RAllocator() {}
RAllocator(Object::DataPtr data) : Object{std::move(data)} {}
int32_t _typeseq() const noexcept { return O::iface()->_typeseq(); }
std::string_view name() const noexcept { return O::iface()->name(O::data()); }
size_type reserved() const noexcept { return O::iface()->reserved(O::data()); }
size_type size() const noexcept { return O::iface()->size(O::data()); }
size_type committed() const noexcept { return O::iface()->committed(O::data()); }
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()); }
bool expand(size_type z) { return O::iface()->expand(O::data(), z); }
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); }
value_type sub_alloc(size_type z,
bool complete_flag) noexcept { return O::iface()->sub_alloc(O::data(),
z, complete_flag); }
static bool _valid;
};
template <typename Object>
bool
RAllocator<Object>::_valid = facet::valid_object_router<Object>();
}
namespace facet {
template <typename Object>
struct RoutingFor<xo::mm::AAllocator, Object> {
using RoutingType = xo::mm::RAllocator<Object>;
};
}
}
/* end RAllocator.hpp */

View file

@ -0,0 +1,4 @@
AAllocator |<-- IAllocator_Any (D=DVariantPlaceholder)
|<-- IAllocator_Xfer<D,..>
OObject<AAllocator,D> |<-- RAllocator<O>