From 3489699f5dee1f025cb6ac2f01559cac0ca75bba Mon Sep 17 00:00:00 2001 From: Roland Conybeare Date: Mon, 6 Apr 2026 15:21:48 -0400 Subject: [PATCH] refactor: use GCObjectVisitor api w/ gco_shallow_move --- idl/GCObject.json5 | 9 ++++++--- idl/GCObjectVisitor.json5 | 20 ++++++++++++++++++-- include/xo/alloc2/gc/AGCObject.hpp | 5 +++-- include/xo/alloc2/gc/IGCObject_Any.hpp | 2 +- include/xo/alloc2/gc/IGCObject_Xfer.hpp | 4 ++-- include/xo/alloc2/gc/RGCObject.hpp | 4 ++-- include/xo/alloc2/gc/RGCObjectVisitor.hpp | 20 ++++++++++++++++++-- src/alloc2/IGCObject_Any.cpp | 2 +- 8 files changed, 51 insertions(+), 15 deletions(-) diff --git a/idl/GCObject.json5 b/idl/GCObject.json5 index fc63bf5..f86e6ca 100644 --- a/idl/GCObject.json5 +++ b/idl/GCObject.json5 @@ -64,11 +64,14 @@ nonconst_methods: [ // Opaque shallow_move(obj) noexcept { - name: "shallow_move", - doc: ["move instance using collector"], + name: "gco_shallow_move", + doc: [ + "move instance using object visitor.", + "Arguably abusing the word 'visitor' here", + ], return_type: "Opaque", args:[ - {type: "obj", name: "gc"}, + {type: "obj", name: "gc"}, ], const: true, noexcept: true, diff --git a/idl/GCObjectVisitor.json5 b/idl/GCObjectVisitor.json5 index 9307219..efe14a7 100644 --- a/idl/GCObjectVisitor.json5 +++ b/idl/GCObjectVisitor.json5 @@ -88,9 +88,25 @@ }, ], router_facet_explicit_content: [ + "/** convenience: allocate copy for typed pointer **/", + "template ", + "void * alloc_copy_for(const T * src) noexcept {", + " return O::iface()->alloc_copy(O::data(), (std::byte *)const_cast(src));", + "}", "", - "/** visit forward faceted object child pointer in place.", - " Defined in RGCObject.hpp to avoid #include cycle", + "/** convenience: move typed pointer **/", + "template ", + "T * std_move_for(T * src) noexcept {", + " void * mem = this->alloc_copy_for(src);", + " if (mem) {", + " return new (mem) T(std::move(*src));", + " }", + " return nullptr;", + "}", + "", + "/** visit a gcobject child pointer in place.", + " Defined in RCollector_aux.hpp to avoid #include cycle", + " (for historical reasons - coul d be in RGCObject_aux.hpp?)", " **/", "template ", "void visit_child(xo::facet::obj * p_obj);", diff --git a/include/xo/alloc2/gc/AGCObject.hpp b/include/xo/alloc2/gc/AGCObject.hpp index 098d0c5..6ed3ee7 100644 --- a/include/xo/alloc2/gc/AGCObject.hpp +++ b/include/xo/alloc2/gc/AGCObject.hpp @@ -67,8 +67,9 @@ public: virtual void _drop(Opaque d) const noexcept = 0; // nonconst methods - /** move instance using collector **/ - virtual Opaque shallow_move(Opaque data, obj gc) const noexcept = 0; + /** move instance using object visitor. +Arguably abusing the word 'visitor' here **/ + virtual Opaque gco_shallow_move(Opaque data, obj gc) const noexcept = 0; /** Invoke fn.visit_child(iface,data) for each child GCObject pointer. Context: provides address of data pointer so it can be updated in place when @p fn invokes garbage collector reentry point **/ diff --git a/include/xo/alloc2/gc/IGCObject_Any.hpp b/include/xo/alloc2/gc/IGCObject_Any.hpp index 7f12f82..6297477 100644 --- a/include/xo/alloc2/gc/IGCObject_Any.hpp +++ b/include/xo/alloc2/gc/IGCObject_Any.hpp @@ -64,7 +64,7 @@ namespace mm { // const methods // nonconst methods - [[noreturn]] Opaque shallow_move(Opaque, obj) const noexcept override; + [[noreturn]] Opaque gco_shallow_move(Opaque, obj) const noexcept override; [[noreturn]] void visit_gco_children(Opaque, obj) const noexcept override; ///@} diff --git a/include/xo/alloc2/gc/IGCObject_Xfer.hpp b/include/xo/alloc2/gc/IGCObject_Xfer.hpp index 361bc51..633c103 100644 --- a/include/xo/alloc2/gc/IGCObject_Xfer.hpp +++ b/include/xo/alloc2/gc/IGCObject_Xfer.hpp @@ -53,8 +53,8 @@ namespace mm { // const methods // non-const methods - Opaque shallow_move(Opaque data, obj gc) const noexcept override { - return I::shallow_move(_dcast(data), gc); + Opaque gco_shallow_move(Opaque data, obj gc) const noexcept override { + return I::gco_shallow_move(_dcast(data), gc); } void visit_gco_children(Opaque data, obj fn) const noexcept override { return I::visit_gco_children(_dcast(data), fn); diff --git a/include/xo/alloc2/gc/RGCObject.hpp b/include/xo/alloc2/gc/RGCObject.hpp index 5ac4c48..1d62559 100644 --- a/include/xo/alloc2/gc/RGCObject.hpp +++ b/include/xo/alloc2/gc/RGCObject.hpp @@ -58,8 +58,8 @@ public: // const methods // non-const methods (still const in router!) - Opaque shallow_move(obj gc) noexcept { - return O::iface()->shallow_move(O::data(), gc); + Opaque gco_shallow_move(obj gc) noexcept { + return O::iface()->gco_shallow_move(O::data(), gc); } void visit_gco_children(obj fn) noexcept { return O::iface()->visit_gco_children(O::data(), fn); diff --git a/include/xo/alloc2/gc/RGCObjectVisitor.hpp b/include/xo/alloc2/gc/RGCObjectVisitor.hpp index b0f2f5b..0b120a8 100644 --- a/include/xo/alloc2/gc/RGCObjectVisitor.hpp +++ b/include/xo/alloc2/gc/RGCObjectVisitor.hpp @@ -46,9 +46,25 @@ public: ///@{ // explicit injected content + /** convenience: allocate copy for typed pointer **/ + template + void * alloc_copy_for(const T * src) noexcept { + return O::iface()->alloc_copy(O::data(), (std::byte *)const_cast(src)); + } - /** visit forward faceted object child pointer in place. - Defined in RGCObject.hpp to avoid #include cycle + /** convenience: move typed pointer **/ + template + T * std_move_for(T * src) noexcept { + void * mem = this->alloc_copy_for(src); + if (mem) { + return new (mem) T(std::move(*src)); + } + return nullptr; + } + + /** visit a gcobject child pointer in place. + Defined in RCollector_aux.hpp to avoid #include cycle + (for historical reasons - coul d be in RGCObject_aux.hpp?) **/ template void visit_child(xo::facet::obj * p_obj); diff --git a/src/alloc2/IGCObject_Any.cpp b/src/alloc2/IGCObject_Any.cpp index 7df0548..914123d 100644 --- a/src/alloc2/IGCObject_Any.cpp +++ b/src/alloc2/IGCObject_Any.cpp @@ -36,7 +36,7 @@ IGCObject_Any::_valid // nonconst methods auto -IGCObject_Any::shallow_move(Opaque, obj) const noexcept -> Opaque +IGCObject_Any::gco_shallow_move(Opaque, obj) const noexcept -> Opaque { _fatal(); }