xo-alloc2: DX1CollectorIterator infra [WIP]

This commit is contained in:
Roland Conybeare 2025-12-17 20:16:46 -05:00
commit bf27314688
30 changed files with 1041 additions and 176 deletions

View file

@ -0,0 +1,199 @@
/** @file AllocIterator.test.cpp
*
* @author Roland Conybeare, Dec 2025
**/
#include "Allocator.hpp"
#include "AllocIterator.hpp"
#include "arena/IAllocator_DArena.hpp"
#include "arena/IAllocIterator_DArenaIterator.hpp"
#include "padding.hpp"
#include <catch2/catch.hpp>
namespace xo {
using xo::mm::AAllocator;
using xo::mm::AllocInfo;
using xo::mm::AAllocIterator;
using xo::mm::IAllocIterator_Any;
using xo::mm::IAllocIterator_Xfer;
using xo::mm::IAllocIterator_DArenaIterator;
using xo::mm::DArenaIterator;
using xo::mm::ArenaConfig;
using xo::mm::DArena;
using xo::mm::padding;
using xo::mm::error;
using std::byte;
namespace ut {
TEST_CASE("IAllocIterator_Xfer_DArenaIterator", "[alloc2]")
{
/* verify IAllocIterator_Xfer is constructible + satisfies concept checks */
IAllocIterator_Xfer<DArenaIterator, IAllocIterator_DArenaIterator> xfer;
REQUIRE(IAllocIterator_Xfer<DArenaIterator, IAllocIterator_DArenaIterator>::_valid);
}
TEST_CASE("IAllocIterator_Any", "[alloc2]")
{
/* verify IAllocIterator_Any is constructible + satisfies concept checks */
IAllocIterator_Any any;
REQUIRE(IAllocIterator_Any::_valid);
}
TEST_CASE("obj_IAllocIterator", "[alloc2]")
{
/* verify variant obj constructible */
obj<AAllocIterator> obj_any;
REQUIRE(obj_any.iface());
REQUIRE(obj_any.data() == nullptr);
}
TEST_CASE("IAllocIterator-disabled-1", "[alloc2]")
{
/* verify iteration over empty arena */
/* typed allocator a1o */
ArenaConfig cfg { .name_ = "testarena",
.size_ = 64*1024,
.debug_flag_ = false };
DArena arena = DArena::map(cfg);
obj<AAllocator, DArena> a1o{&arena};
REQUIRE(a1o.reserved() >= cfg.size_);
REQUIRE(a1o.committed() == 0);
REQUIRE(a1o.available() == 0);
REQUIRE(a1o.allocated() == 0);
DArenaIterator ix = arena.begin();
/* iteration not supported since we did not set
* ArenaConfig.store_header_flag_
*/
REQUIRE(ix.is_invalid());
REQUIRE(ix != ix);
REQUIRE(arena.error_count_ == 1);
REQUIRE(arena.last_error_.error_seq_ == 1);
REQUIRE(arena.last_error_.error_ == error::alloc_iterator_not_supported);
DArenaIterator end_ix = arena.end();
/* iteration not supported since we did not set
* ArenaConfig.store_header_flag_
*/
REQUIRE(end_ix.is_invalid());
REQUIRE(end_ix != end_ix);
REQUIRE(arena.error_count_ == 2);
REQUIRE(arena.last_error_.error_seq_ == 2);
REQUIRE(arena.last_error_.error_ == error::alloc_iterator_not_supported);
}
TEST_CASE("IAllocIterator-emptyarena", "[alloc2]")
{
/* verify iteration over empty arena */
/* typed allocator a1o */
ArenaConfig cfg { .name_ = "testarena",
.size_ = 64*1024,
.store_header_flag_ = true,
.debug_flag_ = false };
DArena arena = DArena::map(cfg);
obj<AAllocator, DArena> a1o{&arena};
REQUIRE(a1o.reserved() >= cfg.size_);
REQUIRE(a1o.committed() == 0);
REQUIRE(a1o.available() == 0);
REQUIRE(a1o.allocated() == 0);
DArenaIterator ix = arena.begin();
DArenaIterator end_ix = arena.end();
REQUIRE(ix.is_valid());
REQUIRE(end_ix.is_valid());
/* iteration not supported since we did not set */
/* arena is empty, so begin==end */
REQUIRE(ix == end_ix);
REQUIRE(arena.error_count_ == 0);
/* empty iterator cannot be dereferenced */
{
AllocInfo bad_info = *ix;
REQUIRE(!bad_info.is_valid());
REQUIRE(arena.error_count_ == 1);
REQUIRE(arena.last_error_.error_seq_ == 1);
REQUIRE(arena.last_error_.error_ == error::alloc_iterator_deref);
}
/* empty iterator cannot be advanced */
{
ix.next();
REQUIRE(arena.error_count_ == 2);
REQUIRE(arena.last_error_.error_seq_ == 2);
REQUIRE(arena.last_error_.error_ == error::alloc_iterator_next);
}
}
TEST_CASE("IAllocIterator-singlearena", "[alloc2]")
{
ArenaConfig cfg { .name_ = "testarena",
.size_ = 64*1024,
.store_header_flag_ = true,
.debug_flag_ = false };
DArena arena = DArena::map(cfg);
obj<AAllocator, DArena> a1o{&arena};
REQUIRE(arena.error_count_ == 0);
REQUIRE(a1o.reserved() >= cfg.size_);
REQUIRE(a1o.committed() == 0);
REQUIRE(a1o.available() == 0);
REQUIRE(a1o.allocated() == 0);
/* arbitrary alloc size */
size_t req_z = 13;
byte * mem = a1o.alloc(req_z);
REQUIRE(arena.error_count_ == 0);
REQUIRE(mem != nullptr);
DArenaIterator ix = arena.begin();
DArenaIterator end_ix = arena.end();
REQUIRE(ix.is_valid());
REQUIRE(end_ix.is_valid());
/* arena is non-empty, so begin!=end */
REQUIRE (ix != end_ix);
REQUIRE(arena.error_count_ == 0);
/* valid iterator can be dereferenced */
{
AllocInfo info = *ix;
REQUIRE(arena.error_count_ == 0);
REQUIRE(info.is_valid());
REQUIRE(info.size() == padding::with_padding(req_z));
auto [payload_lo, payload_hi] = info.payload();
REQUIRE(payload_lo == mem);
REQUIRE(payload_hi == mem + info.size());
}
/* valid iterator can be advanced */
{
ix.next();
REQUIRE(arena.error_count_ == 0);
REQUIRE(ix == end_ix);
}
}
} /*namespace ut*/
} /*namespace xo*/
/* end AllocIterator.test.cpp */

