feat(kcp, pfa): Add kcp::Spinlock class

This commit is contained in:
Daniel Hammer
2025-03-12 19:55:05 +01:00
parent bd8a1fdbca
commit 2b6c2fa827
7 changed files with 57 additions and 5 deletions
+1
View File
@@ -187,6 +187,7 @@ override CXXFLAGS += \
-std=gnu++20 \
-fno-rtti \
-fno-exceptions \
-fmodules-ts \
$(SHARED_FLAGS)
# Internal linker flags that should not be changed by the user.
+17
View File
@@ -0,0 +1,17 @@
/*
* Spinlock.cpp
* C++ Spinlock
* Copyright (c) 2025 Daniel Hammer
*/
#include "Spinlock.hpp"
namespace kcp {
void Spinlock::Aquire() {
while (atomic_flag.test_and_set(std::memory_order_acquire));
}
void Spinlock::Release() {
atomic_flag.clear(std::memory_order_release);
}
};
+17
View File
@@ -0,0 +1,17 @@
/*
* Spinlock.cpp
* C++ Spinlock
* Copyright (c) 2025 Daniel Hammer
*/
#pragma once
#include <atomic>
namespace kcp {
class Spinlock {
std::atomic_flag atomic_flag{ATOMIC_FLAG_INIT};
public:
void Aquire();
void Release();
};
};
+5 -2
View File
@@ -7,7 +7,9 @@
#pragma once
#include <cstdint>
#include <Terminal/Terminal.hpp>
#include <Memory/Heap.hpp>
#include <Memory/PageFrameAllocator.hpp>
#include <Common/Panic.hpp>
@@ -65,13 +67,14 @@ namespace kcp
size_t size() {
return sz;
}
};
template<typename T>
class NoMallocVector : public vector<T> {
class noHeapVector : public vector<T> {
size_t pages = 0;
public:
NoMallocVector() {
noHeapVector() {
kout << "NoMallocVector: constructor called" << Kt::newline;
pages = 0;
this->sz = 0;
+3 -2
View File
@@ -96,12 +96,13 @@ extern "C" void kmain() {
#if defined (__x86_64__)
Hal::IDTInitialize();
#endif
kcp::NoMallocVector<int> hello{};
kcp::noHeapVector<int> hello{};
hello.push_back(10);
hello.push_back(20);
hello.push_back(30);
kout << base::dec << hello.at(0) << hello.at(1) << hello.at(2) << newline;
hcf();
}
+11
View File
@@ -10,6 +10,8 @@
#include <Libraries/Memory.hpp>
#include <Terminal/Terminal.hpp>
#include <CppLib/Spinlock.hpp>
namespace Memory {
PageFrameAllocator::PageFrameAllocator(LargestSection section) {
/* we need the virtual address rather than the physical address, so we call the helper */
@@ -24,6 +26,8 @@ namespace Memory {
}
void* PageFrameAllocator::Allocate() {
Lock.Aquire();
Page* current = head.next;
Page* prev = &head;
@@ -35,11 +39,15 @@ namespace Memory {
if (new_size == 0) {
prev->next = current->next;
Lock.Release();
return (void*)current;
}
else
{
current->size -= 0x1000;
Lock.Release();
return (void*)current_end_addr - 0x1000;
}
}
@@ -48,6 +56,7 @@ namespace Memory {
current = current->next;
}
Lock.Release();
return nullptr;
}
@@ -67,11 +76,13 @@ namespace Memory {
}
void PageFrameAllocator::Free(void* ptr) {
Lock.Aquire();
auto prev_next = head.next;
head.next = (Page*)ptr;
head.next->next = prev_next;
head.next->size = 0x1000;
Lock.Release();
}
void PageFrameAllocator::Free(void* ptr, int n) {
+3 -1
View File
@@ -7,6 +7,8 @@
#pragma once
#include "Memmap.hpp"
#include <CppLib/Spinlock.hpp>
namespace Memory {
class PageFrameAllocator {
struct Page {
@@ -15,7 +17,7 @@ namespace Memory {
};
Page head{};
kcp::Spinlock Lock{};
LargestSection g_section;
public:
PageFrameAllocator(LargestSection section);