aboutsummaryrefslogtreecommitdiff
path: root/arch/x86_64/src/memory/heap
diff options
context:
space:
mode:
Diffstat (limited to 'arch/x86_64/src/memory/heap')
-rw-r--r--arch/x86_64/src/memory/heap/linked_list_allocator.cpp30
1 files changed, 12 insertions, 18 deletions
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 d922dc8..6600c6e 100644
--- a/arch/x86_64/src/memory/heap/linked_list_allocator.cpp
+++ b/arch/x86_64/src/memory/heap/linked_list_allocator.cpp
@@ -36,7 +36,7 @@ namespace teachos::arch::memory::heap
{
if (current->size == size)
{
- return remove_free_memory_block(previous, current, size);
+ return remove_free_memory_block(previous, current);
}
else if (current->size >= size + min_allocatable_size())
{
@@ -81,31 +81,25 @@ namespace teachos::arch::memory::heap
mutex.unlock();
}
- auto linked_list_allocator::remove_free_memory_block(memory_block * previous_block, memory_block * current_block,
- std::size_t size) -> void *
+ auto linked_list_allocator::remove_free_memory_block(memory_block * previous_block,
+ memory_block * current_block) -> void *
{
- auto const start_address = reinterpret_cast<std::size_t>(current_block);
- // If we want to allocate into the first block that is before any other free block, then there exists no previous
- // free block (nullptr). Therefore we have to overwrite the first block instead of overwriting its next value.
- if (previous_block == nullptr)
- {
- first = nullptr;
- }
- else
- {
- previous_block->next = current_block->next;
- }
- clear_memory_block_header(current_block);
- return reinterpret_cast<void *>(start_address);
+ return replace_free_memory_block(previous_block, current_block, current_block->next);
}
auto linked_list_allocator::split_free_memory_block(memory_block * previous_block, memory_block * current_block,
std::size_t size) -> void *
{
- auto const start_address = reinterpret_cast<std::size_t>(current_block);
- auto const end_address = start_address + size;
+ auto const end_address = reinterpret_cast<std::size_t>(current_block) + size;
auto const new_block =
new (reinterpret_cast<void *>(end_address)) memory_block(current_block->size - size, current_block->next);
+ return replace_free_memory_block(previous_block, current_block, new_block);
+ }
+
+ auto linked_list_allocator::replace_free_memory_block(memory_block * previous_block, memory_block * current_block,
+ memory_block * new_block) -> void *
+ {
+ auto const start_address = reinterpret_cast<std::size_t>(current_block);
// If we want to allocate into the first block that is before any other free block, then there exists no previous
// free block (nullptr). Therefore we have to overwrite the first block instead of overwriting its next value.
if (previous_block == nullptr)