Heap.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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/HashTable.h>
  8. #include <AK/Noncopyable.h>
  9. #include <AK/NonnullOwnPtr.h>
  10. #include <AK/Types.h>
  11. #include <AK/Vector.h>
  12. #include <LibCore/Forward.h>
  13. #include <LibJS/Forward.h>
  14. #include <LibJS/Heap/BlockAllocator.h>
  15. #include <LibJS/Heap/Cell.h>
  16. #include <LibJS/Heap/CellAllocator.h>
  17. #include <LibJS/Heap/Handle.h>
  18. #include <LibJS/Runtime/Object.h>
  19. namespace JS {
  20. class Heap {
  21. AK_MAKE_NONCOPYABLE(Heap);
  22. AK_MAKE_NONMOVABLE(Heap);
  23. public:
  24. explicit Heap(VM&);
  25. ~Heap();
  26. template<typename T, typename... Args>
  27. T* allocate_without_global_object(Args&&... args)
  28. {
  29. auto* memory = allocate_cell(sizeof(T));
  30. new (memory) T(forward<Args>(args)...);
  31. return static_cast<T*>(memory);
  32. }
  33. template<typename T, typename... Args>
  34. T* allocate(GlobalObject& global_object, Args&&... args)
  35. {
  36. auto* memory = allocate_cell(sizeof(T));
  37. new (memory) T(forward<Args>(args)...);
  38. auto* cell = static_cast<T*>(memory);
  39. constexpr bool is_object = IsBaseOf<Object, T>;
  40. if constexpr (is_object)
  41. static_cast<Object*>(cell)->disable_transitions();
  42. cell->initialize(global_object);
  43. if constexpr (is_object)
  44. static_cast<Object*>(cell)->enable_transitions();
  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. VM& vm() { return m_vm; }
  53. bool should_collect_on_every_allocation() const { return m_should_collect_on_every_allocation; }
  54. void set_should_collect_on_every_allocation(bool b) { m_should_collect_on_every_allocation = b; }
  55. void did_create_handle(Badge<HandleImpl>, HandleImpl&);
  56. void did_destroy_handle(Badge<HandleImpl>, HandleImpl&);
  57. void did_create_marked_value_list(Badge<MarkedValueList>, MarkedValueList&);
  58. void did_destroy_marked_value_list(Badge<MarkedValueList>, MarkedValueList&);
  59. void did_create_weak_container(Badge<WeakContainer>, WeakContainer&);
  60. void did_destroy_weak_container(Badge<WeakContainer>, WeakContainer&);
  61. void defer_gc(Badge<DeferGC>);
  62. void undefer_gc(Badge<DeferGC>);
  63. BlockAllocator& block_allocator() { return m_block_allocator; }
  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(const HashTable<Cell*>& live_cells);
  69. void sweep_dead_cells(bool print_report, const Core::ElapsedTimer&);
  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 { 10000 };
  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. HashTable<HandleImpl*> m_handles;
  85. HashTable<MarkedValueList*> m_marked_value_lists;
  86. HashTable<WeakContainer*> m_weak_containers;
  87. BlockAllocator m_block_allocator;
  88. size_t m_gc_deferrals { 0 };
  89. bool m_should_gc_when_deferral_ends { false };
  90. bool m_collecting_garbage { false };
  91. };
  92. }