2020-03-08 19:23:58 +01:00
|
|
|
/*
|
2024-10-04 13:19:50 +02:00
|
|
|
* Copyright (c) 2020, Andreas Kling <andreas@ladybird.org>
|
2020-03-08 19:23:58 +01:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-03-08 19:23:58 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2020-10-07 14:04:52 +02:00
|
|
|
#include <AK/IntrusiveList.h>
|
2021-05-29 06:36:18 -06:00
|
|
|
#include <AK/Platform.h>
|
2022-03-16 18:26:49 -06:00
|
|
|
#include <AK/StringView.h>
|
2020-03-08 19:23:58 +01:00
|
|
|
#include <AK/Types.h>
|
2024-11-15 04:01:23 +13:00
|
|
|
#include <LibGC/Cell.h>
|
|
|
|
#include <LibGC/Forward.h>
|
|
|
|
#include <LibGC/Internals.h>
|
2020-03-08 19:23:58 +01:00
|
|
|
|
2021-05-29 06:36:18 -06:00
|
|
|
#ifdef HAS_ADDRESS_SANITIZER
|
|
|
|
# include <sanitizer/asan_interface.h>
|
|
|
|
#endif
|
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
namespace GC {
|
2020-03-08 19:23:58 +01:00
|
|
|
|
2025-06-28 01:34:31 -07:00
|
|
|
class GC_API HeapBlock : public HeapBlockBase {
|
2020-10-06 18:50:47 +02:00
|
|
|
AK_MAKE_NONCOPYABLE(HeapBlock);
|
|
|
|
AK_MAKE_NONMOVABLE(HeapBlock);
|
|
|
|
|
2020-03-08 19:23:58 +01:00
|
|
|
public:
|
2023-07-09 20:05:02 +03:30
|
|
|
using HeapBlockBase::block_size;
|
2023-12-31 12:39:46 +01:00
|
|
|
static NonnullOwnPtr<HeapBlock> create_with_cell_size(Heap&, CellAllocator&, size_t cell_size, char const* class_name);
|
2020-03-08 19:23:58 +01:00
|
|
|
|
|
|
|
size_t cell_size() const { return m_cell_size; }
|
|
|
|
size_t cell_count() const { return (block_size - sizeof(HeapBlock)) / m_cell_size; }
|
2021-05-17 19:01:08 +02:00
|
|
|
bool is_full() const { return !has_lazy_freelist() && !m_freelist; }
|
2020-03-08 19:23:58 +01:00
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
ALWAYS_INLINE Cell* allocate()
|
2020-10-04 18:11:54 +02:00
|
|
|
{
|
2024-11-15 04:01:23 +13:00
|
|
|
Cell* allocated_cell = nullptr;
|
2021-05-17 19:51:09 +02:00
|
|
|
if (m_freelist) {
|
|
|
|
VERIFY(is_valid_cell_pointer(m_freelist));
|
2021-05-29 06:36:18 -06:00
|
|
|
allocated_cell = exchange(m_freelist, m_freelist->next);
|
|
|
|
} else if (has_lazy_freelist()) {
|
|
|
|
allocated_cell = cell(m_next_lazy_freelist_index++);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (allocated_cell) {
|
|
|
|
ASAN_UNPOISON_MEMORY_REGION(allocated_cell, m_cell_size);
|
2021-05-17 19:51:09 +02:00
|
|
|
}
|
2021-05-29 06:36:18 -06:00
|
|
|
return allocated_cell;
|
2020-10-04 18:11:54 +02:00
|
|
|
}
|
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
void deallocate(Cell*);
|
2020-03-08 19:23:58 +01:00
|
|
|
|
|
|
|
template<typename Callback>
|
|
|
|
void for_each_cell(Callback callback)
|
|
|
|
{
|
2021-05-17 19:01:08 +02:00
|
|
|
auto end = has_lazy_freelist() ? m_next_lazy_freelist_index : cell_count();
|
|
|
|
for (size_t i = 0; i < end; ++i)
|
2020-03-08 19:23:58 +01:00
|
|
|
callback(cell(i));
|
|
|
|
}
|
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
template<Cell::State state, typename Callback>
|
2021-05-25 18:35:27 +02:00
|
|
|
void for_each_cell_in_state(Callback callback)
|
|
|
|
{
|
|
|
|
for_each_cell([&](auto* cell) {
|
|
|
|
if (cell->state() == state)
|
|
|
|
callback(cell);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
static HeapBlock* from_cell(Cell const* cell)
|
2020-03-13 11:01:44 +01:00
|
|
|
{
|
2023-07-09 20:05:02 +03:30
|
|
|
return static_cast<HeapBlock*>(HeapBlockBase::from_cell(cell));
|
2020-03-13 11:01:44 +01:00
|
|
|
}
|
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
Cell* cell_from_possible_pointer(FlatPtr pointer)
|
2020-03-16 19:08:59 +01:00
|
|
|
{
|
|
|
|
if (pointer < reinterpret_cast<FlatPtr>(m_storage))
|
|
|
|
return nullptr;
|
|
|
|
size_t cell_index = (pointer - reinterpret_cast<FlatPtr>(m_storage)) / m_cell_size;
|
2021-05-17 19:57:40 +02:00
|
|
|
auto end = has_lazy_freelist() ? m_next_lazy_freelist_index : cell_count();
|
|
|
|
if (cell_index >= end)
|
2020-10-01 20:54:36 +02:00
|
|
|
return nullptr;
|
2020-03-16 19:08:59 +01:00
|
|
|
return cell(cell_index);
|
|
|
|
}
|
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
bool is_valid_cell_pointer(Cell const* cell)
|
2021-02-13 00:17:28 +01:00
|
|
|
{
|
|
|
|
return cell_from_possible_pointer((FlatPtr)cell);
|
|
|
|
}
|
|
|
|
|
2021-04-16 16:33:24 +04:30
|
|
|
IntrusiveListNode<HeapBlock> m_list_node;
|
2020-10-07 14:04:52 +02:00
|
|
|
|
2023-12-23 15:13:51 +01:00
|
|
|
CellAllocator& cell_allocator() { return m_cell_allocator; }
|
|
|
|
|
2020-03-08 19:23:58 +01:00
|
|
|
private:
|
2023-12-23 15:13:51 +01:00
|
|
|
HeapBlock(Heap&, CellAllocator&, size_t cell_size);
|
2020-03-13 11:01:44 +01:00
|
|
|
|
2021-05-17 19:01:08 +02:00
|
|
|
bool has_lazy_freelist() const { return m_next_lazy_freelist_index < cell_count(); }
|
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
struct FreelistEntry final : public Cell {
|
|
|
|
GC_CELL(FreelistEntry, Cell);
|
2020-06-01 17:01:04 +03:00
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
RawPtr<FreelistEntry> next;
|
2020-03-08 19:23:58 +01:00
|
|
|
};
|
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
Cell* cell(size_t index)
|
2020-06-01 17:01:04 +03:00
|
|
|
{
|
2024-11-15 04:01:23 +13:00
|
|
|
return reinterpret_cast<Cell*>(&m_storage[index * cell_size()]);
|
2020-06-01 17:01:04 +03:00
|
|
|
}
|
|
|
|
|
2023-12-23 15:13:51 +01:00
|
|
|
CellAllocator& m_cell_allocator;
|
2020-03-08 19:23:58 +01:00
|
|
|
size_t m_cell_size { 0 };
|
2021-05-17 19:01:08 +02:00
|
|
|
size_t m_next_lazy_freelist_index { 0 };
|
2024-11-15 04:01:23 +13:00
|
|
|
Ptr<FreelistEntry> m_freelist;
|
2023-06-15 23:02:15 +01:00
|
|
|
alignas(__BIGGEST_ALIGNMENT__) u8 m_storage[];
|
2021-05-29 06:24:30 -06:00
|
|
|
|
|
|
|
public:
|
|
|
|
static constexpr size_t min_possible_cell_size = sizeof(FreelistEntry);
|
2020-03-08 19:23:58 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|