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