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