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 3b89f367d7
41 changed files with 14 additions and 2438 deletions

View file

@ -7,15 +7,15 @@ set(UTEST_SRCS
objectmodel.test.cpp
arena.test.cpp
DArenaIterator.test.cpp
Collector.test.cpp
DX1CollectorIterator.test.cpp
# Collector.test.cpp
# DX1CollectorIterator.test.cpp
random_allocs.cpp
)
if (ENABLE_TESTING)
xo_add_utest_executable(${UTEST_EXE} ${UTEST_SRCS})
xo_headeronly_dependency(${UTEST_EXE} randomgen)
xo_self_dependency(${UTEST_EXE} xo_alloc2)
xo_headeronly_dependency(${UTEST_EXE} randomgen)
xo_headeronly_dependency(${UTEST_EXE} indentlog)
xo_headeronly_dependency(${UTEST_EXE} xo_facet)
xo_external_target_dependency(${UTEST_EXE} Catch2 Catch2::Catch2)

View file

@ -1,243 +0,0 @@
/** @file Collector.test.cpp
*
* @author Roland Conybeare, Dec 2025
*
* NOTE: properly unit testing gc behavior requires
* xo-object2 dependency;
* see xo-object2/utest
**/
#include "Allocator.hpp"
#include "Collector.hpp"
#include "random_allocs.hpp"
#include "gc/ICollector_DX1Collector.hpp"
#include "gc/IAllocator_DX1Collector.hpp"
//#include "gc/DX1Collector.hpp"
#include <xo/randomgen/xoshiro256.hpp>
#include <xo/randomgen/random_seed.hpp>
#include <xo/indentlog/scope.hpp>
#include <xo/indentlog/print/tag.hpp>
#include <xo/indentlog/print/array.hpp>
#include <catch2/catch.hpp>
namespace xo {
using xo::mm::AAllocator;
using xo::mm::ACollector;
using xo::mm::CollectorConfig;
using xo::mm::DX1Collector;
using xo::mm::ArenaConfig;
using xo::mm::AllocHeaderConfig;
using xo::mm::generation;
using xo::mm::c_max_generation;
using xo::facet::with_facet;
using xo::scope;
namespace ut {
// checklist
// - obj<ACollector> constructible [ ]
// - obj<ACollector> truthy [ ]
// - obj<ACollector,DX1Collector> constructible [ ]
//
// - obj<AAllocator,DX1Collector> constructible [ ]
// - obj<AAllocator,DX1Collector> allocation
TEST_CASE("collector-any-null", "[alloc2][gc][ACollector]")
{
/* empty variant collector */
obj<ACollector> gc1;
REQUIRE(!gc1);
REQUIRE(gc1.iface() != nullptr);
REQUIRE(gc1.data() == nullptr);
}
TEST_CASE("DX1Collector-1", "[alloc2][gc][DX1Collector]")
{
ArenaConfig arena_cfg = { .name_ = "_test_unused",
.size_ = 4*1024*1024,
.store_header_flag_ = true,
.header_ = AllocHeaderConfig(0 /*guard_z*/,
0xfd /*guard_byte*/,
0 /*tseq_bits*/,
0 /*age_bits*/,
16 /*size_bits*/), };
CollectorConfig cfg = { .arena_config_ = arena_cfg,
.n_generation_ = 2,
.gc_trigger_v_ = {{64*1024, 1024*1024, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0}} };
DX1Collector gc = DX1Collector{cfg};
generation g0 = generation{0};
REQUIRE(gc.to_space(g0));
REQUIRE(gc.from_space(g0));
REQUIRE(gc.to_space(g0)->is_mapped());
REQUIRE(gc.from_space(g0)->is_mapped());
generation g1 = generation{1};
REQUIRE(gc.to_space(g1));
REQUIRE(gc.from_space(g1));
REQUIRE(gc.to_space(g1)->is_mapped());
REQUIRE(gc.from_space(g1)->is_mapped());
/* verify from/to x N/T are unique */
REQUIRE(gc.to_space(g0) != gc.from_space(g0));
REQUIRE(gc.to_space(g1) != gc.to_space(g0));
REQUIRE(gc.from_space(g1) != gc.from_space(g0));
REQUIRE(gc.to_space(g0) != gc.from_space(g1));
for (generation gi = generation(2); gi < c_max_generation; ++gi) {
INFO(xtag("gi", gi));
REQUIRE(!gc.to_space(gi));
REQUIRE(!gc.from_space(gi));
REQUIRE(!gc.space_storage_[0][gi].is_mapped());
REQUIRE(!gc.space_storage_[1][gi].is_mapped());
}
}
TEST_CASE("collector-x1-obj", "[alloc2][gc]")
{
ArenaConfig arena_cfg = { .name_ = "_test_unused",
.size_ = 4*1024*1024,
.store_header_flag_ = true,
.header_ = AllocHeaderConfig(0 /*guard_z*/,
0xfd /*guard_byte*/,
0 /*tseq_bits*/,
0 /*age_bits*/,
16 /*size_bits*/), };
CollectorConfig cfg = { .arena_config_ = arena_cfg,
.n_generation_ = 2,
.gc_trigger_v_ = {{64*1024, 1024*1024, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0}} };
DX1Collector gc = DX1Collector{cfg};
/* typed collector -- repr known at compile time */
obj<ACollector, DX1Collector> x1(&gc);
REQUIRE(x1.iface());
REQUIRE(x1.data());
}
TEST_CASE("collector-x1-facet-mkobj", "[alloc2][gc]")
{
ArenaConfig arena_cfg = { .name_ = "_test_unused",
.size_ = 4*1024*1024,
.store_header_flag_ = true,
.header_ = AllocHeaderConfig(0 /*guard_z*/,
0xfd /*guard_byte*/,
0 /*tseq-bits*/,
0 /*age-bits*/,
16 /*size-bits*/), };
CollectorConfig cfg = { .arena_config_ = arena_cfg,
.n_generation_ = 2,
.gc_trigger_v_ = {{64*1024, 1024*1024, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0}} };
DX1Collector gc = DX1Collector{cfg};
/* typed collector -- repr inferred at compile time */
auto x1 = with_facet<ACollector>::mkobj(&gc);
REQUIRE(x1.iface());
REQUIRE(x1.data());
}
TEST_CASE("collector-x1-alloc", "[alloc2][gc]")
{
scope log(XO_DEBUG(false), "DX1Collector alloc test");
ArenaConfig arena_cfg = { .name_ = "_test_unused",
.size_ = 4*1024*1024,
.store_header_flag_ = true,
.header_ = AllocHeaderConfig(0 /*guard_z*/,
0xfd /*guard-byte*/,
0 /*tseq-bits*/,
0 /*age-bits*/,
16 /*size-bits*/), };
/* collector with one generation collapses to a non-generational copying collector */
CollectorConfig cfg = { .arena_config_ = arena_cfg,
.n_generation_ = 1,
.gc_trigger_v_ = {{64*1024, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0}} };
DX1Collector x1state = DX1Collector{cfg};
/* typed collector */
auto x1gc = with_facet<ACollector>::mkobj(&x1state);
auto x1alloc = with_facet<AAllocator>::mkobj(&x1state);
REQUIRE(x1gc.iface());
REQUIRE(x1gc.data());
REQUIRE(x1alloc.iface());
REQUIRE(x1alloc.data());
rng::Seed<rng::xoshiro256ss> seed;
log && log(xtag("seed", seed));
auto rng = rng::xoshiro256ss(seed);
bool catch_flag = false;
REQUIRE(utest::AllocUtil::random_allocs(25, catch_flag, &rng, x1alloc));
}
TEST_CASE("collector-x1-alloc2", "[alloc2][gc]")
{
scope log(XO_DEBUG(false),
"DX1Collector alloc test2");
ArenaConfig arena_cfg = { .name_ = "_test_unused",
.size_ = 4*1024*1024,
.store_header_flag_ = true,
.header_ = AllocHeaderConfig(8 /*guard_z*/,
0xfd /*guard-byte*/,
0 /*tseq-bits*/,
0 /*age-bits*/,
16 /*size-bits*/),
};
/* collector with one generation collapses to a non-generational copying collector */
CollectorConfig cfg = { .arena_config_ = arena_cfg,
.n_generation_ = 1,
.gc_trigger_v_ = {{64*1024, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0}} };
/* X1 allocator+collector */
DX1Collector x1state = DX1Collector{cfg};
/* typed collector i/face */
auto x1gc = with_facet<ACollector>::mkobj(&x1state);
/* typed allocator i/face */
auto x1alloc = with_facet<AAllocator>::mkobj(&x1state);
REQUIRE(x1gc.iface());
REQUIRE(x1gc.data());
REQUIRE(x1alloc.iface());
REQUIRE(x1alloc.data());
rng::Seed<rng::xoshiro256ss> seed;
log && log("ratio: seed=", seed);
auto rng = rng::xoshiro256ss(seed);
REQUIRE(utest::AllocUtil::random_allocs(25, false, &rng, x1alloc));
}
}
}
/* end Collector.test.cpp */

