2020-03-08 18:23:58 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-03-08 18:23:58 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2020-10-07 12:04:52 +00:00
|
|
|
#include <AK/IntrusiveList.h>
|
2021-05-29 12:36:18 +00:00
|
|
|
#include <AK/Platform.h>
|
2022-03-17 00:26:49 +00:00
|
|
|
#include <AK/StringView.h>
|
2020-03-08 18:23:58 +00:00
|
|
|
#include <AK/Types.h>
|
|
|
|
#include <LibJS/Forward.h>
|
2021-05-17 17:50:20 +00:00
|
|
|
#include <LibJS/Heap/Cell.h>
|
2020-03-08 18:23:58 +00:00
|
|
|
|
2021-05-29 12:36:18 +00:00
|
|
|
#ifdef HAS_ADDRESS_SANITIZER
|
|
|
|
# include <sanitizer/asan_interface.h>
|
|
|
|
#endif
|
|
|
|
|
2020-03-08 18:23:58 +00:00
|
|
|
namespace JS {
|
|
|
|
|
|
|
|
class HeapBlock {
|
2020-10-06 16:50:47 +00:00
|
|
|
AK_MAKE_NONCOPYABLE(HeapBlock);
|
|
|
|
AK_MAKE_NONMOVABLE(HeapBlock);
|
|
|
|
|
2020-03-08 18:23:58 +00: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 17:55:00 +00:00
|
|
|
static constexpr size_t block_size = 16 * KiB;
|
2020-03-13 10:01:44 +00:00
|
|
|
static NonnullOwnPtr<HeapBlock> create_with_cell_size(Heap&, size_t);
|
2020-03-08 18:23:58 +00: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 17:01:08 +00:00
|
|
|
bool is_full() const { return !has_lazy_freelist() && !m_freelist; }
|
2020-03-08 18:23:58 +00:00
|
|
|
|
2020-10-04 16:11:54 +00:00
|
|
|
ALWAYS_INLINE Cell* allocate()
|
|
|
|
{
|
2021-05-29 12:36:18 +00:00
|
|
|
Cell* allocated_cell = nullptr;
|
2021-05-17 17:51:09 +00:00
|
|
|
if (m_freelist) {
|
|
|
|
VERIFY(is_valid_cell_pointer(m_freelist));
|
2021-05-29 12:36:18 +00: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 17:51:09 +00:00
|
|
|
}
|
2021-05-29 12:36:18 +00:00
|
|
|
return allocated_cell;
|
2020-10-04 16:11:54 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 18:23:58 +00:00
|
|
|
void deallocate(Cell*);
|
|
|
|
|
|
|
|
template<typename Callback>
|
|
|
|
void for_each_cell(Callback callback)
|
|
|
|
{
|
2021-05-17 17:01:08 +00:00
|
|
|
auto end = has_lazy_freelist() ? m_next_lazy_freelist_index : cell_count();
|
|
|
|
for (size_t i = 0; i < end; ++i)
|
2020-03-08 18:23:58 +00:00
|
|
|
callback(cell(i));
|
|
|
|
}
|
|
|
|
|
2021-05-25 16:35:27 +00: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 10:01:44 +00:00
|
|
|
Heap& heap() { return m_heap; }
|
|
|
|
|
2022-04-01 17:58:27 +00:00
|
|
|
static HeapBlock* from_cell(Cell const* cell)
|
2020-03-13 10:01:44 +00:00
|
|
|
{
|
|
|
|
return reinterpret_cast<HeapBlock*>((FlatPtr)cell & ~(block_size - 1));
|
|
|
|
}
|
|
|
|
|
2020-03-16 18:08:59 +00: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 17:57:40 +00:00
|
|
|
auto end = has_lazy_freelist() ? m_next_lazy_freelist_index : cell_count();
|
|
|
|
if (cell_index >= end)
|
2020-10-01 18:54:36 +00:00
|
|
|
return nullptr;
|
2020-03-16 18:08:59 +00:00
|
|
|
return cell(cell_index);
|
|
|
|
}
|
|
|
|
|
2022-04-01 17:58:27 +00:00
|
|
|
bool is_valid_cell_pointer(Cell const* cell)
|
2021-02-12 23:17:28 +00:00
|
|
|
{
|
|
|
|
return cell_from_possible_pointer((FlatPtr)cell);
|
|
|
|
}
|
|
|
|
|
2021-04-16 12:03:24 +00:00
|
|
|
IntrusiveListNode<HeapBlock> m_list_node;
|
2020-10-07 12:04:52 +00:00
|
|
|
|
2020-03-08 18:23:58 +00:00
|
|
|
private:
|
2020-03-13 10:01:44 +00:00
|
|
|
HeapBlock(Heap&, size_t cell_size);
|
|
|
|
|
2021-05-17 17:01:08 +00:00
|
|
|
bool has_lazy_freelist() const { return m_next_lazy_freelist_index < cell_count(); }
|
|
|
|
|
2020-06-01 14:01:04 +00:00
|
|
|
struct FreelistEntry final : public Cell {
|
2022-08-28 20:11:20 +00:00
|
|
|
JS_CELL(FreelistEntry, Cell);
|
2020-06-01 14:01:04 +00:00
|
|
|
|
2023-02-26 23:09:02 +00:00
|
|
|
GCPtr<FreelistEntry> next;
|
2020-03-08 18:23:58 +00:00
|
|
|
};
|
|
|
|
|
2020-06-01 14:01:04 +00:00
|
|
|
Cell* cell(size_t index)
|
|
|
|
{
|
|
|
|
return reinterpret_cast<Cell*>(&m_storage[index * cell_size()]);
|
|
|
|
}
|
|
|
|
|
2020-03-13 10:01:44 +00:00
|
|
|
Heap& m_heap;
|
2020-03-08 18:23:58 +00:00
|
|
|
size_t m_cell_size { 0 };
|
2021-05-17 17:01:08 +00:00
|
|
|
size_t m_next_lazy_freelist_index { 0 };
|
2023-02-26 23:09:02 +00:00
|
|
|
GCPtr<FreelistEntry> m_freelist;
|
2023-06-15 22:02:15 +00:00
|
|
|
alignas(__BIGGEST_ALIGNMENT__) u8 m_storage[];
|
2021-05-29 12:24:30 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
static constexpr size_t min_possible_cell_size = sizeof(FreelistEntry);
|
2020-03-08 18:23:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|