Heap.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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/Heap.h>
  29. #include <LibJS/Heap/HeapBlock.h>
  30. #include <LibJS/Interpreter.h>
  31. #include <LibJS/Runtime/Object.h>
  32. #include <serenity.h>
  33. #include <setjmp.h>
  34. #include <stdio.h>
  35. #define HEAP_DEBUG
  36. namespace JS {
  37. Heap::Heap(Interpreter& interpreter)
  38. : m_interpreter(interpreter)
  39. {
  40. }
  41. Heap::~Heap()
  42. {
  43. }
  44. Cell* Heap::allocate_cell(size_t size)
  45. {
  46. if (should_collect_on_every_allocation())
  47. collect_garbage();
  48. for (auto& block : m_blocks) {
  49. if (size > block->cell_size())
  50. continue;
  51. if (auto* cell = block->allocate())
  52. return cell;
  53. }
  54. auto block = HeapBlock::create_with_cell_size(*this, size);
  55. auto* cell = block->allocate();
  56. m_blocks.append(move(block));
  57. return cell;
  58. }
  59. void Heap::collect_garbage()
  60. {
  61. HashTable<Cell*> roots;
  62. gather_roots(roots);
  63. mark_live_cells(roots);
  64. sweep_dead_cells();
  65. }
  66. void Heap::gather_roots(HashTable<Cell*>& roots)
  67. {
  68. m_interpreter.gather_roots({}, roots);
  69. gather_conservative_roots(roots);
  70. #ifdef HEAP_DEBUG
  71. dbg() << "gather_roots:";
  72. for (auto* root : roots) {
  73. dbg() << " + " << root;
  74. }
  75. #endif
  76. }
  77. void Heap::gather_conservative_roots(HashTable<Cell*>& roots)
  78. {
  79. FlatPtr dummy;
  80. #ifdef HEAP_DEBUG
  81. dbg() << "gather_conservative_roots:";
  82. #endif
  83. jmp_buf buf;
  84. setjmp(buf);
  85. HashTable<FlatPtr> possible_pointers;
  86. for (size_t i = 0; i < (sizeof(buf->regs) / sizeof(FlatPtr)); ++i)
  87. possible_pointers.set(buf->regs[i]);
  88. FlatPtr stack_base;
  89. size_t stack_size;
  90. if (get_stack_bounds(&stack_base, &stack_size) < 0) {
  91. perror("get_stack_bounds");
  92. ASSERT_NOT_REACHED();
  93. }
  94. FlatPtr stack_reference = reinterpret_cast<FlatPtr>(&dummy);
  95. FlatPtr stack_top = stack_base + stack_size;
  96. for (FlatPtr stack_address = stack_reference; stack_address < stack_top; stack_address += sizeof(FlatPtr)) {
  97. auto data = *reinterpret_cast<FlatPtr*>(stack_address);
  98. possible_pointers.set(data);
  99. }
  100. for (auto possible_pointer : possible_pointers) {
  101. if (!possible_pointer)
  102. continue;
  103. #ifdef HEAP_DEBUG
  104. dbg() << " ? " << (const void*)possible_pointer;
  105. #endif
  106. if (auto* cell = cell_from_possible_pointer(possible_pointer)) {
  107. if (cell->is_live()) {
  108. #ifdef HEAP_DEBUG
  109. dbg() << " ?-> " << (const void*)cell;
  110. #endif
  111. roots.set(cell);
  112. } else {
  113. #ifdef HEAP_DEBUG
  114. dbg() << " #-> " << (const void*)cell;
  115. #endif
  116. }
  117. }
  118. }
  119. }
  120. Cell* Heap::cell_from_possible_pointer(FlatPtr pointer)
  121. {
  122. auto* possible_heap_block = HeapBlock::from_cell(reinterpret_cast<const Cell*>(pointer));
  123. if (m_blocks.find([possible_heap_block](auto& block) { return block.ptr() == possible_heap_block; }) == m_blocks.end())
  124. return nullptr;
  125. return possible_heap_block->cell_from_possible_pointer(pointer);
  126. }
  127. class MarkingVisitor final : public Cell::Visitor {
  128. public:
  129. MarkingVisitor() {}
  130. virtual void visit(Cell* cell)
  131. {
  132. if (cell->is_marked())
  133. return;
  134. #ifdef HEAP_DEBUG
  135. dbg() << " ! " << cell;
  136. #endif
  137. cell->set_marked(true);
  138. cell->visit_children(*this);
  139. }
  140. };
  141. void Heap::mark_live_cells(const HashTable<Cell*>& roots)
  142. {
  143. #ifdef HEAP_DEBUG
  144. dbg() << "mark_live_cells:";
  145. #endif
  146. MarkingVisitor visitor;
  147. for (auto* root : roots) {
  148. if (!root)
  149. continue;
  150. visitor.visit(root);
  151. }
  152. }
  153. void Heap::sweep_dead_cells()
  154. {
  155. #ifdef HEAP_DEBUG
  156. dbg() << "sweep_dead_cells:";
  157. #endif
  158. for (auto& block : m_blocks) {
  159. block->for_each_cell([&](Cell* cell) {
  160. if (cell->is_live()) {
  161. if (!cell->is_marked()) {
  162. #ifdef HEAP_DEBUG
  163. dbg() << " ~ " << cell;
  164. #endif
  165. block->deallocate(cell);
  166. } else {
  167. cell->set_marked(false);
  168. }
  169. }
  170. });
  171. }
  172. }
  173. }