1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 22:18:12 +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:
Andreas Kling 2020-03-13 11:01:44 +01:00
parent d9c7009604
commit 6089d6566b
5 changed files with 44 additions and 7 deletions

View file

@ -35,8 +35,9 @@ namespace JS {
class HeapBlock {
public:
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_count() const { return (block_size - sizeof(HeapBlock)) / m_cell_size; }
@ -53,11 +54,21 @@ public:
callback(cell(i));
}
Heap& heap() { return m_heap; }
static HeapBlock* from_cell(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[];