2020-03-08 19:23:58 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-03-08 19:23:58 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2021-08-28 17:12:14 +01:00
|
|
|
#include <AK/Badge.h>
|
2020-03-18 20:03:17 +01:00
|
|
|
#include <AK/HashTable.h>
|
2021-07-21 19:45:21 +02:00
|
|
|
#include <AK/IntrusiveList.h>
|
2020-03-08 19:23:58 +01:00
|
|
|
#include <AK/Noncopyable.h>
|
|
|
|
#include <AK/NonnullOwnPtr.h>
|
|
|
|
#include <AK/Types.h>
|
|
|
|
#include <AK/Vector.h>
|
2020-08-16 20:33:56 +02:00
|
|
|
#include <LibCore/Forward.h>
|
2020-03-08 19:23:58 +01:00
|
|
|
#include <LibJS/Forward.h>
|
2021-05-27 19:01:26 +02:00
|
|
|
#include <LibJS/Heap/BlockAllocator.h>
|
2021-05-17 19:50:20 +02:00
|
|
|
#include <LibJS/Heap/Cell.h>
|
2021-05-27 19:03:41 +02:00
|
|
|
#include <LibJS/Heap/CellAllocator.h>
|
2020-03-18 20:03:17 +01:00
|
|
|
#include <LibJS/Heap/Handle.h>
|
2023-07-09 20:05:02 +03:30
|
|
|
#include <LibJS/Heap/Internals.h>
|
2021-12-16 18:54:06 +01:00
|
|
|
#include <LibJS/Heap/MarkedVector.h>
|
2023-07-09 11:40:17 +03:30
|
|
|
#include <LibJS/Runtime/Completion.h>
|
2021-07-21 19:57:41 +02:00
|
|
|
#include <LibJS/Runtime/WeakContainer.h>
|
2020-03-08 19:23:58 +01:00
|
|
|
|
|
|
|
namespace JS {
|
|
|
|
|
2023-07-09 20:05:02 +03:30
|
|
|
class Heap : public HeapBase {
|
2020-03-08 19:23:58 +01:00
|
|
|
AK_MAKE_NONCOPYABLE(Heap);
|
|
|
|
AK_MAKE_NONMOVABLE(Heap);
|
|
|
|
|
|
|
|
public:
|
2020-09-20 19:24:44 +02:00
|
|
|
explicit Heap(VM&);
|
2020-03-08 19:23:58 +01:00
|
|
|
~Heap();
|
|
|
|
|
|
|
|
template<typename T, typename... Args>
|
2022-12-14 17:40:33 +00:00
|
|
|
NonnullGCPtr<T> allocate_without_realm(Args&&... args)
|
2020-03-08 19:23:58 +01:00
|
|
|
{
|
|
|
|
auto* memory = allocate_cell(sizeof(T));
|
|
|
|
new (memory) T(forward<Args>(args)...);
|
2022-12-14 17:40:33 +00:00
|
|
|
return *static_cast<T*>(memory);
|
2020-03-08 19:23:58 +01:00
|
|
|
}
|
|
|
|
|
2020-06-20 15:40:48 +02:00
|
|
|
template<typename T, typename... Args>
|
2023-01-28 13:39:44 -05:00
|
|
|
ThrowCompletionOr<NonnullGCPtr<T>> allocate(Realm& realm, Args&&... args)
|
2020-06-20 15:40:48 +02:00
|
|
|
{
|
|
|
|
auto* memory = allocate_cell(sizeof(T));
|
|
|
|
new (memory) T(forward<Args>(args)...);
|
|
|
|
auto* cell = static_cast<T*>(memory);
|
2023-01-28 13:39:44 -05:00
|
|
|
MUST_OR_THROW_OOM(memory->initialize(realm));
|
2022-12-14 17:40:33 +00:00
|
|
|
return *cell;
|
2020-06-20 15:40:48 +02:00
|
|
|
}
|
|
|
|
|
2020-03-23 14:11:19 +01:00
|
|
|
enum class CollectionType {
|
|
|
|
CollectGarbage,
|
|
|
|
CollectEverything,
|
|
|
|
};
|
|
|
|
|
2020-08-16 20:33:56 +02:00
|
|
|
void collect_garbage(CollectionType = CollectionType::CollectGarbage, bool print_report = false);
|
2020-03-08 19:23:58 +01:00
|
|
|
|
2020-03-16 19:18:46 +01: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; }
|
|
|
|
|
2020-03-18 20:03:17 +01:00
|
|
|
void did_create_handle(Badge<HandleImpl>, HandleImpl&);
|
|
|
|
void did_destroy_handle(Badge<HandleImpl>, HandleImpl&);
|
|
|
|
|
2021-12-16 18:54:06 +01:00
|
|
|
void did_create_marked_vector(Badge<MarkedVectorBase>, MarkedVectorBase&);
|
|
|
|
void did_destroy_marked_vector(Badge<MarkedVectorBase>, MarkedVectorBase&);
|
|
|
|
|
2021-06-12 05:23:33 +03:00
|
|
|
void did_create_weak_container(Badge<WeakContainer>, WeakContainer&);
|
|
|
|
void did_destroy_weak_container(Badge<WeakContainer>, WeakContainer&);
|
2021-06-09 20:10:47 +03:00
|
|
|
|
2020-04-19 11:30:47 +02:00
|
|
|
void defer_gc(Badge<DeferGC>);
|
|
|
|
void undefer_gc(Badge<DeferGC>);
|
|
|
|
|
2021-05-27 19:01:26 +02:00
|
|
|
BlockAllocator& block_allocator() { return m_block_allocator; }
|
|
|
|
|
2023-07-22 06:53:22 +02:00
|
|
|
void uproot_cell(Cell* cell);
|
|
|
|
|
2020-03-08 19:23:58 +01:00
|
|
|
private:
|
2022-10-24 12:37:54 +02:00
|
|
|
static bool cell_must_survive_garbage_collection(Cell const&);
|
|
|
|
|
2020-03-08 19:23:58 +01:00
|
|
|
Cell* allocate_cell(size_t);
|
|
|
|
|
2020-03-15 15:12:34 +01:00
|
|
|
void gather_roots(HashTable<Cell*>&);
|
2020-03-16 19:08:59 +01:00
|
|
|
void gather_conservative_roots(HashTable<Cell*>&);
|
2023-06-30 18:46:12 -06:00
|
|
|
void gather_asan_fake_stack_roots(HashTable<FlatPtr>&, FlatPtr);
|
2022-04-01 20:58:27 +03:00
|
|
|
void mark_live_cells(HashTable<Cell*> const& live_cells);
|
2022-10-20 18:35:19 +02:00
|
|
|
void finalize_unmarked_cells();
|
2022-04-01 20:58:27 +03:00
|
|
|
void sweep_dead_cells(bool print_report, Core::ElapsedTimer const&);
|
2020-03-08 19:23:58 +01:00
|
|
|
|
2021-05-27 19:03:41 +02:00
|
|
|
CellAllocator& allocator_for_size(size_t);
|
2020-10-06 18:50:47 +02:00
|
|
|
|
|
|
|
template<typename Callback>
|
|
|
|
void for_each_block(Callback callback)
|
|
|
|
{
|
|
|
|
for (auto& allocator : m_allocators) {
|
|
|
|
if (allocator->for_each_block(callback) == IterationDecision::Break)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-17 15:21:40 +02:00
|
|
|
size_t m_max_allocations_between_gc { 100000 };
|
2021-03-28 18:22:54 +02:00
|
|
|
size_t m_allocations_since_last_gc { 0 };
|
2020-04-06 12:36:49 +02:00
|
|
|
|
2020-03-16 19:18:46 +01:00
|
|
|
bool m_should_collect_on_every_allocation { false };
|
|
|
|
|
2021-05-27 19:03:41 +02:00
|
|
|
Vector<NonnullOwnPtr<CellAllocator>> m_allocators;
|
2021-07-21 19:45:21 +02:00
|
|
|
|
|
|
|
HandleImpl::List m_handles;
|
2021-12-16 18:54:06 +01:00
|
|
|
MarkedVectorBase::List m_marked_vectors;
|
2021-07-21 19:57:41 +02:00
|
|
|
WeakContainer::List m_weak_containers;
|
2021-06-09 20:10:47 +03:00
|
|
|
|
2023-07-22 06:53:22 +02:00
|
|
|
Vector<GCPtr<Cell>> m_uprooted_cells;
|
|
|
|
|
2021-05-27 19:01:26 +02:00
|
|
|
BlockAllocator m_block_allocator;
|
|
|
|
|
2020-04-19 11:30:47 +02:00
|
|
|
size_t m_gc_deferrals { 0 };
|
|
|
|
bool m_should_gc_when_deferral_ends { false };
|
2020-09-21 14:35:19 +02:00
|
|
|
|
|
|
|
bool m_collecting_garbage { false };
|
2020-03-08 19:23:58 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|