View file

@ -1,221 +0,0 @@
/** @file DX1CollectorIterator.test.cpp
*
* @author Roland Conybeare, Dec 2025
**/
#include "Allocator.hpp"
#include "AllocIterator.hpp"
#include "gc/DX1CollectorIterator.hpp"
#include "gc/IAllocator_DX1Collector.hpp"
#include "gc/IAllocIterator_DX1CollectorIterator.hpp"
#include "arena/ArenaConfig.hpp"
#include "padding.hpp"
#include <xo/indentlog/scope.hpp>
#include <xo/indentlog/print/tag.hpp>
#include <catch2/catch.hpp>
namespace xo {
using xo::mm::AAllocator;
using xo::mm::AAllocIterator;
using xo::mm::IAllocIterator_Any;
using xo::mm::IAllocIterator_Xfer;
using xo::mm::IAllocIterator_DX1CollectorIterator;
using xo::mm::DX1Collector;
using xo::mm::DX1CollectorIterator;
using xo::mm::DArena;
using xo::mm::DArenaIterator;
using xo::mm::CollectorConfig;
using xo::mm::ArenaConfig;
using xo::mm::AllocHeaderConfig;
using xo::mm::cmpresult;
using xo::mm::padding;
using xo::facet::with_facet;
using std::byte;
namespace ut {
TEST_CASE("IAllocIterator_Xfer_DX1CollectorIterator", "[alloc2]")
{
/* verify IAllocIterator_Xfer is constructible + satisfies concept checks */
IAllocIterator_Xfer<DX1CollectorIterator, IAllocIterator_DX1CollectorIterator> xfer;
REQUIRE(IAllocIterator_Xfer<DX1CollectorIterator, IAllocIterator_DX1CollectorIterator>::_valid);
}
TEST_CASE("DX1CollectorIterator-1", "[alloc2][gc][DX1Collector]")
{
ArenaConfig arena_cfg = { .name_ = "_test_unused",
.size_ = 4*1024*1024,
.store_header_flag_ = true,
.header_ = AllocHeaderConfig(0 /*guard_z*/,
0xfd /*guard_byte*/,
0 /*tseq_bits*/,
0 /*age_bits*/,
16 /*size_bits*/), };
CollectorConfig cfg = { .arena_config_ = arena_cfg,
.n_generation_ = 2,
.gc_trigger_v_ = {{64*1024, 1024*1024, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0}} };
DX1Collector gc = DX1Collector{cfg};
auto ix = gc.begin();
auto end_ix = gc.end();
REQUIRE(ix.is_valid());
REQUIRE(end_ix.is_valid());
REQUIRE(ix == end_ix);
/* verify obj 'fat pointer' packaging */
obj<AAllocIterator,DX1CollectorIterator> ix_vt{&ix};
obj<AAllocIterator,DX1CollectorIterator> end_ix_vt{&end_ix};
REQUIRE(ix_vt.iface());
REQUIRE(ix_vt.data());
REQUIRE(end_ix_vt.iface());
REQUIRE(end_ix_vt.data());
cmpresult cmp = ix_vt.compare(end_ix_vt);
REQUIRE(cmp.is_equal());
REQUIRE(ix_vt == end_ix_vt);
}
TEST_CASE("DX1CollectorIterator-2", "[alloc2][gc][DX1Collector]")
{
scope log(XO_DEBUG(false));
ArenaConfig arena_cfg = { .name_ = "_test_unused",
.size_ = 4*1024*1024,
.store_header_flag_ = true,
.header_ = AllocHeaderConfig(0 /*guard_z*/,
0xfd /*guard_byte*/,
0 /*tseq_bits*/,
0 /*age_bits*/,
16 /*size_bits*/), };
CollectorConfig cfg = { .arena_config_ = arena_cfg,
.n_generation_ = 2,
.gc_trigger_v_ = {{64*1024, 1024*1024, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0}} };
DX1Collector gc = DX1Collector{cfg};
obj<AAllocator, DX1Collector> a1o{&gc};
REQUIRE(a1o.reserved() >= arena_cfg.size_);
REQUIRE(a1o.committed() == 0);
REQUIRE(a1o.available() == 0);
REQUIRE(a1o.allocated() == 0);
size_t req_z = 13;
byte * mem = gc.alloc(req_z);
REQUIRE(mem != nullptr);
log && log("should have iterators separated by one alloc");
{
auto ix = gc.begin();
auto end_ix = gc.end();
REQUIRE(ix.is_valid());
REQUIRE(end_ix.is_valid());
REQUIRE(ix != end_ix);
/* verify obj 'fat pointer' packaging */
auto ix_vt = with_facet<AAllocIterator>::mkobj(&ix);
auto end_ix_vt = with_facet<AAllocIterator>::mkobj(&end_ix);
REQUIRE(ix_vt.iface());
REQUIRE(ix_vt.data());
REQUIRE(end_ix_vt.iface());
REQUIRE(end_ix_vt.data());
cmpresult cmp = ix_vt.compare(end_ix_vt);
REQUIRE(cmp.is_lesser());
REQUIRE(ix_vt != end_ix_vt);
/* we only did one alloc, should be able
* to visit it
*/
auto info = ix_vt.deref();
REQUIRE(info.is_valid());
REQUIRE(info.payload().first == mem);
REQUIRE(info.size() == padding::with_padding(req_z));
ix_vt.next();
log && log(xtag("ix.gen", ix.gen_ix()),
xtag("ix.arena_ix.arena", ix.arena_ix().arena_));
log && log(xtag("end_ix.gen", end_ix.gen_ix()),
xtag("end_ix.arena_ix.arena", end_ix.arena_ix().arena_));
REQUIRE(ix_vt == end_ix_vt);
}
{
//auto range = gc.alloc_range
DArena scratch_mm
= DArena::map(
ArenaConfig{
.size_ = 4*1024,
.hugepage_z_ = 4*1024});
auto range = a1o.alloc_range(scratch_mm);
obj<AAllocIterator> ix = range.begin();
obj<AAllocIterator> end_ix = range.end();
REQUIRE(ix.iface());
REQUIRE(ix.data());
REQUIRE(end_ix.iface());
REQUIRE(end_ix.data());
REQUIRE(scratch_mm.allocated() >= 2*sizeof(DArenaIterator));
REQUIRE(scratch_mm.available() > 0);
REQUIRE(ix.compare(ix).is_equal());
REQUIRE(ix != end_ix);
{
REQUIRE(ix.deref().is_valid());
REQUIRE(ix.deref().size() == padding::with_padding(req_z));
auto [payload_lo, payload_hi] = ix.deref().payload();
REQUIRE(payload_lo == mem);
REQUIRE(payload_hi == mem + ix.deref().size());
}
{
++ix;
REQUIRE(ix == end_ix);
}
}
// repeat, this time using range iteration
{
DArena scratch_mm
= DArena::map(
ArenaConfig{
.size_ = 4*1024,
.hugepage_z_ = 4*1024});
for (const auto & info : a1o.alloc_range(scratch_mm)) {
REQUIRE(info.is_valid());
REQUIRE(info.size() == padding::with_padding(req_z));
REQUIRE(info.payload().first == mem);
REQUIRE(info.payload().second == mem + info.size());
}
}
}
} /*namespace ut*/
} /*namespace xo*/
/* end DX1CollectorIterator.test.cpp */