Truly independent web browser
LibGC: Let root containers adopt the storage of a moved container
Shannon Boothcf82acf66fbf
diff --git a/Libraries/LibGC/ConservativeVector.h b/Libraries/LibGC/ConservativeVector.hindex 90deb9e0e3d..7b86995d608 100644--- a/Libraries/LibGC/ConservativeVector.h+++ b/Libraries/LibGC/ConservativeVector.h@@ -47,6 +47,12 @@ public:{}+ ConservativeVector(Vector<T, inline_capacity>&& other)+ : ConservativeVectorBase()+ , Vector<T, inline_capacity>(move(other))+ {+ }+~ConservativeVector() = default;ConservativeVector(ConservativeVector const& other)diff --git a/Libraries/LibGC/RootHashTable.h b/Libraries/LibGC/RootHashTable.hindex b21c79e7eef..ee4a2209b94 100644--- a/Libraries/LibGC/RootHashTable.h+++ b/Libraries/LibGC/RootHashTable.h@@ -51,6 +51,12 @@ public:~RootHashTable() = default;+ RootHashTable(HashTableBase&& other)+ : RootHashTableBase()+ , HashTableBase(move(other))+ {+ }+RootHashTable(RootHashTable const& other): RootHashTableBase(*other.m_heap), HashTableBase(static_cast<HashTableBase const&>(other))diff --git a/Libraries/LibGC/RootVector.h b/Libraries/LibGC/RootVector.hindex 95648c39cd6..f00a8afabf5 100644--- a/Libraries/LibGC/RootVector.h+++ b/Libraries/LibGC/RootVector.h@@ -58,6 +58,12 @@ public:{}+ RootVector(VectorBase&& other)+ : RootVectorBase()+ , VectorBase(move(other))+ {+ }+RootVector(RootVector const& other): RootVectorBase(*other.m_heap), Vector<T, inline_capacity>(other)diff --git a/Tests/LibGC/TestGCContainers.cpp b/Tests/LibGC/TestGCContainers.cppindex 12b77b22914..38e5b5f36b1 100644--- a/Tests/LibGC/TestGCContainers.cpp+++ b/Tests/LibGC/TestGCContainers.cpp@@ -241,6 +241,34 @@ TEST_CASE(cleared_container_reports_no_roots)EXPECT_EQ(roots.size(), 0u);}+TEST_CASE(root_vector_adopts_moved_vector)+{+ auto& heap = test_heap();+ Vector<GC::Ref<TestCell>> vector;++ auto cell = heap.allocate<TestCell>();+ vector.append(cell);+ GC::RootVector<GC::Ref<TestCell>> root_vector { move(vector) };++ HashMap<GC::Cell*, GC::HeapRoot> roots;+ root_vector.gather_roots(roots);++ EXPECT(roots.contains(cell.ptr()));+ EXPECT_EQ(roots.size(), 1u);+}++TEST_CASE(conservative_vector_adopts_moved_vector)+{+ auto& heap = test_heap();+ Vector<GC::Ref<TestCell>> vector;++ auto cell = heap.allocate<TestCell>();+ vector.append(cell);+ GC::ConservativeVector<GC::Ref<TestCell>> conservative_vector { move(vector) };++ EXPECT(possible_values_contain(conservative_vector, cell.ptr()));+}+TEST_CASE(conservative_vector_reports_possible_values){auto& heap = test_heap();@@ -337,6 +365,22 @@ static void expect_transferred_container_keeps_cell_alive(AddCell add_cell, TranEXPECT(weak_cell);}+TEST_CASE(root_hash_table_adopts_moved_hash_table)+{+ auto& heap = test_heap();+ HashTable<GC::Ref<TestCell>> table;++ auto cell = heap.allocate<TestCell>();+ table.set(cell);+ GC::RootHashTable<GC::Ref<TestCell>> root_table { move(table) };++ HashMap<GC::Cell*, GC::HeapRoot> roots;+ root_table.gather_roots(roots);++ EXPECT(roots.contains(cell.ptr()));+ EXPECT_EQ(roots.size(), 1u);+}+TEST_CASE(copied_and_moved_root_hash_maps_keep_cells_alive){using Map = GC::RootHashMap<int, GC::Ref<TestCell>>;