Coverage Report

Created: 2026-02-16 07:47

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/serenity/Userland/Libraries/LibJS/Heap/CellAllocator.cpp
Line
Count
Source
1
/*
2
 * Copyright (c) 2020-2023, Andreas Kling <kling@serenityos.org>
3
 *
4
 * SPDX-License-Identifier: BSD-2-Clause
5
 */
6
7
#include <AK/Badge.h>
8
#include <LibJS/Heap/BlockAllocator.h>
9
#include <LibJS/Heap/CellAllocator.h>
10
#include <LibJS/Heap/Heap.h>
11
#include <LibJS/Heap/HeapBlock.h>
12
13
namespace JS {
14
15
CellAllocator::CellAllocator(size_t cell_size, char const* class_name)
16
564
    : m_class_name(class_name)
17
564
    , m_cell_size(cell_size)
18
564
{
19
564
}
20
21
Cell* CellAllocator::allocate_cell(Heap& heap)
22
0
{
23
0
    if (!m_list_node.is_in_list())
24
0
        heap.register_cell_allocator({}, *this);
25
26
0
    if (m_usable_blocks.is_empty()) {
27
0
        auto block = HeapBlock::create_with_cell_size(heap, *this, m_cell_size, m_class_name);
28
0
        auto block_ptr = reinterpret_cast<FlatPtr>(block.ptr());
29
0
        if (m_min_block_address > block_ptr)
30
0
            m_min_block_address = block_ptr;
31
0
        if (m_max_block_address < block_ptr)
32
0
            m_max_block_address = block_ptr;
33
0
        m_usable_blocks.append(*block.leak_ptr());
34
0
    }
35
36
0
    auto& block = *m_usable_blocks.last();
37
0
    auto* cell = block.allocate();
38
0
    VERIFY(cell);
39
0
    if (block.is_full())
40
0
        m_full_blocks.append(*m_usable_blocks.last());
41
0
    return cell;
42
0
}
43
44
void CellAllocator::block_did_become_empty(Badge<Heap>, HeapBlock& block)
45
0
{
46
0
    block.m_list_node.remove();
47
    // NOTE: HeapBlocks are managed by the BlockAllocator, so we don't want to `delete` the block here.
48
0
    block.~HeapBlock();
49
0
    m_block_allocator.deallocate_block(&block);
50
0
}
51
52
void CellAllocator::block_did_become_usable(Badge<Heap>, HeapBlock& block)
53
0
{
54
0
    VERIFY(!block.is_full());
55
0
    m_usable_blocks.append(block);
56
0
}
57
58
}