Heap.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 <LibJS/Forward.h>
  33. #include <LibJS/Heap/Handle.h>
  34. #include <LibJS/Runtime/Cell.h>
  35. namespace JS {
  36. class Heap {
  37. AK_MAKE_NONCOPYABLE(Heap);
  38. AK_MAKE_NONMOVABLE(Heap);
  39. public:
  40. explicit Heap(Interpreter&);
  41. ~Heap();
  42. template<typename T, typename... Args>
  43. T* allocate(Args&&... args)
  44. {
  45. auto* memory = allocate_cell(sizeof(T));
  46. new (memory) T(forward<Args>(args)...);
  47. return static_cast<T*>(memory);
  48. }
  49. enum class CollectionType {
  50. CollectGarbage,
  51. CollectEverything,
  52. };
  53. void collect_garbage(CollectionType = CollectionType::CollectGarbage);
  54. Interpreter& interpreter() { return m_interpreter; }
  55. bool should_collect_on_every_allocation() const { return m_should_collect_on_every_allocation; }
  56. void set_should_collect_on_every_allocation(bool b) { m_should_collect_on_every_allocation = b; }
  57. void did_create_handle(Badge<HandleImpl>, HandleImpl&);
  58. void did_destroy_handle(Badge<HandleImpl>, HandleImpl&);
  59. void did_create_marked_value_list(Badge<MarkedValueList>, MarkedValueList&);
  60. void did_destroy_marked_value_list(Badge<MarkedValueList>, MarkedValueList&);
  61. void defer_gc(Badge<DeferGC>);
  62. void undefer_gc(Badge<DeferGC>);
  63. private:
  64. Cell* allocate_cell(size_t);
  65. void gather_roots(HashTable<Cell*>&);
  66. void gather_conservative_roots(HashTable<Cell*>&);
  67. void mark_live_cells(const HashTable<Cell*>& live_cells);
  68. void sweep_dead_cells();
  69. Cell* cell_from_possible_pointer(FlatPtr);
  70. size_t m_max_allocations_between_gc { 10000 };
  71. size_t m_allocations_since_last_gc { false };
  72. bool m_should_collect_on_every_allocation { false };
  73. Interpreter& m_interpreter;
  74. Vector<NonnullOwnPtr<HeapBlock>> m_blocks;
  75. HashTable<HandleImpl*> m_handles;
  76. HashTable<MarkedValueList*> m_marked_value_lists;
  77. size_t m_gc_deferrals { 0 };
  78. bool m_should_gc_when_deferral_ends { false };
  79. };
  80. }