2020-10-06 18:50:47 +02:00
|
|
|
/*
|
2024-10-04 13:19:50 +02:00
|
|
|
* Copyright (c) 2020-2023, Andreas Kling <andreas@ladybird.org>
|
2020-10-06 18:50:47 +02:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-10-06 18:50:47 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <AK/Badge.h>
|
2024-11-15 04:01:23 +13:00
|
|
|
#include <LibGC/BlockAllocator.h>
|
|
|
|
#include <LibGC/CellAllocator.h>
|
|
|
|
#include <LibGC/Heap.h>
|
|
|
|
#include <LibGC/HeapBlock.h>
|
2020-10-06 18:50:47 +02:00
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
namespace GC {
|
2020-10-06 18:50:47 +02:00
|
|
|
|
2023-12-31 12:39:46 +01:00
|
|
|
CellAllocator::CellAllocator(size_t cell_size, char const* class_name)
|
|
|
|
: m_class_name(class_name)
|
|
|
|
, m_cell_size(cell_size)
|
2020-10-06 18:50:47 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
Cell* CellAllocator::allocate_cell(Heap& heap)
|
2020-10-06 18:50:47 +02:00
|
|
|
{
|
2023-12-23 15:13:51 +01:00
|
|
|
if (!m_list_node.is_in_list())
|
|
|
|
heap.register_cell_allocator({}, *this);
|
|
|
|
|
2020-10-06 18:50:47 +02:00
|
|
|
if (m_usable_blocks.is_empty()) {
|
2023-12-31 12:39:46 +01:00
|
|
|
auto block = HeapBlock::create_with_cell_size(heap, *this, m_cell_size, m_class_name);
|
2024-04-23 16:53:59 +02:00
|
|
|
auto block_ptr = reinterpret_cast<FlatPtr>(block.ptr());
|
|
|
|
if (m_min_block_address > block_ptr)
|
|
|
|
m_min_block_address = block_ptr;
|
|
|
|
if (m_max_block_address < block_ptr)
|
|
|
|
m_max_block_address = block_ptr;
|
2020-10-07 14:04:52 +02:00
|
|
|
m_usable_blocks.append(*block.leak_ptr());
|
2020-10-06 18:50:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
auto& block = *m_usable_blocks.last();
|
|
|
|
auto* cell = block.allocate();
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(cell);
|
2020-10-07 14:04:52 +02:00
|
|
|
if (block.is_full())
|
|
|
|
m_full_blocks.append(*m_usable_blocks.last());
|
2020-10-06 18:50:47 +02:00
|
|
|
return cell;
|
|
|
|
}
|
|
|
|
|
2021-05-27 19:03:41 +02:00
|
|
|
void CellAllocator::block_did_become_empty(Badge<Heap>, HeapBlock& block)
|
2020-10-06 18:50:47 +02:00
|
|
|
{
|
2020-10-07 14:04:52 +02:00
|
|
|
block.m_list_node.remove();
|
2021-05-27 19:01:26 +02:00
|
|
|
// NOTE: HeapBlocks are managed by the BlockAllocator, so we don't want to `delete` the block here.
|
|
|
|
block.~HeapBlock();
|
2023-12-31 11:36:18 +01:00
|
|
|
m_block_allocator.deallocate_block(&block);
|
2020-10-06 18:50:47 +02:00
|
|
|
}
|
|
|
|
|
2021-05-27 19:03:41 +02:00
|
|
|
void CellAllocator::block_did_become_usable(Badge<Heap>, HeapBlock& block)
|
2020-10-06 18:50:47 +02:00
|
|
|
{
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(!block.is_full());
|
2020-10-07 14:04:52 +02:00
|
|
|
m_usable_blocks.append(block);
|
2020-10-06 18:50:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|