diff options
4 files changed, 84 insertions, 3 deletions
diff --git a/arch/x86_64/CMakeLists.txt b/arch/x86_64/CMakeLists.txt index e1b7352..496752f 100644 --- a/arch/x86_64/CMakeLists.txt +++ b/arch/x86_64/CMakeLists.txt @@ -44,6 +44,7 @@ target_sources("_memory" PRIVATE "src/memory/multiboot/elf_symbols_section.cpp" "src/memory/multiboot/reader.cpp" "src/memory/allocator/area_frame_allocator.cpp" + "src/memory/allocator/tiny_frame_allocator.cpp" "src/memory/allocator/physical_frame.cpp" "src/memory/paging/page_entry.cpp" "src/memory/paging/page_table.cpp" diff --git a/arch/x86_64/include/arch/memory/allocator/area_frame_allocator.hpp b/arch/x86_64/include/arch/memory/allocator/area_frame_allocator.hpp index 599723f..6135184 100644 --- a/arch/x86_64/include/arch/memory/allocator/area_frame_allocator.hpp +++ b/arch/x86_64/include/arch/memory/allocator/area_frame_allocator.hpp @@ -27,7 +27,7 @@ namespace teachos::arch::memory::allocator area_frame_allocator(multiboot::memory_information mem_info); /** - * @brief Allocate memory by finding and returning a free physical_frame. + * @brief Allocate memory by finding and returning a free physical frame. * * @note The physical_frame allocation executes multiple checks before returning * the physical_frame that is available to allocate. It must at least @@ -35,17 +35,19 @@ namespace teachos::arch::memory::allocator * - check if the next_free_frame is within the current_area * - check if the next_free_frame is actually free * - update the next_free_frame after finding a free physical_frame + * + * @return next free physical frame or nullopt if none was found. */ auto allocate_frame() -> std::optional<physical_frame>; /** - * @brief Deallocates a previously allocated physical_frame. + * @brief Deallocates a previously allocated physical frame. * * @note Simply does nothing, because the simply area frame * allocator implementation does not keep track of free or used frames and can therefore not deallocate, because it * does not know which frames have ben alocated in the first place. * - * @param physical_frame Previously allocated physical_frame that should be allocated. + * @param physical_frame Previously allocated physical_frame that should be deallocated. */ auto deallocate_frame(physical_frame physical_frame) -> void; diff --git a/arch/x86_64/include/arch/memory/allocator/tiny_frame_allocator.hpp b/arch/x86_64/include/arch/memory/allocator/tiny_frame_allocator.hpp new file mode 100644 index 0000000..70c7de6 --- /dev/null +++ b/arch/x86_64/include/arch/memory/allocator/tiny_frame_allocator.hpp @@ -0,0 +1,44 @@ +#ifndef TEACHOS_ARCH_X86_64_MEMORY_ALLOCATOR_TINY_FRAME_ALLOCATOR_HPP +#define TEACHOS_ARCH_X86_64_MEMORY_ALLOCATOR_TINY_FRAME_ALLOCATOR_HPP + +#include "arch/memory/allocator/physical_frame.hpp" + +#include <array> +#include <optional> + +namespace teachos::arch::memory::allocator +{ + /** + * @brief Allocates memory using memory areas read from the multiboot2 information pointer. + */ + struct tiny_frame_allocator + { + /** + * @brief Defaulted constructor. + */ + tiny_frame_allocator() = default; + + /** + * @brief Allocate memory by finding and returning one of the three free physical frames. + * + * @return First free physical frames of the three frames held by this allocator or nullopt if we used up all three + * frames already. + */ + auto allocate_frame() -> std::optional<physical_frame>; + + /** + * @brief Deallocates one of the three previously allocated physical frames. + * + * @note If more than the three frames are deallocated the method will halt execution, because it can only hold 3 + * frames. + * + * @param physical_frame Previously allocated physical_frame that should be deallocated. + */ + auto deallocate_frame(physical_frame physical_frame) -> void; + + private: + std::array<std::optional<physical_frame>, 3U> frames = {}; + }; +} // namespace teachos::arch::memory::allocator + +#endif // TEACHOS_ARCH_X86_64_MEMORY_ALLOCATOR_TINY_FRAME_ALLOCATOR_HPP diff --git a/arch/x86_64/src/memory/allocator/tiny_frame_allocator.cpp b/arch/x86_64/src/memory/allocator/tiny_frame_allocator.cpp new file mode 100644 index 0000000..d07398a --- /dev/null +++ b/arch/x86_64/src/memory/allocator/tiny_frame_allocator.cpp @@ -0,0 +1,34 @@ +#include "arch/memory/allocator/tiny_frame_allocator.hpp" + +#include "arch/exception_handling/panic.hpp" + +namespace teachos::arch::memory::allocator +{ + auto tiny_frame_allocator::allocate_frame() -> std::optional<physical_frame> + { + for (auto & frame_option : frames) + { + if (frame_option.has_value()) + { + auto value = frame_option; + frame_option = std::nullopt; + return value; + } + } + return std::nullopt; + } + + auto tiny_frame_allocator::deallocate_frame(physical_frame physical_frame) -> void + { + for (auto & frame_option : frames) + { + if (!frame_option.has_value()) + { + frame_option = physical_frame; + return; + } + } + exception_handling::panic( + "[Tiny Frame Allocator] Attempted to deallocate more than the 3 frames, that can be held"); + } +} // namespace teachos::arch::memory::allocator |
