Heap.h 6.2 KB

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