summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibJS
diff options
context:
space:
mode:
authorAndreas Kling <kling@serenityos.org>2021-07-21 19:57:41 +0200
committerAndreas Kling <kling@serenityos.org>2021-07-21 20:17:55 +0200
commitcdc1315dc851b2b1f820eb7fe0dc973c78c71caa (patch)
tree23c4d52c6fd260d2872c37c2762d7ddd4a1aefbc /Userland/Libraries/LibJS
parent3fe1be20b7ec04542947159f996b62c620dd5171 (diff)
downloadserenity-cdc1315dc851b2b1f820eb7fe0dc973c78c71caa.zip
LibJS: Use IntrusiveList for keeping track of WeakContainers
Diffstat (limited to 'Userland/Libraries/LibJS')
-rw-r--r--Userland/Libraries/LibJS/CMakeLists.txt1
-rw-r--r--Userland/Libraries/LibJS/Heap/Heap.cpp12
-rw-r--r--Userland/Libraries/LibJS/Heap/Heap.h3
-rw-r--r--Userland/Libraries/LibJS/Runtime/WeakContainer.cpp31
-rw-r--r--Userland/Libraries/LibJS/Runtime/WeakContainer.h26
5 files changed, 49 insertions, 24 deletions
diff --git a/Userland/Libraries/LibJS/CMakeLists.txt b/Userland/Libraries/LibJS/CMakeLists.txt
index 53c3ecd4b4..94bc6e2807 100644
--- a/Userland/Libraries/LibJS/CMakeLists.txt
+++ b/Userland/Libraries/LibJS/CMakeLists.txt
@@ -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
diff --git a/Userland/Libraries/LibJS/Heap/Heap.cpp b/Userland/Libraries/LibJS/Heap/Heap.cpp
index 0c46359a2d..a233864ef7 100644
--- a/Userland/Libraries/LibJS/Heap/Heap.cpp
+++ b/Userland/Libraries/LibJS/Heap/Heap.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>)
diff --git a/Userland/Libraries/LibJS/Heap/Heap.h b/Userland/Libraries/LibJS/Heap/Heap.h
index 4dbbc73a8c..0e3e4cbf9a 100644
--- a/Userland/Libraries/LibJS/Heap/Heap.h
+++ b/Userland/Libraries/LibJS/Heap/Heap.h
@@ -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;
diff --git a/Userland/Libraries/LibJS/Runtime/WeakContainer.cpp b/Userland/Libraries/LibJS/Runtime/WeakContainer.cpp
new file mode 100644
index 0000000000..387cbea5cc
--- /dev/null
+++ b/Userland/Libraries/LibJS/Runtime/WeakContainer.cpp
@@ -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;
+}
+
+}
diff --git a/Userland/Libraries/LibJS/Runtime/WeakContainer.h b/Userland/Libraries/LibJS/Runtime/WeakContainer.h
index fe49325fd1..4ac251279f 100644
--- a/Userland/Libraries/LibJS/Runtime/WeakContainer.h
+++ b/Userland/Libraries/LibJS/Runtime/WeakContainer.h
@@ -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>;
};
}