From 4d01e548652cad1505a397dafaa96caa757a6c41 Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Fri, 18 Apr 2025 10:36:08 -0400 Subject: [PATCH] LibGC: Allow move-assigning RootVector instances Rule of 5 - we were missing a move-assignment operator, thus all move assignments resulted in a copy. --- Libraries/LibGC/RootVector.cpp | 13 ++++++------- Libraries/LibGC/RootVector.h | 20 ++++++++++++++++---- 2 files changed, 22 insertions(+), 11 deletions(-) diff --git a/Libraries/LibGC/RootVector.cpp b/Libraries/LibGC/RootVector.cpp index 7676c170bf7..47b7a262792 100644 --- a/Libraries/LibGC/RootVector.cpp +++ b/Libraries/LibGC/RootVector.cpp @@ -21,16 +21,15 @@ RootVectorBase::~RootVectorBase() 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) { - m_heap = other.m_heap; + if (m_heap == heap) + return; - // NOTE: IntrusiveList will remove this RootVectorBase from the old heap it was part of. - m_heap->did_create_root_vector({}, *this); - } + m_heap = heap; - return *this; + // NOTE: IntrusiveList will remove this RootVectorBase from the old heap it was part of. + m_heap->did_create_root_vector({}, *this); } } diff --git a/Libraries/LibGC/RootVector.h b/Libraries/LibGC/RootVector.h index 9c7b7b3cf36..a448ea54e6b 100644 --- a/Libraries/LibGC/RootVector.h +++ b/Libraries/LibGC/RootVector.h @@ -24,7 +24,7 @@ protected: explicit RootVectorBase(Heap&); ~RootVectorBase(); - RootVectorBase& operator=(RootVectorBase const&); + void assign_heap(Heap*); Heap* m_heap { nullptr }; IntrusiveListNode m_list_node; @@ -38,6 +38,8 @@ class RootVector final : public RootVectorBase , public Vector { + using VectorBase = Vector; + public: explicit RootVector(Heap& heap) : RootVectorBase(heap) @@ -60,14 +62,24 @@ public: RootVector(RootVector&& other) : RootVectorBase(*other.m_heap) - , Vector(move(static_cast&>(other))) + , VectorBase(move(static_cast(other))) { } RootVector& operator=(RootVector const& other) { - Vector::operator=(other); - RootVectorBase::operator=(other); + if (&other == this) + 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(other))); return *this; }