Heap.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * Copyright (c) 2020, 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/Internals.h>
  21. #include <LibJS/Heap/MarkedVector.h>
  22. #include <LibJS/Runtime/Completion.h>
  23. #include <LibJS/Runtime/WeakContainer.h>
  24. namespace JS {
  25. class Heap : public HeapBase {
  26. AK_MAKE_NONCOPYABLE(Heap);
  27. AK_MAKE_NONMOVABLE(Heap);
  28. public:
  29. explicit Heap(VM&);
  30. ~Heap();
  31. template<typename T, typename... Args>
  32. NonnullGCPtr<T> allocate_without_realm(Args&&... args)
  33. {
  34. auto* memory = allocate_cell(sizeof(T));
  35. new (memory) T(forward<Args>(args)...);
  36. return *static_cast<T*>(memory);
  37. }
  38. template<typename T, typename... Args>
  39. ThrowCompletionOr<NonnullGCPtr<T>> allocate(Realm& realm, Args&&... args)
  40. {
  41. auto* memory = allocate_cell(sizeof(T));
  42. new (memory) T(forward<Args>(args)...);
  43. auto* cell = static_cast<T*>(memory);
  44. MUST_OR_THROW_OOM(memory->initialize(realm));
  45. return *cell;
  46. }
  47. enum class CollectionType {
  48. CollectGarbage,
  49. CollectEverything,
  50. };
  51. void collect_garbage(CollectionType = CollectionType::CollectGarbage, bool print_report = false);
  52. bool should_collect_on_every_allocation() const { return m_should_collect_on_every_allocation; }
  53. void set_should_collect_on_every_allocation(bool b) { m_should_collect_on_every_allocation = b; }
  54. void did_create_handle(Badge<HandleImpl>, HandleImpl&);
  55. void did_destroy_handle(Badge<HandleImpl>, HandleImpl&);
  56. void did_create_marked_vector(Badge<MarkedVectorBase>, MarkedVectorBase&);
  57. void did_destroy_marked_vector(Badge<MarkedVectorBase>, MarkedVectorBase&);
  58. void did_create_weak_container(Badge<WeakContainer>, WeakContainer&);
  59. void did_destroy_weak_container(Badge<WeakContainer>, WeakContainer&);
  60. void defer_gc(Badge<DeferGC>);
  61. void undefer_gc(Badge<DeferGC>);
  62. BlockAllocator& block_allocator() { return m_block_allocator; }
  63. void uproot_cell(Cell* cell);
  64. private:
  65. static bool cell_must_survive_garbage_collection(Cell const&);
  66. Cell* allocate_cell(size_t);
  67. void gather_roots(HashTable<Cell*>&);
  68. void gather_conservative_roots(HashTable<Cell*>&);
  69. void gather_asan_fake_stack_roots(HashTable<FlatPtr>&, FlatPtr);
  70. void mark_live_cells(HashTable<Cell*> const& live_cells);
  71. void finalize_unmarked_cells();
  72. void sweep_dead_cells(bool print_report, Core::ElapsedTimer const&);
  73. CellAllocator& allocator_for_size(size_t);
  74. template<typename Callback>
  75. void for_each_block(Callback callback)
  76. {
  77. for (auto& allocator : m_allocators) {
  78. if (allocator->for_each_block(callback) == IterationDecision::Break)
  79. return;
  80. }
  81. }
  82. size_t m_max_allocations_between_gc { 100000 };
  83. size_t m_allocations_since_last_gc { 0 };
  84. bool m_should_collect_on_every_allocation { false };
  85. Vector<NonnullOwnPtr<CellAllocator>> m_allocators;
  86. HandleImpl::List m_handles;
  87. MarkedVectorBase::List m_marked_vectors;
  88. WeakContainer::List m_weak_containers;
  89. Vector<GCPtr<Cell>> m_uprooted_cells;
  90. BlockAllocator m_block_allocator;
  91. size_t m_gc_deferrals { 0 };
  92. bool m_should_gc_when_deferral_ends { false };
  93. bool m_collecting_garbage { false };
  94. };
  95. }