xo-gc: refactor to extract DX1Collector etc from xo-alloc2/

This commit is contained in:
Roland Conybeare 2025-12-22 22:24:10 -05:00
commit 093cf3c969
56 changed files with 452 additions and 83 deletions

View file

@ -3,7 +3,7 @@
* @author Roland Conybeare, Dec 2025
**/
#include "alloc/AllocInfo.hpp"
#include "AllocInfo.hpp"
namespace xo {
namespace mm {

View file

@ -15,15 +15,6 @@ set(SELF_SRCS
DArenaIterator.cpp
IAllocIterator_DArenaIterator.cpp
ICollector_Any.cpp
IGCObject_Any.cpp
IAllocator_DX1Collector.cpp
ICollector_DX1Collector.cpp
IAllocIterator_DX1CollectorIterator.cpp
DX1Collector.cpp
DX1CollectorIterator.cpp
)
xo_add_shared_library4(${SELF_LIB} ${PROJECT_NAME}Targets ${PROJECT_VERSION} 1 ${SELF_SRCS})

View file

@ -1,289 +0,0 @@
/** @file DX1Collector.cpp
*
* @author Roland Conybeare, Dec 2025
**/
#include "Allocator.hpp"
#include "arena/IAllocator_DArena.hpp"
#include "gc/DX1Collector.hpp"
#include "gc/DX1CollectorIterator.hpp"
#include "gc/generation.hpp"
#include "gc/object_age.hpp"
#include <xo/facet/obj.hpp>
#include <xo/indentlog/scope.hpp>
#include <cassert>
#include <cstdint>
namespace xo {
using xo::mm::AAllocator;
using xo::facet::with_facet;
namespace mm {
#ifdef NOT_USING
constexpr std::uint64_t
CollectorConfig::gen_mult() const {
return 1ul << arena_config_.header_size_bits_;
}
#endif
#ifdef NOT_USING
constexpr std::uint64_t
CollectorConfig::tseq_mult() const {
return 1ul << (gen_bits_ + arena_config_.header_size_bits_);
}
#endif
// ----- GCRunState -----
GCRunState::GCRunState(generation gc_upto)
: gc_upto_{gc_upto}
{}
GCRunState
GCRunState::gc_not_running()
{
return GCRunState(generation(0));
}
GCRunState
GCRunState::gc_upto(generation g)
{
return GCRunState(generation(g + 1));
}
// ----- DX1Collector -----
using size_type = xo::mm::DX1Collector::size_type;
DX1Collector::DX1Collector(const CollectorConfig & cfg) : config_{cfg}
{
assert(config_.arena_config_.header_.size_bits_ +
config_.arena_config_.header_.age_bits_ +
config_.arena_config_.header_.tseq_bits_ <= 64);
for (uint32_t igen = 0, ngen = cfg.n_generation_; igen < ngen; ++igen) {
space_storage_[0][igen] = DArena::map(cfg.arena_config_);
space_storage_[1][igen] = DArena::map(cfg.arena_config_);
space_[role::to_space()][igen] = &space_storage_[0][igen];
space_[role::from_space()][igen] = &space_storage_[1][igen];
}
for (uint32_t igen = cfg.n_generation_; igen < c_max_generation; ++igen) {
space_[role::to_space()][igen] = nullptr;
space_[role::from_space()][igen] = nullptr;
}
}
bool
DX1Collector::contains(role r, const void * addr) const noexcept
{
for (generation gi{0}; gi < config_.n_generation_; ++gi) {
const DArena * arena = get_space(r, gi);
if (arena->contains(addr))
return true;
}
return false;
}
AllocError
DX1Collector::last_error() const noexcept
{
// TODO:
// need to adjust here if runtime errors
// encountered during gc.
return get_space(role::to_space(), generation::nursery())->last_error_;
}
namespace {
size_type
accumulate_total_aux(const DX1Collector & d,
size_t (DArena::* get_stat_fn)() const) noexcept
{
size_t z = 0;
for (role ri : role::all()) {
for (generation gj{0}; gj < d.config_.n_generation_; ++gj) {
const DArena * arena = d.get_space(ri, gj);
assert(arena);
z += (arena->*get_stat_fn)();
}
}
return z;
}
}
size_type
DX1Collector::reserved_total() const noexcept
{
return accumulate_total_aux(*this, &DArena::reserved);
}
size_type
DX1Collector::size_total() const noexcept
{
return committed_total();
}
size_type
DX1Collector::committed_total() const noexcept
{
return accumulate_total_aux(*this, &DArena::committed);
}
size_type
DX1Collector::available_total() const noexcept
{
return accumulate_total_aux(*this, &DArena::available);
}
size_type
DX1Collector::allocated_total() const noexcept
{
return accumulate_total_aux(*this, &DArena::allocated);
}
size_type
DX1Collector::header2size(header_type hdr) const noexcept
{
uint32_t z = config_.arena_config_.header_.size(hdr);
return z;
}
object_age
DX1Collector::header2age(header_type hdr) const noexcept
{
uint32_t age = config_.arena_config_.header_.age(hdr);
assert(age < c_max_object_age);
return object_age(age);
}
uint32_t
DX1Collector::header2tseq(header_type hdr) const noexcept
{
uint32_t tseq = config_.arena_config_.header_.tseq(hdr);
return tseq;
}
bool
DX1Collector::is_forwarding_header(header_type hdr) const noexcept
{
/** forwarding pointer encoded as sentinel tseq **/
return config_.arena_config_.header_.is_forwarding_tseq(hdr);
}
auto
DX1Collector::alloc(size_type z) noexcept -> value_type
{
return with_facet<AAllocator>::mkobj(new_space()).alloc(z);
}
auto
DX1Collector::super_alloc(size_type z) noexcept -> value_type {
return with_facet<AAllocator>::mkobj(new_space()).super_alloc(z);
}
auto
DX1Collector::sub_alloc(size_type z, bool complete) noexcept -> value_type {
return with_facet<AAllocator>::mkobj(new_space()).sub_alloc(z, complete);
}
bool
DX1Collector::expand(size_type z) noexcept
{
if (with_facet<AAllocator>::mkobj(to_space(generation{0})).expand(z))
return with_facet<AAllocator>::mkobj(from_space(generation{0})).expand(z);
return false;
}
AllocInfo
DX1Collector::alloc_info(value_type mem) const noexcept {
for (role ri : role::all()) {
for (generation gj{0}; gj < config_.n_generation_; ++gj) {
const DArena * arena = this->get_space(ri, gj);
assert(arena);
if (arena->contains(mem)) {
return arena->alloc_info(mem);
}
}
}
// deliberately attempt on nursery to-space, to capture error info + return sentinel
return this->get_space(role::to_space(), generation{0})->alloc_info(mem);
}
DX1CollectorIterator
DX1Collector::begin() const noexcept
{
scope log(XO_DEBUG(false));
const DArena * arena
= get_space(role::to_space(),
generation{0});
return DX1CollectorIterator(this,
generation{0},
generation{config_.n_generation_},
arena->begin(),
arena->end());
}
DX1CollectorIterator
DX1Collector::end() const noexcept {
scope log(XO_DEBUG(false));
generation gen_hi = generation{config_.n_generation_};
/** valid iterator for end points to end of last DArena.
* otherwise will interfere with working compare
* (since invalid iterators are incomparable)
**/
const DArena * arena
= get_space(role::to_space(),
generation(config_.n_generation_ - 1));
DArenaIterator arena_end = arena->end();
return DX1CollectorIterator(this,
gen_hi,
gen_hi,
arena_end,
arena_end);
}
void
DX1Collector::reverse_roles(generation g) noexcept {
assert(g < config_.n_generation_);
std::swap(space_[0][g], space_[1][g]);
}
void
DX1Collector::clear() noexcept {
for (role ri : role::all()) {
for (generation gj{0}; gj < config_.n_generation_; ++gj) {
DArena * arena = this->get_space(ri, gj);
assert(arena);
arena->clear();
}
}
}
} /*namespace mm*/
} /*namespace xo*/
/* end DX1Collector.cpp */

View file

@ -1,126 +0,0 @@
/** @file DX1CollectorIterator.cpp
*
* @author Roland Conybeare, Dec 2025
**/
#include "gc/DX1CollectorIterator.hpp"
#include "gc/DX1Collector.hpp"
#include <xo/indentlog/scope.hpp>
#include <xo/indentlog/print/tag.hpp>
namespace xo {
namespace mm {
DX1CollectorIterator::DX1CollectorIterator(const DX1Collector * gc,
generation gen_ix,
generation gen_hi,
DArenaIterator arena_ix,
DArenaIterator arena_hi) : gc_{gc},
gen_ix_{gen_ix},
gen_hi_{gen_hi},
arena_ix_{arena_ix},
arena_hi_{arena_hi}
{
this->normalize();
}
void
DX1CollectorIterator::normalize() noexcept
{
scope log(XO_DEBUG(false),
xtag("gen_ix", gen_ix_),
xtag("gen_hi", gen_hi_),
xtag("arena_ix.pos", arena_ix_.pos_),
xtag("arena_hi.pos", arena_hi_.pos_));
/* normalize: find lowest generation with non-empty to-space */
if (arena_ix_.pos_ == arena_hi_.pos_) {
log && log(xtag("action", "look-lub-nonempty-gen"));
if (gen_ix_ < gen_hi_)
++gen_ix_;
for (; gen_ix_ < gen_hi_; ++gen_ix_) {
const DArena * arena
= gc_->get_space(role::to_space(), gen_ix_);
assert(arena);
arena_ix_ = arena->begin();
arena_hi_ = arena->end();
if (arena_ix_ != arena_hi_) {
// normalization achieved!
break;
}
}
log && log(xtag("gen_ix", gen_ix_),
xtag("arena_ix.pos", arena_ix_.pos_),
xtag("arena_hi.pos", arena_hi_.pos_));
} else {
log && log(xtag("action", "noop"));
}
}
AllocInfo
DX1CollectorIterator::deref() const noexcept
{
return arena_ix_.deref();
}
cmpresult
DX1CollectorIterator::compare(const DX1CollectorIterator & other_ix) const noexcept
{
scope log(XO_DEBUG(false),
xtag("is_valid", is_valid()),
xtag("other_ix.is_valid", other_ix.is_valid()) );
if (is_invalid() || (gc_ != other_ix.gc_)) {
log && log("incomparable!");
return cmpresult::incomparable();
}
if (gen_ix_ != other_ix.gen_ix_) {
log && log(xtag("gen", gen_ix_), xtag("other.gen", other_ix.gen_ix_));
/* same collector, different arenas -> compare based on gen# */
return cmpresult::from_cmp(gen_ix_, other_ix.gen_ix_);
}
/* both iterators refer to the same arena,
* so can compare their arena iterators directly
*/
cmpresult retval = arena_ix_.compare(other_ix.arena_ix_);
log && log(xtag("retval", retval));
return retval;
}
void
DX1CollectorIterator::next() noexcept
{
scope log(XO_DEBUG(false),
xtag("arena_ix.arena", arena_ix_.arena_),
xtag("arena_ix.pos", arena_ix_.pos_),
xtag("arena_hi.arena", arena_hi_.arena_),
xtag("arena_hi.pos", arena_hi_.pos_));
if (arena_ix_ != arena_hi_) {
++arena_ix_;
log && log(xtag("++arena_ix.pos", arena_ix_.pos_));
this->normalize();
log && log(xtag("arena_ix.arena", arena_ix_.arena_),
xtag("arena_ix.pos", arena_ix_.pos_));
} else {
log && log(xtag("action", "arena-at-end"));
}
}
} /*namespace mm*/
} /*namespace xo*/
/* end DX1CollectorIterator.cpp */

View file

@ -1,41 +0,0 @@
/** @file IAllocIterator_DX1CollectorIterator.cpp
*
* @author Roland Conybeare, Dec 2025
**/
#include "gc/IAllocIterator_DX1CollectorIterator.hpp"
#include "AllocIterator.hpp"
//#include <cassert>
namespace xo {
namespace mm {
AllocInfo
IAllocIterator_DX1CollectorIterator::deref(const DX1CollectorIterator & ix) noexcept
{
return ix.deref();
}
cmpresult
IAllocIterator_DX1CollectorIterator::compare(const DX1CollectorIterator & ix,
const obj<AAllocIterator> & other_arg) noexcept
{
/* downcast from variant */
auto other = obj<AAllocIterator, DX1CollectorIterator>::from(other_arg);
if (!other)
return cmpresult::incomparable();
DX1CollectorIterator & other_ix = *other.data();
return ix.compare(other_ix);
}
void
IAllocIterator_DX1CollectorIterator::next(DX1CollectorIterator & ix) noexcept
{
ix.next();
}
} /*namespace mm*/
} /*namespace xo*/
/* end IAllocIterator_DX1CollectorIterator.cpp */

View file

@ -1,127 +0,0 @@
/** @file IAllocator_DX1Collector.cpp
*
* @author Roland Conybeare, Dec 2025
*
* See also ICollector_DX1Collector.cpp for collector facet
**/
#include "gc/IAllocator_DX1Collector.hpp"
#include "gc/IAllocIterator_DX1CollectorIterator.hpp"
#include "gc/DX1CollectorIterator.hpp"
#include "arena/IAllocator_DArena.hpp"
namespace xo {
using xo::facet::with_facet;
using std::size_t;
using std::byte;
namespace mm {
using value_type = IAllocator_DX1Collector::value_type;
std::string_view
IAllocator_DX1Collector::name(const DX1Collector & d) noexcept
{
return d.config_.name_;
}
auto
IAllocator_DX1Collector::reserved(const DX1Collector & d) noexcept -> size_type
{
return d.reserved_total();
}
auto
IAllocator_DX1Collector::size(const DX1Collector & d) noexcept -> size_type
{
return d.size_total();
}
auto
IAllocator_DX1Collector::committed(const DX1Collector & d) noexcept -> size_type
{
return d.committed_total();
}
auto
IAllocator_DX1Collector::available(const DX1Collector & d) noexcept -> size_type
{
return d.available_total();
}
auto
IAllocator_DX1Collector::allocated(const DX1Collector & d) noexcept -> size_type
{
return d.allocated_total();
}
bool
IAllocator_DX1Collector::contains(const DX1Collector & d, const void * addr) noexcept
{
return d.contains(role::to_space(), addr);
}
AllocError
IAllocator_DX1Collector::last_error(const DX1Collector & d) noexcept
{
return d.last_error();
}
auto
IAllocator_DX1Collector::alloc_range(const DX1Collector & d,
DArena & ialloc) noexcept -> range_type
{
DX1CollectorIterator * begin_ix = construct_with<DX1CollectorIterator>(ialloc, d.begin());
DX1CollectorIterator * end_ix = construct_with<DX1CollectorIterator>(ialloc, d.end());
obj<AAllocIterator> begin_obj = with_facet<AAllocIterator>::mkobj(begin_ix);
obj<AAllocIterator> end_obj = with_facet<AAllocIterator>::mkobj( end_ix);
return AllocRange(std::make_pair(begin_obj, end_obj));
}
auto
IAllocator_DX1Collector::alloc(DX1Collector & d, size_type z) noexcept -> value_type
{
return d.alloc(z);
}
auto
IAllocator_DX1Collector::super_alloc(DX1Collector & d, size_type z) noexcept -> value_type
{
return d.super_alloc(z);
}
auto
IAllocator_DX1Collector::sub_alloc(DX1Collector & d, size_type z, bool complete) noexcept -> value_type
{
return d.sub_alloc(z, complete);
}
bool
IAllocator_DX1Collector::expand(DX1Collector & d, size_type z) noexcept
{
return d.expand(z);
}
AllocInfo
IAllocator_DX1Collector::alloc_info(const DX1Collector & d, value_type mem) noexcept
{
return d.alloc_info(mem);
}
void
IAllocator_DX1Collector::clear(DX1Collector & d)
{
d.clear();
}
void
IAllocator_DX1Collector::destruct_data(DX1Collector & d)
{
d.~DX1Collector();
}
} /*namespace mm*/
} /*namespace xo*/
/* end IAllocator_DX1Collector.cpp */

View file

@ -1,39 +0,0 @@
/** @file ICollector_Any.cpp
*
* @author Roland Conybeare, Dec 2025
**/
#include "gc/ICollector_Any.hpp"
#include <iostream>
namespace xo {
using xo::facet::DVariantPlaceholder;
using xo::facet::typeseq;
using xo::facet::valid_facet_implementation;
namespace mm {
void
ICollector_Any::_fatal() {
/* control here on uninitialized ICollector_Any.
* Initialized instance will have specific implementation type
* e.g. ICollector_Xfer<DCollector>
*/
std::cerr << "fatal"
<< ": attempt to call uninitialized"
<< " ICollector_Any method"
<< std::endl;
std::terminate();
}
int32_t
ICollector_Any::s_typeseq = typeseq::id<DVariantPlaceholder>();
bool
ICollector_Any::_valid = valid_facet_implementation<ACollector, ICollector_Any>();
} /*namespace mm*/
} /*namespace xo*/
/** end ICollector_Any.cpp */

View file

@ -1,191 +0,0 @@
/** @file ICollector_DX1Collector.cpp
*
* @author Roland Conybeare, Dec 2025
*
* See also IAllocator_DX1Collector.cpp for allocator facet
**/
#include "gc/ICollector_DX1Collector.hpp"
#include "GCObject.hpp"
namespace xo {
namespace mm {
using size_type = ICollector_DX1Collector::size_type;
using std::byte;
namespace {
size_type
stat_helper(const DX1Collector & d,
size_type (DArena::* getter)() const,
generation g,
role r)
{
const DArena * arena = d.get_space(r, g);
if (arena) [[likely]]
return (arena->*getter)();
return 0;
}
}
size_type
ICollector_DX1Collector::reserved(const DX1Collector & d, generation g, role r)
{
return stat_helper(d, &DArena::reserved, g, r);
}
size_type
ICollector_DX1Collector::allocated(const DX1Collector & d, generation g, role r)
{
return stat_helper(d, &DArena::allocated, g, r);
}
size_type
ICollector_DX1Collector::committed(const DX1Collector & d, generation g, role r)
{
return stat_helper(d, &DArena::committed, g, r);
}
void
ICollector_DX1Collector::install_type(DX1Collector & d,
std::int32_t tseq,
IGCObject_Any & iface)
{
(void)d;
(void)tseq;
(void)iface;
assert(false);
}
void
ICollector_DX1Collector::add_gc_root(DX1Collector & d,
int32_t tseq,
Opaque * root)
{
(void)d;
(void)tseq;
(void)root;
assert(false);
}
void
ICollector_DX1Collector::forward_inplace(DX1Collector & d,
obj<AGCObject> * lhs)
{
assert(d.runstate_.is_running());
/*
* lhs obj<AGCObject>
* | +---------+ +---+-+----+
* \--->| .iface | | T |G|size| header
* +---------+ object_data +---+-+----+
* | .data x----------------->| alloc |
* +---------+ | data |
* | for |
* | instance |
* | ... |
* +----------+
*/
void * object_data = (byte *)(*lhs).opaque_data();
if (!d.contains(role::from_space(), object_data)) {
/* *lhs isn't in GC-allocated space.
*
* This happens for a modest number of global
* constant, for example DBoolean {true, false}.
*
* It's important we recognize these up front.
* Since not allocated from GC, they don't have
* an alloc-header.
*/
return;
}
/** NOTE: for form's sake:
* better to lookup actual arena that
* allocated object data.
*
**/
DArena * some_arena = d.to_space(generation(0));
DArena::header_type * p_header
= some_arena->obj2hdr(object_data);
DArena::header_type alloc_hdr = *p_header;
/* recover allocation size */
std::size_t alloc_z = some_arena->config_.header_.size(alloc_hdr);
/* need to be able to fit forwarding pointer
* in place of forwarded object.
*
* This is guaranteed anyway, by alignment rules
*/
assert(alloc_z > sizeof(uintptr_t));
if (d.is_forwarding_header(alloc_hdr)) {
/* *lhs already refers to a forwarding pointer */
/*
* lhs obj<AGCObject>
* | +---------+ +---+-+----+
* \--->| .iface | |FWD|G|size| alloc_hdr
* +---------+ object_data +---+-+----+
* | .data x----------------->| x-------->
* +---------+ | | dest
* | |
* +----------+
*/
void * dest = *(void**)object_data;
/* update *lhs in-place */
(*lhs).reset_opaque(dest);
} else if (check_move_policy(d, alloc_hdr, object_data)) {
/* copy object *lhs + replace with forwarding pointer */
/* which arena are we writing to? need allocator interface */
assert(false);
#ifdef NOT_YET
// to do this need IAllocator_DX1Collector fully implemented
void * copy = (*lhs).shallow_copy(xxx);
xxx mm xxx;
#endif
} else {
/* object doesn't need to move.
* e.g. incremental collection + object is tenured
*/
}
}
bool
ICollector_DX1Collector::check_move_policy(const DX1Collector & d,
header_type alloc_hdr,
void * object_data)
{
(void)object_data;
// when gc is moving objects, to- and from- spaces have been
// reversed: forwarding pointers are located in from-space and
// refer to to-space.
object_age age = d.header2age(alloc_hdr);
generation g = d.config_.age2gen(age);
assert(d.runstate_.is_running());
return (g < d.runstate_.gc_upto());
}
} /*namespace mm*/
} /*namespace xo*/
/* end ICollector_DX1Collector.cpp */

View file

@ -1,34 +0,0 @@
/** @file IGCObject_Any.cpp
*
* @author Roland Conybeare, Dec 2025
**/
#include "gcobject/IGCObject_Any.hpp"
#include <iostream>
namespace xo {
using xo::facet::DVariantPlaceholder;
using xo::facet::typeseq;
using xo::facet::valid_facet_implementation;
namespace mm {
void
IGCObject_Any::_fatal() {
std::cerr << "fatal"
<< ": attempt to call uninitialized"
<< " IGCObject_Any method"
<< std::endl;
std::terminate();
}
int32_t
IGCObject_Any::s_typeseq = typeseq::id<DVariantPlaceholder>();
bool
IGCObject_Any::_valid = valid_facet_implementation<AGCObject, IGCObject_Any>();
} /*namespace mm*/
} /*namespace xo*/
/* end IGCObject_Any.cpp */