Heap.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. #include <AK/Badge.h>
  27. #include <AK/HashTable.h>
  28. #include <LibJS/Heap.h>
  29. #include <LibJS/HeapBlock.h>
  30. #include <LibJS/Interpreter.h>
  31. #include <LibJS/Object.h>
  32. #define HEAP_DEBUG
  33. namespace JS {
  34. Heap::Heap(Interpreter& interpreter)
  35. : m_interpreter(interpreter)
  36. {
  37. }
  38. Heap::~Heap()
  39. {
  40. }
  41. Cell* Heap::allocate_cell(size_t size)
  42. {
  43. for (auto& block : m_blocks) {
  44. if (size > block->cell_size())
  45. continue;
  46. if (auto* cell = block->allocate())
  47. return cell;
  48. }
  49. auto* block = (HeapBlock*)malloc(HeapBlock::block_size);
  50. new (block) HeapBlock(size);
  51. m_blocks.append(NonnullOwnPtr<HeapBlock>(NonnullOwnPtr<HeapBlock>::Adopt, *block));
  52. return block->allocate();
  53. }
  54. void Heap::collect_garbage()
  55. {
  56. HashTable<Cell*> roots;
  57. collect_roots(roots);
  58. HashTable<Cell*> live_cells;
  59. visit_live_cells(roots, live_cells);
  60. mark_live_cells(live_cells);
  61. sweep_dead_cells();
  62. }
  63. void Heap::collect_roots(HashTable<Cell*>& roots)
  64. {
  65. m_interpreter.collect_roots({}, roots);
  66. #ifdef HEAP_DEBUG
  67. dbg() << "collect_roots:";
  68. for (auto* root : roots) {
  69. dbg() << " + " << root;
  70. }
  71. #endif
  72. }
  73. class LivenessVisitor final : public Cell::Visitor {
  74. public:
  75. LivenessVisitor(HashTable<Cell*>& live_cells)
  76. : m_live_cells(live_cells)
  77. {
  78. }
  79. virtual void did_visit(Cell* cell) override
  80. {
  81. m_live_cells.set(cell);
  82. }
  83. private:
  84. HashTable<Cell*>& m_live_cells;
  85. };
  86. void Heap::visit_live_cells(const HashTable<Cell*>& roots, HashTable<Cell*>& live_cells)
  87. {
  88. LivenessVisitor visitor(live_cells);
  89. for (auto* root : roots) {
  90. root->visit_graph(visitor);
  91. }
  92. #ifdef HEAP_DEBUG
  93. dbg() << "visit_live_cells:";
  94. for (auto* cell : live_cells) {
  95. dbg() << " @ " << cell;
  96. }
  97. #endif
  98. }
  99. void Heap::mark_live_cells(const HashTable<Cell*>& live_cells)
  100. {
  101. #ifdef HEAP_DEBUG
  102. dbg() << "mark_live_cells:";
  103. #endif
  104. for (auto& cell : live_cells) {
  105. #ifdef HEAP_DEBUG
  106. dbg() << " ! " << cell;
  107. #endif
  108. cell->set_marked(true);
  109. }
  110. }
  111. void Heap::sweep_dead_cells()
  112. {
  113. #ifdef HEAP_DEBUG
  114. dbg() << "sweep_dead_cells:";
  115. #endif
  116. for (auto& block : m_blocks) {
  117. block->for_each_cell([&](Cell* cell) {
  118. if (cell->is_live()) {
  119. if (!cell->is_marked()) {
  120. #ifdef HEAP_DEBUG
  121. dbg() << " ~ " << cell;
  122. #endif
  123. block->deallocate(cell);
  124. } else {
  125. cell->set_marked(false);
  126. }
  127. }
  128. });
  129. }
  130. }
  131. }