aboutsummaryrefslogtreecommitdiff
path: root/arch/x86_64/src/memory/heap
diff options
context:
space:
mode:
authorFabian Imhof <fabian.imhof@ost.ch>2025-02-27 09:21:43 +0100
committerFabian Imhof <fabian.imhof@ost.ch>2025-02-27 09:21:43 +0100
commit3fb836101a2032e93f7b82c924ce208d7377a5ea (patch)
tree52f9aa1f5316b9938e74f43637c0641803fda01a /arch/x86_64/src/memory/heap
parentcd42c21f2460751428b3e1b4ae07ea0b924967bc (diff)
parent2f1f0fa8b0dc1893219be68ab8bf653b5d6349fb (diff)
downloadteachos-3fb836101a2032e93f7b82c924ce208d7377a5ea.tar.xz
teachos-3fb836101a2032e93f7b82c924ce208d7377a5ea.zip
Merge branch 'feat_new_delete_override' into 'develop_ba'
new delete override See merge request teachos/kernel!4
Diffstat (limited to 'arch/x86_64/src/memory/heap')
-rw-r--r--arch/x86_64/src/memory/heap/bump_allocator.cpp4
-rw-r--r--arch/x86_64/src/memory/heap/global_heap_allocator.cpp78
-rw-r--r--arch/x86_64/src/memory/heap/linked_list_allocator.cpp41
3 files changed, 106 insertions, 17 deletions
diff --git a/arch/x86_64/src/memory/heap/bump_allocator.cpp b/arch/x86_64/src/memory/heap/bump_allocator.cpp
index bbf2021..df95346 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) noexcept -> 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
new file mode 100644
index 0000000..c1ca160
--- /dev/null
+++ b/arch/x86_64/src/memory/heap/global_heap_allocator.cpp
@@ -0,0 +1,78 @@
+#include "arch/memory/heap/global_heap_allocator.hpp"
+
+#include "arch/exception_handling/assert.hpp"
+#include "arch/memory/heap/bump_allocator.hpp"
+#include "arch/memory/heap/linked_list_allocator.hpp"
+
+namespace teachos::arch::memory::heap
+{
+ heap_allocator * global_heap_allocator::allocator_instance = nullptr;
+
+ auto global_heap_allocator::allocate(std::size_t size) -> void * { return get().allocate(size); }
+
+ 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
+ {
+ exception_handling::assert(allocator_instance == nullptr,
+ "Calling register_heap_allocator_type can only be done once.");
+
+ switch (new_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_instance = &allocator;
+ break;
+ }
+ case heap_allocator_type::LINKED_LIST: {
+ static linked_list_allocator allocator{HEAP_START, HEAP_START + HEAP_SIZE};
+ allocator_instance = &allocator;
+ break;
+ }
+ }
+ }
+
+ 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
+
+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);
+}
+
+auto operator delete(void * pointer, std::size_t size) noexcept -> void
+{
+ (void)size;
+ teachos::arch::memory::heap::global_heap_allocator::deallocate(pointer);
+}
+
+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);
+}
+
+auto operator delete[](void * pointer, std::size_t size) noexcept -> void
+{
+ (void)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..a824c8a 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 <algorithm>
+
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<void *>(reinterpret_cast<std::size_t>(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<void *>(reinterpret_cast<std::size_t>(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) noexcept -> 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<std::size_t>(pointer);
- auto const end_address = start_address + size;
+ // Read configured header size of the complete allocated block
+ auto const header_pointer = reinterpret_cast<void *>(reinterpret_cast<std::size_t>(pointer) - sizeof(std::size_t));
+ auto const total_size = *reinterpret_cast<std::size_t *>(header_pointer);
+
+ auto const start_address = reinterpret_cast<std::size_t>(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);
}