LibGC: Treat GC::Ref and GC::Ptr as rootable values

Allow root containers to gather GC::Ref<T> and GC::Ptr<T> by treating
their stored pointer as a Cell pointer.

This avoids requiring T to be complete when rooting containers such as
RootVector<GC::Ref<T>>.
This commit is contained in:
Shannon Booth 2026-05-22 21:12:59 +02:00 committed by Shannon Booth
parent 47bd8388c5
commit aa0eb13a89

View file

@ -8,6 +8,7 @@
#include <LibGC/Cell.h>
#include <LibGC/HeapRoot.h>
#include <LibGC/Ptr.h>
namespace GC::Detail {
@ -28,6 +29,26 @@ struct RootableValueTraits {
}
};
template<typename T>
struct RootableValueTraits<Ref<T>> {
static constexpr bool is_rootable = true;
static Cell* cell(Ref<T> const& value)
{
return const_cast<Cell*>(reinterpret_cast<Cell const*>(value.ptr()));
}
};
template<typename T>
struct RootableValueTraits<Ptr<T>> {
static constexpr bool is_rootable = true;
static Cell* cell(Ptr<T> const& value)
{
return const_cast<Cell*>(reinterpret_cast<Cell const*>(value.ptr()));
}
};
template<typename T>
static void gather_root(HashMap<Cell*, GC::HeapRoot>& roots, T const& value, HeapRoot::Type root_type)
{