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>
|
2020-10-05 20:08:14 +02:00
|
|
|
#include <LibJS/Runtime/Object.h>
|
2021-07-21 19:57:41 +02:00
|
|
|
#include <LibJS/Runtime/WeakContainer.h>
|
2020-03-08 19:23:58 +01:00
|
|
|
|
|
|
|
namespace JS {
|
|
|
|
|
|
|
|
class Heap {
|
|
|
|
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>
|
2020-06-20 15:40:48 +02:00
|
|
|
T* allocate_without_global_object(Args&&... args)
|
2020-03-08 19:23:58 +01:00
|
|
|
{
|
|
|
|
auto* memory = allocate_cell(sizeof(T));
|
|
|
|
new (memory) T(forward<Args>(args)...);
|
|
|
|
return static_cast<T*>(memory);
|
|
|
|
}
|
|
|
|
|
2020-06-20 15:40:48 +02:00
|
|
|
template<typename T, typename... Args>
|
|
|
|
T* allocate(GlobalObject& global_object, Args&&... args)
|
|
|
|
{
|
|
|
|
auto* memory = allocate_cell(sizeof(T));
|
|
|
|
new (memory) T(forward<Args>(args)...);
|
|
|
|
auto* cell = static_cast<T*>(memory);
|
2020-07-22 17:50:18 +02:00
|
|
|
cell->initialize(global_object);
|
2020-06-20 15:40:48 +02:00
|
|
|
return cell;
|
|
|
|
}
|
|
|
|
|
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-09-20 19:24:44 +02:00
|
|
|
VM& vm() { return m_vm; }
|
2020-03-15 15:08:27 +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; }
|
|
|
|
|
2021-10-02 16:35:55 +02:00
|
|
|
#ifdef JS_TRACK_ZOMBIE_CELLS
|
|
|
|
void set_zombify_dead_cells(bool b)
|
|
|
|
{
|
|
|
|
m_zombify_dead_cells = b;
|
|
|
|
}
|
|
|
|
#endif
|
2021-09-11 16:44:40 +02:00
|
|
|
|
2020-03-18 20:03:17 +01:00
|
|
|
void did_create_handle(Badge<HandleImpl>, HandleImpl&);
|
|
|
|
void did_destroy_handle(Badge<HandleImpl>, HandleImpl&);
|
|
|
|
|
2020-04-19 17:24:56 +02:00
|
|
|
void did_create_marked_value_list(Badge<MarkedValueList>, MarkedValueList&);
|
|
|
|
void did_destroy_marked_value_list(Badge<MarkedValueList>, MarkedValueList&);
|
|
|
|
|
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; }
|
|
|
|
|
2021-09-07 17:14:05 +02:00
|
|
|
void uproot_cell(Cell* cell);
|
|
|
|
|
2020-03-08 19:23:58 +01:00
|
|
|
private:
|
|
|
|
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*>&);
|
2020-03-08 19:23:58 +01:00
|
|
|
void mark_live_cells(const HashTable<Cell*>& live_cells);
|
2020-08-16 20:33:56 +02:00
|
|
|
void sweep_dead_cells(bool print_report, const Core::ElapsedTimer&);
|
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 };
|
|
|
|
|
2020-09-20 19:24:44 +02:00
|
|
|
VM& m_vm;
|
2020-10-06 18:50:47 +02:00
|
|
|
|
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;
|
2020-04-19 11:30:47 +02:00
|
|
|
|
2021-07-21 19:51:19 +02:00
|
|
|
MarkedValueList::List m_marked_value_lists;
|
2020-04-19 17:24:56 +02:00
|
|
|
|
2021-07-21 19:57:41 +02:00
|
|
|
WeakContainer::List m_weak_containers;
|
2021-06-09 20:10:47 +03:00
|
|
|
|
2021-09-07 17:14:05 +02:00
|
|
|
Vector<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 };
|
2021-10-02 16:35:55 +02:00
|
|
|
|
|
|
|
#ifdef JS_TRACK_ZOMBIE_CELLS
|
2021-09-11 16:44:40 +02:00
|
|
|
bool m_zombify_dead_cells { false };
|
2021-10-02 16:35:55 +02:00
|
|
|
#endif
|
2020-03-08 19:23:58 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|