mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 20:17:44 +00:00
LibJS: Add "Heap" and "Runtime" subdirectories
Let's try to keep LibJS tidy as it expands. :^)
This commit is contained in:
parent
6780d70fb1
commit
19452230cd
39 changed files with 99 additions and 98 deletions
128
Libraries/LibJS/Heap/Heap.cpp
Normal file
128
Libraries/LibJS/Heap/Heap.cpp
Normal file
|
@ -0,0 +1,128 @@
|
|||
/*
|
||||
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <AK/Badge.h>
|
||||
#include <AK/HashTable.h>
|
||||
#include <LibJS/Heap/Heap.h>
|
||||
#include <LibJS/Heap/HeapBlock.h>
|
||||
#include <LibJS/Interpreter.h>
|
||||
#include <LibJS/Runtime/Object.h>
|
||||
|
||||
#define HEAP_DEBUG
|
||||
|
||||
namespace JS {
|
||||
|
||||
Heap::Heap(Interpreter& interpreter)
|
||||
: m_interpreter(interpreter)
|
||||
{
|
||||
}
|
||||
|
||||
Heap::~Heap()
|
||||
{
|
||||
}
|
||||
|
||||
Cell* Heap::allocate_cell(size_t size)
|
||||
{
|
||||
for (auto& block : m_blocks) {
|
||||
if (size > block->cell_size())
|
||||
continue;
|
||||
if (auto* cell = block->allocate())
|
||||
return cell;
|
||||
}
|
||||
|
||||
auto block = HeapBlock::create_with_cell_size(*this, size);
|
||||
auto* cell = block->allocate();
|
||||
m_blocks.append(move(block));
|
||||
return cell;
|
||||
}
|
||||
|
||||
void Heap::collect_garbage()
|
||||
{
|
||||
HashTable<Cell*> roots;
|
||||
gather_roots(roots);
|
||||
mark_live_cells(roots);
|
||||
sweep_dead_cells();
|
||||
}
|
||||
|
||||
void Heap::gather_roots(HashTable<Cell*>& roots)
|
||||
{
|
||||
m_interpreter.gather_roots({}, roots);
|
||||
|
||||
#ifdef HEAP_DEBUG
|
||||
dbg() << "collect_roots:";
|
||||
for (auto* root : roots) {
|
||||
dbg() << " + " << root;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
class MarkingVisitor final : public Cell::Visitor {
|
||||
public:
|
||||
MarkingVisitor() {}
|
||||
|
||||
virtual void visit(Cell* cell)
|
||||
{
|
||||
if (cell->is_marked())
|
||||
return;
|
||||
#ifdef HEAP_DEBUG
|
||||
dbg() << " ! " << cell;
|
||||
#endif
|
||||
cell->set_marked(true);
|
||||
cell->visit_children(*this);
|
||||
}
|
||||
};
|
||||
|
||||
void Heap::mark_live_cells(const HashTable<Cell*>& roots)
|
||||
{
|
||||
#ifdef HEAP_DEBUG
|
||||
dbg() << "mark_live_cells:";
|
||||
#endif
|
||||
MarkingVisitor visitor;
|
||||
for (auto* root : roots)
|
||||
visitor.visit(root);
|
||||
}
|
||||
|
||||
void Heap::sweep_dead_cells()
|
||||
{
|
||||
#ifdef HEAP_DEBUG
|
||||
dbg() << "sweep_dead_cells:";
|
||||
#endif
|
||||
for (auto& block : m_blocks) {
|
||||
block->for_each_cell([&](Cell* cell) {
|
||||
if (cell->is_live()) {
|
||||
if (!cell->is_marked()) {
|
||||
#ifdef HEAP_DEBUG
|
||||
dbg() << " ~ " << cell;
|
||||
#endif
|
||||
block->deallocate(cell);
|
||||
} else {
|
||||
cell->set_marked(false);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
69
Libraries/LibJS/Heap/Heap.h
Normal file
69
Libraries/LibJS/Heap/Heap.h
Normal file
|
@ -0,0 +1,69 @@
|
|||
/*
|
||||
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <AK/Noncopyable.h>
|
||||
#include <AK/NonnullOwnPtr.h>
|
||||
#include <AK/Types.h>
|
||||
#include <AK/Vector.h>
|
||||
#include <LibJS/Forward.h>
|
||||
#include <LibJS/Runtime/Cell.h>
|
||||
|
||||
namespace JS {
|
||||
|
||||
class Heap {
|
||||
AK_MAKE_NONCOPYABLE(Heap);
|
||||
AK_MAKE_NONMOVABLE(Heap);
|
||||
|
||||
public:
|
||||
explicit Heap(Interpreter&);
|
||||
~Heap();
|
||||
|
||||
template<typename T, typename... Args>
|
||||
T* allocate(Args&&... args)
|
||||
{
|
||||
auto* memory = allocate_cell(sizeof(T));
|
||||
new (memory) T(forward<Args>(args)...);
|
||||
return static_cast<T*>(memory);
|
||||
}
|
||||
|
||||
void collect_garbage();
|
||||
|
||||
Interpreter& interpreter() { return m_interpreter; }
|
||||
|
||||
private:
|
||||
Cell* allocate_cell(size_t);
|
||||
|
||||
void gather_roots(HashTable<Cell*>&);
|
||||
void mark_live_cells(const HashTable<Cell*>& live_cells);
|
||||
void sweep_dead_cells();
|
||||
|
||||
Interpreter& m_interpreter;
|
||||
Vector<NonnullOwnPtr<HeapBlock>> m_blocks;
|
||||
};
|
||||
|
||||
}
|
82
Libraries/LibJS/Heap/HeapBlock.cpp
Normal file
82
Libraries/LibJS/Heap/HeapBlock.cpp
Normal file
|
@ -0,0 +1,82 @@
|
|||
/*
|
||||
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <AK/Assertions.h>
|
||||
#include <AK/NonnullOwnPtr.h>
|
||||
#include <AK/kmalloc.h>
|
||||
#include <LibJS/Heap/HeapBlock.h>
|
||||
#include <sys/mman.h>
|
||||
|
||||
namespace JS {
|
||||
|
||||
NonnullOwnPtr<HeapBlock> HeapBlock::create_with_cell_size(Heap& heap, size_t cell_size)
|
||||
{
|
||||
auto* block = (HeapBlock*)serenity_mmap(nullptr, block_size, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, 0, 0, block_size, "HeapBlock");
|
||||
ASSERT(block != MAP_FAILED);
|
||||
new (block) HeapBlock(heap, cell_size);
|
||||
return NonnullOwnPtr<HeapBlock>(NonnullOwnPtr<HeapBlock>::Adopt, *block);
|
||||
}
|
||||
|
||||
void HeapBlock::operator delete(void* ptr)
|
||||
{
|
||||
int rc = munmap(ptr, block_size);
|
||||
ASSERT(rc == 0);
|
||||
}
|
||||
|
||||
HeapBlock::HeapBlock(Heap& heap, size_t cell_size)
|
||||
: m_heap(heap)
|
||||
, m_cell_size(cell_size)
|
||||
{
|
||||
for (size_t i = 0; i < cell_count(); ++i) {
|
||||
auto* freelist_entry = static_cast<FreelistEntry*>(cell(i));
|
||||
freelist_entry->set_live(false);
|
||||
if (i == cell_count() - 1)
|
||||
freelist_entry->next = nullptr;
|
||||
else
|
||||
freelist_entry->next = static_cast<FreelistEntry*>(cell(i + 1));
|
||||
}
|
||||
m_freelist = static_cast<FreelistEntry*>(cell(0));
|
||||
}
|
||||
|
||||
Cell* HeapBlock::allocate()
|
||||
{
|
||||
if (!m_freelist)
|
||||
return nullptr;
|
||||
return exchange(m_freelist, m_freelist->next);
|
||||
}
|
||||
|
||||
void HeapBlock::deallocate(Cell* cell)
|
||||
{
|
||||
ASSERT(cell->is_live());
|
||||
ASSERT(!cell->is_marked());
|
||||
cell->~Cell();
|
||||
auto* freelist_entry = static_cast<FreelistEntry*>(cell);
|
||||
freelist_entry->set_live(false);
|
||||
freelist_entry->next = m_freelist;
|
||||
m_freelist = freelist_entry;
|
||||
}
|
||||
|
||||
}
|
77
Libraries/LibJS/Heap/HeapBlock.h
Normal file
77
Libraries/LibJS/Heap/HeapBlock.h
Normal file
|
@ -0,0 +1,77 @@
|
|||
/*
|
||||
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <AK/Types.h>
|
||||
#include <LibJS/Forward.h>
|
||||
#include <LibJS/Runtime/Cell.h>
|
||||
|
||||
namespace JS {
|
||||
|
||||
class HeapBlock {
|
||||
public:
|
||||
static constexpr size_t block_size = 16 * KB;
|
||||
static NonnullOwnPtr<HeapBlock> create_with_cell_size(Heap&, size_t);
|
||||
|
||||
void operator delete(void*);
|
||||
|
||||
size_t cell_size() const { return m_cell_size; }
|
||||
size_t cell_count() const { return (block_size - sizeof(HeapBlock)) / m_cell_size; }
|
||||
|
||||
Cell* cell(size_t index) { return reinterpret_cast<Cell*>(&m_storage[index * cell_size()]); }
|
||||
|
||||
Cell* allocate();
|
||||
void deallocate(Cell*);
|
||||
|
||||
template<typename Callback>
|
||||
void for_each_cell(Callback callback)
|
||||
{
|
||||
for (size_t i = 0; i < cell_count(); ++i)
|
||||
callback(cell(i));
|
||||
}
|
||||
|
||||
Heap& heap() { return m_heap; }
|
||||
|
||||
static HeapBlock* from_cell(const Cell* cell)
|
||||
{
|
||||
return reinterpret_cast<HeapBlock*>((FlatPtr)cell & ~(block_size - 1));
|
||||
}
|
||||
|
||||
private:
|
||||
HeapBlock(Heap&, size_t cell_size);
|
||||
|
||||
struct FreelistEntry : public Cell {
|
||||
FreelistEntry* next;
|
||||
};
|
||||
|
||||
Heap& m_heap;
|
||||
size_t m_cell_size { 0 };
|
||||
FreelistEntry* m_freelist { nullptr };
|
||||
u8 m_storage[];
|
||||
};
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue