mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-10 13:00:29 +03:00
a863363b06
Note: MarkedVector is still relatively new and has zero users right now, so these changes don't affect any code other than the class itself. Reasons for this are the rather limited API: - Despite the name and unlike MarkedValueList, MarkedVector isn't actually a Vector, it *wraps* a Vector. This means that plenty of convenient APIs are unavailable and have to be exported on the class separately and forwarded to the internal Vector, or need to go through the exposed Span - both not great options. - Exposing append(Cell*) and prepend(Cell*) on the base class means that it was possible to append any Cell type, not just T! All the strong typing guarantees are basically gone, and MarkedVector doesn't do much more than casting Cells to the appropriate type through the exposed Span. All of this combined means that MarkedVector - in its current form - doesn't provide much value over MarkedValueList, and that we have to maintain two separate, yet almost identical classes. Let's fix this! The updated MarkedVector steals various concepts from the existing MarkedValueList, especially the ability to copy. On the other hand, it remains generic enough to handle both Cell* and Value for T, making MarkedValueList effectively redundant :^) Additionally, by inheriting from Vector we get all the current and future APIs without having to select and expose them separately. MarkedVectorBase remains and takes care of communicating creation and destruction of the class to the heap. Visiting the contained values is handled via a pure virtual method gather_roots(), which is being called by the Heap's function of the same name; much like the VM has one. From there, values are added to the roots HashTable if they are cells for T = Value, and unconditionally for any other T. As a small additional improvement the template now also takes an inline_capacity parameter, defaulting to 32, and forwards it to the Vector template; allowing for possible future optimizations of current uses of MarkedValueList, which hard-codes it to 32.
81 lines
1.8 KiB
C++
81 lines
1.8 KiB
C++
/*
|
|
* Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
|
|
* Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
|
|
*
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include <AK/HashTable.h>
|
|
#include <AK/IntrusiveList.h>
|
|
#include <AK/Vector.h>
|
|
#include <LibJS/Forward.h>
|
|
#include <LibJS/Heap/Cell.h>
|
|
|
|
namespace JS {
|
|
|
|
class MarkedVectorBase {
|
|
public:
|
|
virtual void gather_roots(HashTable<Cell*>&) const = 0;
|
|
|
|
protected:
|
|
explicit MarkedVectorBase(Heap&);
|
|
~MarkedVectorBase();
|
|
|
|
MarkedVectorBase& operator=(MarkedVectorBase const&);
|
|
|
|
Heap* m_heap { nullptr };
|
|
IntrusiveListNode<MarkedVectorBase> m_list_node;
|
|
|
|
public:
|
|
using List = IntrusiveList<&MarkedVectorBase::m_list_node>;
|
|
};
|
|
|
|
template<typename T, size_t inline_capacity>
|
|
class MarkedVector
|
|
: public MarkedVectorBase
|
|
, public Vector<T, inline_capacity> {
|
|
|
|
public:
|
|
explicit MarkedVector(Heap& heap)
|
|
: MarkedVectorBase(heap)
|
|
{
|
|
}
|
|
|
|
virtual ~MarkedVector() = default;
|
|
|
|
MarkedVector(MarkedVector const& other)
|
|
: MarkedVectorBase(*other.m_heap)
|
|
, Vector<T, inline_capacity>(other)
|
|
{
|
|
}
|
|
|
|
MarkedVector(MarkedVector&& other)
|
|
: MarkedVectorBase(*other.m_heap)
|
|
, Vector<T, inline_capacity>(move(static_cast<Vector<T, inline_capacity>&>(other)))
|
|
{
|
|
}
|
|
|
|
MarkedVector& operator=(MarkedVector const& other)
|
|
{
|
|
Vector<T, inline_capacity>::operator=(other);
|
|
MarkedVectorBase::operator=(other);
|
|
return *this;
|
|
}
|
|
|
|
virtual void gather_roots(HashTable<Cell*>& roots) const override
|
|
{
|
|
for (auto& value : *this) {
|
|
if constexpr (IsSame<Value, T>) {
|
|
if (value.is_cell())
|
|
roots.set(&const_cast<T&>(value).as_cell());
|
|
} else {
|
|
roots.set(value);
|
|
}
|
|
}
|
|
};
|
|
};
|
|
|
|
}
|