diff options
| author | Matteo Gmür <matteo.gmuer1@ost.ch> | 2025-02-21 14:13:39 +0000 |
|---|---|---|
| committer | Matteo Gmür <matteo.gmuer1@ost.ch> | 2025-02-21 14:13:39 +0000 |
| commit | 405b5b1018397bff48e32c75e10a6b3b58bb6a20 (patch) | |
| tree | 69359619d8f0dd498fc5439b9361f27ee28a1cde /arch/x86_64/src/memory | |
| parent | 00fd8cf8f72d6c5c3d6150f3ec833ded9e34b2b1 (diff) | |
| download | teachos-405b5b1018397bff48e32c75e10a6b3b58bb6a20.tar.xz teachos-405b5b1018397bff48e32c75e10a6b3b58bb6a20.zip | |
Update factory method code
Diffstat (limited to 'arch/x86_64/src/memory')
| -rw-r--r-- | arch/x86_64/src/memory/cpu/control_register.cpp | 8 | ||||
| -rw-r--r-- | arch/x86_64/src/memory/heap/global_heap_allocator.cpp | 75 | ||||
| -rw-r--r-- | arch/x86_64/src/memory/main.cpp | 2 |
3 files changed, 35 insertions, 50 deletions
diff --git a/arch/x86_64/src/memory/cpu/control_register.cpp b/arch/x86_64/src/memory/cpu/control_register.cpp index 298874f..7ee88b5 100644 --- a/arch/x86_64/src/memory/cpu/control_register.cpp +++ b/arch/x86_64/src/memory/cpu/control_register.cpp @@ -1,6 +1,6 @@ #include "arch/memory/cpu/control_register.hpp" -#include "arch/exception_handling/assert.hpp" +#include "arch/exception_handling/panic.hpp" #include <type_traits> @@ -24,8 +24,7 @@ namespace teachos::arch::memory::cpu asm volatile("mov %%cr4, %[output]" : [output] "=r"(current_value)); break; default: - exception_handling::assert(false, - "[Control Register] Attempted to read non-existent or reserved control register"); + exception_handling::panic("[Control Register] Attempted to read non-existent or reserved control register"); break; } return current_value; @@ -60,8 +59,7 @@ namespace teachos::arch::memory::cpu : "memory"); break; default: - exception_handling::assert(false, - "[Control Register] Attempted to write non-existent or reserved control register"); + exception_handling::panic("[Control Register] Attempted to write non-existent or reserved control register"); break; } } diff --git a/arch/x86_64/src/memory/heap/global_heap_allocator.cpp b/arch/x86_64/src/memory/heap/global_heap_allocator.cpp index 243f5d8..71fe775 100644 --- a/arch/x86_64/src/memory/heap/global_heap_allocator.cpp +++ b/arch/x86_64/src/memory/heap/global_heap_allocator.cpp @@ -1,56 +1,47 @@ #include "arch/memory/heap/global_heap_allocator.hpp" #include "arch/exception_handling/assert.hpp" -#include "arch/exception_handling/panic.hpp" #include "arch/memory/heap/bump_allocator.hpp" -#include "arch/memory/heap/concept.hpp" -#include "arch/memory/heap/heap_allocator.hpp" #include "arch/memory/heap/linked_list_allocator.hpp" namespace teachos::arch::memory::heap { - heap_allocator_type global_heap_allocator::allocator_type = heap_allocator_type::NONE; + heap_allocator * global_heap_allocator::allocator_instance = nullptr; - auto global_heap_allocator::allocate(std::size_t size) -> void * { return create_or_get().allocate(size); } + auto global_heap_allocator::allocate(std::size_t size) -> void * { return get().allocate(size); } - auto global_heap_allocator::deallocate(void * pointer, std::size_t size) -> void - { - create_or_get().deallocate(pointer, size); - } + auto global_heap_allocator::deallocate(void * pointer, std::size_t size) -> void { get().deallocate(pointer, size); } - auto global_heap_allocator::register_heap_allocator_type(heap_allocator_type new_type) -> void + auto global_heap_allocator::register_heap_allocator(heap_allocator_type new_type) -> void { - allocator_type = new_type; - } + exception_handling::assert(allocator_instance != nullptr, + "Calling register_heap_allocator_type can only be done once."); - auto global_heap_allocator::create_or_get() -> heap_allocator & - { - static heap_allocator * allocator_ptr = nullptr; - - if (allocator_ptr == nullptr) + switch (new_type) { - switch (allocator_type) - { - case heap_allocator_type::NONE: - // Nothing to do. - break; - case heap_allocator_type::BUMP: { - static bump_allocator allocator{HEAP_START, HEAP_START + HEAP_SIZE}; - allocator_ptr = &allocator; - break; - } - case heap_allocator_type::LINKED_LIST: { - static linked_list_allocator allocator{HEAP_START, HEAP_START + HEAP_SIZE}; - allocator_ptr = &allocator; - break; - } + case heap_allocator_type::NONE: + // Nothing to do + break; + case heap_allocator_type::BUMP: { + static bump_allocator allocator{HEAP_START, HEAP_START + HEAP_SIZE}; + allocator_instance = &allocator; + break; + } + case heap_allocator_type::LINKED_LIST: { + static linked_list_allocator allocator{HEAP_START, HEAP_START + HEAP_SIZE}; + allocator_instance = &allocator; + break; } - exception_handling::assert(allocator_ptr != nullptr, - "Attempted to allocate or deallocate using the global_heap_allocator before " - "register_heap_allocation_type was called."); } + } - return *allocator_ptr; + auto global_heap_allocator::get() -> heap_allocator & + { + exception_handling::assert(allocator_instance != nullptr, + "Attempted to allocate or deallocate using the global_heap_allocator before " + "register_heap_allocation_type was called."); + + return *allocator_instance; } } // namespace teachos::arch::memory::heap @@ -61,10 +52,8 @@ auto operator new(std::size_t size) -> void * auto operator delete(void * pointer) noexcept -> void { - if (pointer == nullptr) - { - } - teachos::arch::exception_handling::panic("Called delete operator without passing required size attribute"); + teachos::arch::exception_handling::assert(false && pointer == nullptr, + "Called delete operator without passing required size attribute"); } auto operator delete(void * pointer, std::size_t size) noexcept -> void @@ -79,10 +68,8 @@ auto operator new[](std::size_t size) -> void * auto operator delete[](void * pointer) noexcept -> void { - if (pointer == nullptr) - { - } - teachos::arch::exception_handling::panic("Called delete[] operator without passing required size attribute"); + teachos::arch::exception_handling::assert(false && pointer == nullptr, + "Called delete[] operator without passing required size attribute"); } auto operator delete[](void * pointer, std::size_t size) noexcept -> void diff --git a/arch/x86_64/src/memory/main.cpp b/arch/x86_64/src/memory/main.cpp index b978319..a6f91d9 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/concept.hpp" +#include "arch/memory/heap/heap_allocator.hpp" #include "arch/memory/paging/active_page_table.hpp" #include "arch/memory/paging/kernel_mapper.hpp" |
