mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 17:17:44 +00:00
LibJS: Make it possible to go from a Cell* to its Heap&
This patch makes all HeapBlock allocations aligned to their block size, enabling us to find the HeapBlock* for a given Cell* by simply masking bits off of the cell address. Use this to make a simple Heap& getter for Cell, which lets us avoid plumbing the Heap& everywhere.
This commit is contained in:
parent
d9c7009604
commit
6089d6566b
5 changed files with 44 additions and 7 deletions
|
@ -26,6 +26,7 @@
|
||||||
|
|
||||||
#include <AK/LogStream.h>
|
#include <AK/LogStream.h>
|
||||||
#include <LibJS/Cell.h>
|
#include <LibJS/Cell.h>
|
||||||
|
#include <LibJS/HeapBlock.h>
|
||||||
#include <LibJS/Object.h>
|
#include <LibJS/Object.h>
|
||||||
#include <LibJS/PrimitiveString.h>
|
#include <LibJS/PrimitiveString.h>
|
||||||
#include <LibJS/Value.h>
|
#include <LibJS/Value.h>
|
||||||
|
@ -38,6 +39,11 @@ void Cell::Visitor::visit(Value value)
|
||||||
visit(value.as_cell());
|
visit(value.as_cell());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Heap& Cell::heap()
|
||||||
|
{
|
||||||
|
return HeapBlock::from_cell(this)->heap();
|
||||||
|
}
|
||||||
|
|
||||||
const LogStream& operator<<(const LogStream& stream, const Cell* cell)
|
const LogStream& operator<<(const LogStream& stream, const Cell* cell)
|
||||||
{
|
{
|
||||||
if (!cell)
|
if (!cell)
|
||||||
|
|
|
@ -51,6 +51,8 @@ public:
|
||||||
|
|
||||||
virtual void visit_children(Visitor&) {}
|
virtual void visit_children(Visitor&) {}
|
||||||
|
|
||||||
|
Heap& heap();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool m_mark { false };
|
bool m_mark { false };
|
||||||
bool m_live { true };
|
bool m_live { true };
|
||||||
|
|
|
@ -53,10 +53,10 @@ Cell* Heap::allocate_cell(size_t size)
|
||||||
return cell;
|
return cell;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto* block = (HeapBlock*)malloc(HeapBlock::block_size);
|
auto block = HeapBlock::create_with_cell_size(*this, size);
|
||||||
new (block) HeapBlock(size);
|
auto* cell = block->allocate();
|
||||||
m_blocks.append(NonnullOwnPtr<HeapBlock>(NonnullOwnPtr<HeapBlock>::Adopt, *block));
|
m_blocks.append(move(block));
|
||||||
return block->allocate();
|
return cell;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Heap::collect_garbage()
|
void Heap::collect_garbage()
|
||||||
|
|
|
@ -25,12 +25,30 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <AK/Assertions.h>
|
#include <AK/Assertions.h>
|
||||||
|
#include <AK/NonnullOwnPtr.h>
|
||||||
|
#include <AK/kmalloc.h>
|
||||||
#include <LibJS/HeapBlock.h>
|
#include <LibJS/HeapBlock.h>
|
||||||
|
#include <sys/mman.h>
|
||||||
|
|
||||||
namespace JS {
|
namespace JS {
|
||||||
|
|
||||||
HeapBlock::HeapBlock(size_t cell_size)
|
NonnullOwnPtr<HeapBlock> HeapBlock::create_with_cell_size(Heap& heap, size_t cell_size)
|
||||||
: m_cell_size(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) {
|
for (size_t i = 0; i < cell_count(); ++i) {
|
||||||
auto* freelist_entry = static_cast<FreelistEntry*>(cell(i));
|
auto* freelist_entry = static_cast<FreelistEntry*>(cell(i));
|
||||||
|
|
|
@ -35,8 +35,9 @@ namespace JS {
|
||||||
class HeapBlock {
|
class HeapBlock {
|
||||||
public:
|
public:
|
||||||
static constexpr size_t block_size = 16 * KB;
|
static constexpr size_t block_size = 16 * KB;
|
||||||
|
static NonnullOwnPtr<HeapBlock> create_with_cell_size(Heap&, size_t);
|
||||||
|
|
||||||
explicit HeapBlock(size_t cell_size);
|
void operator delete(void*);
|
||||||
|
|
||||||
size_t cell_size() const { return m_cell_size; }
|
size_t cell_size() const { return m_cell_size; }
|
||||||
size_t cell_count() const { return (block_size - sizeof(HeapBlock)) / m_cell_size; }
|
size_t cell_count() const { return (block_size - sizeof(HeapBlock)) / m_cell_size; }
|
||||||
|
@ -53,11 +54,21 @@ public:
|
||||||
callback(cell(i));
|
callback(cell(i));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Heap& heap() { return m_heap; }
|
||||||
|
|
||||||
|
static HeapBlock* from_cell(Cell* cell)
|
||||||
|
{
|
||||||
|
return reinterpret_cast<HeapBlock*>((FlatPtr)cell & ~(block_size - 1));
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
HeapBlock(Heap&, size_t cell_size);
|
||||||
|
|
||||||
struct FreelistEntry : public Cell {
|
struct FreelistEntry : public Cell {
|
||||||
FreelistEntry* next;
|
FreelistEntry* next;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Heap& m_heap;
|
||||||
size_t m_cell_size { 0 };
|
size_t m_cell_size { 0 };
|
||||||
FreelistEntry* m_freelist { nullptr };
|
FreelistEntry* m_freelist { nullptr };
|
||||||
u8 m_storage[];
|
u8 m_storage[];
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue