HeapBlock.cpp 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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/Heap/HeapBlock.h>
  30. #include <stdio.h>
  31. #include <sys/mman.h>
  32. namespace JS {
  33. NonnullOwnPtr<HeapBlock> HeapBlock::create_with_cell_size(Heap& heap, size_t cell_size)
  34. {
  35. char name[64];
  36. snprintf(name, sizeof(name), "LibJS: HeapBlock(%zu)", cell_size);
  37. #ifdef __serenity__
  38. auto* block = (HeapBlock*)serenity_mmap(nullptr, block_size, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, 0, 0, block_size, name);
  39. #else
  40. auto* block = (HeapBlock*)aligned_alloc(block_size, block_size);
  41. #endif
  42. ASSERT(block != MAP_FAILED);
  43. new (block) HeapBlock(heap, cell_size);
  44. return NonnullOwnPtr<HeapBlock>(NonnullOwnPtr<HeapBlock>::Adopt, *block);
  45. }
  46. void HeapBlock::operator delete(void* ptr)
  47. {
  48. #ifdef __serenity__
  49. int rc = munmap(ptr, block_size);
  50. ASSERT(rc == 0);
  51. #else
  52. free(ptr);
  53. #endif
  54. }
  55. HeapBlock::HeapBlock(Heap& heap, size_t cell_size)
  56. : m_heap(heap)
  57. , m_cell_size(cell_size)
  58. {
  59. ASSERT(cell_size >= sizeof(FreelistEntry));
  60. FreelistEntry* next = nullptr;
  61. for (ssize_t i = cell_count() - 1; i >= 0; i--) {
  62. auto* freelist_entry = init_freelist_entry(i);
  63. freelist_entry->set_live(false);
  64. freelist_entry->next = next;
  65. next = freelist_entry;
  66. }
  67. m_freelist = next;
  68. }
  69. Cell* HeapBlock::allocate()
  70. {
  71. if (!m_freelist)
  72. return nullptr;
  73. return exchange(m_freelist, m_freelist->next);
  74. }
  75. void HeapBlock::deallocate(Cell* cell)
  76. {
  77. ASSERT(cell->is_live());
  78. ASSERT(!cell->is_marked());
  79. cell->~Cell();
  80. auto* freelist_entry = new (cell) FreelistEntry();
  81. freelist_entry->set_live(false);
  82. freelist_entry->next = m_freelist;
  83. m_freelist = freelist_entry;
  84. }
  85. }