LibJS: Always collect all garbage when destroying Heap

When the Heap is going down, it's our last chance to run destructors,
so add a separate collector mode where we simply skip over the marking
phase and go directly to sweeping. This causes everything to get swept
and all live cells get destroyed.

This way, valgrind reports 0 leaks on exit. :^)
This commit is contained in:
Andreas Kling 2020-03-23 14:11:19 +01:00
parent 6dc4b23e2f
commit b2f005125d
Notes: sideshowbarker 2024-07-19 08:10:15 +09:00
3 changed files with 13 additions and 7 deletions

View file

@ -51,6 +51,7 @@ Heap::Heap(Interpreter& interpreter)
Heap::~Heap()
{
collect_garbage(CollectionType::CollectEverything);
}
Cell* Heap::allocate_cell(size_t size)
@ -72,11 +73,13 @@ Cell* Heap::allocate_cell(size_t size)
return cell;
}
void Heap::collect_garbage()
void Heap::collect_garbage(CollectionType collection_type)
{
HashTable<Cell*> roots;
gather_roots(roots);
mark_live_cells(roots);
if (collection_type == CollectionType::CollectGarbage) {
HashTable<Cell*> roots;
gather_roots(roots);
mark_live_cells(roots);
}
sweep_dead_cells();
}

View file

@ -53,7 +53,12 @@ public:
return static_cast<T*>(memory);
}
void collect_garbage();
enum class CollectionType {
CollectGarbage,
CollectEverything,
};
void collect_garbage(CollectionType = CollectionType::CollectGarbage);
Interpreter& interpreter() { return m_interpreter; }

View file

@ -85,7 +85,5 @@ int main(int argc, char** argv)
if (print_last_result)
printf("%s\n", result.to_string().characters());
dbg() << "Collecting garbage on exit...";
interpreter.heap().collect_garbage();
return 0;
}