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/x86_64/src/memory/heap/global_heap_allocator.cpp | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 arch/x86_64/src/memory/heap/global_heap_allocator.cpp (limited to 'arch/x86_64/src/memory') 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 From 6c4ce82c3f9cc920bcde74fc10fdfd39b477b9f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Thu, 20 Feb 2025 10:41:11 +0000 Subject: Fix compilation issues --- .../src/memory/heap/global_heap_allocator.cpp | 40 ++++++++++++++++++---- 1 file changed, 34 insertions(+), 6 deletions(-) (limited to 'arch/x86_64/src/memory') 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 6c31de0..11848ed 100644 --- a/arch/x86_64/src/memory/heap/global_heap_allocator.cpp +++ b/arch/x86_64/src/memory/heap/global_heap_allocator.cpp @@ -5,14 +5,42 @@ 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); - } + static global_heap_allocator::allocator = linked_list_allocator{HEAP_START, HEAP_START + HEAP_SIZE}; + + auto global_heap_allocator::allocate(std::size_t size) -> void * { return allocator.allocate(size); } auto global_heap_allocator::deallocate(void * pointer, std::size_t size) -> void { - // allocator.deallocate(pointer, size); + allocator.deallocate(pointer, size); } } // 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); +} -- cgit v1.2.3 From cd502936227e48a36d9e933d26aac2ee29d3bc29 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Thu, 20 Feb 2025 10:58:17 +0000 Subject: Add singelton implementation for global heap allocator --- arch/x86_64/src/memory/heap/global_heap_allocator.cpp | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) (limited to 'arch/x86_64/src/memory') 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 11848ed..4e3a274 100644 --- a/arch/x86_64/src/memory/heap/global_heap_allocator.cpp +++ b/arch/x86_64/src/memory/heap/global_heap_allocator.cpp @@ -5,13 +5,17 @@ namespace teachos::arch::memory::heap { - static global_heap_allocator::allocator = linked_list_allocator{HEAP_START, HEAP_START + HEAP_SIZE}; - - auto global_heap_allocator::allocate(std::size_t size) -> void * { return allocator.allocate(size); } + auto global_heap_allocator::allocate(std::size_t size) -> void * { return get_underlying_allocator().allocate(size); } auto global_heap_allocator::deallocate(void * pointer, std::size_t size) -> void { - allocator.deallocate(pointer, size); + get_underlying_allocator().deallocate(pointer, size); + } + + auto global_heap_allocator::get_underlying_allocator() -> linked_list_allocator & + { + static linked_list_allocator allocator{HEAP_START, HEAP_START + HEAP_SIZE}; + return allocator; } } // namespace teachos::arch::memory::heap -- cgit v1.2.3 From 27874721a35fe7ccde843c7ab88ab72e74fe6b42 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Thu, 20 Feb 2025 12:14:30 +0000 Subject: Make allocation type configurable --- .../src/memory/heap/global_heap_allocator.cpp | 45 ++++++++++++++++++---- 1 file changed, 38 insertions(+), 7 deletions(-) (limited to 'arch/x86_64/src/memory') 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 4e3a274..d82606e 100644 --- a/arch/x86_64/src/memory/heap/global_heap_allocator.cpp +++ b/arch/x86_64/src/memory/heap/global_heap_allocator.cpp @@ -1,21 +1,52 @@ #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/linked_list_allocator.hpp" namespace teachos::arch::memory::heap { - auto global_heap_allocator::allocate(std::size_t size) -> void * { return get_underlying_allocator().allocate(size); } + heap_allocator_type global_heap_allocator::allocator_type = heap_allocator_type::NONE; + + auto global_heap_allocator::allocate(std::size_t size) -> void * { return create_or_get().allocate(size); } auto global_heap_allocator::deallocate(void * pointer, std::size_t size) -> void { - get_underlying_allocator().deallocate(pointer, size); + create_or_get().deallocate(pointer, size); } - auto global_heap_allocator::get_underlying_allocator() -> linked_list_allocator & + auto global_heap_allocator::register_heap_allocator_type(heap_allocator_type new_type) -> void { - static linked_list_allocator allocator{HEAP_START, HEAP_START + HEAP_SIZE}; - return allocator; + allocator_type = new_type; + } + + auto global_heap_allocator::create_or_get() -> linked_list_allocator & + { + linked_list_allocator * allocator_ptr = nullptr; + + 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; + } + } + 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; } } // namespace teachos::arch::memory::heap @@ -26,7 +57,7 @@ auto operator new(std::size_t size) -> void * auto operator delete(void * pointer) noexcept -> void { - teachos::arch::memory::heap::global_heap_allocator::deallocate(pointer, 64); + teachos::arch::exception_handling::panic("Called delete operator without passing required size attribute"); } auto operator delete(void * pointer, std::size_t size) noexcept -> void @@ -41,7 +72,7 @@ auto operator new[](std::size_t size) -> void * auto operator delete[](void * pointer) noexcept -> void { - teachos::arch::memory::heap::global_heap_allocator::deallocate(pointer, 64); + teachos::arch::exception_handling::panic("Called delete[] operator without passing required size attribute"); } auto operator delete[](void * pointer, std::size_t size) noexcept -> void -- cgit v1.2.3 From 00fd8cf8f72d6c5c3d6150f3ec833ded9e34b2b1 Mon Sep 17 00:00:00 2001 From: Fabian Imhof Date: Thu, 20 Feb 2025 16:05:19 +0000 Subject: add heap_allocator base class --- .../src/memory/heap/global_heap_allocator.cpp | 48 +++++++++++++--------- 1 file changed, 29 insertions(+), 19 deletions(-) (limited to 'arch/x86_64/src/memory') 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 d82606e..243f5d8 100644 --- a/arch/x86_64/src/memory/heap/global_heap_allocator.cpp +++ b/arch/x86_64/src/memory/heap/global_heap_allocator.cpp @@ -1,10 +1,10 @@ - #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 @@ -23,29 +23,33 @@ namespace teachos::arch::memory::heap allocator_type = new_type; } - auto global_heap_allocator::create_or_get() -> linked_list_allocator & + auto global_heap_allocator::create_or_get() -> heap_allocator & { - linked_list_allocator * allocator_ptr = nullptr; + static heap_allocator * allocator_ptr = nullptr; - switch (allocator_type) + if (allocator_ptr == nullptr) { - 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; + 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; + } } + exception_handling::assert(allocator_ptr != nullptr, + "Attempted to allocate or deallocate using the global_heap_allocator before " + "register_heap_allocation_type was called."); } - 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; } } // namespace teachos::arch::memory::heap @@ -57,6 +61,9 @@ 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"); } @@ -72,6 +79,9 @@ 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"); } -- cgit v1.2.3 From 405b5b1018397bff48e32c75e10a6b3b58bb6a20 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Fri, 21 Feb 2025 14:13:39 +0000 Subject: Update factory method code --- arch/x86_64/src/memory/cpu/control_register.cpp | 8 +-- .../src/memory/heap/global_heap_allocator.cpp | 75 +++++++++------------- arch/x86_64/src/memory/main.cpp | 2 +- 3 files changed, 35 insertions(+), 50 deletions(-) (limited to 'arch/x86_64/src/memory') 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 @@ -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" -- cgit v1.2.3 From fcc586a846562e024c1cd77042634494cf380bd8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Sun, 23 Feb 2025 10:27:46 +0000 Subject: Adjust linked list allocator to allow for deallocation without passing size parameter and with arbitrary size --- arch/x86_64/src/memory/heap/bump_allocator.cpp | 4 +-- .../src/memory/heap/global_heap_allocator.cpp | 20 ++++++----- .../src/memory/heap/linked_list_allocator.cpp | 41 ++++++++++++++-------- 3 files changed, 40 insertions(+), 25 deletions(-) (limited to 'arch/x86_64/src/memory') diff --git a/arch/x86_64/src/memory/heap/bump_allocator.cpp b/arch/x86_64/src/memory/heap/bump_allocator.cpp index bbf2021..a9fb121 100644 --- a/arch/x86_64/src/memory/heap/bump_allocator.cpp +++ b/arch/x86_64/src/memory/heap/bump_allocator.cpp @@ -42,9 +42,9 @@ namespace teachos::arch::memory::heap } } - auto bump_allocator::deallocate(void * pointer, std::size_t size) -> void + auto bump_allocator::deallocate(void * pointer) -> void { - if (pointer || size) + if (pointer) { } } 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 71fe775..09db9ba 100644 --- a/arch/x86_64/src/memory/heap/global_heap_allocator.cpp +++ b/arch/x86_64/src/memory/heap/global_heap_allocator.cpp @@ -10,11 +10,11 @@ namespace teachos::arch::memory::heap 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 { get().deallocate(pointer, size); } + auto global_heap_allocator::deallocate(void * pointer) -> void { get().deallocate(pointer); } auto global_heap_allocator::register_heap_allocator(heap_allocator_type new_type) -> void { - exception_handling::assert(allocator_instance != nullptr, + exception_handling::assert(allocator_instance == nullptr, "Calling register_heap_allocator_type can only be done once."); switch (new_type) @@ -52,13 +52,15 @@ auto operator new(std::size_t size) -> void * auto operator delete(void * pointer) noexcept -> void { - teachos::arch::exception_handling::assert(false && pointer == nullptr, - "Called delete operator without passing required size attribute"); + teachos::arch::memory::heap::global_heap_allocator::deallocate(pointer); } auto operator delete(void * pointer, std::size_t size) noexcept -> void { - teachos::arch::memory::heap::global_heap_allocator::deallocate(pointer, size); + if (size) + { + } + teachos::arch::memory::heap::global_heap_allocator::deallocate(pointer); } auto operator new[](std::size_t size) -> void * @@ -68,11 +70,13 @@ auto operator new[](std::size_t size) -> void * auto operator delete[](void * pointer) noexcept -> void { - teachos::arch::exception_handling::assert(false && pointer == nullptr, - "Called delete[] operator without passing required size attribute"); + teachos::arch::memory::heap::global_heap_allocator::deallocate(pointer); } auto operator delete[](void * pointer, std::size_t size) noexcept -> void { - teachos::arch::memory::heap::global_heap_allocator::deallocate(pointer, size); + if (size) + { + } + teachos::arch::memory::heap::global_heap_allocator::deallocate(pointer); } diff --git a/arch/x86_64/src/memory/heap/linked_list_allocator.cpp b/arch/x86_64/src/memory/heap/linked_list_allocator.cpp index e5bae21..9beb466 100644 --- a/arch/x86_64/src/memory/heap/linked_list_allocator.cpp +++ b/arch/x86_64/src/memory/heap/linked_list_allocator.cpp @@ -3,6 +3,8 @@ #include "arch/exception_handling/assert.hpp" #include "arch/exception_handling/panic.hpp" +#include + namespace teachos::arch::memory::heap { linked_list_allocator::linked_list_allocator(std::size_t heap_start, std::size_t heap_end) @@ -21,8 +23,9 @@ namespace teachos::arch::memory::heap auto linked_list_allocator::allocate(std::size_t size) -> void * { - exception_handling::assert(size > min_allocatable_size(), - "[Linked List Allocator] Allocated memory cannot be smaller than 16 bytes"); + // Add size of size_t to the total allocated size, because we add a header that includes the size of the allocated + // block, to allow for deallocation without the need to call with the corresponding size + auto const total_size = size + sizeof(std::size_t); mutex.lock(); memory_block * previous = nullptr; @@ -30,17 +33,23 @@ namespace teachos::arch::memory::heap while (current != nullptr) { - if (current->size == size) + if (current->size == total_size) { auto const memory_address = remove_free_memory_block(previous, current); + new (memory_address) std::size_t(total_size); mutex.unlock(); - return memory_address; + return reinterpret_cast(reinterpret_cast(memory_address) + sizeof(std::size_t)); } - else if (current->size >= size + min_allocatable_size()) + else if (current->size >= total_size + min_allocatable_size()) { - auto const memory_address = split_free_memory_block(previous, current, size); + // Ensure that the allocated size block is atleast 16 bytes (required because if we free the hole afterwards + // there needs to be enough space for a memory block). Therefore we allocate more than is actually required if + // the total size was less and simply deallocate it as well + auto const max_size = std::max(total_size, min_allocatable_size()); + auto const memory_address = split_free_memory_block(previous, current, max_size); + new (memory_address) std::size_t(max_size); mutex.unlock(); - return memory_address; + return reinterpret_cast(reinterpret_cast(memory_address) + sizeof(std::size_t)); } previous = current; @@ -50,14 +59,16 @@ namespace teachos::arch::memory::heap exception_handling::panic("[Linked List Allocator] Out of memory"); } - auto linked_list_allocator::deallocate(void * pointer, std::size_t size) -> void + auto linked_list_allocator::deallocate(void * pointer) -> void { - exception_handling::assert(size > min_allocatable_size(), - "[Linked List Allocator] Allocated memory cannot be smaller than 16 bytes"); mutex.lock(); - auto const start_address = reinterpret_cast(pointer); - auto const end_address = start_address + size; + // Read configured header size of the complete allocated block + auto const header_pointer = reinterpret_cast(reinterpret_cast(pointer) - sizeof(std::size_t)); + auto const total_size = *reinterpret_cast(header_pointer); + + auto const start_address = reinterpret_cast(header_pointer); + auto const end_address = start_address + total_size; memory_block * previous = nullptr; auto current = first; @@ -75,12 +86,12 @@ namespace teachos::arch::memory::heap current = current->next; } - coalesce_free_memory_block(previous, current, pointer, size); + coalesce_free_memory_block(previous, current, header_pointer, total_size); mutex.unlock(); } - auto linked_list_allocator::remove_free_memory_block(memory_block * previous_block, - memory_block * current_block) -> void * + auto linked_list_allocator::remove_free_memory_block(memory_block * previous_block, memory_block * current_block) + -> void * { return replace_free_memory_block(previous_block, current_block, current_block->next); } -- cgit v1.2.3 From 20f2a4a3e9b8100544a7b3dd57c5959dc6dc066f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Sun, 23 Feb 2025 14:37:28 +0000 Subject: Remove useless if statements --- arch/x86_64/src/memory/heap/global_heap_allocator.cpp | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) (limited to 'arch/x86_64/src/memory') 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 09db9ba..235c544 100644 --- a/arch/x86_64/src/memory/heap/global_heap_allocator.cpp +++ b/arch/x86_64/src/memory/heap/global_heap_allocator.cpp @@ -57,9 +57,7 @@ auto operator delete(void * pointer) noexcept -> void auto operator delete(void * pointer, std::size_t size) noexcept -> void { - if (size) - { - } + (void)size; teachos::arch::memory::heap::global_heap_allocator::deallocate(pointer); } @@ -75,8 +73,6 @@ auto operator delete[](void * pointer) noexcept -> void auto operator delete[](void * pointer, std::size_t size) noexcept -> void { - if (size) - { - } + (void)size; teachos::arch::memory::heap::global_heap_allocator::deallocate(pointer); } -- cgit v1.2.3 From 191e7ef3001e422c2f58efe7381d13932e1c1537 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Mon, 24 Feb 2025 08:40:46 +0000 Subject: Add noexpect to deallocate calls --- arch/x86_64/src/memory/heap/bump_allocator.cpp | 2 +- arch/x86_64/src/memory/heap/global_heap_allocator.cpp | 2 +- arch/x86_64/src/memory/heap/linked_list_allocator.cpp | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) (limited to 'arch/x86_64/src/memory') diff --git a/arch/x86_64/src/memory/heap/bump_allocator.cpp b/arch/x86_64/src/memory/heap/bump_allocator.cpp index a9fb121..df95346 100644 --- a/arch/x86_64/src/memory/heap/bump_allocator.cpp +++ b/arch/x86_64/src/memory/heap/bump_allocator.cpp @@ -42,7 +42,7 @@ namespace teachos::arch::memory::heap } } - auto bump_allocator::deallocate(void * pointer) -> void + auto bump_allocator::deallocate(void * pointer) noexcept -> void { if (pointer) { 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 235c544..c1ca160 100644 --- a/arch/x86_64/src/memory/heap/global_heap_allocator.cpp +++ b/arch/x86_64/src/memory/heap/global_heap_allocator.cpp @@ -10,7 +10,7 @@ namespace teachos::arch::memory::heap auto global_heap_allocator::allocate(std::size_t size) -> void * { return get().allocate(size); } - auto global_heap_allocator::deallocate(void * pointer) -> void { get().deallocate(pointer); } + auto global_heap_allocator::deallocate(void * pointer) noexcept -> void { get().deallocate(pointer); } auto global_heap_allocator::register_heap_allocator(heap_allocator_type new_type) -> void { diff --git a/arch/x86_64/src/memory/heap/linked_list_allocator.cpp b/arch/x86_64/src/memory/heap/linked_list_allocator.cpp index 9beb466..a824c8a 100644 --- a/arch/x86_64/src/memory/heap/linked_list_allocator.cpp +++ b/arch/x86_64/src/memory/heap/linked_list_allocator.cpp @@ -59,7 +59,7 @@ namespace teachos::arch::memory::heap exception_handling::panic("[Linked List Allocator] Out of memory"); } - auto linked_list_allocator::deallocate(void * pointer) -> void + auto linked_list_allocator::deallocate(void * pointer) noexcept -> void { mutex.lock(); -- cgit v1.2.3