mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 01:37:35 +00:00
LibJS: Add Cell::must_survive_garbage_collection() mechanism
This allows cells to prevent themselves from being garbage collected, even when there are no references to them.
This commit is contained in:
parent
8ace6b4f1d
commit
51579810bd
3 changed files with 24 additions and 4 deletions
|
@ -1,11 +1,12 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
|
* Copyright (c) 2020-2022, Andreas Kling <kling@serenityos.org>
|
||||||
*
|
*
|
||||||
* SPDX-License-Identifier: BSD-2-Clause
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <AK/Badge.h>
|
||||||
#include <AK/Format.h>
|
#include <AK/Format.h>
|
||||||
#include <AK/Forward.h>
|
#include <AK/Forward.h>
|
||||||
#include <AK/Noncopyable.h>
|
#include <AK/Noncopyable.h>
|
||||||
|
@ -80,15 +81,25 @@ public:
|
||||||
// This will be called on unmarked objects by the garbage collector in a separate pass before destruction.
|
// This will be called on unmarked objects by the garbage collector in a separate pass before destruction.
|
||||||
virtual void finalize() { }
|
virtual void finalize() { }
|
||||||
|
|
||||||
|
// This allows cells to survive GC by choice, even if nothing points to them.
|
||||||
|
// It's used to implement special rules in the web platform.
|
||||||
|
// NOTE: Cells must call set_overrides_must_survive_garbage_collection() for this to be honored.
|
||||||
|
virtual bool must_survive_garbage_collection() const { return false; }
|
||||||
|
|
||||||
|
bool overrides_must_survive_garbage_collection(Badge<Heap>) const { return m_overrides_must_survive_garbage_collection; }
|
||||||
|
|
||||||
Heap& heap() const;
|
Heap& heap() const;
|
||||||
VM& vm() const;
|
VM& vm() const;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
Cell() = default;
|
Cell() = default;
|
||||||
|
|
||||||
|
void set_overrides_must_survive_garbage_collection(bool b) { m_overrides_must_survive_garbage_collection = b; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool m_mark : 1 { false };
|
bool m_mark : 1 { false };
|
||||||
State m_state : 7 { State::Live };
|
bool m_overrides_must_survive_garbage_collection { false };
|
||||||
|
State m_state : 1 { State::Live };
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -232,11 +232,18 @@ void Heap::mark_live_cells(HashTable<Cell*> const& roots)
|
||||||
m_uprooted_cells.clear();
|
m_uprooted_cells.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool Heap::cell_must_survive_garbage_collection(Cell const& cell)
|
||||||
|
{
|
||||||
|
if (!cell.overrides_must_survive_garbage_collection({}))
|
||||||
|
return false;
|
||||||
|
return cell.must_survive_garbage_collection();
|
||||||
|
}
|
||||||
|
|
||||||
void Heap::finalize_unmarked_cells()
|
void Heap::finalize_unmarked_cells()
|
||||||
{
|
{
|
||||||
for_each_block([&](auto& block) {
|
for_each_block([&](auto& block) {
|
||||||
block.template for_each_cell_in_state<Cell::State::Live>([](Cell* cell) {
|
block.template for_each_cell_in_state<Cell::State::Live>([](Cell* cell) {
|
||||||
if (!cell->is_marked())
|
if (!cell->is_marked() && !cell_must_survive_garbage_collection(*cell))
|
||||||
cell->finalize();
|
cell->finalize();
|
||||||
});
|
});
|
||||||
return IterationDecision::Continue;
|
return IterationDecision::Continue;
|
||||||
|
@ -258,7 +265,7 @@ void Heap::sweep_dead_cells(bool print_report, Core::ElapsedTimer const& measure
|
||||||
bool block_has_live_cells = false;
|
bool block_has_live_cells = false;
|
||||||
bool block_was_full = block.is_full();
|
bool block_was_full = block.is_full();
|
||||||
block.template for_each_cell_in_state<Cell::State::Live>([&](Cell* cell) {
|
block.template for_each_cell_in_state<Cell::State::Live>([&](Cell* cell) {
|
||||||
if (!cell->is_marked()) {
|
if (!cell->is_marked() && !cell_must_survive_garbage_collection(*cell)) {
|
||||||
dbgln_if(HEAP_DEBUG, " ~ {}", cell);
|
dbgln_if(HEAP_DEBUG, " ~ {}", cell);
|
||||||
block.deallocate(cell);
|
block.deallocate(cell);
|
||||||
++collected_cells;
|
++collected_cells;
|
||||||
|
|
|
@ -79,6 +79,8 @@ public:
|
||||||
void uproot_cell(Cell* cell);
|
void uproot_cell(Cell* cell);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
static bool cell_must_survive_garbage_collection(Cell const&);
|
||||||
|
|
||||||
Cell* allocate_cell(size_t);
|
Cell* allocate_cell(size_t);
|
||||||
|
|
||||||
void gather_roots(HashTable<Cell*>&);
|
void gather_roots(HashTable<Cell*>&);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue