Heap.h 3.5 KB

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