Heap.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #pragma once
  27. #include <AK/HashTable.h>
  28. #include <AK/Noncopyable.h>
  29. #include <AK/NonnullOwnPtr.h>
  30. #include <AK/Types.h>
  31. #include <AK/Vector.h>
  32. #include <LibCore/Forward.h>
  33. #include <LibJS/Forward.h>
  34. #include <LibJS/Heap/Allocator.h>
  35. #include <LibJS/Heap/Handle.h>
  36. #include <LibJS/Runtime/Cell.h>
  37. #include <LibJS/Runtime/Object.h>
  38. namespace JS {
  39. class Heap {
  40. AK_MAKE_NONCOPYABLE(Heap);
  41. AK_MAKE_NONMOVABLE(Heap);
  42. public:
  43. explicit Heap(VM&);
  44. ~Heap();
  45. template<typename T, typename... Args>
  46. T* allocate_without_global_object(Args&&... args)
  47. {
  48. auto* memory = allocate_cell(sizeof(T));
  49. new (memory) T(forward<Args>(args)...);
  50. return static_cast<T*>(memory);
  51. }
  52. template<typename T, typename... Args>
  53. T* allocate(GlobalObject& global_object, Args&&... args)
  54. {
  55. auto* memory = allocate_cell(sizeof(T));
  56. new (memory) T(forward<Args>(args)...);
  57. auto* cell = static_cast<T*>(memory);
  58. constexpr bool is_object = IsBaseOf<Object, T>::value;
  59. if constexpr (is_object)
  60. static_cast<Object*>(cell)->disable_transitions();
  61. cell->initialize(global_object);
  62. if constexpr (is_object)
  63. static_cast<Object*>(cell)->enable_transitions();
  64. return cell;
  65. }
  66. enum class CollectionType {
  67. CollectGarbage,
  68. CollectEverything,
  69. };
  70. void collect_garbage(CollectionType = CollectionType::CollectGarbage, bool print_report = false);
  71. VM& vm() { return m_vm; }
  72. bool should_collect_on_every_allocation() const { return m_should_collect_on_every_allocation; }
  73. void set_should_collect_on_every_allocation(bool b) { m_should_collect_on_every_allocation = b; }
  74. void did_create_handle(Badge<HandleImpl>, HandleImpl&);
  75. void did_destroy_handle(Badge<HandleImpl>, HandleImpl&);
  76. void did_create_marked_value_list(Badge<MarkedValueList>, MarkedValueList&);
  77. void did_destroy_marked_value_list(Badge<MarkedValueList>, MarkedValueList&);
  78. void defer_gc(Badge<DeferGC>);
  79. void undefer_gc(Badge<DeferGC>);
  80. private:
  81. Cell* allocate_cell(size_t);
  82. void gather_roots(HashTable<Cell*>&);
  83. void gather_conservative_roots(HashTable<Cell*>&);
  84. void mark_live_cells(const HashTable<Cell*>& live_cells);
  85. void sweep_dead_cells(bool print_report, const Core::ElapsedTimer&);
  86. Cell* cell_from_possible_pointer(FlatPtr);
  87. Allocator& allocator_for_size(size_t);
  88. template<typename Callback>
  89. void for_each_block(Callback callback)
  90. {
  91. for (auto& allocator : m_allocators) {
  92. if (allocator->for_each_block(callback) == IterationDecision::Break)
  93. return;
  94. }
  95. }
  96. size_t m_max_allocations_between_gc { 10000 };
  97. size_t m_allocations_since_last_gc { false };
  98. bool m_should_collect_on_every_allocation { false };
  99. VM& m_vm;
  100. Vector<NonnullOwnPtr<Allocator>> m_allocators;
  101. HashTable<HandleImpl*> m_handles;
  102. HashTable<MarkedValueList*> m_marked_value_lists;
  103. size_t m_gc_deferrals { 0 };
  104. bool m_should_gc_when_deferral_ends { false };
  105. bool m_collecting_garbage { false };
  106. };
  107. }