From bff6c39a8d4571cd5c41e3926d5fc1428916f32c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Thu, 20 Feb 2025 10:07:03 +0000 Subject: Create global heap allocator attempt --- .../arch/memory/heap/global_heap_allocator.hpp | 46 ++++++++++++++++++++++ .../arch/memory/heap/new_delete_override.hpp | 27 ------------- arch/x86_64/src/kernel/main.cpp | 45 +++++++++++++-------- .../src/memory/heap/global_heap_allocator.cpp | 18 +++++++++ 4 files changed, 92 insertions(+), 44 deletions(-) create mode 100644 arch/x86_64/include/arch/memory/heap/global_heap_allocator.hpp delete mode 100644 arch/x86_64/include/arch/memory/heap/new_delete_override.hpp create mode 100644 arch/x86_64/src/memory/heap/global_heap_allocator.cpp (limited to 'arch/x86_64') diff --git a/arch/x86_64/include/arch/memory/heap/global_heap_allocator.hpp b/arch/x86_64/include/arch/memory/heap/global_heap_allocator.hpp new file mode 100644 index 0000000..b7b2073 --- /dev/null +++ b/arch/x86_64/include/arch/memory/heap/global_heap_allocator.hpp @@ -0,0 +1,46 @@ +#ifndef TEACHOS_ARCH_X86_64_MEMORY_HEAP_GLOBAL_HEAP_ALLOCATOR_HPP +#define TEACHOS_ARCH_X86_64_MEMORY_HEAP_GLOBAL_HEAP_ALLOCATOR_HPP + +#include "arch/memory/heap/linked_list_allocator.hpp" + +namespace teachos::arch::memory::heap +{ + struct global_heap_allocator + { + static auto allocate(std::size_t size) -> void *; + + static auto deallocate(void * pointer, std::size_t size) -> void; + }; +} // namespace teachos::arch::memory::heap + +auto operator new(std::size_t size) -> void * +{ + return teachos::arch::memory::heap::global_heap_allocator::allocate(size); +} + +auto operator delete(void * pointer) noexcept -> void +{ + teachos::arch::memory::heap::global_heap_allocator::deallocate(pointer, 64); +} + +auto operator delete(void * pointer, std::size_t size) noexcept -> void +{ + teachos::arch::memory::heap::global_heap_allocator::deallocate(pointer, size); +} + +auto operator new[](std::size_t size) -> void * +{ + return teachos::arch::memory::heap::global_heap_allocator::allocate(size); +} + +auto operator delete[](void * pointer) noexcept -> void +{ + teachos::arch::memory::heap::global_heap_allocator::deallocate(pointer, 64); +} + +auto operator delete[](void * pointer, std::size_t size) noexcept -> void +{ + teachos::arch::memory::heap::global_heap_allocator::deallocate(pointer, size); +} + +#endif // TEACHOS_ARCH_X86_64_MEMORY_HEAP_GLOBAL_HEAP_ALLOCATOR_HPP diff --git a/arch/x86_64/include/arch/memory/heap/new_delete_override.hpp b/arch/x86_64/include/arch/memory/heap/new_delete_override.hpp deleted file mode 100644 index c51c737..0000000 --- a/arch/x86_64/include/arch/memory/heap/new_delete_override.hpp +++ /dev/null @@ -1,27 +0,0 @@ -#ifndef TEACHOS_ARCH_X86_64_MEMORY_NEW_DELETE_OVERRIDE_CONCEPT_HPP -#define TEACHOS_ARCH_X86_64_MEMORY_NEW_DELETE_OVERRIDE_CONCEPT_HPP - -#include "linked_list_allocator.hpp" -#include - -void * operator new(std::size_t size) { teachos::arch::memory::heap::kernel_heap.allocate(size); } - -void operator delete(void * pointer) noexcept { teachos::arch::memory::heap::kernel_heap.deallocate(pointer, 64); } - -void operator delete(void * pointer, std::size_t size) noexcept -{ - teachos::arch::memory::heap::kernel_heap.deallocate(pointer, size); -} - -void * operator new[](std::size_t size) { teachos::arch::memory::heap::kernel_heap.allocate(size); } - -void operator delete[](void * pointer) noexcept -{ - // NOPE -} - -void operator delete[](void * pointer, std::size_t size) noexcept -{ - teachos::arch::memory::heap::kernel_heap.deallocate(pointer, size); -} -#endif // TEACHOS_ARCH_X86_64_KERNEL_NEW_DELETE_OVERRIDE_HPP \ No newline at end of file diff --git a/arch/x86_64/src/kernel/main.cpp b/arch/x86_64/src/kernel/main.cpp index a41132d..f88ea41 100644 --- a/arch/x86_64/src/kernel/main.cpp +++ b/arch/x86_64/src/kernel/main.cpp @@ -2,7 +2,7 @@ #include "arch/memory/heap/bump_allocator.hpp" #include "arch/memory/heap/concept.hpp" -#include "arch/memory/heap/linked_list_allocator.hpp" +#include "arch/memory/heap/global_heap_allocator.hpp" #include "arch/memory/main.hpp" #include "arch/memory/multiboot/reader.hpp" #include "arch/video/vga/text.hpp" @@ -22,33 +22,44 @@ namespace teachos::arch::kernel auto heap_test() -> void { - memory::heap::linked_list_allocator kernel_heap(memory::heap::HEAP_START, - memory::heap::HEAP_START + memory::heap::HEAP_SIZE); - - auto test2 = new memory::multiboot::memory_information{}; + /*memory::heap::linked_list_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) memory::multiboot::memory_information{}; + auto test3 = new (static_cast(static_cast(test) + 1)) + memory::multiboot::memory_information{}; auto test4 = *test2; + auto test5 = *test3; test4.kernel_end = 5000; + test5.kernel_end = 3000; auto test6 = test4.kernel_end; + auto test7 = test5.kernel_end; auto test8 = memory::multiboot::read_multiboot2(); - if (test6 && test8.kernel_end) + if (test6 && test7 && test8.kernel_end) { video::vga::text::write("Heap test successful", video::vga::text::common_attributes::green_on_black); } test2->kernel_end = 2000; test2->kernel_start = 1000; test2->multiboot_start = 2000; - delete test2; + heap_allocator.deallocate(test, 1024); + + auto test9 = heap_allocator.allocate(1024); + auto test10 = heap_allocator.allocate(1024); + auto test11 = heap_allocator.allocate(1024); + heap_allocator.deallocate(test9, 1024); + auto test12 = heap_allocator.allocate(1024); + auto test13 = heap_allocator.allocate(1024); + heap_allocator.deallocate(test11, 1024); + heap_allocator.deallocate(test10, 1024); + heap_allocator.deallocate(test13, 1024); + heap_allocator.deallocate(test12, 1024);*/ + + int * test = new int(); - // auto test9 = heap_allocator.allocate(1024); - // auto test10 = heap_allocator.allocate(1024); - // auto test11 = heap_allocator.allocate(1024); - // heap_allocator.deallocate(test9, 1024); - // auto test12 = heap_allocator.allocate(1024); - // auto test13 = heap_allocator.allocate(1024); - // heap_allocator.deallocate(test11, 1024); - // heap_allocator.deallocate(test10, 1024); - // heap_allocator.deallocate(test13, 1024); - // heap_allocator.deallocate(test12, 1024); + if (test == nullptr) + { + } } auto main() -> void diff --git a/arch/x86_64/src/memory/heap/global_heap_allocator.cpp b/arch/x86_64/src/memory/heap/global_heap_allocator.cpp new file mode 100644 index 0000000..6c31de0 --- /dev/null +++ b/arch/x86_64/src/memory/heap/global_heap_allocator.cpp @@ -0,0 +1,18 @@ + +#include "arch/memory/heap/global_heap_allocator.hpp" + +#include "arch/memory/heap/concept.hpp" + +namespace teachos::arch::memory::heap +{ + auto global_heap_allocator::allocate(std::size_t size) -> void * + { + static linked_list_allocator allocator{HEAP_START, HEAP_START + HEAP_SIZE}; + return allocator.allocate(size); + } + + auto global_heap_allocator::deallocate(void * pointer, std::size_t size) -> void + { + // allocator.deallocate(pointer, size); + } +} // namespace teachos::arch::memory::heap -- cgit v1.2.3