2020-03-08 18:23:58 +00:00
|
|
|
/*
|
2024-10-04 11:19:50 +00:00
|
|
|
* Copyright (c) 2020-2024, Andreas Kling <andreas@ladybird.org>
|
2020-03-08 18:23:58 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-03-08 18:23:58 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2021-08-28 16:12:14 +00:00
|
|
|
#include <AK/Badge.h>
|
2024-11-14 07:17:33 +00:00
|
|
|
#include <AK/Function.h>
|
2020-03-18 19:03:17 +00:00
|
|
|
#include <AK/HashTable.h>
|
2021-07-21 17:45:21 +00:00
|
|
|
#include <AK/IntrusiveList.h>
|
2020-03-08 18:23:58 +00:00
|
|
|
#include <AK/Noncopyable.h>
|
|
|
|
#include <AK/NonnullOwnPtr.h>
|
2024-08-18 10:04:55 +00:00
|
|
|
#include <AK/StackInfo.h>
|
2024-11-16 21:58:28 +00:00
|
|
|
#include <AK/Swift.h>
|
2020-03-08 18:23:58 +00:00
|
|
|
#include <AK/Types.h>
|
|
|
|
#include <AK/Vector.h>
|
2020-08-16 18:33:56 +00:00
|
|
|
#include <LibCore/Forward.h>
|
2024-11-14 15:01:23 +00:00
|
|
|
#include <LibGC/Cell.h>
|
|
|
|
#include <LibGC/CellAllocator.h>
|
|
|
|
#include <LibGC/ConservativeVector.h>
|
|
|
|
#include <LibGC/Forward.h>
|
|
|
|
#include <LibGC/HeapRoot.h>
|
|
|
|
#include <LibGC/Internals.h>
|
|
|
|
#include <LibGC/MarkedVector.h>
|
|
|
|
#include <LibGC/Root.h>
|
|
|
|
#include <LibGC/WeakContainer.h>
|
|
|
|
|
|
|
|
namespace GC {
|
2020-03-08 18:23:58 +00:00
|
|
|
|
2023-07-09 16:35:02 +00:00
|
|
|
class Heap : public HeapBase {
|
2020-03-08 18:23:58 +00:00
|
|
|
AK_MAKE_NONCOPYABLE(Heap);
|
|
|
|
AK_MAKE_NONMOVABLE(Heap);
|
|
|
|
|
|
|
|
public:
|
2024-11-14 15:01:23 +00:00
|
|
|
explicit Heap(void* private_data, AK::Function<void(HashMap<Cell*, GC::HeapRoot>&)> gather_embedder_roots);
|
2020-03-08 18:23:58 +00:00
|
|
|
~Heap();
|
|
|
|
|
|
|
|
template<typename T, typename... Args>
|
2024-11-14 15:01:23 +00:00
|
|
|
Ref<T> allocate(Args&&... args)
|
2020-03-08 18:23:58 +00:00
|
|
|
{
|
2023-11-19 08:45:05 +00:00
|
|
|
auto* memory = allocate_cell<T>();
|
2023-09-23 11:47:16 +00:00
|
|
|
defer_gc();
|
2020-03-08 18:23:58 +00:00
|
|
|
new (memory) T(forward<Args>(args)...);
|
2023-09-23 11:47:16 +00:00
|
|
|
undefer_gc();
|
2022-12-14 17:40:33 +00:00
|
|
|
return *static_cast<T*>(memory);
|
2020-03-08 18:23:58 +00:00
|
|
|
}
|
|
|
|
|
2020-03-23 13:11:19 +00:00
|
|
|
enum class CollectionType {
|
|
|
|
CollectGarbage,
|
|
|
|
CollectEverything,
|
|
|
|
};
|
|
|
|
|
2020-08-16 18:33:56 +00:00
|
|
|
void collect_garbage(CollectionType = CollectionType::CollectGarbage, bool print_report = false);
|
2023-12-12 12:25:06 +00:00
|
|
|
AK::JsonObject dump_graph();
|
2020-03-08 18:23:58 +00:00
|
|
|
|
2020-03-16 18:18:46 +00:00
|
|
|
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; }
|
|
|
|
|
2024-11-14 15:01:23 +00:00
|
|
|
void did_create_root(Badge<RootImpl>, RootImpl&);
|
|
|
|
void did_destroy_root(Badge<RootImpl>, RootImpl&);
|
2020-03-18 19:03:17 +00:00
|
|
|
|
2021-12-16 17:54:06 +00:00
|
|
|
void did_create_marked_vector(Badge<MarkedVectorBase>, MarkedVectorBase&);
|
|
|
|
void did_destroy_marked_vector(Badge<MarkedVectorBase>, MarkedVectorBase&);
|
|
|
|
|
2024-02-22 12:18:31 +00:00
|
|
|
void did_create_conservative_vector(Badge<ConservativeVectorBase>, ConservativeVectorBase&);
|
|
|
|
void did_destroy_conservative_vector(Badge<ConservativeVectorBase>, ConservativeVectorBase&);
|
|
|
|
|
2021-06-12 02:23:33 +00:00
|
|
|
void did_create_weak_container(Badge<WeakContainer>, WeakContainer&);
|
|
|
|
void did_destroy_weak_container(Badge<WeakContainer>, WeakContainer&);
|
2021-06-09 17:10:47 +00:00
|
|
|
|
2023-12-23 14:13:51 +00:00
|
|
|
void register_cell_allocator(Badge<CellAllocator>, CellAllocator&);
|
|
|
|
|
2024-11-14 15:01:23 +00:00
|
|
|
void uproot_cell(Cell* cell);
|
2023-07-22 04:53:22 +00:00
|
|
|
|
2024-11-17 01:45:19 +00:00
|
|
|
bool is_gc_deferred() const { return m_gc_deferrals > 0; }
|
|
|
|
|
2020-03-08 18:23:58 +00:00
|
|
|
private:
|
2023-08-18 16:21:33 +00:00
|
|
|
friend class MarkingVisitor;
|
|
|
|
friend class GraphConstructorVisitor;
|
2023-09-23 11:44:13 +00:00
|
|
|
friend class DeferGC;
|
2024-11-16 21:56:27 +00:00
|
|
|
friend class ForeignCell;
|
2023-09-23 11:44:13 +00:00
|
|
|
|
|
|
|
void defer_gc();
|
|
|
|
void undefer_gc();
|
2023-08-18 16:21:33 +00:00
|
|
|
|
2024-11-14 15:01:23 +00:00
|
|
|
static bool cell_must_survive_garbage_collection(Cell const&);
|
2022-10-24 10:37:54 +00:00
|
|
|
|
2023-11-19 08:45:05 +00:00
|
|
|
template<typename T>
|
2024-11-14 15:01:23 +00:00
|
|
|
Cell* allocate_cell()
|
2023-11-19 08:45:05 +00:00
|
|
|
{
|
|
|
|
will_allocate(sizeof(T));
|
2023-12-23 19:00:21 +00:00
|
|
|
if constexpr (requires { T::cell_allocator.allocator.get().allocate_cell(*this); }) {
|
2023-11-19 08:45:05 +00:00
|
|
|
if constexpr (IsSame<T, typename decltype(T::cell_allocator)::CellType>) {
|
2023-12-23 19:00:21 +00:00
|
|
|
return T::cell_allocator.allocator.get().allocate_cell(*this);
|
2023-11-19 08:45:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return allocator_for_size(sizeof(T)).allocate_cell(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void will_allocate(size_t);
|
2020-03-08 18:23:58 +00:00
|
|
|
|
2023-09-29 17:14:11 +00:00
|
|
|
void find_min_and_max_block_addresses(FlatPtr& min_address, FlatPtr& max_address);
|
2024-11-14 15:01:23 +00:00
|
|
|
void gather_roots(HashMap<Cell*, HeapRoot>&);
|
|
|
|
void gather_conservative_roots(HashMap<Cell*, HeapRoot>&);
|
2023-09-29 17:14:11 +00:00
|
|
|
void gather_asan_fake_stack_roots(HashMap<FlatPtr, HeapRoot>&, FlatPtr, FlatPtr min_block_address, FlatPtr max_block_address);
|
2024-11-14 15:01:23 +00:00
|
|
|
void mark_live_cells(HashMap<Cell*, HeapRoot> const& live_cells);
|
2022-10-20 16:35:19 +00:00
|
|
|
void finalize_unmarked_cells();
|
2022-04-01 17:58:27 +00:00
|
|
|
void sweep_dead_cells(bool print_report, Core::ElapsedTimer const&);
|
2020-03-08 18:23:58 +00:00
|
|
|
|
2023-11-19 08:45:05 +00:00
|
|
|
ALWAYS_INLINE CellAllocator& allocator_for_size(size_t cell_size)
|
|
|
|
{
|
|
|
|
// FIXME: Use binary search?
|
2023-12-23 14:13:51 +00:00
|
|
|
for (auto& allocator : m_size_based_cell_allocators) {
|
2023-11-19 08:45:05 +00:00
|
|
|
if (allocator->cell_size() >= cell_size)
|
|
|
|
return *allocator;
|
|
|
|
}
|
2023-12-23 14:13:51 +00:00
|
|
|
dbgln("Cannot get CellAllocator for cell size {}, largest available is {}!", cell_size, m_size_based_cell_allocators.last()->cell_size());
|
2023-11-19 08:45:05 +00:00
|
|
|
VERIFY_NOT_REACHED();
|
|
|
|
}
|
2020-10-06 16:50:47 +00:00
|
|
|
|
|
|
|
template<typename Callback>
|
|
|
|
void for_each_block(Callback callback)
|
|
|
|
{
|
2023-12-23 14:13:51 +00:00
|
|
|
for (auto& allocator : m_all_cell_allocators) {
|
|
|
|
if (allocator.for_each_block(callback) == IterationDecision::Break)
|
2020-10-06 16:50:47 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-08 17:50:32 +00:00
|
|
|
static constexpr size_t GC_MIN_BYTES_THRESHOLD { 4 * 1024 * 1024 };
|
|
|
|
size_t m_gc_bytes_threshold { GC_MIN_BYTES_THRESHOLD };
|
|
|
|
size_t m_allocated_bytes_since_last_gc { 0 };
|
2020-04-06 10:36:49 +00:00
|
|
|
|
2020-03-16 18:18:46 +00:00
|
|
|
bool m_should_collect_on_every_allocation { false };
|
|
|
|
|
2023-12-23 14:13:51 +00:00
|
|
|
Vector<NonnullOwnPtr<CellAllocator>> m_size_based_cell_allocators;
|
|
|
|
CellAllocator::List m_all_cell_allocators;
|
2021-07-21 17:45:21 +00:00
|
|
|
|
2024-11-14 15:01:23 +00:00
|
|
|
RootImpl::List m_roots;
|
2021-12-16 17:54:06 +00:00
|
|
|
MarkedVectorBase::List m_marked_vectors;
|
2024-02-22 12:18:31 +00:00
|
|
|
ConservativeVectorBase::List m_conservative_vectors;
|
2021-07-21 17:57:41 +00:00
|
|
|
WeakContainer::List m_weak_containers;
|
2021-06-09 17:10:47 +00:00
|
|
|
|
2024-11-14 15:01:23 +00:00
|
|
|
Vector<Ptr<Cell>> m_uprooted_cells;
|
2023-07-22 04:53:22 +00:00
|
|
|
|
2020-04-19 09:30:47 +00:00
|
|
|
size_t m_gc_deferrals { 0 };
|
|
|
|
bool m_should_gc_when_deferral_ends { false };
|
2020-09-21 12:35:19 +00:00
|
|
|
|
|
|
|
bool m_collecting_garbage { false };
|
2024-08-18 10:04:55 +00:00
|
|
|
StackInfo m_stack_info;
|
2024-11-14 15:01:23 +00:00
|
|
|
AK::Function<void(HashMap<Cell*, GC::HeapRoot>&)> m_gather_embedder_roots;
|
2024-11-16 21:58:28 +00:00
|
|
|
} SWIFT_IMMORTAL_REFERENCE;
|
2020-03-08 18:23:58 +00:00
|
|
|
|
2024-11-14 15:01:23 +00:00
|
|
|
inline void Heap::did_create_root(Badge<RootImpl>, RootImpl& impl)
|
2023-10-07 07:15:09 +00:00
|
|
|
{
|
2024-11-14 15:01:23 +00:00
|
|
|
VERIFY(!m_roots.contains(impl));
|
|
|
|
m_roots.append(impl);
|
2023-10-07 07:15:09 +00:00
|
|
|
}
|
|
|
|
|
2024-11-14 15:01:23 +00:00
|
|
|
inline void Heap::did_destroy_root(Badge<RootImpl>, RootImpl& impl)
|
2023-10-07 07:15:09 +00:00
|
|
|
{
|
2024-11-14 15:01:23 +00:00
|
|
|
VERIFY(m_roots.contains(impl));
|
|
|
|
m_roots.remove(impl);
|
2023-10-07 07:15:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline void Heap::did_create_marked_vector(Badge<MarkedVectorBase>, MarkedVectorBase& vector)
|
|
|
|
{
|
|
|
|
VERIFY(!m_marked_vectors.contains(vector));
|
|
|
|
m_marked_vectors.append(vector);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void Heap::did_destroy_marked_vector(Badge<MarkedVectorBase>, MarkedVectorBase& vector)
|
|
|
|
{
|
|
|
|
VERIFY(m_marked_vectors.contains(vector));
|
|
|
|
m_marked_vectors.remove(vector);
|
|
|
|
}
|
|
|
|
|
2024-02-22 12:18:31 +00:00
|
|
|
inline void Heap::did_create_conservative_vector(Badge<ConservativeVectorBase>, ConservativeVectorBase& vector)
|
|
|
|
{
|
|
|
|
VERIFY(!m_conservative_vectors.contains(vector));
|
|
|
|
m_conservative_vectors.append(vector);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void Heap::did_destroy_conservative_vector(Badge<ConservativeVectorBase>, ConservativeVectorBase& vector)
|
|
|
|
{
|
|
|
|
VERIFY(m_conservative_vectors.contains(vector));
|
|
|
|
m_conservative_vectors.remove(vector);
|
|
|
|
}
|
|
|
|
|
2023-10-07 07:15:09 +00:00
|
|
|
inline void Heap::did_create_weak_container(Badge<WeakContainer>, WeakContainer& set)
|
|
|
|
{
|
|
|
|
VERIFY(!m_weak_containers.contains(set));
|
|
|
|
m_weak_containers.append(set);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void Heap::did_destroy_weak_container(Badge<WeakContainer>, WeakContainer& set)
|
|
|
|
{
|
|
|
|
VERIFY(m_weak_containers.contains(set));
|
|
|
|
m_weak_containers.remove(set);
|
|
|
|
}
|
|
|
|
|
2023-12-23 14:13:51 +00:00
|
|
|
inline void Heap::register_cell_allocator(Badge<CellAllocator>, CellAllocator& allocator)
|
|
|
|
{
|
|
|
|
m_all_cell_allocators.append(allocator);
|
|
|
|
}
|
|
|
|
|
2020-03-08 18:23:58 +00:00
|
|
|
}
|