LibGC: Allow move-assigning RootVector instances

Rule of 5 - we were missing a move-assignment operator, thus all move
assignments resulted in a copy.
This commit is contained in:
Timothy Flynn 2025-04-18 10:36:08 -04:00
commit 4d01e54865
2 changed files with 22 additions and 11 deletions

View file

@ -21,16 +21,15 @@ RootVectorBase::~RootVectorBase()
m_heap->did_destroy_root_vector({}, *this); m_heap->did_destroy_root_vector({}, *this);
} }
RootVectorBase& RootVectorBase::operator=(RootVectorBase const& other) void RootVectorBase::assign_heap(Heap* heap)
{ {
if (m_heap != other.m_heap) { if (m_heap == heap)
m_heap = other.m_heap; return;
// NOTE: IntrusiveList will remove this RootVectorBase from the old heap it was part of. m_heap = heap;
m_heap->did_create_root_vector({}, *this);
}
return *this; // NOTE: IntrusiveList will remove this RootVectorBase from the old heap it was part of.
m_heap->did_create_root_vector({}, *this);
} }
} }

View file

@ -24,7 +24,7 @@ protected:
explicit RootVectorBase(Heap&); explicit RootVectorBase(Heap&);
~RootVectorBase(); ~RootVectorBase();
RootVectorBase& operator=(RootVectorBase const&); void assign_heap(Heap*);
Heap* m_heap { nullptr }; Heap* m_heap { nullptr };
IntrusiveListNode<RootVectorBase> m_list_node; IntrusiveListNode<RootVectorBase> m_list_node;
@ -38,6 +38,8 @@ class RootVector final
: public RootVectorBase : public RootVectorBase
, public Vector<T, inline_capacity> { , public Vector<T, inline_capacity> {
using VectorBase = Vector<T, inline_capacity>;
public: public:
explicit RootVector(Heap& heap) explicit RootVector(Heap& heap)
: RootVectorBase(heap) : RootVectorBase(heap)
@ -60,14 +62,24 @@ public:
RootVector(RootVector&& other) RootVector(RootVector&& other)
: RootVectorBase(*other.m_heap) : RootVectorBase(*other.m_heap)
, Vector<T, inline_capacity>(move(static_cast<Vector<T, inline_capacity>&>(other))) , VectorBase(move(static_cast<VectorBase&>(other)))
{ {
} }
RootVector& operator=(RootVector const& other) RootVector& operator=(RootVector const& other)
{ {
Vector<T, inline_capacity>::operator=(other); if (&other == this)
RootVectorBase::operator=(other); return *this;
assign_heap(other.m_heap);
VectorBase::operator=(other);
return *this;
}
RootVector& operator=(RootVector&& other)
{
assign_heap(other.m_heap);
VectorBase::operator=(move(static_cast<VectorBase&>(other)));
return *this; return *this;
} }