feat(pfa, kcp): Add kcp::NoMallocVector and realloc in PFA

This commit is contained in:
Daniel Hammer
2025-03-12 17:18:31 +01:00
parent 53739b0059
commit bd8a1fdbca
4 changed files with 72 additions and 4 deletions
+40 -2
View File
@@ -8,6 +8,8 @@
#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 <Common/Panic.hpp>
namespace kcp namespace kcp
{ {
@@ -16,6 +18,7 @@ namespace kcp
protected: protected:
T* array; T* array;
std::size_t sz; std::size_t sz;
std::size_t capacity;
public: public:
vector() vector()
@@ -26,7 +29,7 @@ namespace kcp
T& operator[](std::size_t position) T& operator[](std::size_t position)
{ {
if (position > (sz - 1)) { if (position > (capacity - 1)) {
kerr << "Vector out of bounds caught!" << Kt::newline; kerr << "Vector out of bounds caught!" << Kt::newline;
return at(0); return at(0);
@@ -44,7 +47,11 @@ namespace kcp
size_t push_back(T value) { size_t push_back(T value) {
size_t _sz = sz; size_t _sz = sz;
array = (T *)Memory::g_heap->Realloc(array, sizeof(T) * (sz + 1)); if (capacity < sz + 1) {
array = (T *)Memory::g_heap->Realloc(array, sizeof(T) * (sz + 1));
capacity++;
}
array[sz++] = value; array[sz++] = value;
return _sz; return _sz;
@@ -59,4 +66,35 @@ namespace kcp
return sz; return sz;
} }
}; };
template<typename T>
class NoMallocVector : public vector<T> {
size_t pages = 0;
public:
NoMallocVector() {
kout << "NoMallocVector: constructor called" << Kt::newline;
pages = 0;
this->sz = 0;
this->array = nullptr;
}
size_t push_back(T value) {
size_t _sz = this->sz;
if ((this->pages * 0x1000) <= (sizeof(T) * this->sz)) {
if (Memory::g_pfa != nullptr) {
this->array = (T *)Memory::g_pfa->ReallocConsecutive(this->array, pages + 1);
pages++;
}
else
{
Panic("kcp::NoMallocVector used when Memory::g_pfa is not initialized!", nullptr);
}
}
this->array[this->sz++] = value;
return _sz;
}
};
}; };
+6 -2
View File
@@ -75,8 +75,6 @@ extern "C" void kmain() {
framebuffer->blue_mask_shift framebuffer->blue_mask_shift
); );
kout << "OS220 research kernel project" << "\n" << "Written by Daniel Hammer" << "\n" << "\n";
#if defined (__x86_64__) #if defined (__x86_64__)
Hal::PrepareGDT(); Hal::PrepareGDT();
Hal::BridgeLoadGDT(); Hal::BridgeLoadGDT();
@@ -98,6 +96,12 @@ extern "C" void kmain() {
#if defined (__x86_64__) #if defined (__x86_64__)
Hal::IDTInitialize(); Hal::IDTInitialize();
#endif #endif
kcp::NoMallocVector<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(); hcf();
} }
+24
View File
@@ -7,6 +7,9 @@
#include "PageFrameAllocator.hpp" #include "PageFrameAllocator.hpp"
#include "HHDM.hpp" #include "HHDM.hpp"
#include <Libraries/Memory.hpp>
#include <Terminal/Terminal.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 */
@@ -48,6 +51,21 @@ namespace Memory {
return nullptr; return nullptr;
} }
void* PageFrameAllocator::ReallocConsecutive(void* ptr, int n) {
auto first = Allocate();
for (int i = 0; i < n - 1; i++) {
Allocate();
}
if (ptr != nullptr) {
memcpy(first, ptr, n);
Free(ptr);
}
return first;
}
void PageFrameAllocator::Free(void* ptr) { void PageFrameAllocator::Free(void* ptr) {
auto prev_next = head.next; auto prev_next = head.next;
head.next = (Page*)ptr; head.next = (Page*)ptr;
@@ -55,4 +73,10 @@ namespace Memory {
head.next->next = prev_next; head.next->next = prev_next;
head.next->size = 0x1000; head.next->size = 0x1000;
} }
void PageFrameAllocator::Free(void* ptr, int n) {
for (int i = 0; i < n; i++) {
Free((void*)(uint64_t)ptr + (0x1000 * n));
}
}
}; };
+2
View File
@@ -21,7 +21,9 @@ public:
PageFrameAllocator(LargestSection section); PageFrameAllocator(LargestSection section);
void* Allocate(); void* Allocate();
void* ReallocConsecutive(void* ptr, int n);
void Free(void* ptr); void Free(void* ptr);
void Free(void* ptr, int n);
}; };
extern PageFrameAllocator* g_pfa; extern PageFrameAllocator* g_pfa;