2020-03-08 19:23:58 +01:00
|
|
|
/*
|
2023-08-13 13:05:26 +02:00
|
|
|
* Copyright (c) 2020-2023, Andreas Kling <kling@serenityos.org>
|
2020-03-08 19:23:58 +01:00
|
|
|
*
|
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-09-22 02:04:16 +02:00
|
|
|
#include <LibJS/Heap/HeapRoot.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));
|
2023-09-23 13:47:16 +02:00
|
|
|
defer_gc();
|
2020-03-08 19:23:58 +01:00
|
|
|
new (memory) T(forward<Args>(args)...);
|
2023-09-23 13:47:16 +02:00
|
|
|
undefer_gc();
|
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-08-13 13:05:26 +02:00
|
|
|
NonnullGCPtr<T> allocate(Realm& realm, Args&&... args)
|
2020-06-20 15:40:48 +02:00
|
|
|
{
|
|
|
|
auto* memory = allocate_cell(sizeof(T));
|
2023-09-23 13:47:16 +02:00
|
|
|
defer_gc();
|
2020-06-20 15:40:48 +02:00
|
|
|
new (memory) T(forward<Args>(args)...);
|
2023-09-23 13:47:16 +02:00
|
|
|
undefer_gc();
|
2020-06-20 15:40:48 +02:00
|
|
|
auto* cell = static_cast<T*>(memory);
|
2023-08-07 08:41:28 +02:00
|
|
|
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);
|
LibJS: Add GC graph dumper
This change introduces a very basic GC graph dumper. The `dump_graph()`
function outputs JSON data that contains information about all nodes in
the graph, including their class types and edges.
Root nodes will have a property indicating their root type or source
location if the root is captured by a SafeFunction. It would be useful
to add source location for other types of roots in the future.
Output JSON dump have following format:
```json
"4908721208": {
"class_name": "Accessor",
"edges": [
"4909298232",
"4909297976"
]
},
"4907520440": {
"root": "SafeFunction Optional Optional.h:137",
"class_name": "Realm",
"edges": [
"4908269624",
"4924821560",
"4908409240",
"4908483960",
"4924527672"
]
},
"4908251320": {
"class_name": "CSSStyleRule",
"edges": [
"4908302648",
"4925101656",
"4908251192"
]
},
```
2023-08-17 15:34:19 +02:00
|
|
|
void dump_graph();
|
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
|
|
|
|
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:
|
2023-08-18 18:21:33 +02:00
|
|
|
friend class MarkingVisitor;
|
|
|
|
friend class GraphConstructorVisitor;
|
2023-09-23 13:44:13 +02:00
|
|
|
friend class DeferGC;
|
|
|
|
|
|
|
|
void defer_gc();
|
|
|
|
void undefer_gc();
|
2023-08-18 18:21:33 +02:00
|
|
|
|
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);
|
|
|
|
|
2023-09-29 19:14:11 +02:00
|
|
|
void find_min_and_max_block_addresses(FlatPtr& min_address, FlatPtr& max_address);
|
2023-09-22 02:04:16 +02:00
|
|
|
void gather_roots(HashMap<Cell*, HeapRoot>&);
|
|
|
|
void gather_conservative_roots(HashMap<Cell*, HeapRoot>&);
|
2023-09-29 19:14:11 +02:00
|
|
|
void gather_asan_fake_stack_roots(HashMap<FlatPtr, HeapRoot>&, FlatPtr, FlatPtr min_block_address, FlatPtr max_block_address);
|
2023-09-22 02:04:16 +02:00
|
|
|
void mark_live_cells(HashMap<Cell*, HeapRoot> 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-08 19:50:32 +02: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 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
|
|
|
};
|
|
|
|
|
2023-10-07 09:15:09 +02:00
|
|
|
inline void Heap::did_create_handle(Badge<HandleImpl>, HandleImpl& impl)
|
|
|
|
{
|
|
|
|
VERIFY(!m_handles.contains(impl));
|
|
|
|
m_handles.append(impl);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void Heap::did_destroy_handle(Badge<HandleImpl>, HandleImpl& impl)
|
|
|
|
{
|
|
|
|
VERIFY(m_handles.contains(impl));
|
|
|
|
m_handles.remove(impl);
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2020-03-08 19:23:58 +01:00
|
|
|
}
|