2020-03-08 19:23:58 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
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>
|
2020-03-08 19:23:58 +01:00
|
|
|
#include <AK/Types.h>
|
|
|
|
#include <LibJS/Forward.h>
|
2021-05-17 19:50:20 +02:00
|
|
|
#include <LibJS/Heap/Cell.h>
|
2020-03-08 19:23:58 +01:00
|
|
|
|
|
|
|
namespace JS {
|
|
|
|
|
|
|
|
class HeapBlock {
|
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:
|
AK: Rename KB, MB, GB to KiB, MiB, GiB
The SI prefixes "k", "M", "G" mean "10^3", "10^6", "10^9".
The IEC prefixes "Ki", "Mi", "Gi" mean "2^10", "2^20", "2^30".
Let's use the correct name, at least in code.
Only changes the name of the constants, no other behavior change.
2020-08-15 13:55:00 -04:00
|
|
|
static constexpr size_t block_size = 16 * KiB;
|
2020-03-13 11:01:44 +01:00
|
|
|
static NonnullOwnPtr<HeapBlock> create_with_cell_size(Heap&, size_t);
|
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
|
|
|
|
2020-10-04 18:11:54 +02:00
|
|
|
ALWAYS_INLINE Cell* allocate()
|
|
|
|
{
|
2021-05-17 19:51:09 +02:00
|
|
|
if (m_freelist) {
|
|
|
|
VERIFY(is_valid_cell_pointer(m_freelist));
|
|
|
|
return exchange(m_freelist, m_freelist->next);
|
|
|
|
}
|
2021-05-17 19:01:08 +02:00
|
|
|
if (has_lazy_freelist())
|
|
|
|
return cell(m_next_lazy_freelist_index++);
|
2021-05-17 19:51:09 +02:00
|
|
|
return nullptr;
|
2020-10-04 18:11:54 +02:00
|
|
|
}
|
|
|
|
|
2020-03-08 19:23:58 +01:00
|
|
|
void deallocate(Cell*);
|
|
|
|
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
2021-05-25 18:35:27 +02:00
|
|
|
template<Cell::State state, typename Callback>
|
|
|
|
void for_each_cell_in_state(Callback callback)
|
|
|
|
{
|
|
|
|
for_each_cell([&](auto* cell) {
|
|
|
|
if (cell->state() == state)
|
|
|
|
callback(cell);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-03-13 11:01:44 +01:00
|
|
|
Heap& heap() { return m_heap; }
|
|
|
|
|
2020-03-16 00:19:41 +02:00
|
|
|
static HeapBlock* from_cell(const Cell* cell)
|
2020-03-13 11:01:44 +01:00
|
|
|
{
|
|
|
|
return reinterpret_cast<HeapBlock*>((FlatPtr)cell & ~(block_size - 1));
|
|
|
|
}
|
|
|
|
|
2020-03-16 19:08:59 +01:00
|
|
|
Cell* cell_from_possible_pointer(FlatPtr pointer)
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2021-02-13 00:17:28 +01:00
|
|
|
bool is_valid_cell_pointer(const Cell* cell)
|
|
|
|
{
|
|
|
|
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
|
|
|
|
2020-03-08 19:23:58 +01:00
|
|
|
private:
|
2020-03-13 11:01:44 +01:00
|
|
|
HeapBlock(Heap&, size_t cell_size);
|
|
|
|
|
2021-05-17 19:01:08 +02:00
|
|
|
bool has_lazy_freelist() const { return m_next_lazy_freelist_index < cell_count(); }
|
|
|
|
|
2020-06-01 17:01:04 +03:00
|
|
|
struct FreelistEntry final : public Cell {
|
|
|
|
FreelistEntry* next { nullptr };
|
|
|
|
|
|
|
|
virtual const char* class_name() const override { return "FreelistEntry"; }
|
2020-03-08 19:23:58 +01:00
|
|
|
};
|
|
|
|
|
2020-06-01 17:01:04 +03:00
|
|
|
Cell* cell(size_t index)
|
|
|
|
{
|
|
|
|
return reinterpret_cast<Cell*>(&m_storage[index * cell_size()]);
|
|
|
|
}
|
|
|
|
|
2020-03-13 11:01:44 +01:00
|
|
|
Heap& m_heap;
|
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 };
|
2020-03-08 19:23:58 +01:00
|
|
|
FreelistEntry* m_freelist { nullptr };
|
2020-10-07 13:09:59 +02:00
|
|
|
alignas(Cell) u8 m_storage[];
|
2020-03-08 19:23:58 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|