xo-gc stack: + gc-location-of() primitive

This commit is contained in:
Roland Conybeare 2026-03-29 19:47:15 -04:00
commit 0137fe11cc
12 changed files with 99 additions and 2 deletions

View file

@ -50,7 +50,7 @@
noexcept: true,
attributes: [],
},
// size_type reserved(Generation g, role r) const noexcept
// size_type committed(Generation g, role r) const noexcept
{
name: "committed",
doc: ["memory committed for this collector"],
@ -76,6 +76,22 @@
noexcept: true,
attributes: [],
},
// int32_t locate() const noexcept
{
name: "locate_address",
doc: [
"Location of object in collector. -1 if not in collector memory.",
"Other negative values represent collector error states (good luck!).",
"Exact meaning of non-negative values up to collector implementation"
],
return_type: "std::int32_t",
args: [
{type: "const void *", name: "addr"},
],
const: true,
noexcept: true,
attributes: [],
},
// bool contains(role r, const void * addr) const noexcept
{
name: "contains",

View file

@ -65,6 +65,10 @@ public:
virtual size_type committed(Copaque data, Generation g, role r) const noexcept = 0;
/** address space reserved for this collector **/
virtual size_type reserved(Copaque data, Generation g, role r) const noexcept = 0;
/** Location of object in collector. -1 if not in collector memory.
Other negative values represent collector error states (good luck!).
Exact meaning of non-negative values up to collector implementation **/
virtual std::int32_t locate_address(Copaque data, const void * addr) const noexcept = 0;
/** true if gc responsible for data at @p addr, and data belongs to role @p r **/
virtual bool contains(Copaque data, role r, const void * addr) const noexcept = 0;
/** true iff gc-aware object of type @p tseq is installed in this collector **/

View file

@ -62,6 +62,7 @@ namespace mm {
[[noreturn]] size_type allocated(Copaque, Generation, role) const noexcept override { _fatal(); }
[[noreturn]] size_type committed(Copaque, Generation, role) const noexcept override { _fatal(); }
[[noreturn]] size_type reserved(Copaque, Generation, role) const noexcept override { _fatal(); }
[[noreturn]] std::int32_t locate_address(Copaque, const void *) const noexcept override { _fatal(); }
[[noreturn]] bool contains(Copaque, role, const void *) const noexcept override { _fatal(); }
[[noreturn]] bool is_type_installed(Copaque, typeseq) const noexcept override { _fatal(); }
[[noreturn]] bool report_statistics(Copaque, obj<AAllocator>, obj<AAllocator>, obj<AGCObject> *) const noexcept override { _fatal(); }

View file

@ -55,6 +55,9 @@ namespace mm {
size_type reserved(Copaque data, Generation g, role r) const noexcept override {
return I::reserved(_dcast(data), g, r);
}
std::int32_t locate_address(Copaque data, const void * addr) const noexcept override {
return I::locate_address(_dcast(data), addr);
}
bool contains(Copaque data, role r, const void * addr) const noexcept override {
return I::contains(_dcast(data), r, addr);
}

View file

@ -88,6 +88,9 @@ public:
size_type reserved(Generation g, role r) const noexcept {
return O::iface()->reserved(O::data(), g, r);
}
std::int32_t locate_address(const void * addr) const noexcept {
return O::iface()->locate_address(O::data(), addr);
}
bool contains(role r, const void * addr) const noexcept {
return O::iface()->contains(O::data(), r, addr);
}

View file

@ -223,6 +223,9 @@ namespace xo {
/** memory (virtual addresses) reserved for generation @p g in role @p r **/
size_type reserved(Generation g, role r) const noexcept;
/** very similar to generation_of(), but satisfies ACollector api **/
std::int32_t locate_address(const void * addr) const noexcept;
// ----- full statistics -----
/** Report gc statistics as a dictionary.

View file

@ -53,6 +53,10 @@ namespace xo {
static size_type committed(const DX1Collector & self, Generation g, role r) noexcept;
/** address space reserved for this collector **/
static size_type reserved(const DX1Collector & self, Generation g, role r) noexcept;
/** Location of object in collector. -1 if not in collector memory.
Other negative values represent collector error states (good luck!).
Exact meaning of non-negative values up to collector implementation **/
static std::int32_t locate_address(const DX1Collector & self, const void * addr) noexcept;
/** true if gc responsible for data at @p addr, and data belongs to role @p r **/
static bool contains(const DX1Collector & self, role r, const void * addr) noexcept;
/** true iff gc-aware object of type @p tseq is installed in this collector **/

View file

@ -342,6 +342,27 @@ namespace xo {
return stat_helper(*this, &DArena::reserved, g, r);
}
std::int32_t
DX1Collector::locate_address(const void * addr) const noexcept
{
Generation g;
g = this->generation_of(role::to_space(), addr);
if (!g.is_sentinel())
return g;
g = this->generation_of(role::from_space(), addr);
if (!g.is_sentinel()) {
// use negative values for
return -1 - g;
}
return -1;
}
// editor bait: report-gc-statistics
bool
DX1Collector::report_statistics(obj<AAllocator> mm,
@ -362,6 +383,7 @@ namespace xo {
//
ok &= rpt->upsert_cstr(mm, "n-generation", DInteger::box(mm, config_.n_generation_));
ok &= rpt->upsert_cstr(mm, "n-survive-threshold", DInteger::box(mm, config_.n_survive_threshold_));
ok &= rpt->upsert_cstr(mm, "allow-incremental-gc", DBoolean::box(mm, config_.allow_incremental_gc_));
ok &= rpt->upsert_cstr(mm, "allocated", DInteger::box(mm, this->allocated()));
ok &= rpt->upsert_cstr(mm, "committed", DInteger::box(mm, this->committed()));
ok &= rpt->upsert_cstr(mm, "reserved", DInteger::box(mm, this->reserved()));

View file

@ -33,6 +33,12 @@ namespace xo {
return self.reserved(g, r);
}
auto
ICollector_DX1Collector::locate_address(const DX1Collector & self, const void * addr) noexcept -> std::int32_t
{
return self.locate_address(addr);
}
auto
ICollector_DX1Collector::contains(const DX1Collector & self, role r, const void * addr) noexcept -> bool
{

View file

@ -27,6 +27,10 @@ namespace xo {
static DPrimitive_gco_0 * make_report_gc_object_types_pm(obj<AAllocator> mm,
StringTable * stbl);
/** create primitive: report gc location of a value **/
static DPrimitive_gco_1_gco * make_gc_location_of_pm(obj<AAllocator> mm,
StringTable * stbl);
/** create primitive: request collection **/
static DPrimitive_gco_1_gco * make_request_gc_pm(obj<AAllocator> mm,
StringTable * stbl);

View file

@ -58,7 +58,6 @@ namespace xo {
obj<AGCObject> stats;
bool ok = rcx.collector().report_object_types(rcx.allocator(), rcx.error_allocator(), &stats);
if (ok && stats)
return stats;
}
@ -79,6 +78,33 @@ namespace xo {
}
// ----- gc-location-of -----
obj<AGCObject>
xfer_gc_location_of(obj<ARuntimeContext> rcx, obj<AGCObject> gco)
{
std::int32_t location_code = 0;
if (rcx.collector()) {
location_code = rcx.collector().locate_address(gco.data());
}
return DInteger::box(rcx.allocator(), location_code);
}
DPrimitive_gco_1_gco *
GcPrimitives::make_gc_location_of_pm(obj<AAllocator> mm,
StringTable * stbl)
{
(void)stbl;
auto int_ty = DAtomicType::make(mm, Metatype::t_integer());
auto any_ty = DAtomicType::make(mm, Metatype::t_any());
auto pm_ty = obj<AType,DFunctionType>(DFunctionType::_make(mm, int_ty, any_ty));
return DPrimitive_gco_1_gco::_make(mm, "gc-location-of", pm_ty, &xfer_gc_location_of);
}
// ----- request-gc -----
obj<AGCObject>

View file

@ -142,6 +142,11 @@ namespace xo {
GcPrimitives::make_report_gc_object_types_pm(mm, stbl),
flags & InstallFlags::f_generalpurpose));
ok = ok & (PrimitiveRegistry::install_aux
(sink,
GcPrimitives::make_gc_location_of_pm(mm, stbl),
flags & InstallFlags::f_generalpurpose));
ok = ok & (PrimitiveRegistry::install_aux
(sink,
GcPrimitives::make_request_gc_pm(mm, stbl),