1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 10:48:11 +00:00

LibJS: Add DeferGC, a RAII way to prevent GC temporarily

This commit is contained in:
Andreas Kling 2020-04-19 11:30:47 +02:00
parent 15daf88e93
commit 2a9e29fbb8
4 changed files with 78 additions and 0 deletions

View file

@ -84,6 +84,10 @@ Cell* Heap::allocate_cell(size_t size)
void Heap::collect_garbage(CollectionType collection_type)
{
if (collection_type == CollectionType::CollectGarbage) {
if (m_gc_deferrals) {
m_should_gc_when_deferral_ends = true;
return;
}
HashTable<Cell*> roots;
gather_roots(roots);
mark_live_cells(roots);
@ -262,4 +266,21 @@ void Heap::did_destroy_handle(Badge<HandleImpl>, HandleImpl& impl)
m_handles.remove(&impl);
}
void Heap::defer_gc(Badge<DeferGC>)
{
++m_gc_deferrals;
}
void Heap::undefer_gc(Badge<DeferGC>)
{
ASSERT(m_gc_deferrals > 0);
--m_gc_deferrals;
if (!m_gc_deferrals) {
if (m_should_gc_when_deferral_ends)
collect_garbage();
m_should_gc_when_deferral_ends = false;
}
}
}