HeapBlock.cpp 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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/Assertions.h>
  27. #include <AK/NonnullOwnPtr.h>
  28. #include <AK/kmalloc.h>
  29. #include <LibJS/HeapBlock.h>
  30. #include <sys/mman.h>
  31. namespace JS {
  32. NonnullOwnPtr<HeapBlock> HeapBlock::create_with_cell_size(Heap& heap, size_t cell_size)
  33. {
  34. auto* block = (HeapBlock*)serenity_mmap(nullptr, block_size, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, 0, 0, block_size, "HeapBlock");
  35. ASSERT(block != MAP_FAILED);
  36. new (block) HeapBlock(heap, cell_size);
  37. return NonnullOwnPtr<HeapBlock>(NonnullOwnPtr<HeapBlock>::Adopt, *block);
  38. }
  39. void HeapBlock::operator delete(void* ptr)
  40. {
  41. int rc = munmap(ptr, block_size);
  42. ASSERT(rc == 0);
  43. }
  44. HeapBlock::HeapBlock(Heap& heap, size_t cell_size)
  45. : m_heap(heap)
  46. , m_cell_size(cell_size)
  47. {
  48. for (size_t i = 0; i < cell_count(); ++i) {
  49. auto* freelist_entry = static_cast<FreelistEntry*>(cell(i));
  50. freelist_entry->set_live(false);
  51. if (i == cell_count() - 1)
  52. freelist_entry->next = nullptr;
  53. else
  54. freelist_entry->next = static_cast<FreelistEntry*>(cell(i + 1));
  55. }
  56. m_freelist = static_cast<FreelistEntry*>(cell(0));
  57. }
  58. Cell* HeapBlock::allocate()
  59. {
  60. if (!m_freelist)
  61. return nullptr;
  62. return exchange(m_freelist, m_freelist->next);
  63. }
  64. void HeapBlock::deallocate(Cell* cell)
  65. {
  66. ASSERT(cell->is_live());
  67. ASSERT(!cell->is_marked());
  68. cell->~Cell();
  69. auto* freelist_entry = static_cast<FreelistEntry*>(cell);
  70. freelist_entry->set_live(false);
  71. freelist_entry->next = m_freelist;
  72. m_freelist = freelist_entry;
  73. }
  74. }