LibJS: Add MarkedVector<T>
This abstracts a vector of Cell* with a strongly typed span() accessor that gives you Span<T*> instead of Span<Cell*>. It is intended to replace MarkedValueList in situations where you only need to store pointers to Cell (or an even more specific type of Cell). The API can definitely be improved, it's just the bare basics for now.
This commit is contained in:
parent
c26b58bc53
commit
8bb9fe63b7
Notes:
sideshowbarker
2024-07-17 22:42:07 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/8bb9fe63b7e
6 changed files with 126 additions and 0 deletions
|
@ -20,6 +20,7 @@ set(SOURCES
|
|||
Heap/Handle.cpp
|
||||
Heap/Heap.cpp
|
||||
Heap/HeapBlock.cpp
|
||||
Heap/MarkedVector.cpp
|
||||
Interpreter.cpp
|
||||
Lexer.cpp
|
||||
MarkupGenerator.cpp
|
||||
|
|
|
@ -228,6 +228,9 @@ class ThrowCompletionOr;
|
|||
template<class T>
|
||||
class Handle;
|
||||
|
||||
template<class T>
|
||||
class MarkedVector;
|
||||
|
||||
namespace Bytecode {
|
||||
class BasicBlock;
|
||||
struct Executable;
|
||||
|
|
|
@ -119,6 +119,11 @@ void Heap::gather_roots(HashTable<Cell*>& roots)
|
|||
}
|
||||
}
|
||||
|
||||
for (auto& vector : m_marked_vectors) {
|
||||
for (auto* cell : vector.cells())
|
||||
roots.set(cell);
|
||||
}
|
||||
|
||||
if constexpr (HEAP_DEBUG) {
|
||||
dbgln("gather_roots:");
|
||||
for (auto* root : roots)
|
||||
|
@ -318,6 +323,18 @@ void Heap::did_destroy_marked_value_list(Badge<MarkedValueList>, MarkedValueList
|
|||
m_marked_value_lists.remove(list);
|
||||
}
|
||||
|
||||
void Heap::did_create_marked_vector(Badge<MarkedVectorBase>, MarkedVectorBase& vector)
|
||||
{
|
||||
VERIFY(!m_marked_vectors.contains(vector));
|
||||
m_marked_vectors.append(vector);
|
||||
}
|
||||
|
||||
void Heap::did_destroy_marked_vector(Badge<MarkedVectorBase>, MarkedVectorBase& vector)
|
||||
{
|
||||
VERIFY(m_marked_vectors.contains(vector));
|
||||
m_marked_vectors.remove(vector);
|
||||
}
|
||||
|
||||
void Heap::did_create_weak_container(Badge<WeakContainer>, WeakContainer& set)
|
||||
{
|
||||
VERIFY(!m_weak_containers.contains(set));
|
||||
|
|
|
@ -19,6 +19,7 @@
|
|||
#include <LibJS/Heap/Cell.h>
|
||||
#include <LibJS/Heap/CellAllocator.h>
|
||||
#include <LibJS/Heap/Handle.h>
|
||||
#include <LibJS/Heap/MarkedVector.h>
|
||||
#include <LibJS/Runtime/Object.h>
|
||||
#include <LibJS/Runtime/WeakContainer.h>
|
||||
|
||||
|
@ -75,6 +76,9 @@ public:
|
|||
void did_create_marked_value_list(Badge<MarkedValueList>, MarkedValueList&);
|
||||
void did_destroy_marked_value_list(Badge<MarkedValueList>, MarkedValueList&);
|
||||
|
||||
void did_create_marked_vector(Badge<MarkedVectorBase>, MarkedVectorBase&);
|
||||
void did_destroy_marked_vector(Badge<MarkedVectorBase>, MarkedVectorBase&);
|
||||
|
||||
void did_create_weak_container(Badge<WeakContainer>, WeakContainer&);
|
||||
void did_destroy_weak_container(Badge<WeakContainer>, WeakContainer&);
|
||||
|
||||
|
@ -115,6 +119,7 @@ private:
|
|||
|
||||
HandleImpl::List m_handles;
|
||||
|
||||
MarkedVectorBase::List m_marked_vectors;
|
||||
MarkedValueList::List m_marked_value_lists;
|
||||
|
||||
WeakContainer::List m_weak_containers;
|
||||
|
|
30
Userland/Libraries/LibJS/Heap/MarkedVector.cpp
Normal file
30
Userland/Libraries/LibJS/Heap/MarkedVector.cpp
Normal file
|
@ -0,0 +1,30 @@
|
|||
/*
|
||||
* Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <LibJS/Heap/Heap.h>
|
||||
#include <LibJS/Heap/MarkedVector.h>
|
||||
|
||||
namespace JS {
|
||||
|
||||
MarkedVectorBase::MarkedVectorBase(Heap& heap)
|
||||
: m_heap(heap)
|
||||
{
|
||||
m_heap.did_create_marked_vector({}, *this);
|
||||
}
|
||||
|
||||
MarkedVectorBase::MarkedVectorBase(MarkedVectorBase&& other)
|
||||
: m_heap(other.m_heap)
|
||||
, m_cells(move(other.m_cells))
|
||||
{
|
||||
m_heap.did_create_marked_vector({}, *this);
|
||||
}
|
||||
|
||||
MarkedVectorBase::~MarkedVectorBase()
|
||||
{
|
||||
m_heap.did_destroy_marked_vector({}, *this);
|
||||
}
|
||||
|
||||
}
|
70
Userland/Libraries/LibJS/Heap/MarkedVector.h
Normal file
70
Userland/Libraries/LibJS/Heap/MarkedVector.h
Normal file
|
@ -0,0 +1,70 @@
|
|||
/*
|
||||
* Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <AK/IntrusiveList.h>
|
||||
#include <AK/Noncopyable.h>
|
||||
#include <AK/Vector.h>
|
||||
#include <LibJS/Forward.h>
|
||||
#include <LibJS/Heap/Cell.h>
|
||||
|
||||
namespace JS {
|
||||
|
||||
class MarkedVectorBase {
|
||||
AK_MAKE_NONCOPYABLE(MarkedVectorBase);
|
||||
|
||||
public:
|
||||
void append(Cell* cell) { m_cells.append(cell); }
|
||||
void prepend(Cell* cell) { m_cells.prepend(cell); }
|
||||
size_t size() const { return m_cells.size(); }
|
||||
|
||||
Span<Cell*> cells() { return m_cells.span(); }
|
||||
|
||||
protected:
|
||||
explicit MarkedVectorBase(Heap&);
|
||||
|
||||
MarkedVectorBase(MarkedVectorBase&&);
|
||||
MarkedVectorBase& operator=(MarkedVectorBase&& other)
|
||||
{
|
||||
m_cells = move(other.m_cells);
|
||||
return *this;
|
||||
}
|
||||
|
||||
~MarkedVectorBase();
|
||||
|
||||
Heap& m_heap;
|
||||
|
||||
IntrusiveListNode<MarkedVectorBase> m_list_node;
|
||||
Vector<Cell*> m_cells;
|
||||
|
||||
public:
|
||||
using List = IntrusiveList<&MarkedVectorBase::m_list_node>;
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
class MarkedVector : public MarkedVectorBase {
|
||||
public:
|
||||
explicit MarkedVector(Heap& heap)
|
||||
: MarkedVectorBase(heap)
|
||||
{
|
||||
}
|
||||
|
||||
~MarkedVector() = default;
|
||||
|
||||
MarkedVector(MarkedVector&&) = default;
|
||||
MarkedVector& operator=(MarkedVector&&) = default;
|
||||
|
||||
Span<T*> span() { return Span<T*> { bit_cast<T**>(m_cells.data()), m_cells.size() }; }
|
||||
|
||||
auto begin() { return span().begin(); }
|
||||
auto begin() const { return span().begin(); }
|
||||
|
||||
auto end() { return span().begin(); }
|
||||
auto end() const { return span().begin(); }
|
||||
};
|
||||
|
||||
}
|
Loading…
Add table
Reference in a new issue