xo-object2: utest: ++ allocation in collector utest

This commit is contained in:
Roland Conybeare 2026-01-02 18:55:53 -05:00
commit 39a16a3f15
5 changed files with 106 additions and 4 deletions

View file

@ -5,10 +5,29 @@
#pragma once
#include <xo/alloc2/Allocator.hpp>
namespace xo {
namespace scm {
using DFloat = double;
} /*nmaespace obj*/
struct DFloat {
using AAllocator = xo::mm::AAllocator;
explicit DFloat(double x) : value_{x} {}
/** allocate boxed value @p x using memory from @p mm **/
static DFloat * make(obj<AAllocator> mm,
double x);
double value() const noexcept { return value_; }
operator double() const noexcept { return value_; }
private:
/** boxed floating-oint value **/
double value_;
};
} /*nmaespace scm*/
} /*namespace xo*/
/* end DFloat.hpp */

View file

@ -3,9 +3,11 @@
* @author Roland Conybeare, Dec 2025
**/
#include "xo/gc/GCObject.hpp"
#pragma once
#include <xo/gc/GCObject.hpp>
//#include "xo/alloc2/gcobject/RGCObject.hpp"
#include "xo/facet/obj.hpp"
#include <xo/facet/obj.hpp>
namespace xo {
namespace scm {

View file

@ -8,6 +8,7 @@ set(SELF_SRCS
ISequence_Any.cpp
ISequence_DList.cpp
DList.cpp
DFloat.cpp
object2_register_types.cpp
)

24
src/object2/DFloat.cpp Normal file
View file

@ -0,0 +1,24 @@
/** @file DFloat.cpp
*
* @author Roland Conybeare, Dec 2025
**/
#include "DFloat.hpp"
namespace xo {
using xo::facet::typeseq;
namespace scm {
DFloat *
DFloat::make(obj<AAllocator> mm,
double x)
{
void * mem = mm.alloc(typeseq::id<DFloat>(),
sizeof(DFloat));
return new (mem) DFloat(x);
}
} /*namespace scm*/
} /*namespace xo*/
/* end DFloat.cpp */

View file

@ -3,17 +3,35 @@
* @author Roland Conybeare, Dec 2025
**/
#include "DFloat.hpp"
#include "DList.hpp"
#include "IGCObject_DFloat.hpp"
#include "IGCObject_DList.hpp"
#include <xo/gc/Collector.hpp>
#include <xo/gc/DX1Collector.hpp>
#include <xo/gc/detail/IAllocator_DX1Collector.hpp>
#include <xo/alloc2/AllocInfo.hpp>
#include <xo/alloc2/padding.hpp>
#include <catch2/catch.hpp>
namespace ut {
using xo::scm::DList;
using xo::scm::DFloat;
using xo::mm::AAllocator;
using xo::mm::AllocInfo;
using xo::mm::AGCObject;
using xo::mm::DX1Collector;
using xo::mm::DArena;
using xo::mm::CollectorConfig;
using xo::mm::ArenaConfig;
using xo::mm::generation;
using xo::mm::role;
using xo::mm::padding;
using xo::facet::with_facet;
using xo::facet::typeseq;
namespace {
struct testcase_x1 {
@ -61,6 +79,7 @@ namespace ut {
DX1Collector gc(cfg);
/* verify initial collector state */
{
REQUIRE(gc.name() == "x1_test");
@ -105,6 +124,43 @@ namespace ut {
}
/* attempt allocation */
auto gc_o = with_facet<AAllocator>::mkobj(&gc);
DFloat * x0 = DFloat::make(gc_o, 3.1415927);
auto x0_o = with_facet<AGCObject>::mkobj(x0);
DList * l0 = DList::list(gc_o, x0_o);
auto l0_o = with_facet<AGCObject>::mkobj(l0);
{
{
REQUIRE(x0_o.iface() != nullptr);
REQUIRE(x0_o.data() != nullptr);
REQUIRE(gc.contains(role::to_space(), x0_o.data()));
/* check alloc info for newly-allocated object */
AllocInfo info = gc.alloc_info((std::byte *)x0_o.data());
REQUIRE(info.age() == 0);
REQUIRE(info.tseq() == typeseq::id<DFloat>().seqno());
REQUIRE(info.size() >= sizeof(DFloat));
REQUIRE(info.size() < sizeof(DFloat) + padding::c_alloc_alignment);
}
{
REQUIRE(l0_o.iface() != nullptr);
REQUIRE(l0_o.data() != nullptr);
REQUIRE(gc.contains(role::to_space(), l0_o.data()));
AllocInfo info = gc.alloc_info((std::byte *)l0_o.data());
REQUIRE(info.age() == 0);
REQUIRE(info.tseq() == typeseq::id<DList>().seqno());
REQUIRE(info.size() >= sizeof(DList));
REQUIRE(info.size() < sizeof(DList) + padding::c_alloc_alignment);
}
}
} catch (std::exception & ex) {
std::cerr << "caught exception: " << ex.what() << std::endl;