blob: 0f614f017d22cb584351a37a2a3134c55a683277 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
#include "kernel/memory.hpp"
#include "kapi/memory.hpp"
#include "kapi/system.hpp"
#include "kernel/memory/block_list_allocator.hpp"
#include "kernel/memory/heap_allocator.hpp"
#include <kstd/print>
#include <atomic>
#include <cstddef>
#include <new>
#include <optional>
namespace kernel::memory
{
namespace
{
struct null_allocator : heap_allocator
{
null_allocator static instance;
[[nodiscard]] auto allocate(std::size_t, std::align_val_t) noexcept -> void * override
{
kstd::print(kstd::print_sink::stderr, "Tried to allocate memory without an active heap!");
return nullptr;
}
auto deallocate(void *) noexcept -> void override
{
kstd::print(kstd::print_sink::stderr, "Tried to deallocate memory without an active heap!");
}
};
constinit null_allocator null_allocator::instance{};
auto constinit active_heap_allocator = std::optional<heap_allocator *>{&null_allocator::instance};
auto constinit basic_allocator = std::optional<block_list_allocator>{};
} // namespace
auto get_heap_allocator() -> heap_allocator &
{
return *active_heap_allocator.value();
}
auto init_heap(kapi::memory::linear_address base) -> void
{
auto static constinit is_initialized = std::atomic_flag{};
if (is_initialized.test_and_set())
{
kapi::system::panic("[OS:MEM] The heap has already been initialized.");
}
auto & instance = basic_allocator.emplace(base);
active_heap_allocator = &instance;
kstd::println("[OS:MEM] Heap initialized. Dynamic memory available.");
}
} // namespace kernel::memory
|