Heap.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*
  2. * Copyright (c) 2020-2024, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Badge.h>
  8. #include <AK/HashTable.h>
  9. #include <AK/IntrusiveList.h>
  10. #include <AK/Noncopyable.h>
  11. #include <AK/NonnullOwnPtr.h>
  12. #include <AK/Types.h>
  13. #include <AK/Vector.h>
  14. #include <LibCore/Forward.h>
  15. #include <LibJS/Forward.h>
  16. #include <LibJS/Heap/Cell.h>
  17. #include <LibJS/Heap/CellAllocator.h>
  18. #include <LibJS/Heap/ConservativeVector.h>
  19. #include <LibJS/Heap/Handle.h>
  20. #include <LibJS/Heap/HeapRoot.h>
  21. #include <LibJS/Heap/Internals.h>
  22. #include <LibJS/Heap/MarkedVector.h>
  23. #include <LibJS/Runtime/Completion.h>
  24. #include <LibJS/Runtime/ExecutionContext.h>
  25. #include <LibJS/Runtime/WeakContainer.h>
  26. namespace JS {
  27. class Heap : public HeapBase {
  28. AK_MAKE_NONCOPYABLE(Heap);
  29. AK_MAKE_NONMOVABLE(Heap);
  30. public:
  31. explicit Heap(VM&);
  32. ~Heap();
  33. template<typename T, typename... Args>
  34. NonnullGCPtr<T> allocate_without_realm(Args&&... args)
  35. {
  36. auto* memory = allocate_cell<T>();
  37. defer_gc();
  38. new (memory) T(forward<Args>(args)...);
  39. undefer_gc();
  40. return *static_cast<T*>(memory);
  41. }
  42. template<typename T, typename... Args>
  43. NonnullGCPtr<T> allocate(Realm& realm, Args&&... args)
  44. {
  45. auto* memory = allocate_cell<T>();
  46. defer_gc();
  47. new (memory) T(forward<Args>(args)...);
  48. undefer_gc();
  49. auto* cell = static_cast<T*>(memory);
  50. memory->initialize(realm);
  51. return *cell;
  52. }
  53. enum class CollectionType {
  54. CollectGarbage,
  55. CollectEverything,
  56. };
  57. void collect_garbage(CollectionType = CollectionType::CollectGarbage, bool print_report = false);
  58. AK::JsonObject dump_graph();
  59. bool should_collect_on_every_allocation() const { return m_should_collect_on_every_allocation; }
  60. void set_should_collect_on_every_allocation(bool b) { m_should_collect_on_every_allocation = b; }
  61. void did_create_handle(Badge<HandleImpl>, HandleImpl&);
  62. void did_destroy_handle(Badge<HandleImpl>, HandleImpl&);
  63. void did_create_marked_vector(Badge<MarkedVectorBase>, MarkedVectorBase&);
  64. void did_destroy_marked_vector(Badge<MarkedVectorBase>, MarkedVectorBase&);
  65. void did_create_conservative_vector(Badge<ConservativeVectorBase>, ConservativeVectorBase&);
  66. void did_destroy_conservative_vector(Badge<ConservativeVectorBase>, ConservativeVectorBase&);
  67. void did_create_weak_container(Badge<WeakContainer>, WeakContainer&);
  68. void did_destroy_weak_container(Badge<WeakContainer>, WeakContainer&);
  69. void did_create_execution_context(Badge<ExecutionContext>, ExecutionContext&);
  70. void did_destroy_execution_context(Badge<ExecutionContext>, ExecutionContext&);
  71. void register_cell_allocator(Badge<CellAllocator>, CellAllocator&);
  72. void uproot_cell(Cell* cell);
  73. private:
  74. friend class MarkingVisitor;
  75. friend class GraphConstructorVisitor;
  76. friend class DeferGC;
  77. void defer_gc();
  78. void undefer_gc();
  79. static bool cell_must_survive_garbage_collection(Cell const&);
  80. template<typename T>
  81. Cell* allocate_cell()
  82. {
  83. will_allocate(sizeof(T));
  84. if constexpr (requires { T::cell_allocator.allocator.get().allocate_cell(*this); }) {
  85. if constexpr (IsSame<T, typename decltype(T::cell_allocator)::CellType>) {
  86. return T::cell_allocator.allocator.get().allocate_cell(*this);
  87. }
  88. }
  89. return allocator_for_size(sizeof(T)).allocate_cell(*this);
  90. }
  91. void will_allocate(size_t);
  92. void find_min_and_max_block_addresses(FlatPtr& min_address, FlatPtr& max_address);
  93. void gather_roots(HashMap<Cell*, HeapRoot>&);
  94. void gather_conservative_roots(HashMap<Cell*, HeapRoot>&);
  95. void gather_asan_fake_stack_roots(HashMap<FlatPtr, HeapRoot>&, FlatPtr, FlatPtr min_block_address, FlatPtr max_block_address);
  96. void mark_live_cells(HashMap<Cell*, HeapRoot> const& live_cells);
  97. void finalize_unmarked_cells();
  98. void sweep_dead_cells(bool print_report, Core::ElapsedTimer const&);
  99. ALWAYS_INLINE CellAllocator& allocator_for_size(size_t cell_size)
  100. {
  101. // FIXME: Use binary search?
  102. for (auto& allocator : m_size_based_cell_allocators) {
  103. if (allocator->cell_size() >= cell_size)
  104. return *allocator;
  105. }
  106. dbgln("Cannot get CellAllocator for cell size {}, largest available is {}!", cell_size, m_size_based_cell_allocators.last()->cell_size());
  107. VERIFY_NOT_REACHED();
  108. }
  109. template<typename Callback>
  110. void for_each_block(Callback callback)
  111. {
  112. for (auto& allocator : m_all_cell_allocators) {
  113. if (allocator.for_each_block(callback) == IterationDecision::Break)
  114. return;
  115. }
  116. }
  117. static constexpr size_t GC_MIN_BYTES_THRESHOLD { 4 * 1024 * 1024 };
  118. size_t m_gc_bytes_threshold { GC_MIN_BYTES_THRESHOLD };
  119. size_t m_allocated_bytes_since_last_gc { 0 };
  120. bool m_should_collect_on_every_allocation { false };
  121. Vector<NonnullOwnPtr<CellAllocator>> m_size_based_cell_allocators;
  122. CellAllocator::List m_all_cell_allocators;
  123. HandleImpl::List m_handles;
  124. MarkedVectorBase::List m_marked_vectors;
  125. ConservativeVectorBase::List m_conservative_vectors;
  126. WeakContainer::List m_weak_containers;
  127. Vector<GCPtr<Cell>> m_uprooted_cells;
  128. size_t m_gc_deferrals { 0 };
  129. bool m_should_gc_when_deferral_ends { false };
  130. bool m_collecting_garbage { false };
  131. };
  132. inline void Heap::did_create_handle(Badge<HandleImpl>, HandleImpl& impl)
  133. {
  134. VERIFY(!m_handles.contains(impl));
  135. m_handles.append(impl);
  136. }
  137. inline void Heap::did_destroy_handle(Badge<HandleImpl>, HandleImpl& impl)
  138. {
  139. VERIFY(m_handles.contains(impl));
  140. m_handles.remove(impl);
  141. }
  142. inline void Heap::did_create_marked_vector(Badge<MarkedVectorBase>, MarkedVectorBase& vector)
  143. {
  144. VERIFY(!m_marked_vectors.contains(vector));
  145. m_marked_vectors.append(vector);
  146. }
  147. inline void Heap::did_destroy_marked_vector(Badge<MarkedVectorBase>, MarkedVectorBase& vector)
  148. {
  149. VERIFY(m_marked_vectors.contains(vector));
  150. m_marked_vectors.remove(vector);
  151. }
  152. inline void Heap::did_create_conservative_vector(Badge<ConservativeVectorBase>, ConservativeVectorBase& vector)
  153. {
  154. VERIFY(!m_conservative_vectors.contains(vector));
  155. m_conservative_vectors.append(vector);
  156. }
  157. inline void Heap::did_destroy_conservative_vector(Badge<ConservativeVectorBase>, ConservativeVectorBase& vector)
  158. {
  159. VERIFY(m_conservative_vectors.contains(vector));
  160. m_conservative_vectors.remove(vector);
  161. }
  162. inline void Heap::did_create_weak_container(Badge<WeakContainer>, WeakContainer& set)
  163. {
  164. VERIFY(!m_weak_containers.contains(set));
  165. m_weak_containers.append(set);
  166. }
  167. inline void Heap::did_destroy_weak_container(Badge<WeakContainer>, WeakContainer& set)
  168. {
  169. VERIFY(m_weak_containers.contains(set));
  170. m_weak_containers.remove(set);
  171. }
  172. inline void Heap::register_cell_allocator(Badge<CellAllocator>, CellAllocator& allocator)
  173. {
  174. m_all_cell_allocators.append(allocator);
  175. }
  176. }