feat(kcp, pfa): Add kcp::Spinlock class
This commit is contained in:
@@ -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
|
||||
#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
@@ -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();
|
||||
}
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user