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
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
2020-10-07 14:04:52 +02:00
|
|
|
#include <AK/IntrusiveList.h>
|
2023-12-23 20:00:21 +01:00
|
|
|
#include <AK/NeverDestroyed.h>
|
2020-10-06 18:50:47 +02:00
|
|
|
#include <AK/NonnullOwnPtr.h>
|
2024-11-15 04:01:23 +13:00
|
|
|
#include <LibGC/BlockAllocator.h>
|
|
|
|
|
#include <LibGC/Forward.h>
|
|
|
|
|
#include <LibGC/HeapBlock.h>
|
2020-10-06 18:50:47 +02:00
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
#define GC_DECLARE_ALLOCATOR(ClassName) \
|
|
|
|
|
static GC::TypeIsolatingCellAllocator<ClassName> cell_allocator
|
2023-11-19 09:45:05 +01:00
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
#define GC_DEFINE_ALLOCATOR(ClassName) \
|
|
|
|
|
GC::TypeIsolatingCellAllocator<ClassName> ClassName::cell_allocator { #ClassName }
|
2023-11-19 09:45:05 +01:00
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
namespace GC {
|
2020-10-06 18:50:47 +02:00
|
|
|
|
2021-05-27 19:03:41 +02:00
|
|
|
class CellAllocator {
|
2020-10-06 18:50:47 +02:00
|
|
|
public:
|
2023-12-31 12:39:46 +01:00
|
|
|
CellAllocator(size_t cell_size, char const* class_name = nullptr);
|
2021-09-16 00:00:32 -07:00
|
|
|
~CellAllocator() = default;
|
2020-10-06 18:50:47 +02:00
|
|
|
|
|
|
|
|
size_t cell_size() const { return m_cell_size; }
|
|
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
Cell* allocate_cell(Heap&);
|
2020-10-06 18:50:47 +02:00
|
|
|
|
|
|
|
|
template<typename Callback>
|
|
|
|
|
IterationDecision for_each_block(Callback callback)
|
|
|
|
|
{
|
|
|
|
|
for (auto& block : m_full_blocks) {
|
2020-10-07 14:04:52 +02:00
|
|
|
if (callback(block) == IterationDecision::Break)
|
2020-10-06 18:50:47 +02:00
|
|
|
return IterationDecision::Break;
|
|
|
|
|
}
|
|
|
|
|
for (auto& block : m_usable_blocks) {
|
2020-10-07 14:04:52 +02:00
|
|
|
if (callback(block) == IterationDecision::Break)
|
2020-10-06 18:50:47 +02:00
|
|
|
return IterationDecision::Break;
|
|
|
|
|
}
|
|
|
|
|
return IterationDecision::Continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void block_did_become_empty(Badge<Heap>, HeapBlock&);
|
|
|
|
|
void block_did_become_usable(Badge<Heap>, HeapBlock&);
|
|
|
|
|
|
2023-12-23 15:13:51 +01:00
|
|
|
IntrusiveListNode<CellAllocator> m_list_node;
|
|
|
|
|
using List = IntrusiveList<&CellAllocator::m_list_node>;
|
|
|
|
|
|
2023-12-31 11:36:18 +01:00
|
|
|
BlockAllocator& block_allocator() { return m_block_allocator; }
|
2024-04-23 16:53:59 +02:00
|
|
|
FlatPtr min_block_address() const { return m_min_block_address; }
|
|
|
|
|
FlatPtr max_block_address() const { return m_max_block_address; }
|
2023-12-31 11:36:18 +01:00
|
|
|
|
2020-10-06 18:50:47 +02:00
|
|
|
private:
|
2023-12-31 12:39:46 +01:00
|
|
|
char const* const m_class_name { nullptr };
|
2023-11-19 09:45:05 +01:00
|
|
|
size_t const m_cell_size;
|
2020-10-06 18:50:47 +02:00
|
|
|
|
2023-12-31 11:36:18 +01:00
|
|
|
BlockAllocator m_block_allocator;
|
|
|
|
|
|
2021-09-09 16:30:59 +04:30
|
|
|
using BlockList = IntrusiveList<&HeapBlock::m_list_node>;
|
2020-10-07 14:04:52 +02:00
|
|
|
BlockList m_full_blocks;
|
|
|
|
|
BlockList m_usable_blocks;
|
2024-04-23 16:53:59 +02:00
|
|
|
FlatPtr m_min_block_address { explode_byte(0xff) };
|
|
|
|
|
FlatPtr m_max_block_address { 0 };
|
2020-10-06 18:50:47 +02:00
|
|
|
};
|
|
|
|
|
|
2023-11-19 09:45:05 +01:00
|
|
|
template<typename T>
|
|
|
|
|
class TypeIsolatingCellAllocator {
|
|
|
|
|
public:
|
|
|
|
|
using CellType = T;
|
|
|
|
|
|
2023-12-31 12:39:46 +01:00
|
|
|
TypeIsolatingCellAllocator(char const* class_name)
|
|
|
|
|
: allocator(sizeof(T), class_name)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
NeverDestroyed<CellAllocator> allocator;
|
2023-11-19 09:45:05 +01:00
|
|
|
};
|
|
|
|
|
|
2020-10-06 18:50:47 +02:00
|
|
|
}
|