From c291e1ed629489c418049f6c4116433636717636 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Sun, 24 Nov 2024 13:51:12 +0000 Subject: Add comments and rename file --- arch/x86_64/CMakeLists.txt | 2 +- arch/x86_64/include/arch/memory/heap/allocator.hpp | 46 ------------------- .../include/arch/memory/heap/bump_allocator.hpp | 53 ++++++++++++++++++++++ arch/x86_64/include/arch/memory/heap/concept.hpp | 7 ++- arch/x86_64/src/kernel/main.cpp | 18 +++++--- arch/x86_64/src/memory/heap/allocator.cpp | 27 ----------- arch/x86_64/src/memory/heap/bump_allocator.cpp | 26 +++++++++++ arch/x86_64/src/memory/main.cpp | 2 +- 8 files changed, 97 insertions(+), 84 deletions(-) delete mode 100644 arch/x86_64/include/arch/memory/heap/allocator.hpp create mode 100644 arch/x86_64/include/arch/memory/heap/bump_allocator.hpp delete mode 100644 arch/x86_64/src/memory/heap/allocator.cpp create mode 100644 arch/x86_64/src/memory/heap/bump_allocator.cpp diff --git a/arch/x86_64/CMakeLists.txt b/arch/x86_64/CMakeLists.txt index 5b0e3a9..b758bf0 100644 --- a/arch/x86_64/CMakeLists.txt +++ b/arch/x86_64/CMakeLists.txt @@ -56,7 +56,7 @@ target_sources("_memory" PRIVATE "src/memory/cpu/tlb.cpp" "src/memory/cpu/control_register.cpp" "src/memory/cpu/msr.cpp" - "src/memory/heap/allocator.cpp" + "src/memory/heap/bump_allocator.cpp" ) #[============================================================================[ diff --git a/arch/x86_64/include/arch/memory/heap/allocator.hpp b/arch/x86_64/include/arch/memory/heap/allocator.hpp deleted file mode 100644 index 6f7535e..0000000 --- a/arch/x86_64/include/arch/memory/heap/allocator.hpp +++ /dev/null @@ -1,46 +0,0 @@ -#ifndef TEACHOS_ARCH_X86_64_MEMORY_HEAP_ALLOCATOR_HPP -#define TEACHOS_ARCH_X86_64_MEMORY_HEAP_ALLOCATOR_HPP - -#include - -namespace teachos::arch::memory::heap -{ - std::size_t constexpr HEAP_START = 0x4000'0000; - std::size_t constexpr HEAP_SIZE = 100 * 1024; - - /** - * @brief Simple heap allocator - */ - struct bump_allocator - { - bump_allocator(std::size_t heap_start, std::size_t heap_end) - : heap_start{heap_start} - , heap_end{heap_end} - , next{heap_start} - { - } - - /** - * @brief Allocates the specified amount of memory in the heap - * - * @param size Amount of memory to allocate - * @return Address of the allocated memory - */ - auto allocate(std::size_t size) -> void *; - - /** - * @brief Deallocates heap memory at the specified location - * - * @param pointer Pointer to the location which should be deallocated - */ - auto deallocate(uint8_t * pointer) -> void; - - private: - std::size_t heap_start; - std::size_t heap_end; - std::size_t next; - }; - -} // namespace teachos::arch::memory::heap - -#endif \ No newline at end of file diff --git a/arch/x86_64/include/arch/memory/heap/bump_allocator.hpp b/arch/x86_64/include/arch/memory/heap/bump_allocator.hpp new file mode 100644 index 0000000..5b581ba --- /dev/null +++ b/arch/x86_64/include/arch/memory/heap/bump_allocator.hpp @@ -0,0 +1,53 @@ +#ifndef TEACHOS_ARCH_X86_64_MEMORY_HEAP_BUMP_ALLOCATOR_HPP +#define TEACHOS_ARCH_X86_64_MEMORY_HEAP_BUMP_ALLOCATOR_HPP + +#include + +namespace teachos::arch::memory::heap +{ + /** + * @brief Simple heap allocator, which leaks all allocated memory, because it does not really deallocate anything. + */ + struct bump_allocator + { + /** + * @brief Constructor. + * + * @param heap_start Start of the allocatable heap area + * @param heap_end End of the allocatable heap area (Start + Size) + */ + bump_allocator(std::size_t heap_start, std::size_t heap_end) + : heap_start{heap_start} + , heap_end{heap_end} + , next{heap_start} + { + // Nothing to do + } + + /** + * @brief Allocates the specified amount of memory in the heap. + * + * @param size Amount of memory to allocate. + * @return Address of the first byte to the allocated area + */ + auto allocate(std::size_t size) -> void *; + + /** + * @brief Deallocates heap memory at the specified location. + * + * @note Simply does nothing, because this allocator leaks all memory. + * + * @param pointer Pointer to the location which should be deallocated. + * @param size Size of the underlying memory area we want to deallocate. + */ + auto deallocate(uint8_t * pointer, std::size_t size) -> void; + + private: + std::size_t heap_start; ///< Start of the allocatable heap area + std::size_t heap_end; ///< End of the allocatable heap area + std::size_t next; ///< Current address, which is the start of still unused allocatable heap area + }; + +} // namespace teachos::arch::memory::heap + +#endif // TEACHOS_ARCH_X86_64_MEMORY_HEAP_BUMP_ALLOCATOR_HPP diff --git a/arch/x86_64/include/arch/memory/heap/concept.hpp b/arch/x86_64/include/arch/memory/heap/concept.hpp index a525b0b..8c8c887 100644 --- a/arch/x86_64/include/arch/memory/heap/concept.hpp +++ b/arch/x86_64/include/arch/memory/heap/concept.hpp @@ -1,10 +1,13 @@ #ifndef TEACHOS_ARCH_X86_64_MEMORY_HEAP_CONCEPT_HPP #define TEACHOS_ARCH_X86_64_MEMORY_HEAP_CONCEPT_HPP -#include +#include namespace teachos::arch::memory::heap { + std::size_t constexpr HEAP_START = 0x4000'0000; + std::size_t constexpr HEAP_SIZE = 100 * 1024; + template concept HeapAllocator = requires(T t, uint8_t * pointer, std::size_t size) { { t.allocate(size) } -> std::same_as; @@ -12,4 +15,4 @@ namespace teachos::arch::memory::heap }; } // namespace teachos::arch::memory::heap -#endif // TEACHOS_ARCH_X86_64_MEMORY_HEAP_CONCEPT_HPP \ No newline at end of file +#endif // TEACHOS_ARCH_X86_64_MEMORY_HEAP_CONCEPT_HPP diff --git a/arch/x86_64/src/kernel/main.cpp b/arch/x86_64/src/kernel/main.cpp index cbd6e68..ce13723 100644 --- a/arch/x86_64/src/kernel/main.cpp +++ b/arch/x86_64/src/kernel/main.cpp @@ -1,6 +1,9 @@ #include "arch/kernel/main.hpp" +#include "arch/memory/heap/bump_allocator.hpp" +#include "arch/memory/heap/concept.hpp" #include "arch/memory/main.hpp" +#include "arch/memory/multiboot/reader.hpp" #include "arch/video/vga/text.hpp" namespace teachos::arch::kernel @@ -13,15 +16,16 @@ namespace teachos::arch::kernel memory::initialize_memory_management(); - // heap::bump_allocator heap_allocator{memory::heap::HEAP_START, heap::HEAP_START + memory::heap::HEAP_SIZE}; - // auto test = heap_allocator.allocate(1024); + memory::heap::bump_allocator heap_allocator{memory::heap::HEAP_START, + memory::heap::HEAP_START + memory::heap::HEAP_SIZE}; + auto test = heap_allocator.allocate(1024); - // auto test2 = new (test) multiboot::memory_information{}; + auto test2 = new (test) memory::multiboot::memory_information{}; - // if (test || test2) - // { - // video::vga::text::write("Kernel remapping successfull", video::vga::text::common_attributes::green_on_black); - // } + if (test || test2) + { + video::vga::text::write("Kernel remapping successfull", video::vga::text::common_attributes::green_on_black); + } // TODO: Why is identity mapping multiboot2 information structure with new kernel not required and // allocator.allocate_frame still works? diff --git a/arch/x86_64/src/memory/heap/allocator.cpp b/arch/x86_64/src/memory/heap/allocator.cpp deleted file mode 100644 index bb61be4..0000000 --- a/arch/x86_64/src/memory/heap/allocator.cpp +++ /dev/null @@ -1,27 +0,0 @@ -#include "arch/memory/heap/allocator.hpp" - -#include "arch/exception_handling/assert.hpp" - -namespace teachos::arch::memory::heap -{ - auto bump_allocator::allocate(std::size_t size) -> void * - { - // Uses some sort of alignment orignally: - // https://github.com/phil-opp/blog_os/blob/7f6576c9dc34e360b81236c54c25c7827fd6a2df/src/memory/heap_allocator.rs#L24 - auto alloc_start = next; - auto alloc_end = next + size; - - arch::exception_handling::assert(alloc_end <= heap_end, "[Heap Allocator] Out of memory!"); - - next = alloc_end; - return reinterpret_cast(alloc_start); - } - - auto bump_allocator::deallocate(uint8_t * pointer) -> void - { - // Not implemented; leaking memory - if (pointer) - { - } - } -} // namespace teachos::arch::memory::heap \ No newline at end of file diff --git a/arch/x86_64/src/memory/heap/bump_allocator.cpp b/arch/x86_64/src/memory/heap/bump_allocator.cpp new file mode 100644 index 0000000..486ece8 --- /dev/null +++ b/arch/x86_64/src/memory/heap/bump_allocator.cpp @@ -0,0 +1,26 @@ +#include "arch/memory/heap/bump_allocator.hpp" + +#include "arch/exception_handling/assert.hpp" + +namespace teachos::arch::memory::heap +{ + auto bump_allocator::allocate(std::size_t size) -> void * + { + // Uses some sort of alignment orignally: + // https://github.com/phil-opp/blog_os/blob/7f6576c9dc34e360b81236c54c25c7827fd6a2df/src/memory/heap_allocator.rs#L24 + auto alloc_start = next; + auto alloc_end = next + size; + + arch::exception_handling::assert(alloc_end <= heap_end, "[Heap Allocator] Out of memory!"); + + next = alloc_end; + return reinterpret_cast(alloc_start); + } + + auto bump_allocator::deallocate(uint8_t * pointer, std::size_t size) -> void + { + if (pointer || size) + { + } + } +} // namespace teachos::arch::memory::heap \ No newline at end of file diff --git a/arch/x86_64/src/memory/main.cpp b/arch/x86_64/src/memory/main.cpp index 80242cc..34ce113 100644 --- a/arch/x86_64/src/memory/main.cpp +++ b/arch/x86_64/src/memory/main.cpp @@ -4,7 +4,7 @@ #include "arch/memory/allocator/area_frame_allocator.hpp" #include "arch/memory/cpu/control_register.hpp" #include "arch/memory/cpu/msr.hpp" -#include "arch/memory/heap/allocator.hpp" +#include "arch/memory/heap/concept.hpp" #include "arch/memory/paging/active_page_table.hpp" #include "arch/memory/paging/kernel_mapper.hpp" -- cgit v1.2.3