xo-alloc + xo-allocutil: refactor to shrink dep surface area

This commit is contained in:
Roland Conybeare 2025-12-01 01:20:49 -05:00
commit 540b43d971
34 changed files with 479 additions and 323 deletions

View file

@ -7,6 +7,8 @@
#include "xo/reflect/TaggedPtr.hpp"
#include "IAlloc.hpp"
#include "xo/allocutil/IObject.hpp"
#include "xo/allocutil/gc_ptr.hpp"
#include <concepts>
#include <cstdint>
@ -16,71 +18,6 @@ namespace xo {
class ObjectStatistics;
};
template <typename T>
class gc_ptr;
template <typename T>
using gp = gc_ptr<T>;
/** wrapper for a pointer to garbage-collector-eligible T.
* Application code will usually use the alias template gp<T>
**/
template <typename T>
class gc_ptr {
public:
using element_type = T;
public:
gc_ptr() = default;
gc_ptr(T * p) : ptr_{p} {}
gc_ptr(const gc_ptr & x) : ptr_{x.ptr_} {}
/** create from gc_ptr to some related type @tparam S **/
template <typename S>
gc_ptr(const gc_ptr<S> & x) : ptr_{x.ptr()} {}
/** runtime downcast. shorthand for dynamic_cast<T*> **/
template <typename S>
static gc_ptr<T> from(const gc_ptr<S> & x) { return gc_ptr<T>{dynamic_cast<T*>(x.ptr())}; }
/** convenience for static asserts **/
static constexpr bool is_gc_ptr = true;
/** see also: xo/refcnt/Refcounted.hpp **/
static constexpr bool is_rc_ptr = false;
static bool is_eq(gc_ptr x1, gc_ptr x2) {
std::uintptr_t u1 = reinterpret_cast<std::uintptr_t>(x1.ptr());
std::uintptr_t u2 = reinterpret_cast<std::uintptr_t>(x2.ptr());
// multiple inheritance shenanigans.
// (allow interface pointers separated by one pointer)
if (u1 >= u2)
return (u1 <= u2 + sizeof(std::uintptr_t));
else
return (u2 <= u1 + sizeof(std::uintptr_t));
}
/** (for consistency's sake) **/
T * get() const { return ptr_; }
T * ptr() const { return ptr_; }
T ** ptr_address() { return &ptr_; }
bool is_null() const { return ptr_ == nullptr; }
void make_null() { ptr_ = nullptr; }
void assign_ptr(T * x) { ptr_ = x; }
gc_ptr & operator=(const gc_ptr & x) { ptr_ = x.ptr(); return *this; }
T * operator->() const { return ptr_; }
T & operator*() const { return *ptr_; }
private:
T * ptr_ = nullptr;
};
/** Root class for all xo GC-collectable objects.
*
* Design note:
@ -92,11 +29,15 @@ namespace xo {
* Would be feasible to relax the must-inherit-from-Object constraint
* by having GC use its own wrapper, at cost of an extra layer of indirection
**/
class Object {
class Object : public IObject {
public:
using TaggedPtr = xo::reflect::TaggedPtr;
public:
static gp<Object> from(gp<IObject> x) {
return dynamic_cast<Object*>(x.ptr());
}
virtual ~Object() = default;
/** memory allocator for objects. Likely this will be a GC instance,
@ -111,7 +52,7 @@ namespace xo {
* add mutation log entry
**/
template <typename T>
static void assign_member(gp<Object> parent, gp<T> * lhs, gp<Object> rhs);
static void assign_member(gp<IObject> parent, gp<T> * lhs, gp<IObject> rhs);
/** use from GC aux functions **/
static gc::GC * _gc() { return reinterpret_cast<gc::GC*>(mm); }
@ -125,11 +66,11 @@ namespace xo {
* @p src. source object to be forwarded
* @p gc. garbage collector
*/
static Object * _forward(Object * src, gc::IAlloc * gc);
static IObject * _forward(IObject * src, gc::IAlloc * gc);
template <typename T>
static void _forward_inplace(T ** src_addr, gc::IAlloc * gc) {
Object * fwd = _forward(*src_addr, gc);
IObject * fwd = _forward(*src_addr, gc);
*src_addr = reinterpret_cast<T *>(fwd);
}
@ -160,12 +101,12 @@ namespace xo {
* @param gc garbage collector
* @param stats per-object-type GC statistics
**/
static Object * _deep_move(Object * src, gc::GC * gc, gc::ObjectStatistics * stats);
static IObject * _deep_move(IObject * src, gc::GC * gc, gc::ObjectStatistics * stats);
/** copy @p src to to-space. Overwrite original with forwarding pointer to new location.
* return the new location
**/
static Object * _shallow_move(Object * src, gc::IAlloc * gc);
static IObject * _shallow_move(IObject * src, gc::IAlloc * gc);
// Reflection support
@ -176,89 +117,25 @@ namespace xo {
/** print on stream @p os **/
virtual void display(std::ostream & os) const = 0;
// GC support
// Inherited from IObject..
/** true iff this object represents a forwarding pointer.
* Forwarding pointers are exclusively created by the garbage collector;
* forwarding pointers (and only forwarding pointers) return true here.
**/
virtual bool _is_forwarded() const { return false; }
//virtual bool _is_forwarded() const override { return false; }
//virtual IObject * _offset_destination(IObject * src) const override { return src; };
virtual void _forward_to(IObject * dest) override;
//virtual IObject * _destination() override { return nullptr; }
/** offset for uncommon situation where pointer address is offset from object
* base address
**/
virtual Object * _offset_destination(Object * src) const { return src; };
/** replace this object with a forwarding pointer referring to @p dest.
**/
virtual void _forward_to(Object * dest);
/** if this object represents a forwarding pointer, return its new location.
* forwarding pointers belong to the garbage collector implementation.
* (if you have to ask -- no, your class is not a forwarding pointer)
* all other objects return nullptr here.
**/
virtual Object * _destination() { return nullptr; }
/** return amount of storage (including padding) consumed by this object,
* excluding immediate Object-pointer children
**/
virtual std::size_t _shallow_size() const = 0;
/** if subject is allocated by GC:
* - create copy C in to-space
* - destination C will be nursery|tenured depending on location of this.
* else
* - return this to disengage from GC
*
* Require: @ref mm is an instance of @ref gc::GC
**/
virtual Object * _shallow_copy(gc::IAlloc * gc) const = 0;
/** update child pointers that refer to forwarding pointers,
* replacing them with the correct destination.
* See @ref Object::deep_move
*
* this gray object, located in to-space.
* fwd1 forwarding objects.
* Located in from-space. Invalid at end of GC cycle.
* p1,p2 source pointers.
* D1,D2 already-forwarded objects. located in to-space.
*
* before:
* this fwd1
* +----+ +-+
* | p1 ----->|x|-------> D1
* | | +-+
* | |
* | p2 ----------------> D2
* +----+
*
* after:
* this
* +----+
* | p1 ----------------> D1
* | |
* | |
* | p2 ----------------> D2
* +----+
*
* this is now white
*
* @return shallow size of *this. Must exactly match the amount of memory in to-space
* allocated by @ref _shallow_move
*
**/
virtual std::size_t _forward_children(gc::IAlloc * gc) = 0;
virtual std::size_t _shallow_size() const override = 0;
virtual IObject * _shallow_copy(gc::IAlloc * gc) const override = 0;
virtual std::size_t _forward_children(gc::IAlloc * gc) override = 0;
};
template <typename T>
void
Object::assign_member(gp<Object> parent, gp<T> * lhs, gp<Object> rhs)
Object::assign_member(gp<IObject> parent, gp<T> * lhs, gp<IObject> rhs)
{
Object::mm->assign_member(parent.ptr(),
reinterpret_cast<Object **>(lhs->ptr_address()),
rhs.ptr());
Object::mm->assign_member(reinterpret_cast<IObject *>(parent.ptr()),
reinterpret_cast<IObject **>(lhs->ptr_address()),
reinterpret_cast<IObject *>(rhs.ptr()));
}
std::ostream &