Heap.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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/BlockAllocator.h>
  17. #include <LibJS/Heap/Cell.h>
  18. #include <LibJS/Heap/CellAllocator.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/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(sizeof(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(sizeof(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. void 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. BlockAllocator& block_allocator() { return m_block_allocator; }
  67. void uproot_cell(Cell* cell);
  68. private:
  69. friend class MarkingVisitor;
  70. friend class GraphConstructorVisitor;
  71. friend class DeferGC;
  72. void defer_gc();
  73. void undefer_gc();
  74. static bool cell_must_survive_garbage_collection(Cell const&);
  75. Cell* allocate_cell(size_t);
  76. void find_min_and_max_block_addresses(FlatPtr& min_address, FlatPtr& max_address);
  77. void gather_roots(HashMap<Cell*, HeapRoot>&);
  78. void gather_conservative_roots(HashMap<Cell*, HeapRoot>&);
  79. void gather_asan_fake_stack_roots(HashMap<FlatPtr, HeapRoot>&, FlatPtr, FlatPtr min_block_address, FlatPtr max_block_address);
  80. void mark_live_cells(HashMap<Cell*, HeapRoot> const& live_cells);
  81. void finalize_unmarked_cells();
  82. void sweep_dead_cells(bool print_report, Core::ElapsedTimer const&);
  83. CellAllocator& allocator_for_size(size_t);
  84. template<typename Callback>
  85. void for_each_block(Callback callback)
  86. {
  87. for (auto& allocator : m_allocators) {
  88. if (allocator->for_each_block(callback) == IterationDecision::Break)
  89. return;
  90. }
  91. }
  92. static constexpr size_t GC_MIN_BYTES_THRESHOLD { 4 * 1024 * 1024 };
  93. size_t m_gc_bytes_threshold { GC_MIN_BYTES_THRESHOLD };
  94. size_t m_allocated_bytes_since_last_gc { 0 };
  95. bool m_should_collect_on_every_allocation { false };
  96. Vector<NonnullOwnPtr<CellAllocator>> m_allocators;
  97. HandleImpl::List m_handles;
  98. MarkedVectorBase::List m_marked_vectors;
  99. WeakContainer::List m_weak_containers;
  100. Vector<GCPtr<Cell>> m_uprooted_cells;
  101. BlockAllocator m_block_allocator;
  102. size_t m_gc_deferrals { 0 };
  103. bool m_should_gc_when_deferral_ends { false };
  104. bool m_collecting_garbage { false };
  105. };
  106. inline void Heap::did_create_handle(Badge<HandleImpl>, HandleImpl& impl)
  107. {
  108. VERIFY(!m_handles.contains(impl));
  109. m_handles.append(impl);
  110. }
  111. inline void Heap::did_destroy_handle(Badge<HandleImpl>, HandleImpl& impl)
  112. {
  113. VERIFY(m_handles.contains(impl));
  114. m_handles.remove(impl);
  115. }
  116. inline void Heap::did_create_marked_vector(Badge<MarkedVectorBase>, MarkedVectorBase& vector)
  117. {
  118. VERIFY(!m_marked_vectors.contains(vector));
  119. m_marked_vectors.append(vector);
  120. }
  121. inline void Heap::did_destroy_marked_vector(Badge<MarkedVectorBase>, MarkedVectorBase& vector)
  122. {
  123. VERIFY(m_marked_vectors.contains(vector));
  124. m_marked_vectors.remove(vector);
  125. }
  126. inline void Heap::did_create_weak_container(Badge<WeakContainer>, WeakContainer& set)
  127. {
  128. VERIFY(!m_weak_containers.contains(set));
  129. m_weak_containers.append(set);
  130. }
  131. inline void Heap::did_destroy_weak_container(Badge<WeakContainer>, WeakContainer& set)
  132. {
  133. VERIFY(m_weak_containers.contains(set));
  134. m_weak_containers.remove(set);
  135. }
  136. }