aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFabian Imhof <fabian.imhof@ost.ch>2024-12-01 13:34:46 +0000
committerFabian Imhof <fabian.imhof@ost.ch>2024-12-01 13:34:46 +0000
commit9072c2a277c0da298b977cf4fb3dbebb5481abd0 (patch)
treee3c4fdf75e3cb73a0cc2d966c7179b371612d77f
parent0cf972394e99dfa69fbaf2ec9f4c718fd36bbc3e (diff)
downloadteachos-9072c2a277c0da298b977cf4fb3dbebb5481abd0.tar.xz
teachos-9072c2a277c0da298b977cf4fb3dbebb5481abd0.zip
implement clear_memory_block_header
-rw-r--r--arch/x86_64/include/arch/memory/heap/linked_list_allocator.hpp3
-rw-r--r--arch/x86_64/src/kernel/main.cpp8
-rw-r--r--arch/x86_64/src/memory/heap/linked_list_allocator.cpp22
3 files changed, 23 insertions, 10 deletions
diff --git a/arch/x86_64/include/arch/memory/heap/linked_list_allocator.hpp b/arch/x86_64/include/arch/memory/heap/linked_list_allocator.hpp
index 236d366..7432561 100644
--- a/arch/x86_64/include/arch/memory/heap/linked_list_allocator.hpp
+++ b/arch/x86_64/include/arch/memory/heap/linked_list_allocator.hpp
@@ -59,6 +59,9 @@ namespace teachos::arch::memory::heap
auto coalesce_free_memory_block(memory_block *& previous_block, memory_block *& current_block, void * pointer,
std::size_t size) -> void;
+ // We cannot call delete, because it causes "undefined reference to `sbrk`".
+ auto clear_memory_block_header(void * pointer) -> void;
+
std::size_t heap_start; ///< Start of the allocatable heap area
std::size_t heap_end; ///< End of the allocatable heap area
memory_block * first; ///< First free entry in our memory
diff --git a/arch/x86_64/src/kernel/main.cpp b/arch/x86_64/src/kernel/main.cpp
index ea18232..a4f138c 100644
--- a/arch/x86_64/src/kernel/main.cpp
+++ b/arch/x86_64/src/kernel/main.cpp
@@ -26,8 +26,8 @@ namespace teachos::arch::kernel
memory::heap::HEAP_START + memory::heap::HEAP_SIZE};
auto test = heap_allocator.allocate(1024);
auto test2 = new (test) memory::multiboot::memory_information{};
- test = static_cast<void *>(static_cast<memory::multiboot::memory_information *>(test) + 1);
- auto test3 = new (test) memory::multiboot::memory_information{};
+ auto test3 = new (static_cast<void *>(static_cast<memory::multiboot::memory_information *>(test) + 1))
+ memory::multiboot::memory_information{};
auto test4 = *test2;
auto test5 = *test3;
test4.kernel_end = 5000;
@@ -39,6 +39,8 @@ namespace teachos::arch::kernel
{
video::vga::text::write("Kernel remapping successfull", video::vga::text::common_attributes::green_on_black);
}
+
+ heap_allocator.deallocate(test, 1024);
}
auto main() -> void
@@ -50,6 +52,6 @@ namespace teachos::arch::kernel
memory::initialize_memory_management();
// stack_overflow_test(0);
- heap_test();
+ // heap_test();
}
} // namespace teachos::arch::kernel
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 63a762e..07d7e5e 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 <string.h>
+
#include <algorithm>
namespace teachos::arch::memory::heap
@@ -14,7 +16,7 @@ namespace teachos::arch::memory::heap
{
auto const heap_size = heap_end - heap_start;
exception_handling::assert(
- heap_size < min_allocatable_size(),
+ heap_size > min_allocatable_size(),
"[Linked List Allocator] Total heap size can not be smaller than minimum of 16 bytes to hold "
"atleast one memory hole entry");
first = new (reinterpret_cast<void *>(heap_start)) memory_block(heap_size, nullptr);
@@ -22,7 +24,7 @@ namespace teachos::arch::memory::heap
auto linked_list_allocator::allocate(std::size_t size) -> void *
{
- exception_handling::assert(size < min_allocatable_size(),
+ exception_handling::assert(size > min_allocatable_size(),
"[Linked List Allocator] Allocated memory cannot be smaller than 16 bytes");
auto & previous = first;
@@ -34,7 +36,8 @@ namespace teachos::arch::memory::heap
{
return split_free_memory_block(current, size);
}
- else if (current->size >= size)
+
+ if (current->size >= size)
{
if (previous != current)
{
@@ -42,10 +45,10 @@ namespace teachos::arch::memory::heap
}
else
{
- current = current->next;
+ first = current->next;
}
- delete current;
+ clear_memory_block_header(current);
return static_cast<void *>(current);
}
@@ -57,7 +60,7 @@ namespace teachos::arch::memory::heap
auto linked_list_allocator::deallocate(void * pointer, std::size_t size) -> void
{
- exception_handling::assert(size < min_allocatable_size(),
+ exception_handling::assert(size > min_allocatable_size(),
"[Linked List Allocator] Allocated memory cannot be smaller than 16 bytes");
auto const start_address = reinterpret_cast<std::size_t>(pointer);
@@ -118,7 +121,7 @@ namespace teachos::arch::memory::heap
block_size += current_block->size;
next_block = current_block->next;
- delete current_block;
+ clear_memory_block_header(current_block);
}
// If the block we want to deallocate is behind another free block and we can therefore combine both into one.
@@ -137,4 +140,9 @@ namespace teachos::arch::memory::heap
previous_block->next = new_block;
}
+ auto linked_list_allocator::clear_memory_block_header(void * pointer) -> void
+ {
+ memset(pointer, 0, min_allocatable_size());
+ }
+
} // namespace teachos::arch::memory::heap