mirror of
https://github.com/RGBCube/serenity
synced 2025-07-24 15:07:45 +00:00
LibJS: Add Handle<T>, a strong C++ handle for keeping GC objects alive
This is pretty heavy and unoptimized, but it will do the trick for now. Basically, Heap now has a HashTable<HandleImpl*> and you can call JS::make_handle(T*) to construct a Handle<T> that guarantees that the pointee will always survive GC until the Handle<T> is destroyed.
This commit is contained in:
parent
e265058768
commit
a119b61782
6 changed files with 103 additions and 0 deletions
|
@ -33,6 +33,7 @@ class Argument;
|
||||||
class Cell;
|
class Cell;
|
||||||
class Expression;
|
class Expression;
|
||||||
class Function;
|
class Function;
|
||||||
|
class HandleImpl;
|
||||||
class Heap;
|
class Heap;
|
||||||
class HeapBlock;
|
class HeapBlock;
|
||||||
class Interpreter;
|
class Interpreter;
|
||||||
|
@ -42,4 +43,7 @@ class ScopeNode;
|
||||||
class Value;
|
class Value;
|
||||||
enum class DeclarationType;
|
enum class DeclarationType;
|
||||||
|
|
||||||
|
template<class T>
|
||||||
|
class Handle;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
18
Libraries/LibJS/Heap/Handle.cpp
Normal file
18
Libraries/LibJS/Heap/Handle.cpp
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
#include <LibJS/Heap/Handle.h>
|
||||||
|
#include <LibJS/Heap/Heap.h>
|
||||||
|
#include <LibJS/Runtime/Cell.h>
|
||||||
|
|
||||||
|
namespace JS {
|
||||||
|
|
||||||
|
HandleImpl::HandleImpl(Cell* cell)
|
||||||
|
: m_cell(cell)
|
||||||
|
{
|
||||||
|
m_cell->heap().did_create_handle({}, *this);
|
||||||
|
}
|
||||||
|
|
||||||
|
HandleImpl::~HandleImpl()
|
||||||
|
{
|
||||||
|
m_cell->heap().did_destroy_handle({}, *this);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
57
Libraries/LibJS/Heap/Handle.h
Normal file
57
Libraries/LibJS/Heap/Handle.h
Normal file
|
@ -0,0 +1,57 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <AK/Badge.h>
|
||||||
|
#include <AK/Noncopyable.h>
|
||||||
|
#include <AK/RefCounted.h>
|
||||||
|
#include <AK/RefPtr.h>
|
||||||
|
#include <LibJS/Forward.h>
|
||||||
|
|
||||||
|
namespace JS {
|
||||||
|
|
||||||
|
class HandleImpl : public RefCounted<HandleImpl> {
|
||||||
|
AK_MAKE_NONCOPYABLE(HandleImpl);
|
||||||
|
AK_MAKE_NONMOVABLE(HandleImpl);
|
||||||
|
|
||||||
|
public:
|
||||||
|
~HandleImpl();
|
||||||
|
|
||||||
|
Cell* cell() { return m_cell; }
|
||||||
|
const Cell* cell() const { return m_cell; }
|
||||||
|
|
||||||
|
private:
|
||||||
|
template<class T>
|
||||||
|
friend class Handle;
|
||||||
|
|
||||||
|
explicit HandleImpl(Cell*);
|
||||||
|
Cell* m_cell { nullptr };
|
||||||
|
};
|
||||||
|
|
||||||
|
template<class T>
|
||||||
|
class Handle {
|
||||||
|
public:
|
||||||
|
Handle() {}
|
||||||
|
|
||||||
|
static Handle create(T* cell)
|
||||||
|
{
|
||||||
|
return Handle(adopt(*new HandleImpl(cell)));
|
||||||
|
}
|
||||||
|
|
||||||
|
T* cell() { return static_cast<T*>(m_impl->cell()); }
|
||||||
|
const T* cell() const { return static_cast<const T*>(m_impl->cell()); }
|
||||||
|
|
||||||
|
private:
|
||||||
|
explicit Handle(NonnullRefPtr<HandleImpl> impl)
|
||||||
|
: m_impl(move(impl))
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
RefPtr<HandleImpl> m_impl;
|
||||||
|
};
|
||||||
|
|
||||||
|
template<class T>
|
||||||
|
inline Handle<T> make_handle(T* cell)
|
||||||
|
{
|
||||||
|
return Handle<T>::create(cell);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -26,6 +26,7 @@
|
||||||
|
|
||||||
#include <AK/Badge.h>
|
#include <AK/Badge.h>
|
||||||
#include <AK/HashTable.h>
|
#include <AK/HashTable.h>
|
||||||
|
#include <LibJS/Heap/Handle.h>
|
||||||
#include <LibJS/Heap/Heap.h>
|
#include <LibJS/Heap/Heap.h>
|
||||||
#include <LibJS/Heap/HeapBlock.h>
|
#include <LibJS/Heap/HeapBlock.h>
|
||||||
#include <LibJS/Interpreter.h>
|
#include <LibJS/Interpreter.h>
|
||||||
|
@ -79,6 +80,9 @@ void Heap::gather_roots(HashTable<Cell*>& roots)
|
||||||
|
|
||||||
gather_conservative_roots(roots);
|
gather_conservative_roots(roots);
|
||||||
|
|
||||||
|
for (auto* handle : m_handles)
|
||||||
|
roots.set(handle->cell());
|
||||||
|
|
||||||
#ifdef HEAP_DEBUG
|
#ifdef HEAP_DEBUG
|
||||||
dbg() << "gather_roots:";
|
dbg() << "gather_roots:";
|
||||||
for (auto* root : roots) {
|
for (auto* root : roots) {
|
||||||
|
@ -196,4 +200,17 @@ void Heap::sweep_dead_cells()
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Heap::did_create_handle(Badge<HandleImpl>, HandleImpl& impl)
|
||||||
|
{
|
||||||
|
ASSERT(!m_handles.contains(&impl));
|
||||||
|
m_handles.set(&impl);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Heap::did_destroy_handle(Badge<HandleImpl>, HandleImpl& impl)
|
||||||
|
{
|
||||||
|
ASSERT(m_handles.contains(&impl));
|
||||||
|
m_handles.remove(&impl);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,11 +26,13 @@
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <AK/HashTable.h>
|
||||||
#include <AK/Noncopyable.h>
|
#include <AK/Noncopyable.h>
|
||||||
#include <AK/NonnullOwnPtr.h>
|
#include <AK/NonnullOwnPtr.h>
|
||||||
#include <AK/Types.h>
|
#include <AK/Types.h>
|
||||||
#include <AK/Vector.h>
|
#include <AK/Vector.h>
|
||||||
#include <LibJS/Forward.h>
|
#include <LibJS/Forward.h>
|
||||||
|
#include <LibJS/Heap/Handle.h>
|
||||||
#include <LibJS/Runtime/Cell.h>
|
#include <LibJS/Runtime/Cell.h>
|
||||||
|
|
||||||
namespace JS {
|
namespace JS {
|
||||||
|
@ -58,6 +60,9 @@ public:
|
||||||
bool should_collect_on_every_allocation() const { return m_should_collect_on_every_allocation; }
|
bool should_collect_on_every_allocation() const { return m_should_collect_on_every_allocation; }
|
||||||
void set_should_collect_on_every_allocation(bool b) { m_should_collect_on_every_allocation = b; }
|
void set_should_collect_on_every_allocation(bool b) { m_should_collect_on_every_allocation = b; }
|
||||||
|
|
||||||
|
void did_create_handle(Badge<HandleImpl>, HandleImpl&);
|
||||||
|
void did_destroy_handle(Badge<HandleImpl>, HandleImpl&);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Cell* allocate_cell(size_t);
|
Cell* allocate_cell(size_t);
|
||||||
|
|
||||||
|
@ -72,6 +77,7 @@ private:
|
||||||
|
|
||||||
Interpreter& m_interpreter;
|
Interpreter& m_interpreter;
|
||||||
Vector<NonnullOwnPtr<HeapBlock>> m_blocks;
|
Vector<NonnullOwnPtr<HeapBlock>> m_blocks;
|
||||||
|
HashTable<HandleImpl*> m_handles;
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
OBJS = \
|
OBJS = \
|
||||||
AST.o \
|
AST.o \
|
||||||
|
Heap/Handle.o \
|
||||||
Heap/Heap.o \
|
Heap/Heap.o \
|
||||||
Heap/HeapBlock.o \
|
Heap/HeapBlock.o \
|
||||||
Interpreter.o \
|
Interpreter.o \
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue