mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 19:47:34 +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/Value.cpp
|
||||
Runtime/VM.cpp
|
||||
Runtime/WeakContainer.cpp
|
||||
Runtime/WeakMap.cpp
|
||||
Runtime/WeakMapConstructor.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);
|
||||
}
|
||||
|
||||
for (auto* weak_container : m_weak_containers)
|
||||
weak_container->remove_swept_cells({}, swept_cells);
|
||||
for (auto& weak_container : m_weak_containers)
|
||||
weak_container.remove_swept_cells({}, swept_cells);
|
||||
|
||||
if constexpr (HEAP_DEBUG) {
|
||||
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)
|
||||
{
|
||||
VERIFY(!m_weak_containers.contains(&set));
|
||||
m_weak_containers.set(&set);
|
||||
VERIFY(!m_weak_containers.contains(set));
|
||||
m_weak_containers.append(set);
|
||||
}
|
||||
|
||||
void Heap::did_destroy_weak_container(Badge<WeakContainer>, WeakContainer& set)
|
||||
{
|
||||
VERIFY(m_weak_containers.contains(&set));
|
||||
m_weak_containers.remove(&set);
|
||||
VERIFY(m_weak_containers.contains(set));
|
||||
m_weak_containers.remove(set);
|
||||
}
|
||||
|
||||
void Heap::defer_gc(Badge<DeferGC>)
|
||||
|
|
|
@ -19,6 +19,7 @@
|
|||
#include <LibJS/Heap/CellAllocator.h>
|
||||
#include <LibJS/Heap/Handle.h>
|
||||
#include <LibJS/Runtime/Object.h>
|
||||
#include <LibJS/Runtime/WeakContainer.h>
|
||||
|
||||
namespace JS {
|
||||
|
||||
|
@ -111,7 +112,7 @@ private:
|
|||
|
||||
MarkedValueList::List m_marked_value_lists;
|
||||
|
||||
HashTable<WeakContainer*> m_weak_containers;
|
||||
WeakContainer::List m_weak_containers;
|
||||
|
||||
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
|
||||
|
||||
#include <LibJS/Heap/Heap.h>
|
||||
#include <AK/IntrusiveList.h>
|
||||
|
||||
namespace JS {
|
||||
|
||||
class WeakContainer {
|
||||
public:
|
||||
explicit WeakContainer(Heap& heap)
|
||||
: m_heap(heap)
|
||||
{
|
||||
m_heap.did_create_weak_container({}, *this);
|
||||
}
|
||||
virtual ~WeakContainer()
|
||||
{
|
||||
deregister();
|
||||
}
|
||||
explicit WeakContainer(Heap&);
|
||||
virtual ~WeakContainer();
|
||||
|
||||
virtual void remove_swept_cells(Badge<Heap>, Vector<Cell*>&) = 0;
|
||||
|
||||
protected:
|
||||
void deregister()
|
||||
{
|
||||
if (!m_registered)
|
||||
return;
|
||||
m_heap.did_destroy_weak_container({}, *this);
|
||||
m_registered = false;
|
||||
}
|
||||
void deregister();
|
||||
|
||||
private:
|
||||
bool m_registered { true };
|
||||
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