Heap.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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/Object.h>
  22. #include <LibJS/Runtime/WeakContainer.h>
  23. namespace JS {
  24. class Heap {
  25. AK_MAKE_NONCOPYABLE(Heap);
  26. AK_MAKE_NONMOVABLE(Heap);
  27. public:
  28. explicit Heap(VM&);
  29. ~Heap();
  30. template<typename T, typename... Args>
  31. T* allocate_without_global_object(Args&&... args)
  32. {
  33. auto* memory = allocate_cell(sizeof(T));
  34. new (memory) T(forward<Args>(args)...);
  35. return static_cast<T*>(memory);
  36. }
  37. template<typename T, typename... Args>
  38. T* allocate(GlobalObject& global_object, Args&&... args)
  39. {
  40. auto* memory = allocate_cell(sizeof(T));
  41. new (memory) T(forward<Args>(args)...);
  42. auto* cell = static_cast<T*>(memory);
  43. cell->initialize(global_object);
  44. return cell;
  45. }
  46. enum class CollectionType {
  47. CollectGarbage,
  48. CollectEverything,
  49. };
  50. void collect_garbage(CollectionType = CollectionType::CollectGarbage, bool print_report = false);
  51. VM& vm() { return m_vm; }
  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. 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 sweep_dead_cells(bool print_report, Core::ElapsedTimer const&);
  70. CellAllocator& allocator_for_size(size_t);
  71. template<typename Callback>
  72. void for_each_block(Callback callback)
  73. {
  74. for (auto& allocator : m_allocators) {
  75. if (allocator->for_each_block(callback) == IterationDecision::Break)
  76. return;
  77. }
  78. }
  79. size_t m_max_allocations_between_gc { 100000 };
  80. size_t m_allocations_since_last_gc { 0 };
  81. bool m_should_collect_on_every_allocation { false };
  82. VM& m_vm;
  83. Vector<NonnullOwnPtr<CellAllocator>> m_allocators;
  84. HandleImpl::List m_handles;
  85. MarkedVectorBase::List m_marked_vectors;
  86. WeakContainer::List m_weak_containers;
  87. Vector<Cell*> m_uprooted_cells;
  88. BlockAllocator m_block_allocator;
  89. size_t m_gc_deferrals { 0 };
  90. bool m_should_gc_when_deferral_ends { false };
  91. bool m_collecting_garbage { false };
  92. };
  93. }