Truly independent web browser
LibGC: Register copies of root hash maps and tables with the heap
Shannon Boothbedf9ec3d02f
diff --git a/Libraries/LibGC/RootHashMap.h b/Libraries/LibGC/RootHashMap.hindex 3b8a93fbe80..40e92fe64a7 100644--- a/Libraries/LibGC/RootHashMap.h+++ b/Libraries/LibGC/RootHashMap.h@@ -16,6 +16,8 @@namespace GC {class GC_API RootHashMapBase {+ AK_MAKE_NONCOPYABLE(RootHashMapBase);+public:virtual void gather_roots(HashMap<Cell*, GC::HeapRoot>&) const = 0;@@ -48,6 +50,35 @@ public:~RootHashMap() = default;+ RootHashMap(RootHashMap const& other)+ : RootHashMapBase(*other.m_heap)+ , HashMapBase(static_cast<HashMapBase const&>(other))+ {+ }++ RootHashMap(RootHashMap&& other)+ : RootHashMapBase(*other.m_heap)+ , HashMapBase(move(static_cast<HashMapBase&>(other)))+ {+ }++ RootHashMap& operator=(RootHashMap const& other)+ {+ if (&other == this)+ return *this;++ assign_heap(other.m_heap);+ HashMapBase::operator=(static_cast<HashMapBase const&>(other));+ return *this;+ }++ RootHashMap& operator=(RootHashMap&& other)+ {+ assign_heap(other.m_heap);+ HashMapBase::operator=(move(static_cast<HashMapBase&>(other)));+ return *this;+ }+virtual void gather_roots(HashMap<Cell*, GC::HeapRoot>& roots) const override{static constexpr bool KeyIsGCType = Detail::RootableValueTraits<K>::is_rootable;diff --git a/Libraries/LibGC/RootHashTable.cpp b/Libraries/LibGC/RootHashTable.cppindex ccfdad893f2..6e6d6a44751 100644--- a/Libraries/LibGC/RootHashTable.cpp+++ b/Libraries/LibGC/RootHashTable.cpp@@ -26,4 +26,15 @@ RootHashTableBase::~RootHashTableBase()m_heap->did_destroy_root_hash_table({}, *this);}+void RootHashTableBase::assign_heap(Heap* heap)+{+ if (m_heap == heap)+ return;++ m_heap = heap;++ // NOTE: IntrusiveList will remove this RootHashTable from the old heap it was part of.+ m_heap->did_create_root_hash_table({}, *this);+}+}diff --git a/Libraries/LibGC/RootHashTable.h b/Libraries/LibGC/RootHashTable.hindex 9ccf6eadc15..b21c79e7eef 100644--- a/Libraries/LibGC/RootHashTable.h+++ b/Libraries/LibGC/RootHashTable.h@@ -17,6 +17,8 @@namespace GC {class GC_API RootHashTableBase {+ AK_MAKE_NONCOPYABLE(RootHashTableBase);+public:virtual void gather_roots(HashMap<Cell*, GC::HeapRoot>&) const = 0;@@ -25,6 +27,8 @@ protected:explicit RootHashTableBase(Heap&);~RootHashTableBase();+ void assign_heap(Heap*);+Heap* m_heap { nullptr };IntrusiveListNode<RootHashTableBase> m_list_node;@@ -47,6 +51,35 @@ public:~RootHashTable() = default;+ RootHashTable(RootHashTable const& other)+ : RootHashTableBase(*other.m_heap)+ , HashTableBase(static_cast<HashTableBase const&>(other))+ {+ }++ RootHashTable(RootHashTable&& other)+ : RootHashTableBase(*other.m_heap)+ , HashTableBase(move(static_cast<HashTableBase&>(other)))+ {+ }++ RootHashTable& operator=(RootHashTable const& other)+ {+ if (&other == this)+ return *this;++ assign_heap(other.m_heap);+ HashTableBase::operator=(static_cast<HashTableBase const&>(other));+ return *this;+ }++ RootHashTable& operator=(RootHashTable&& other)+ {+ assign_heap(other.m_heap);+ HashTableBase::operator=(move(static_cast<HashTableBase&>(other)));+ return *this;+ }+virtual void gather_roots(HashMap<Cell*, GC::HeapRoot>& roots) const override{static_assert(Detail::RootableValueTraits<T>::is_rootable,diff --git a/Libraries/LibGC/RootVector.h b/Libraries/LibGC/RootVector.hindex bb958a7dd90..95648c39cd6 100644--- a/Libraries/LibGC/RootVector.h+++ b/Libraries/LibGC/RootVector.h@@ -18,6 +18,8 @@namespace GC {class GC_API RootVectorBase {+ AK_MAKE_NONCOPYABLE(RootVectorBase);+public:virtual void gather_roots(HashMap<Cell*, GC::HeapRoot>&) const = 0;diff --git a/Tests/LibGC/TestGCContainers.cpp b/Tests/LibGC/TestGCContainers.cppindex 1cf3a965363..f12d265c2b1 100644--- a/Tests/LibGC/TestGCContainers.cpp+++ b/Tests/LibGC/TestGCContainers.cpp@@ -297,6 +297,58 @@ TEST_CASE(root_hash_table_reports_roots)EXPECT_EQ(roots.size(), 1u);}+template<typename Container, typename AddCell, typename Transfer>+static void expect_transferred_container_keeps_cell_alive(AddCell add_cell, Transfer transfer)+{+ auto& heap = test_heap();+ GC::Weak<TestCell> weak_cell;+ Optional<Container> destination;++ {+ Container source;+ auto cell = heap.allocate<TestCell>();+ weak_cell = cell.ptr();+ add_cell(source, cell);+ transfer(destination, source);+ }++ scrub_stack();+ heap.collect_garbage();+ EXPECT(weak_cell);+}++TEST_CASE(copied_and_moved_root_hash_maps_keep_cells_alive)+{+ using Map = GC::RootHashMap<int, GC::Ref<TestCell>>;+ auto add_cell = [](Map& map, GC::Ref<TestCell> cell) { map.set(42, cell); };+ expect_transferred_container_keeps_cell_alive<Map>(add_cell, [](Optional<Map>& destination, Map& source) { destination = source; });+ expect_transferred_container_keeps_cell_alive<Map>(add_cell, [](Optional<Map>& destination, Map& source) { destination = move(source); });+ expect_transferred_container_keeps_cell_alive<Map>(add_cell, [](Optional<Map>& destination, Map& source) {+ destination.emplace();+ *destination = source;+ });+ expect_transferred_container_keeps_cell_alive<Map>(add_cell, [](Optional<Map>& destination, Map& source) {+ destination.emplace();+ *destination = move(source);+ });+}++TEST_CASE(copied_and_moved_root_hash_tables_keep_cells_alive)+{+ using Table = GC::RootHashTable<GC::Ref<TestCell>>;+ auto add_cell = [](Table& table, GC::Ref<TestCell> cell) { table.set(cell); };+ expect_transferred_container_keeps_cell_alive<Table>(add_cell, [](Optional<Table>& destination, Table& source) { destination = source; });+ expect_transferred_container_keeps_cell_alive<Table>(add_cell, [](Optional<Table>& destination, Table& source) { destination = move(source); });+ expect_transferred_container_keeps_cell_alive<Table>(add_cell, [](Optional<Table>& destination, Table& source) {+ destination.emplace();+ *destination = source;+ });+ expect_transferred_container_keeps_cell_alive<Table>(add_cell, [](Optional<Table>& destination, Table& source) {+ destination.emplace();+ *destination = move(source);+ });+}+TEST_CASE(empty_containers_report_no_roots){GC::RootVector<GC::Ref<TestCell>> vector;