From fe7edbe19268837047ef9e1842f7e9593ae9dd1c Mon Sep 17 00:00:00 2001 From: Roland Conybeare Date: Tue, 2 Dec 2025 17:07:19 -0500 Subject: [PATCH] xo-alloc: UT for allocator interation + misc improvements --- include/xo/allocutil/IAlloc.hpp | 6 ++- include/xo/allocutil/ObjectVisitor.hpp | 53 ++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 2 deletions(-) create mode 100644 include/xo/allocutil/ObjectVisitor.hpp diff --git a/include/xo/allocutil/IAlloc.hpp b/include/xo/allocutil/IAlloc.hpp index a322e1d..b126d8a 100644 --- a/include/xo/allocutil/IAlloc.hpp +++ b/include/xo/allocutil/IAlloc.hpp @@ -46,9 +46,11 @@ namespace xo { public: virtual ~IAlloc() {} + /** word size for alignment **/ + static constexpr uint32_t c_alloc_alignment = sizeof(std::uintptr_t); + static inline std::uint32_t alloc_padding(std::size_t z) { - /* word size for alignment */ - constexpr uint32_t c_bpw = sizeof(std::uintptr_t); + constexpr uint32_t c_bpw = c_alloc_alignment; /* round up to multiple of c_bpw, but map 0 -> 0 * (table assuming c_bpw==8) diff --git a/include/xo/allocutil/ObjectVisitor.hpp b/include/xo/allocutil/ObjectVisitor.hpp new file mode 100644 index 0000000..1719ef7 --- /dev/null +++ b/include/xo/allocutil/ObjectVisitor.hpp @@ -0,0 +1,53 @@ +/** @file ObjectVisitor.hpp + * + **/ + +#include "IAlloc.hpp" +#include + +namespace xo { + namespace gc { + /** @class ObjectVisitor + * @brief visit IObject* members of a T instance + * + * Garbage collector relies on being able to navigate to + * an IObject-instance to find+update embedded pointers to + * other IObjects. + * + * An IObject implemnetation must override + * IObject::_forward_children(IAlloc * gc) + * and call + * gc->forward_inplace(&child_) + * for each such child pointer. + * + * For non-template classes this is straightforward + * See for example + * xo::obj::List in object/List.hpp + * + * For a template class Foo that contains T-instances, + * need to handle case where T contains IObject pointers. + * See for example the + * xo::tree::RedBlackTree in ordinaltree/RedBlackTree.hpp + * + * Use ObjectVisitor::forward_children() to pick up + * navigation code for such template arguments. + **/ + template + class ObjectVisitor { + //void forward_children(T & target, IAlloc * gc) { (void)target; (void)gc; } + }; + +#define XO_TRIVIAL_OBJECT_VISITOR(TYPE) \ + template <> \ + class ObjectVisitor { \ + public: \ + static void forward_children(TYPE &, IAlloc *) {} \ + } + + XO_TRIVIAL_OBJECT_VISITOR(int32_t); + XO_TRIVIAL_OBJECT_VISITOR(double); + + } /*namespace gc*/ +} /*namespace xo*/ + +/* ObjectVisitor.hpp */