feat(kcp, pfa): Add kcp::Spinlock class
This commit is contained in:
@@ -187,6 +187,7 @@ override CXXFLAGS += \
|
|||||||
-std=gnu++20 \
|
-std=gnu++20 \
|
||||||
-fno-rtti \
|
-fno-rtti \
|
||||||
-fno-exceptions \
|
-fno-exceptions \
|
||||||
|
-fmodules-ts \
|
||||||
$(SHARED_FLAGS)
|
$(SHARED_FLAGS)
|
||||||
|
|
||||||
# Internal linker flags that should not be changed by the user.
|
# Internal linker flags that should not be changed by the user.
|
||||||
|
|||||||
@@ -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);
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -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();
|
||||||
|
};
|
||||||
|
};
|
||||||
@@ -7,7 +7,9 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <Terminal/Terminal.hpp>
|
#include <Terminal/Terminal.hpp>
|
||||||
|
|
||||||
#include <Memory/Heap.hpp>
|
#include <Memory/Heap.hpp>
|
||||||
|
|
||||||
#include <Memory/PageFrameAllocator.hpp>
|
#include <Memory/PageFrameAllocator.hpp>
|
||||||
#include <Common/Panic.hpp>
|
#include <Common/Panic.hpp>
|
||||||
|
|
||||||
@@ -65,13 +67,14 @@ namespace kcp
|
|||||||
size_t size() {
|
size_t size() {
|
||||||
return sz;
|
return sz;
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
class NoMallocVector : public vector<T> {
|
class noHeapVector : public vector<T> {
|
||||||
size_t pages = 0;
|
size_t pages = 0;
|
||||||
public:
|
public:
|
||||||
NoMallocVector() {
|
noHeapVector() {
|
||||||
kout << "NoMallocVector: constructor called" << Kt::newline;
|
kout << "NoMallocVector: constructor called" << Kt::newline;
|
||||||
pages = 0;
|
pages = 0;
|
||||||
this->sz = 0;
|
this->sz = 0;
|
||||||
|
|||||||
+2
-1
@@ -96,7 +96,8 @@ extern "C" void kmain() {
|
|||||||
#if defined (__x86_64__)
|
#if defined (__x86_64__)
|
||||||
Hal::IDTInitialize();
|
Hal::IDTInitialize();
|
||||||
#endif
|
#endif
|
||||||
kcp::NoMallocVector<int> hello{};
|
kcp::noHeapVector<int> hello{};
|
||||||
|
|
||||||
hello.push_back(10);
|
hello.push_back(10);
|
||||||
hello.push_back(20);
|
hello.push_back(20);
|
||||||
hello.push_back(30);
|
hello.push_back(30);
|
||||||
|
|||||||
@@ -10,6 +10,8 @@
|
|||||||
#include <Libraries/Memory.hpp>
|
#include <Libraries/Memory.hpp>
|
||||||
#include <Terminal/Terminal.hpp>
|
#include <Terminal/Terminal.hpp>
|
||||||
|
|
||||||
|
#include <CppLib/Spinlock.hpp>
|
||||||
|
|
||||||
namespace Memory {
|
namespace Memory {
|
||||||
PageFrameAllocator::PageFrameAllocator(LargestSection section) {
|
PageFrameAllocator::PageFrameAllocator(LargestSection section) {
|
||||||
/* we need the virtual address rather than the physical address, so we call the helper */
|
/* we need the virtual address rather than the physical address, so we call the helper */
|
||||||
@@ -24,6 +26,8 @@ namespace Memory {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void* PageFrameAllocator::Allocate() {
|
void* PageFrameAllocator::Allocate() {
|
||||||
|
Lock.Aquire();
|
||||||
|
|
||||||
Page* current = head.next;
|
Page* current = head.next;
|
||||||
Page* prev = &head;
|
Page* prev = &head;
|
||||||
|
|
||||||
@@ -35,11 +39,15 @@ namespace Memory {
|
|||||||
|
|
||||||
if (new_size == 0) {
|
if (new_size == 0) {
|
||||||
prev->next = current->next;
|
prev->next = current->next;
|
||||||
|
|
||||||
|
Lock.Release();
|
||||||
return (void*)current;
|
return (void*)current;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
current->size -= 0x1000;
|
current->size -= 0x1000;
|
||||||
|
|
||||||
|
Lock.Release();
|
||||||
return (void*)current_end_addr - 0x1000;
|
return (void*)current_end_addr - 0x1000;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -48,6 +56,7 @@ namespace Memory {
|
|||||||
current = current->next;
|
current = current->next;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Lock.Release();
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -67,11 +76,13 @@ namespace Memory {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void PageFrameAllocator::Free(void* ptr) {
|
void PageFrameAllocator::Free(void* ptr) {
|
||||||
|
Lock.Aquire();
|
||||||
auto prev_next = head.next;
|
auto prev_next = head.next;
|
||||||
head.next = (Page*)ptr;
|
head.next = (Page*)ptr;
|
||||||
|
|
||||||
head.next->next = prev_next;
|
head.next->next = prev_next;
|
||||||
head.next->size = 0x1000;
|
head.next->size = 0x1000;
|
||||||
|
Lock.Release();
|
||||||
}
|
}
|
||||||
|
|
||||||
void PageFrameAllocator::Free(void* ptr, int n) {
|
void PageFrameAllocator::Free(void* ptr, int n) {
|
||||||
|
|||||||
@@ -7,6 +7,8 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include "Memmap.hpp"
|
#include "Memmap.hpp"
|
||||||
|
|
||||||
|
#include <CppLib/Spinlock.hpp>
|
||||||
|
|
||||||
namespace Memory {
|
namespace Memory {
|
||||||
class PageFrameAllocator {
|
class PageFrameAllocator {
|
||||||
struct Page {
|
struct Page {
|
||||||
@@ -15,7 +17,7 @@ namespace Memory {
|
|||||||
};
|
};
|
||||||
|
|
||||||
Page head{};
|
Page head{};
|
||||||
|
kcp::Spinlock Lock{};
|
||||||
LargestSection g_section;
|
LargestSection g_section;
|
||||||
public:
|
public:
|
||||||
PageFrameAllocator(LargestSection section);
|
PageFrameAllocator(LargestSection section);
|
||||||
|
|||||||
Reference in New Issue
Block a user