View file

@ -4,9 +4,11 @@
set(UTEST_EXE utest.alloc2)
set(UTEST_SRCS
alloc2_utest_main.cpp
arena.test.cpp
objectmodel.test.cpp
arena.test.cpp
AllocIterator.test.cpp
Collector.test.cpp
DX1CollectorIterator.test.cpp
random_allocs.cpp
)
@ -19,5 +21,4 @@ if (ENABLE_TESTING)
xo_external_target_dependency(${UTEST_EXE} Catch2 Catch2::Catch2)
endif()
# end CMakeLists.txt

View file

@ -0,0 +1,64 @@
/** @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 <catch2/catch.hpp>
namespace xo {
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::CollectorConfig;
using xo::mm::ArenaConfig;
using xo::mm::AllocHeaderConfig;
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", "[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);
}
} /*namespace ut*/
} /*namespace xo*/
/* end DX1CollectorIterator.test.cpp */

View file

@ -269,7 +269,11 @@ namespace xo {
REQUIRE(a1o.last_error().error_ == error::none);
REQUIRE(a1o.last_error().error_seq_ == 0);
REQUIRE(a1o.allocated() == cfg.header_.guard_z_ + sizeof(header_type) + z0 + pad + cfg.header_.guard_z_);
REQUIRE(a1o.allocated() == (cfg.header_.guard_z_
+ sizeof(header_type)
+ z0
+ pad
+ cfg.header_.guard_z_));
REQUIRE(a1o.allocated() <= a1o.committed());
REQUIRE(a1o.allocated() + a1o.available() == a1o.committed());
REQUIRE(a1o.committed() <= a1o.reserved());

View file

@ -162,6 +162,19 @@ namespace utest {
info.guard_hi().second == nullptr);
}
#ifdef NOT_YET // to verify iteration here, need iterator support in AAllocator
/* verify iteration visits all the allocs, exactly once */
{
auto alloc_map = allocs_by_lo_map;
for (AllocInfo info : mm) {
}
}
#endif
}
return true;