LibGC: Allow move-assigning RootVector instances
Some checks are pending
CI / Lagom (arm64, Sanitizer_CI, false, macos-15, macOS, Clang) (push) Waiting to run
CI / Lagom (x86_64, Fuzzers_CI, false, ubuntu-24.04, Linux, Clang) (push) Waiting to run
CI / Lagom (x86_64, Sanitizer_CI, false, ubuntu-24.04, Linux, GNU) (push) Waiting to run
CI / Lagom (x86_64, Sanitizer_CI, true, ubuntu-24.04, Linux, Clang) (push) Waiting to run
Package the js repl as a binary artifact / build-and-package (arm64, macos-15, macOS, macOS-universal2) (push) Waiting to run
Package the js repl as a binary artifact / build-and-package (x86_64, ubuntu-24.04, Linux, Linux-x86_64) (push) Waiting to run
Run test262 and test-wasm / run_and_update_results (push) Waiting to run
Lint Code / lint (push) Waiting to run
Label PRs with merge conflicts / auto-labeler (push) Waiting to run
Push notes / build (push) Waiting to run

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 committed by Andreas Kling
commit 8ec420bc28
Notes: github-actions[bot] 2025-04-19 00:04:37 +00:00
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;
} }