aboutsummaryrefslogtreecommitdiff
path: root/kernel/src/memory.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'kernel/src/memory.cpp')
-rw-r--r--kernel/src/memory.cpp55
1 files changed, 55 insertions, 0 deletions
diff --git a/kernel/src/memory.cpp b/kernel/src/memory.cpp
new file mode 100644
index 0000000..14bedf1
--- /dev/null
+++ b/kernel/src/memory.cpp
@@ -0,0 +1,55 @@
+#include "kernel/memory.hpp"
+
+#include "kapi/memory.hpp"
+#include "kapi/system.hpp"
+
+#include "kernel/memory/free_list_allocator.hpp"
+#include "kernel/memory/heap_allocator.hpp"
+
+#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
+ {
+ return nullptr;
+ }
+
+ auto deallocate(void *) noexcept -> void override {}
+ };
+
+ constinit null_allocator null_allocator::instance{};
+
+ auto constinit active_heap_allocator = std::optional<heap_allocator *>{&null_allocator::instance};
+ auto constinit basic_allocator = std::optional<free_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] The heap has already been initialized.");
+ }
+
+ auto & instance = basic_allocator.emplace(base);
+ active_heap_allocator = &instance;
+ }
+
+} // namespace kernel::memory \ No newline at end of file