mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-04-22 20:45:14 +00:00
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:
parent
6dc4b23e2f
commit
b2f005125d
Notes:
sideshowbarker
2024-07-19 08:10:15 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/b2f005125de
3 changed files with 13 additions and 7 deletions
|
@ -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();
|
||||
}
|
||||
|
||||
|
|
|
@ -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; }
|
||||
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue