mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 19:27:45 +00:00
LibJS: Use IntrusiveList for keeping track of WeakContainers
This commit is contained in:
parent
3fe1be20b7
commit
cdc1315dc8
5 changed files with 49 additions and 24 deletions
|
@ -148,6 +148,7 @@ set(SOURCES
|
||||||
Runtime/TypedArrayPrototype.cpp
|
Runtime/TypedArrayPrototype.cpp
|
||||||
Runtime/Value.cpp
|
Runtime/Value.cpp
|
||||||
Runtime/VM.cpp
|
Runtime/VM.cpp
|
||||||
|
Runtime/WeakContainer.cpp
|
||||||
Runtime/WeakMap.cpp
|
Runtime/WeakMap.cpp
|
||||||
Runtime/WeakMapConstructor.cpp
|
Runtime/WeakMapConstructor.cpp
|
||||||
Runtime/WeakMapPrototype.cpp
|
Runtime/WeakMapPrototype.cpp
|
||||||
|
|
|
@ -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);
|
allocator_for_size(block->cell_size()).block_did_become_usable({}, *block);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (auto* weak_container : m_weak_containers)
|
for (auto& weak_container : m_weak_containers)
|
||||||
weak_container->remove_swept_cells({}, swept_cells);
|
weak_container.remove_swept_cells({}, swept_cells);
|
||||||
|
|
||||||
if constexpr (HEAP_DEBUG) {
|
if constexpr (HEAP_DEBUG) {
|
||||||
for_each_block([&](auto& block) {
|
for_each_block([&](auto& block) {
|
||||||
|
@ -282,14 +282,14 @@ void Heap::did_destroy_marked_value_list(Badge<MarkedValueList>, MarkedValueList
|
||||||
|
|
||||||
void Heap::did_create_weak_container(Badge<WeakContainer>, WeakContainer& set)
|
void Heap::did_create_weak_container(Badge<WeakContainer>, WeakContainer& set)
|
||||||
{
|
{
|
||||||
VERIFY(!m_weak_containers.contains(&set));
|
VERIFY(!m_weak_containers.contains(set));
|
||||||
m_weak_containers.set(&set);
|
m_weak_containers.append(set);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Heap::did_destroy_weak_container(Badge<WeakContainer>, WeakContainer& set)
|
void Heap::did_destroy_weak_container(Badge<WeakContainer>, WeakContainer& set)
|
||||||
{
|
{
|
||||||
VERIFY(m_weak_containers.contains(&set));
|
VERIFY(m_weak_containers.contains(set));
|
||||||
m_weak_containers.remove(&set);
|
m_weak_containers.remove(set);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Heap::defer_gc(Badge<DeferGC>)
|
void Heap::defer_gc(Badge<DeferGC>)
|
||||||
|
|
|
@ -19,6 +19,7 @@
|
||||||
#include <LibJS/Heap/CellAllocator.h>
|
#include <LibJS/Heap/CellAllocator.h>
|
||||||
#include <LibJS/Heap/Handle.h>
|
#include <LibJS/Heap/Handle.h>
|
||||||
#include <LibJS/Runtime/Object.h>
|
#include <LibJS/Runtime/Object.h>
|
||||||
|
#include <LibJS/Runtime/WeakContainer.h>
|
||||||
|
|
||||||
namespace JS {
|
namespace JS {
|
||||||
|
|
||||||
|
@ -111,7 +112,7 @@ private:
|
||||||
|
|
||||||
MarkedValueList::List m_marked_value_lists;
|
MarkedValueList::List m_marked_value_lists;
|
||||||
|
|
||||||
HashTable<WeakContainer*> m_weak_containers;
|
WeakContainer::List m_weak_containers;
|
||||||
|
|
||||||
BlockAllocator m_block_allocator;
|
BlockAllocator m_block_allocator;
|
||||||
|
|
||||||
|
|
31
Userland/Libraries/LibJS/Runtime/WeakContainer.cpp
Normal file
31
Userland/Libraries/LibJS/Runtime/WeakContainer.cpp
Normal file
|
@ -0,0 +1,31 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org>
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <LibJS/Heap/Heap.h>
|
||||||
|
#include <LibJS/Runtime/WeakContainer.h>
|
||||||
|
|
||||||
|
namespace JS {
|
||||||
|
|
||||||
|
WeakContainer::WeakContainer(Heap& heap)
|
||||||
|
: m_heap(heap)
|
||||||
|
{
|
||||||
|
m_heap.did_create_weak_container({}, *this);
|
||||||
|
}
|
||||||
|
|
||||||
|
WeakContainer::~WeakContainer()
|
||||||
|
{
|
||||||
|
deregister();
|
||||||
|
}
|
||||||
|
|
||||||
|
void WeakContainer::deregister()
|
||||||
|
{
|
||||||
|
if (!m_registered)
|
||||||
|
return;
|
||||||
|
m_heap.did_destroy_weak_container({}, *this);
|
||||||
|
m_registered = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -6,36 +6,28 @@
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <LibJS/Heap/Heap.h>
|
#include <AK/IntrusiveList.h>
|
||||||
|
|
||||||
namespace JS {
|
namespace JS {
|
||||||
|
|
||||||
class WeakContainer {
|
class WeakContainer {
|
||||||
public:
|
public:
|
||||||
explicit WeakContainer(Heap& heap)
|
explicit WeakContainer(Heap&);
|
||||||
: m_heap(heap)
|
virtual ~WeakContainer();
|
||||||
{
|
|
||||||
m_heap.did_create_weak_container({}, *this);
|
|
||||||
}
|
|
||||||
virtual ~WeakContainer()
|
|
||||||
{
|
|
||||||
deregister();
|
|
||||||
}
|
|
||||||
|
|
||||||
virtual void remove_swept_cells(Badge<Heap>, Vector<Cell*>&) = 0;
|
virtual void remove_swept_cells(Badge<Heap>, Vector<Cell*>&) = 0;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
void deregister()
|
void deregister();
|
||||||
{
|
|
||||||
if (!m_registered)
|
|
||||||
return;
|
|
||||||
m_heap.did_destroy_weak_container({}, *this);
|
|
||||||
m_registered = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool m_registered { true };
|
bool m_registered { true };
|
||||||
Heap& m_heap;
|
Heap& m_heap;
|
||||||
|
|
||||||
|
IntrusiveListNode<WeakContainer> m_list_node;
|
||||||
|
|
||||||
|
public:
|
||||||
|
using List = IntrusiveList<WeakContainer, RawPtr<WeakContainer>, &WeakContainer::m_list_node>;
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue