LibJS: Generify the garbage collector's weak container notifications

This will allow us to use the same interface for other JS weak
containers like the WeakMap & WeakRef.
This commit is contained in:
Idan Horowitz 2021-06-12 05:23:33 +03:00 committed by Linus Groh
parent 06fdc26656
commit 1a8ee5d8d7
6 changed files with 51 additions and 17 deletions

View file

@ -141,6 +141,7 @@ class Symbol;
class Token;
class VM;
class Value;
class WeakContainer;
enum class DeclarationKind;
struct AlreadyResolved;
struct JobCallback;

View file

@ -16,7 +16,7 @@
#include <LibJS/Heap/HeapBlock.h>
#include <LibJS/Interpreter.h>
#include <LibJS/Runtime/Object.h>
#include <LibJS/Runtime/WeakSet.h>
#include <LibJS/Runtime/WeakContainer.h>
#include <setjmp.h>
namespace JS {
@ -190,7 +190,7 @@ void Heap::sweep_dead_cells(bool print_report, const Core::ElapsedTimer& measure
size_t collected_cell_bytes = 0;
size_t live_cell_bytes = 0;
auto should_store_sweeped_cells = !m_weak_sets.is_empty();
auto should_store_sweeped_cells = !m_weak_containers.is_empty();
for_each_block([&](auto& block) {
bool block_has_live_cells = false;
bool block_was_full = block.is_full();
@ -226,8 +226,8 @@ void Heap::sweep_dead_cells(bool print_report, const Core::ElapsedTimer& measure
allocator_for_size(block->cell_size()).block_did_become_usable({}, *block);
}
for (auto* weak_set : m_weak_sets)
weak_set->remove_sweeped_cells({}, sweeped_cells);
for (auto* weak_container : m_weak_containers)
weak_container->remove_sweeped_cells({}, sweeped_cells);
if constexpr (HEAP_DEBUG) {
for_each_block([&](auto& block) {
@ -280,16 +280,16 @@ void Heap::did_destroy_marked_value_list(Badge<MarkedValueList>, MarkedValueList
m_marked_value_lists.remove(&list);
}
void Heap::did_create_weak_set(Badge<WeakSet>, WeakSet& set)
void Heap::did_create_weak_container(Badge<WeakContainer>, WeakContainer& set)
{
VERIFY(!m_weak_sets.contains(&set));
m_weak_sets.set(&set);
VERIFY(!m_weak_containers.contains(&set));
m_weak_containers.set(&set);
}
void Heap::did_destroy_weak_set(Badge<WeakSet>, WeakSet& set)
void Heap::did_destroy_weak_container(Badge<WeakContainer>, WeakContainer& set)
{
VERIFY(m_weak_sets.contains(&set));
m_weak_sets.remove(&set);
VERIFY(m_weak_containers.contains(&set));
m_weak_containers.remove(&set);
}
void Heap::defer_gc(Badge<DeferGC>)

View file

@ -70,8 +70,8 @@ public:
void did_create_marked_value_list(Badge<MarkedValueList>, MarkedValueList&);
void did_destroy_marked_value_list(Badge<MarkedValueList>, MarkedValueList&);
void did_create_weak_set(Badge<WeakSet>, WeakSet&);
void did_destroy_weak_set(Badge<WeakSet>, WeakSet&);
void did_create_weak_container(Badge<WeakContainer>, WeakContainer&);
void did_destroy_weak_container(Badge<WeakContainer>, WeakContainer&);
void defer_gc(Badge<DeferGC>);
void undefer_gc(Badge<DeferGC>);
@ -109,7 +109,7 @@ private:
HashTable<MarkedValueList*> m_marked_value_lists;
HashTable<WeakSet*> m_weak_sets;
HashTable<WeakContainer*> m_weak_containers;
BlockAllocator m_block_allocator;

View file

@ -0,0 +1,31 @@
/*
* Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <LibJS/Heap/Heap.h>
namespace JS {
class WeakContainer {
public:
explicit WeakContainer(Heap& heap)
: m_heap(heap)
{
m_heap.did_create_weak_container({}, *this);
}
virtual ~WeakContainer()
{
m_heap.did_destroy_weak_container({}, *this);
}
virtual void remove_sweeped_cells(Badge<Heap>, Vector<Cell*>&) = 0;
private:
Heap& m_heap;
};
}

View file

@ -15,13 +15,12 @@ WeakSet* WeakSet::create(GlobalObject& global_object)
WeakSet::WeakSet(Object& prototype)
: Object(prototype)
, WeakContainer(heap())
{
heap().did_create_weak_set({}, *this);
}
WeakSet::~WeakSet()
{
heap().did_destroy_weak_set({}, *this);
}
void WeakSet::remove_sweeped_cells(Badge<Heap>, Vector<Cell*>& cells)

View file

@ -9,10 +9,13 @@
#include <AK/HashTable.h>
#include <LibJS/Runtime/GlobalObject.h>
#include <LibJS/Runtime/Object.h>
#include <LibJS/Runtime/WeakContainer.h>
namespace JS {
class WeakSet : public Object {
class WeakSet final
: public Object
, public WeakContainer {
JS_OBJECT(WeakSet, Object);
public:
@ -24,7 +27,7 @@ public:
HashTable<Cell*> const& values() const { return m_values; };
HashTable<Cell*>& values() { return m_values; };
void remove_sweeped_cells(Badge<Heap>, Vector<Cell*>&);
virtual void remove_sweeped_cells(Badge<Heap>, Vector<Cell*>&) override;
private:
HashTable<Cell*> m_values; // This stores Cell pointers instead of Object pointers to aide with sweeping