From ee982276c9d173ef899fd264cea0b1f29061bfc2 Mon Sep 17 00:00:00 2001 From: Roland Conybeare Date: Thu, 5 Mar 2026 00:50:58 +1100 Subject: [PATCH] refactor: + xo-stringtable2 w/ DString impl --- include/xo/alloc2/CollectorTypeRegistry.hpp | 74 +++++++++++++++++++++ src/alloc2/CMakeLists.txt | 5 ++ src/alloc2/CollectorTypeRegistry.cpp | 48 +++++++++++++ src/alloc2/ICollector_Any.cpp | 40 +++++++++++ src/alloc2/IGCObject_Any.cpp | 48 +++++++++++++ 5 files changed, 215 insertions(+) create mode 100644 include/xo/alloc2/CollectorTypeRegistry.hpp create mode 100644 src/alloc2/CollectorTypeRegistry.cpp create mode 100644 src/alloc2/ICollector_Any.cpp create mode 100644 src/alloc2/IGCObject_Any.cpp diff --git a/include/xo/alloc2/CollectorTypeRegistry.hpp b/include/xo/alloc2/CollectorTypeRegistry.hpp new file mode 100644 index 0000000..4474e20 --- /dev/null +++ b/include/xo/alloc2/CollectorTypeRegistry.hpp @@ -0,0 +1,74 @@ +/** @file CollectorTypeRegistry.hpp + * + * @brief Runtime type registration for gc-aware types + * + * @author Roland Conybeare, Jan 2026 + **/ + +#pragma once + +#include "Collector.hpp" +#include + +namespace xo { + namespace mm { + /** @class CollectorTypeRegistry + * + * @brief Runtime registry for gc-aware types + * + * Singleton to remember known gc-aware types; + * use to simplify registering such types + * with a collector instance. + * + * Remark: splitting work here between + * 1. static initializer work: tracking gc-aware types, + * 2. runtime post-configuration work: report + * gc-aware types to GC instances + * + * Use: + * 1. subsystem foo provides function foo_register_types(obj gc) + * Function calls + * gc.install_type(impl_for()) + * for each gc-aware type provided by subsystem foo + * + * Example: in file xo-object2/src/object2/object2_register_types.cpp, see + * object2_register_types() + * + * 2. during subsystem init, call + * CollectorTypeRegistry::instance().register_types(&foo_register_types); + * + * Example: in file xo-object2/src/object2/init_object2.cpp, see + * InitSubsys::init() + * + * 3. during Collector setup, call + * obj gc = ...; + * CollectorTypeRegistry::instance().install_types(gc); + * + * Example: in file xo-object2/utest/X1Collector.test.cpp + * TEST_CASE("x1") + **/ + class CollectorTypeRegistry { + public: + using init_function_type = std::function)>; + + public: + /** singleton instance **/ + static CollectorTypeRegistry & instance(); + + /** remember a gc-aware type-registration function **/ + void register_types(init_function_type init_fn); + + /** register known GC-aware types with @p gc. + * Calls @c gc.isntall_type() for each + * such type. + **/ + bool install_types(obj gc); + + private: + /** initialization steps for a new Collector instance **/ + std::vector init_seq_v_; + }; + } +} + +/* end CollectorTypeRegistry.hpp */ diff --git a/src/alloc2/CMakeLists.txt b/src/alloc2/CMakeLists.txt index 29890c5..00230b9 100644 --- a/src/alloc2/CMakeLists.txt +++ b/src/alloc2/CMakeLists.txt @@ -6,6 +6,11 @@ set(SELF_SRCS init_alloc2.cpp alloc2_register_facets.cpp + CollectorTypeRegistry.cpp + + ICollector_Any.cpp + IGCObject_Any.cpp + AAllocator.cpp IAllocator_Any.cpp IAllocator_DArena.cpp diff --git a/src/alloc2/CollectorTypeRegistry.cpp b/src/alloc2/CollectorTypeRegistry.cpp new file mode 100644 index 0000000..98d45a8 --- /dev/null +++ b/src/alloc2/CollectorTypeRegistry.cpp @@ -0,0 +1,48 @@ +/** @file CollectorTypeRegistry.cpp + **/ + +#include "CollectorTypeRegistry.hpp" +#include + +namespace xo { + namespace mm { + CollectorTypeRegistry & + CollectorTypeRegistry::instance() + { + static CollectorTypeRegistry s_instance; + + return s_instance; + } + + void + CollectorTypeRegistry::register_types(init_function_type fn) + { + scope log(XO_DEBUG(true)); + + init_seq_v_.push_back(fn); + } + + bool + CollectorTypeRegistry::install_types(obj gc) + { + scope log(XO_DEBUG(true)); + + bool ok = true; + + size_t i = 0; + size_t n = init_seq_v_.size(); + log && log("run n init steps", xtag("n", n)); + + for (const auto & fn : init_seq_v_) { + log && log("do install fn (", i+1, "/", n, ")"); + + ok = ok & fn(gc); + } + + return ok; + } + + } /*namespace mm*/ +} /*namespace xo*/ + +/* end CollectorTypeRegistry.cpp */ diff --git a/src/alloc2/ICollector_Any.cpp b/src/alloc2/ICollector_Any.cpp new file mode 100644 index 0000000..b39aa99 --- /dev/null +++ b/src/alloc2/ICollector_Any.cpp @@ -0,0 +1,40 @@ +/** @file ICollector_Any.cpp + * + * @author Roland Conybeare, Dec 2025 + **/ + +#include "gc/ICollector_Any.hpp" +#include +#include + +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 + */ + + std::cerr << "fatal" + << ": attempt to call uninitialized" + << " ICollector_Any method" + << std::endl; + std::terminate(); + } + + typeseq + ICollector_Any::s_typeseq = typeseq::id(); + + bool + ICollector_Any::_valid = valid_facet_implementation(); + + } /*namespace mm*/ +} /*namespace xo*/ + +/** end ICollector_Any.cpp */ diff --git a/src/alloc2/IGCObject_Any.cpp b/src/alloc2/IGCObject_Any.cpp new file mode 100644 index 0000000..10ff8f6 --- /dev/null +++ b/src/alloc2/IGCObject_Any.cpp @@ -0,0 +1,48 @@ +/** @file IGCObject_Any.cpp + * + **/ + +#include "gc/IGCObject_Any.hpp" +#include +#include + +namespace xo { +namespace mm { + +using xo::facet::DVariantPlaceholder; +using xo::facet::typeseq; +using xo::facet::valid_facet_implementation; + +void +IGCObject_Any::_fatal() +{ + /* control here on uninitialized IAllocator_Any. + * Initialized instance will have specific implementation type + */ + std::cerr << "fatal" + << ": attempt to call uninitialized" + << " IGCObject_Any method" + << std::endl; + std::terminate(); +} + +typeseq +IGCObject_Any::s_typeseq = typeseq::id(); + +bool +IGCObject_Any::_valid + = valid_facet_implementation(); + +// nonconst methods + +auto +IGCObject_Any::forward_children(Opaque, obj) const noexcept -> size_type +{ + _fatal(); +} + + +} /*namespace mm*/ +} /*namespace xo*/ + +/* end IGCObject_Any.cpp */