#include "kapi/memory.hpp" #include "kapi/system.hpp" #include #include #include namespace teachos::memory { namespace { struct bad_frame_allocator final : public frame_allocator { bad_frame_allocator static instance; auto allocate_many(std::size_t) noexcept -> std::optional> override { system::panic("Tried to allocate frames without an active allocator."); } auto release_many(std::pair) -> void override { system::panic("Tried to release frames without an active allocator."); } }; struct bad_page_mapper final : public page_mapper { bad_page_mapper static instance; auto map(page, frame, flags) -> std::byte * override { system::panic("Tried to map a page without an active mapper."); } auto unmap(page) -> void override { system::panic("Tried to unmap a page without an active mapper."); } auto try_unmap(page) noexcept -> bool override { return false; } }; constinit bad_frame_allocator bad_frame_allocator::instance{}; constinit bad_page_mapper bad_page_mapper::instance{}; } // namespace constinit auto static active_frame_allocator = static_cast(&bad_frame_allocator::instance); constinit auto static active_page_mapper = static_cast(&bad_page_mapper::instance); auto set_frame_allocator(frame_allocator & allocator) -> std::optional { if (&allocator == active_frame_allocator) { return {}; } return std::exchange(active_frame_allocator, &allocator); } auto set_page_mapper(page_mapper & mapper) -> std::optional { if (&mapper == active_page_mapper) { return {}; } return std::exchange(active_page_mapper, &mapper); } auto allocate_frame() -> std::optional { return active_frame_allocator->allocate(); } auto allocate_many_frames(std::size_t count) -> std::optional> { return active_frame_allocator->allocate_many(count); } auto map(page page, frame frame) -> std::byte * { return active_page_mapper->map(page, frame, page_mapper::flags::empty); } auto unmap(page page) -> void { return active_page_mapper->unmap(page); } } // namespace teachos::memory