1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 08:08: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

@ -25,12 +25,30 @@
*/
#include <AK/Assertions.h>
#include <AK/NonnullOwnPtr.h>
#include <AK/kmalloc.h>
#include <LibJS/HeapBlock.h>
#include <sys/mman.h>
namespace JS {
HeapBlock::HeapBlock(size_t cell_size)
: m_cell_size(cell_size)
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));