aboutsummaryrefslogtreecommitdiff
path: root/arch/x86_64/src/memory
diff options
context:
space:
mode:
authorFabian Imhof <fabian.imhof@ost.ch>2024-12-03 08:00:26 +0000
committerFabian Imhof <fabian.imhof@ost.ch>2024-12-03 08:00:26 +0000
commitb4962c8c7b94fce2e67a00671de87fa96fdbb659 (patch)
tree1c1429f12d282002c928cc410f22e76a8d3aeae2 /arch/x86_64/src/memory
parentdcd83b71c833e86c7e00e2b8f75ab6208b5d360d (diff)
downloadteachos-b4962c8c7b94fce2e67a00671de87fa96fdbb659.tar.xz
teachos-b4962c8c7b94fce2e67a00671de87fa96fdbb659.zip
add mutex to linked_list_allocator
Diffstat (limited to 'arch/x86_64/src/memory')
-rw-r--r--arch/x86_64/src/memory/heap/linked_list_allocator.cpp14
1 files changed, 10 insertions, 4 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 22b5757..01838f9 100644
--- a/arch/x86_64/src/memory/heap/linked_list_allocator.cpp
+++ b/arch/x86_64/src/memory/heap/linked_list_allocator.cpp
@@ -13,6 +13,7 @@ namespace teachos::arch::memory::heap
: heap_start(heap_start)
, heap_end(heap_end)
, first(nullptr)
+ , mutex{shared::mutex{}}
{
auto const heap_size = heap_end - heap_start;
exception_handling::assert(
@@ -26,9 +27,10 @@ namespace teachos::arch::memory::heap
{
exception_handling::assert(size > min_allocatable_size(),
"[Linked List Allocator] Allocated memory cannot be smaller than 16 bytes");
+ mutex.lock();
memory_block * previous = nullptr;
- auto current = first.load(std::memory_order::relaxed);
+ auto current = first;
while (current != nullptr)
{
@@ -40,6 +42,8 @@ namespace teachos::arch::memory::heap
previous = current;
current = current->next;
}
+
+ mutex.unlock();
exception_handling::panic("[Linked List Allocator] Out of memory");
}
@@ -47,12 +51,13 @@ namespace teachos::arch::memory::heap
{
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;
memory_block * previous = nullptr;
- auto current = first.load(std::memory_order::relaxed);
+ auto current = first;
while (current != nullptr)
{
@@ -68,6 +73,7 @@ namespace teachos::arch::memory::heap
}
coalesce_free_memory_block(previous, current, pointer, size);
+ mutex.unlock();
}
auto linked_list_allocator::split_free_memory_block(memory_block * previous_block, memory_block * current_block,
@@ -81,11 +87,11 @@ namespace teachos::arch::memory::heap
// free block (nullptr). Therefore we have to overwrite the first block instead of overwriting its next value.
if (previous_block == nullptr)
{
- first.compare_exchange_weak(previous_block, new_block, std::memory_order::relaxed);
+ first = new_block;
}
else
{
- previous_block->next.compare_exchange_weak(current_block, new_block, std::memory_order::relaxed);
+ previous_block = new_block;
}
clear_memory_block_header(current_block);
return reinterpret_cast<void *>(start_address);