Skip to content

Commit

Permalink
LibGC+Everywhere: Factor out a LibGC from LibJS
Browse files Browse the repository at this point in the history
Resulting in a massive rename across almost everywhere! Alongside the
namespace change, we now have the following names:

 * JS::NonnullGCPtr -> GC::Ref
 * JS::GCPtr -> GC::Ptr
 * JS::HeapFunction -> GC::Function
 * JS::CellImpl -> GC::Cell
 * JS::Handle -> GC::Root
  • Loading branch information
shannonbooth authored and awesomekling committed Nov 15, 2024
1 parent ce23efc commit f87041b
Show file tree
Hide file tree
Showing 1,722 changed files with 9,942 additions and 9,909 deletions.
6 changes: 3 additions & 3 deletions Documentation/LibWebPatterns.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ This is the most common and at the same time most broad error type in LibWeb. In
variant of supported errors:

- `SimpleException`
- `JS::NonnullGCPtr<DOMException>`
- `GC::Ref<DOMException>`
- `JS::Completion` (from `JS::ThrowCompletionOr<T>`, assumed to be of `Type::Throw`)

Use this error type for anything that needs to interact with the JS bindings, which will generally
Expand Down Expand Up @@ -86,7 +86,7 @@ must have:

```cpp
// https://fetch.spec.whatwg.org/#concept-fetch
WebIDL::ExceptionOr<JS::NonnullGCPtr<Infrastructure::FetchController>> fetch(JS::Realm& realm, Infrastructure::Request& request, Infrastructure::FetchAlgorithms const& algorithms, UseParallelQueue use_parallel_queue)
WebIDL::ExceptionOr<GC::Ref<Infrastructure::FetchController>> fetch(JS::Realm& realm, Infrastructure::Request& request, Infrastructure::FetchAlgorithms const& algorithms, UseParallelQueue use_parallel_queue)
{
// ...
}
Expand All @@ -99,7 +99,7 @@ must have:
VERIFY(request.mode() == Infrastructure::Request::Mode::Navigate || !algorithms.process_early_hints_response().has_value());

// 2. Let taskDestination be null.
JS::GCPtr<JS::Object> task_destination;
GC::Ptr<JS::Object> task_destination;

// ...
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
#include <AK/Platform.h>
#include <AK/Random.h>
#include <AK/Vector.h>
#include <LibJS/Heap/BlockAllocator.h>
#include <LibJS/Heap/HeapBlock.h>
#include <LibGC/BlockAllocator.h>
#include <LibGC/HeapBlock.h>
#include <sys/mman.h>

#ifdef HAS_ADDRESS_SANITIZER
Expand All @@ -20,7 +20,7 @@
# define USE_FALLBACK_BLOCK_DEALLOCATION
#endif

namespace JS {
namespace GC {

BlockAllocator::~BlockAllocator()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
#pragma once

#include <AK/Vector.h>
#include <LibJS/Forward.h>
#include <LibGC/Forward.h>

namespace JS {
namespace GC {

class BlockAllocator {
public:
Expand Down
14 changes: 14 additions & 0 deletions Libraries/LibGC/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
set(SOURCES
BlockAllocator.cpp
Cell.cpp
CellAllocator.cpp
ConservativeVector.cpp
Root.cpp
Heap.cpp
HeapBlock.cpp
MarkedVector.cpp
WeakContainer.cpp
)

serenity_lib(LibGC gc)
target_link_libraries(LibGC PRIVATE LibCore)
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
* SPDX-License-Identifier: BSD-2-Clause
*/

#include <LibJS/Heap/CellImpl.h>
#include <LibJS/Heap/NanBoxedValue.h>
#include <LibGC/Cell.h>
#include <LibGC/NanBoxedValue.h>

namespace JS {
namespace GC {

void JS::CellImpl::Visitor::visit(NanBoxedValue const& value)
void GC::Cell::Visitor::visit(NanBoxedValue const& value)
{
if (value.is_cell())
visit_impl(value.as_cell());
Expand Down
44 changes: 22 additions & 22 deletions Libraries/LibJS/Heap/CellImpl.h → Libraries/LibGC/Cell.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
#include <AK/Noncopyable.h>
#include <AK/StringView.h>
#include <AK/Weakable.h>
#include <LibJS/Forward.h>
#include <LibJS/Heap/GCPtr.h>
#include <LibJS/Heap/Internals.h>
#include <LibGC/Forward.h>
#include <LibGC/Internals.h>
#include <LibGC/Ptr.h>

namespace JS {
namespace GC {

// This instrumentation tells analysis tooling to ignore a potentially mis-wrapped GC-allocated member variable
// It should only be used when the lifetime of the GC-allocated member is always longer than the object
Expand All @@ -27,21 +27,21 @@ namespace JS {
# define IGNORE_GC
#endif

#define JS_CELL(class_, base_class) \
#define GC_CELL(class_, base_class) \
public: \
using Base = base_class; \
virtual StringView class_name() const override \
{ \
return #class_##sv; \
} \
friend class JS::Heap;
friend class GC::Heap;

class CellImpl : public Weakable<CellImpl> {
AK_MAKE_NONCOPYABLE(CellImpl);
AK_MAKE_NONMOVABLE(CellImpl);
class Cell : public Weakable<Cell> {
AK_MAKE_NONCOPYABLE(Cell);
AK_MAKE_NONMOVABLE(Cell);

public:
virtual ~CellImpl() = default;
virtual ~Cell() = default;

bool is_marked() const { return m_mark; }
void set_marked(bool b) { m_mark = b; }
Expand All @@ -58,36 +58,36 @@ class CellImpl : public Weakable<CellImpl> {

class Visitor {
public:
void visit(CellImpl* cell)
void visit(Cell* cell)
{
if (cell)
visit_impl(*cell);
}

void visit(CellImpl& cell)
void visit(Cell& cell)
{
visit_impl(cell);
}

void visit(CellImpl const* cell)
void visit(Cell const* cell)
{
visit(const_cast<CellImpl*>(cell));
visit(const_cast<Cell*>(cell));
}

void visit(CellImpl const& cell)
void visit(Cell const& cell)
{
visit(const_cast<CellImpl&>(cell));
visit(const_cast<Cell&>(cell));
}

template<typename T>
void visit(GCPtr<T> cell)
void visit(Ptr<T> cell)
{
if (cell)
visit_impl(const_cast<RemoveConst<T>&>(*cell.ptr()));
}

template<typename T>
void visit(NonnullGCPtr<T> cell)
void visit(Ref<T> cell)
{
visit_impl(const_cast<RemoveConst<T>&>(*cell.ptr()));
}
Expand Down Expand Up @@ -161,7 +161,7 @@ class CellImpl : public Weakable<CellImpl> {
virtual void visit_possible_values(ReadonlyBytes) = 0;

protected:
virtual void visit_impl(CellImpl&) = 0;
virtual void visit_impl(Cell&) = 0;
virtual ~Visitor() = default;
};

Expand All @@ -180,7 +180,7 @@ class CellImpl : public Weakable<CellImpl> {
ALWAYS_INLINE Heap& heap() const { return HeapBlockBase::from_cell(this)->heap(); }

protected:
CellImpl() = default;
Cell() = default;

ALWAYS_INLINE void* private_data() const { return bit_cast<HeapBase*>(&heap())->private_data(); }

Expand All @@ -195,8 +195,8 @@ class CellImpl : public Weakable<CellImpl> {
}

template<>
struct AK::Formatter<JS::CellImpl> : AK::Formatter<FormatString> {
ErrorOr<void> format(FormatBuilder& builder, JS::CellImpl const* cell)
struct AK::Formatter<GC::Cell> : AK::Formatter<FormatString> {
ErrorOr<void> format(FormatBuilder& builder, GC::Cell const* cell)
{
if (!cell)
return builder.put_string("Cell{nullptr}"sv);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@
*/

#include <AK/Badge.h>
#include <LibJS/Heap/BlockAllocator.h>
#include <LibJS/Heap/CellAllocator.h>
#include <LibJS/Heap/Heap.h>
#include <LibJS/Heap/HeapBlock.h>
#include <LibGC/BlockAllocator.h>
#include <LibGC/CellAllocator.h>
#include <LibGC/Heap.h>
#include <LibGC/HeapBlock.h>

namespace JS {
namespace GC {

CellAllocator::CellAllocator(size_t cell_size, char const* class_name)
: m_class_name(class_name)
, m_cell_size(cell_size)
{
}

CellImpl* CellAllocator::allocate_cell(Heap& heap)
Cell* CellAllocator::allocate_cell(Heap& heap)
{
if (!m_list_node.is_in_list())
heap.register_cell_allocator({}, *this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@
#include <AK/IntrusiveList.h>
#include <AK/NeverDestroyed.h>
#include <AK/NonnullOwnPtr.h>
#include <LibJS/Forward.h>
#include <LibJS/Heap/BlockAllocator.h>
#include <LibJS/Heap/HeapBlock.h>
#include <LibGC/BlockAllocator.h>
#include <LibGC/Forward.h>
#include <LibGC/HeapBlock.h>

#define JS_DECLARE_ALLOCATOR(ClassName) \
static JS::TypeIsolatingCellAllocator<ClassName> cell_allocator
#define GC_DECLARE_ALLOCATOR(ClassName) \
static GC::TypeIsolatingCellAllocator<ClassName> cell_allocator

#define JS_DEFINE_ALLOCATOR(ClassName) \
JS::TypeIsolatingCellAllocator<ClassName> ClassName::cell_allocator { #ClassName }
#define GC_DEFINE_ALLOCATOR(ClassName) \
GC::TypeIsolatingCellAllocator<ClassName> ClassName::cell_allocator { #ClassName }

namespace JS {
namespace GC {

class CellAllocator {
public:
Expand All @@ -28,7 +28,7 @@ class CellAllocator {

size_t cell_size() const { return m_cell_size; }

CellImpl* allocate_cell(Heap&);
Cell* allocate_cell(Heap&);

template<typename Callback>
IterationDecision for_each_block(Callback callback)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
* SPDX-License-Identifier: BSD-2-Clause
*/

#include <LibJS/Heap/ConservativeVector.h>
#include <LibJS/Heap/Heap.h>
#include <LibGC/ConservativeVector.h>
#include <LibGC/Heap.h>

namespace JS {
namespace GC {

ConservativeVectorBase::ConservativeVectorBase(Heap& heap)
: m_heap(&heap)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
#include <AK/HashMap.h>
#include <AK/IntrusiveList.h>
#include <AK/Vector.h>
#include <LibJS/Forward.h>
#include <LibJS/Heap/CellImpl.h>
#include <LibJS/Heap/HeapRoot.h>
#include <LibGC/Cell.h>
#include <LibGC/Forward.h>
#include <LibGC/HeapRoot.h>

namespace JS {
namespace GC {

class ConservativeVectorBase {
public:
Expand Down
4 changes: 2 additions & 2 deletions Libraries/LibJS/Heap/DeferGC.h → Libraries/LibGC/DeferGC.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

#pragma once

#include <LibJS/Heap/Heap.h>
#include <LibGC/Heap.h>

namespace JS {
namespace GC {

class DeferGC {
public:
Expand Down
32 changes: 32 additions & 0 deletions Libraries/LibGC/Forward.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright (c) 2024, Shannon Booth <[email protected]>
*
* SPDX-License-Identifier: BSD-2-Clause
*/

#pragma once

namespace GC {

class Cell;
class CellAllocator;
class DeferGC;
class RootImpl;
class Heap;
class HeapBlock;
class NanBoxedValue;
class WeakContainer;

template<typename T>
class Function;

template<class T>
class Root;

template<class T, size_t inline_capacity = 0>
class ConservativeVector;

template<class T, size_t inline_capacity = 0>
class MarkedVector;

}
50 changes: 50 additions & 0 deletions Libraries/LibGC/Function.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright (c) 2023, Andreas Kling <[email protected]>
*
* SPDX-License-Identifier: BSD-2-Clause
*/

#pragma once

#include <AK/Function.h>
#include <LibGC/Cell.h>
#include <LibGC/Heap.h>

namespace GC {

template<typename T>
class Function final : public Cell {
GC_CELL(Function, Cell);

public:
static Ref<Function> create(Heap& heap, AK::Function<T> function)
{
return heap.allocate<Function>(move(function));
}

virtual ~Function() override = default;

[[nodiscard]] AK::Function<T> const& function() const { return m_function; }

private:
Function(AK::Function<T> function)
: m_function(move(function))
{
}

virtual void visit_edges(Visitor& visitor) override
{
Base::visit_edges(visitor);
visitor.visit_possible_values(m_function.raw_capture_range());
}

AK::Function<T> m_function;
};

template<typename Callable, typename T = EquivalentFunctionType<Callable>>
static Ref<Function<T>> create_function(Heap& heap, Callable&& function)
{
return Function<T>::create(heap, AK::Function<T> { forward<Callable>(function) });
}

}
Loading

0 comments on commit f87041b

Please sign in to comment.