Heap.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. mark_live_cells(roots);
  59. sweep_dead_cells();
  60. }
  61. void Heap::collect_roots(HashTable<Cell*>& roots)
  62. {
  63. m_interpreter.collect_roots({}, roots);
  64. #ifdef HEAP_DEBUG
  65. dbg() << "collect_roots:";
  66. for (auto* root : roots) {
  67. dbg() << " + " << root;
  68. }
  69. #endif
  70. }
  71. class MarkingVisitor final : public Cell::Visitor {
  72. public:
  73. MarkingVisitor() {}
  74. virtual void visit(Cell* cell)
  75. {
  76. if (cell->is_marked())
  77. return;
  78. #ifdef HEAP_DEBUG
  79. dbg() << " ! " << cell;
  80. #endif
  81. cell->set_marked(true);
  82. cell->visit_children(*this);
  83. }
  84. };
  85. void Heap::mark_live_cells(const HashTable<Cell*>& roots)
  86. {
  87. #ifdef HEAP_DEBUG
  88. dbg() << "mark_live_cells:";
  89. #endif
  90. MarkingVisitor visitor;
  91. for (auto* root : roots)
  92. visitor.visit(root);
  93. }
  94. void Heap::sweep_dead_cells()
  95. {
  96. #ifdef HEAP_DEBUG
  97. dbg() << "sweep_dead_cells:";
  98. #endif
  99. for (auto& block : m_blocks) {
  100. block->for_each_cell([&](Cell* cell) {
  101. if (cell->is_live()) {
  102. if (!cell->is_marked()) {
  103. #ifdef HEAP_DEBUG
  104. dbg() << " ~ " << cell;
  105. #endif
  106. block->deallocate(cell);
  107. } else {
  108. cell->set_marked(false);
  109. }
  110. }
  111. });
  112. }
  113. }
  114. }