aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatteo Gmür <matteo.gmuer1@ost.ch>2024-12-02 13:51:58 +0000
committerMatteo Gmür <matteo.gmuer1@ost.ch>2024-12-02 13:51:58 +0000
commitdcd83b71c833e86c7e00e2b8f75ab6208b5d360d (patch)
tree9d35296dfa4fc45de2347942abce3ccec8676fe7
parentf7abde02150deacbc2ad1957e6165769cc2fea0b (diff)
downloadteachos-dcd83b71c833e86c7e00e2b8f75ab6208b5d360d.tar.xz
teachos-dcd83b71c833e86c7e00e2b8f75ab6208b5d360d.zip
WIP thread safe linked list
-rw-r--r--arch/x86_64/include/arch/memory/heap/linked_list_allocator.hpp6
-rw-r--r--arch/x86_64/include/arch/memory/heap/memory_block.hpp3
-rw-r--r--arch/x86_64/src/memory/heap/linked_list_allocator.cpp8
3 files changed, 9 insertions, 8 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 6af6298..e77602c 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
@@ -96,9 +96,9 @@ namespace teachos::arch::memory::heap
*/
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
+ std::size_t heap_start; ///< Start of the allocatable heap area
+ std::size_t heap_end; ///< End of the allocatable heap area
+ std::atomic<memory_block *> first; ///< First free entry in our memory
};
} // namespace teachos::arch::memory::heap
diff --git a/arch/x86_64/include/arch/memory/heap/memory_block.hpp b/arch/x86_64/include/arch/memory/heap/memory_block.hpp
index c48d0cd..1fbbfd5 100644
--- a/arch/x86_64/include/arch/memory/heap/memory_block.hpp
+++ b/arch/x86_64/include/arch/memory/heap/memory_block.hpp
@@ -1,6 +1,7 @@
#ifndef TEACHOS_ARCH_X86_64_MEMORY_HEAP_MEMORY_BLOCK_HPP
#define TEACHOS_ARCH_X86_64_MEMORY_HEAP_MEMORY_BLOCK_HPP
+#include <atomic>
#include <cstdint>
namespace teachos::arch::memory::heap
@@ -21,7 +22,7 @@ namespace teachos::arch::memory::heap
std::size_t size; ///< Amount of free memory this hole contains, has to always be atleast 16 bytes to hold the size
///< variable and the pointer to the next hole.
- memory_block * next; ///< Optional pointer to the next free memory, holds nullptr if there is none.
+ std::atomic<memory_block *> next; ///< Optional pointer to the next free memory, holds nullptr if there is none.
};
} // namespace teachos::arch::memory::heap
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 f596f27..22b5757 100644
--- a/arch/x86_64/src/memory/heap/linked_list_allocator.cpp
+++ b/arch/x86_64/src/memory/heap/linked_list_allocator.cpp
@@ -28,7 +28,7 @@ namespace teachos::arch::memory::heap
"[Linked List Allocator] Allocated memory cannot be smaller than 16 bytes");
memory_block * previous = nullptr;
- auto current = first;
+ auto current = first.load(std::memory_order::relaxed);
while (current != nullptr)
{
@@ -52,7 +52,7 @@ namespace teachos::arch::memory::heap
auto const end_address = start_address + size;
memory_block * previous = nullptr;
- auto current = first;
+ auto current = first.load(std::memory_order::relaxed);
while (current != nullptr)
{
@@ -81,11 +81,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 = new_block;
+ first.compare_exchange_weak(previous_block, new_block, std::memory_order::relaxed);
}
else
{
- previous_block->next = new_block;
+ previous_block->next.compare_exchange_weak(current_block, new_block, std::memory_order::relaxed);
}
clear_memory_block_header(current_block);
return reinterpret_cast<void *>(start_address);