mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-22 07:30:19 +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
Notes:
sideshowbarker
2024-07-17 05:09:31 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/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
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <AK/Badge.h>
|
||||
#include <AK/Format.h>
|
||||
#include <AK/Forward.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.
|
||||
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;
|
||||
VM& vm() const;
|
||||
|
||||
protected:
|
||||
Cell() = default;
|
||||
|
||||
void set_overrides_must_survive_garbage_collection(bool b) { m_overrides_must_survive_garbage_collection = b; }
|
||||
|
||||
private:
|
||||
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();
|
||||
}
|
||||
|
||||
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()
|
||||
{
|
||||
for_each_block([&](auto& block) {
|
||||
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();
|
||||
});
|
||||
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_was_full = block.is_full();
|
||||
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);
|
||||
block.deallocate(cell);
|
||||
++collected_cells;
|
||||
|
|
|
@ -79,6 +79,8 @@ public:
|
|||
void uproot_cell(Cell* cell);
|
||||
|
||||
private:
|
||||
static bool cell_must_survive_garbage_collection(Cell const&);
|
||||
|
||||
Cell* allocate_cell(size_t);
|
||||
|
||||
void gather_roots(HashTable<Cell*>&);
|
||||
|
|
Loading…
Reference in a new issue