xo-alloc2: header reorg + DX1Collector utest

This commit is contained in:
Roland Conybeare 2025-12-14 16:58:58 -05:00
commit 9d4a920736
23 changed files with 75 additions and 588 deletions

View file

@ -5,6 +5,7 @@
#pragma once
#include "AllocatorError.hpp"
#include "xo/facet/facet_implementation.hpp"
#include "xo/facet/typeseq.hpp"
#include <string>
@ -14,54 +15,6 @@ namespace xo {
using Copaque = const void *;
using Opaque = void *;
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;
};
/** @class AAllocator
* @brief Abstract facet for allocation
*

View file

@ -1,60 +0,0 @@
/** @file ACollector.hpp
*
* @author Roland Conybeare, Dec 2025
**/
#pragma once
#include "IGCObject_Any.hpp"
#include <xo/facet/facet_implementation.hpp>
#include <xo/facet/typeseq.hpp>
#include <xo/facet/obj.hpp>
#include "gc/generation.hpp"
#include "gc/role.hpp"
#include <cstdint>
#include <cstddef>
namespace xo {
namespace mm {
using Copaque = const void *;
using Opaque = void *;
struct IGCObject_Any; // see IGCObject_Any.hpp
/** @class ACollector
* @brief Abstract facet for the XO garbage collector
*
* Collector also supports the @ref AAllocator facet, see also
**/
struct ACollector {
using size_type = std::size_t;
virtual int32_t _typeseq() const noexcept = 0;
virtual size_type allocated(Copaque d, generation g, role r) const noexcept = 0;
virtual size_type reserved(Copaque d, generation g, role r) const noexcept = 0;
virtual size_type committed(Copaque d, generation g, role r) const noexcept = 0;
/** install interface @p iface for representation with typeseq @p tseq
* in collector @p d.
*
* The type AGCObject_Any here is misleading.
* Will have been replaced by an instance of
* @c AGCObject_Xfer<DFoo,AGCObject_DFoo> for some @c DFoo
* in which case calls through @c std::launder(&iface)
* will properly act on @c DFoo.
**/
virtual void install_type(Opaque d, int32_t tseq, IGCObject_Any & iface);
virtual void add_gc_root(Opaque d, int32_t tseq, Opaque * root) = 0;
/** evacuate @p *lhs to to-space and replace with forwarding pointer
* Require: gc in progress
**/
virtual void forward_inplace(Opaque d, obj<AGCObject> * lhs) = 0;
};
}
} /*namespace xo*/

View file

@ -1,54 +0,0 @@
/** @file AGCObject.hpp
*
* @author Roland Conybeare, Dec 2025
**/
#pragma once
#include "IAllocator_Any.hpp"
#include "RAllocator.hpp"
#include "xo/facet/facet_implementation.hpp"
#include "xo/facet/typeseq.hpp"
#include "xo/facet/obj.hpp" // for obj<AAllocator> in shallow_copy
#include <cstdint>
#include <cstddef>
namespace xo {
namespace mm {
using Copaque = const void *;
using Opaque = void *;
/** @class AObject
* @brief Abstract facet for collector-eligible data
*
* Data that supports AGCObject can have memory managed
* by ACollector
**/
struct AGCObject {
using size_type = std::size_t;
/** RTTI: unique id# for actual runtime data representation **/
virtual int32_t _typeseq() const noexcept = 0;
virtual size_type shallow_size(Copaque d) const noexcept = 0;
virtual Opaque * shallow_copy(Copaque d,
obj<AAllocator> mm) const noexcept = 0;
virtual size_type forward_children(Opaque d) const noexcept = 0;
};
// implementation IGCObject_DRepr of AGCObject for state DRepr
// should provide a specialization:
//
// template <>
// struct xo::facet::FacetImplementation<AGCObjectx, DRepr> {
// using ImplType = IGCObject_DRepr;
// };
//
// then IGCObject_ImplType<DRepr> --> IGCObject_DRepr
//
template <typename DRepr>
using IGCObject_ImplType = xo::facet::FacetImplType<AGCObject, DRepr>;
} /*namespace mm*/
} /*namespace xo*/
/* end AGCObject.hpp */

View file

@ -5,6 +5,7 @@
#pragma once
#include "AllocatorError.hpp"
#include <string>
#include <cstdint>

View file

@ -48,6 +48,8 @@ namespace xo {
/** create arena per configuration @p cfg. **/
static DArena map(const ArenaConfig & cfg);
/** null ctor **/
DArena() = default;
/** ctor from already-mapped (but not committed) address range **/
DArena(const ArenaConfig & cfg, size_type page_z, std::byte * lo, std::byte * hi);
/** DArena is not copyable **/
@ -57,6 +59,9 @@ namespace xo {
/** dtor releases mapped memory **/
~DArena();
/** move-assignment **/
DArena & operator=(DArena && other);
///@}
/** obtain uncommitted contiguous memory range comprising
@ -65,6 +70,14 @@ namespace xo {
**/
static range_type map_aligned_range(size_type req_z, size_type hugepage_z);
/** @defgroup mm-arena-methods **/
///@{
/** true if arena is mapped i.e. has a reserved address range **/
bool is_mapped() const { return (lo_ != nullptr) && (hi_ != nullptr); }
///@}
/** @defgroup mm-arena-instance-vars **/
///@{

View file

@ -1,84 +0,0 @@
/** @file DCollector.hpp
*
* @author Roland Conybeare, Dec 2025
**/
#pragma once
#include "ArenaConfig.hpp"
#include "DArena.hpp"
#include <array>
namespace xo {
namespace mm {
template <typename T>
using up = std::unique_ptr<T>;
struct CollectorConfig {
using size_type = std::size_t;
/** Configuration for collector spaces.
* Will have at least {nursery,tenured} x {from,to} spaces.
* Not using name_ member.
**/
ArenaConfig arena_config_;
/** Number of generations.
* Must be at least 2.
**/
uint32_t n_generation_ = 2;
/** Number of promotion steps.
* An object that survives this number of collections
* advances to the next generation.
**/
uint32_t n_survive_threshold_ = 2;
/** Trigger garbage collection when to-space allocation for
* generation g reaches gc_trigger_v_[g]
**/
std::array<size_type, c_max_generation> gc_trigger_v_;
/** true -> enable incremental collection.
* false -> only do full collection.
*
* Incremental collection requires mutation logs.
**/
bool allow_incremental_gc_ = true;
/** If non-zero remember statistics for
* the last @p stats_history_z_ collections.
**/
uint32_t stats_history_z_ = false;
/** true to enable debug logging **/
bool debug_flag_ = false;
};
/** State associated with a single DCollector generation
**/
struct Generation {
Generation(uint8_t gen_id, up<DArena> from_space, up<Darena> to_space);
~Generation() = default;
/** identity of this generation. Generations are numbered from
* 0 (youngest) to N (oldest), with N <= c_max_generation
**/
uint8_t gen_id_;
/** from-space. empty between collection episodes.
* During collection holds former to-space
**/
up<DArena> from_space_;
/** to-space. New allocations occur here **/
up<DArena> to_space_;
};
struct DCollector {
std::uint32_t
std::array<up<DArena>, c_max_generation> generations_[2];
};
} /*namespace mm*/
} /*namespace xo*/
/* end DCollector.hpp */

View file

@ -1,51 +0,0 @@
/** @file ICollector_Any.hpp
*
* @author Roland Conybeare, Dec 2025
**/
#pragma once
#include "ACollector.hpp"
//#include <cassert>
namespace xo {
namespace mm { struct ICollector_Any; }
namespace facet {
template <>
struct FacetImplementation<xo::mm::ACollector, DVariantPlaceholder> {
using ImplType = xo::mm::ICollector_Any;
};
}
namespace mm {
/** @class ICollector_Any
* @brief Stub Collector Implementation for empty variant instance
**/
struct ICollector_Any : public ACollector {
using size_type = std::size_t;
// from ACollector
int32_t _typeseq() const noexcept override { return s_typeseq; }
// const methods
[[noreturn]] size_type allocated(Copaque, generation, role) const noexcept { _fatal(); }
[[noreturn]] size_type reserved(Copaque, generation, role) const noexcept { _fatal(); }
[[noreturn]] size_type committed(Copaque, generation, role) const noexcept { _fatal(); }
// non-const methods
[[noreturn]] void install_type(Opaque, int32_t, IGCObject_Any &) noexcept { _fatal(); }
[[noreturn]] void add_gc_root(Opaque, int32_t, Opaque *) { _fatal(); }
[[noreturn]] void forward_inplace(Opaque, obj<AGCObject> *) { _fatal(); }
private:
[[noreturn]] static void _fatal();
public:
static int32_t s_typeseq;
static bool _valid;
};
} /*namespace mm*/
} /*namespace xo*/
/* end ICollector_Any.hpp */

View file

@ -1,48 +0,0 @@
/** @file IGCObject_Any.hpp
*
* @author Roland Conybeare, Dec 2025
**/
#pragma once
#include "AGCObject.hpp"
#include <new>
namespace xo {
namespace mm { struct IGCObject_Any; }
namespace facet {
template <>
struct FacetImplementation<xo::mm::AGCObject, DVariantPlaceholder> {
using ImplType = xo::mm::IGCObject_Any;
};
}
namespace mm {
/** @class IGCObject_Any
* @brief AGCObject implementation for empty variant instance
**/
struct IGCObject_Any : public AGCObject {
using size_type = std::size_t;
const AGCObject * iface() const { return std::launder(this); }
// from AGCObject
int32_t _typeseq() const noexcept override { return s_typeseq; }
[[noreturn]] size_type shallow_size(Copaque) const noexcept override { _fatal(); }
[[noreturn]] Opaque * shallow_copy(Copaque,
obj<AAllocator>) const noexcept override { _fatal(); }
[[noreturn]] size_type forward_children(Opaque) const noexcept override { _fatal(); }
private:
[[noreturn]] static void _fatal();
public:
static int32_t s_typeseq;
static bool _valid;
};
} /*namespace mm*/
} /*namespace xo*/
/* end IGCObject_Any.hpp */

View file

@ -1,62 +0,0 @@
/** @file IGCObject_Xfer.hpp
*
* @author Roland Conybeare, Dec 2025
**/
#pragma once
#include "AGCObject.hpp"
namespace xo {
namespace mm {
/** @class IGCObject_Xfer
*
* Adapts typed GC object implementation @tparam IGCObject_DRepr
* to type-erased @ref AGCObject interface
**/
template <typename DRepr, typename IGCObject_DRepr>
struct IGCObject_Xfer : public AGCObject {
using Impl = IGCObject_DRepr;
using size_type = AGCObject::size_type;
static const DRepr & _dcast(Copaque d) { return *(const DRepr *)d; }
static DRepr & _dcast(Opaque d) { return *(DRepr *)d; }
// from AGCObject
// const methods
int32_t _typeseq() const noexcept override { return s_typeseq; }
size_type shallow_size(Copaque d) const noexcept override {
return I::shallow_copy(_dcast(d));
}
Opaque * shallow_copy(Copaque d, obj<AAllocator> mm) const noexcept override {
return I::shallow_size(_dcast(d), mm);
}
// non-const methods
size_type forward_children(Opaque d) const noexcept override {
return I::forward_children(d);
}
private:
using I = Impl;
public:
static int32_t s_typeseq;
static bool _valid;
};
template <typename DRepr, typename IGCObject_DRepr>
int32_t
IGCObject_Xfer<DRepr, IGCObject_DRepr>::s_typeseq = facet::typeseq::id<DRepr>();
template <typename DRepr, typename IGCObject_DRepr>
bool
IGCObject_Xfer<DRepr, IGCObject_DRepr>::_valid = facet::valid_facet_implementation<AGCObject, IGCObject_Xfer>();
} /*namespace mm*/
} /*namespace xo*/
/* end IGCObject_Xfer.hpp */

View file

@ -1,51 +0,0 @@
/** @file RCollector.hpp
*
* @author Roland Conybeare, Dec 2025
**/
#include "ACollector.hpp"
#include <xo/facet/RRouter.hpp>
namespace xo {
namespace mm {
/** @class RCollector **/
template <typename Object>
struct RCollector : 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 *;
RCollector() = default;
RCollector(DataPtr data) : Object{std::move(data)} {}
int32_t _typeseq() const noexcept { return O::iface()->_typeseq(); }
size_type allocated(generation g, role r) const noexcept { return O::iface()->allocated(O::data()); }
size_type reserved(generation g, role r) const noexcept { return O::iface()->reserved(O::data()); }
size_type committed(generation g, role r) const noexcept { return O::iface()->committed(O::data()); }
void install_type(int32_t tseq, IGCObject_Any & iface) { return O::iface()->install_type(O::data()); }
void add_gc_root(int32_t tseq, Opaque * root) { O::iface()->add_gc_root(O::data()); }
void forward_inplace(obj<AGCObject> * lhs) { O::iface()->forward_inplace(O::data(), lhs); }
static bool _valid;
};
template <typename Object>
bool
RCollector<Object>::_valid = facet::valid_object_router<Object>();
} /*namespace mm*/
namespace facet {
template <typename Object>
struct RoutingFor<xo::mm::ACollector, Object> {
using RoutingType = xo::mm::RCollector<Object>;
};
}
} /*namespace xo*/
/* end RCollector.hpp */

View file

@ -1,51 +0,0 @@
/** @file RCollector_Any.hpp
*
* @author Roland Conybeare, Dec 2025
**/
#pragma once
#include "ACollector.hpp"
//#include <cassert>
namespace xo {
namespace mm { struct ICollector_Any; }
namespace facet {
template <>
struct FacetImplementation<xo::mm::ACollector, DVariantPlaceholder> {
using ImplType = xo::mm::ICollector_Any;
};
}
namespace mm {
/** @class ICollector_Any
* @brief Stub Collector Implementation for empty variant instance
**/
struct ICollector_Any : public ACollector {
using size_type = std::size_t;
// from ACollector
int32_t _typeseq() const noexcept override { return s_typeseq; }
// const methods
[[noreturn]] size_type allocated(Copaque, generation, role) const noexcept { _fatal(); }
[[noreturn]] size_type reserved(Copaque, generation, role) const noexcept { _fatal(); }
[[noreturn]] size_type committed(Copaque, generation, role) const noexcept { _fatal(); }
// non-const methods
[[noreturn]] void install_type(Opaque, int32_t, IGCObject_Any &) noexcept { _fatal(); }
[[noreturn]] void add_gc_root(Opaque, int32_t, Opaque *) { _fatal(); }
[[noreturn]] void forward_inplace(Opaque, Opaque **) { _fatail(); }
private:
[[noreturn]] static void _fatal();
public:
static int32_t s_typeseq;
static bool _valid;
};
} /*namespace mm*/
} /*namespace xo*/
/* end RCollector_Any.hpp */

View file

@ -1,47 +0,0 @@
/** @file RGCObject.hpp
*
* @author Roland Conybeare, Dec 2025
**/
#pragma once
#include "AGCObject.hpp"
#include <xo/facet/RRouter.hpp>
namespace xo {
namespace mm {
/** @class RGCObject **/
template <typename Object>
struct RGCObject : public Object {
private:
using O = Object;
public:
using ObjectType = Object;
using DataPtr = Object::DataPtr;
using size_type = std::size_t;
RGCObject() = default;
RGCObject(Object::DataPtr data) : Object{std::move(data)} {}
int32_t _typeseq() const noexcept { return O::iface()->_typeseq(); }
size_type shallow_size() const noexcept { O::iface()->shallow_size(O::data()); }
Opaque * shallow_copy(obj<AAllocator> mm) const noexcept { O::iface()->shallow_copy(O::data(), mm); }
size_type forward_children() noexcept { O::iface()->forward_children(O::data()); }
static bool _valid;
};
template <typename Object>
bool
RGCObject<Object>::_valid = facet::valid_object_router<RGCObject>();
} /*namespace mm*/
namespace facet {
template <typename Object>
struct RoutingFor<xo::mm::AGCObject, Object> {
using RoutingType = xo::mm::RGCObject<Object>;
};
}
} /*namespace xo*/
/* end RGCObject.hpp */

View file

@ -19,9 +19,11 @@ namespace xo {
struct generation {
using value_type = std::uint32_t;
explicit generation(value_type x) : value_{x} {}
explicit constexpr generation(value_type x) : value_{x} {}
operator value_type() const { return value_; }
constexpr operator value_type() const { return value_; }
generation & operator++() { ++value_; return *this; }
std::uint32_t value_;
};

View file

@ -9,23 +9,20 @@
namespace xo {
namespace mm {
enum class role {
/** GC will keep one to-space for each generation.
* Application allocs always happen in to-space.
**/
to_space,
/** During normal operation from-space is empty.
* During collection phase itself,
* to-space and from-space are exchanged,
* with from-space becoming the space to be collected
**/
from_space,
/** counts entries **/
N
static constexpr uint32_t c_n_role = 2;
struct role {
using value_type = std::uint32_t;
explicit constexpr role(value_type x) : role_{x} {}
static constexpr role to_space() { return role{0}; }
static constexpr role from_space() { return role{1}; }
operator value_type() const { return role_; }
std::uint32_t role_;
};
constexpr uint32_t role2int(role x) { return static_cast<uint32_t>(x); }
} /*namespace mm*/
} /*namespace xo*/

View file

@ -9,8 +9,10 @@ set(SELF_SRCS
IAllocator_DArena.cpp
ICollector_Any.cpp
IGCObject_Any.cpp
DX1Collector.cpp
)
xo_add_shared_library4(${SELF_LIB} ${PROJECT_NAME}Targets ${PROJECT_VERSION} 1 ${SELF_SRCS})

View file

@ -192,6 +192,31 @@ namespace xo {
other.last_error_ = AllocatorError();
}
DArena &
DArena::operator=(DArena && other)
{
config_ = other.config_;
page_z_ = other.page_z_;
lo_ = other.lo_;
committed_z_ = other.committed_z_;
free_ = other.free_;
limit_ = other.limit_;
hi_ = other.hi_;
error_count_ = other.error_count_;
last_error_ = other.last_error_;
other.config_ = ArenaConfig();
other.lo_ = nullptr;
other.committed_z_ = 0;
other.free_ = nullptr;
other.limit_ = nullptr;
other.hi_ = nullptr;
other.error_count_ = 0;
other.last_error_ = AllocatorError();
return *this;
}
DArena::~DArena()
{
if (lo_) {

View file

@ -3,7 +3,7 @@
* @author Roland Conybeare, Dec 2025
**/
#include "ICollector_Any.hpp"
#include "gc/ICollector_Any.hpp"
#include <iostream>
namespace xo {

View file

@ -3,7 +3,7 @@
* @author Roland Conybeare, Dec 2025
**/
#include "IGCObject_Any.hpp"
#include "gc/IGCObject_Any.hpp"
#include <iostream>
namespace xo {

View file

@ -5,7 +5,9 @@ set(UTEST_EXE utest.alloc2)
set(UTEST_SRCS
alloc2_utest_main.cpp
arena.test.cpp
objectmodel.test.cpp)
objectmodel.test.cpp
Collector.test.cpp
)
if (ENABLE_TESTING)
xo_add_utest_executable(${UTEST_EXE} ${UTEST_SRCS})

View file

@ -3,8 +3,8 @@
* @author Roland Conybeare, Dec 2025
**/
#include "xo/alloc2/RGCObject.hpp"
#include "xo/alloc2/IGCObject_Any.hpp"
#include "xo/alloc2/gc/RGCObject.hpp"
#include "xo/alloc2/gc/IGCObject_Any.hpp"
#include "xo/facet/obj.hpp"
namespace xo {

View file

@ -6,8 +6,8 @@
#pragma once
#include "xo/alloc2/AAllocator.hpp"
#include "xo/alloc2/AGCObject.hpp"
#include "xo/alloc2/IGCObject_Xfer.hpp"
#include <xo/alloc2/gc/AGCObject.hpp>
#include <xo/alloc2/gc/IGCObject_Xfer.hpp>
#include "DFloat.hpp"
namespace xo {

View file

@ -6,8 +6,8 @@
#pragma once
#include "xo/alloc2/AAllocator.hpp"
#include "xo/alloc2/AGCObject.hpp"
#include "xo/alloc2/IGCObject_Xfer.hpp"
#include <xo/alloc2/gc/AGCObject.hpp>
#include <xo/alloc2/gc/IGCObject_Xfer.hpp>
#include "DInteger.hpp"
namespace xo {

View file

@ -7,11 +7,11 @@
#include <xo/alloc2/AAllocator.hpp>
#include <xo/alloc2/RAllocator.hpp>
#include <xo/alloc2/ACollector.hpp>
#include <xo/alloc2/ICollector_Any.hpp>
#include <xo/alloc2/RCollector.hpp>
#include <xo/alloc2/AGCObject.hpp>
#include <xo/alloc2/IGCObject_Xfer.hpp>
#include <xo/alloc2/gc/ACollector.hpp>
#include <xo/alloc2/gc/ICollector_Any.hpp>
#include <xo/alloc2/gc/RCollector.hpp>
#include <xo/alloc2/gc/AGCObject.hpp>
#include <xo/alloc2/gc/IGCObject_Xfer.hpp>
#include "DList.hpp"
namespace xo {