diff --git a/kernel/src/CppLib/Vector.hpp b/kernel/src/CppLib/Vector.hpp index 77cde59..8cb00cc 100644 --- a/kernel/src/CppLib/Vector.hpp +++ b/kernel/src/CppLib/Vector.hpp @@ -8,6 +8,8 @@ #include #include #include +#include +#include namespace kcp { @@ -16,6 +18,7 @@ namespace kcp protected: T* array; std::size_t sz; + std::size_t capacity; public: vector() @@ -26,7 +29,7 @@ namespace kcp T& operator[](std::size_t position) { - if (position > (sz - 1)) { + if (position > (capacity - 1)) { kerr << "Vector out of bounds caught!" << Kt::newline; return at(0); @@ -44,7 +47,11 @@ namespace kcp size_t push_back(T value) { 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; return _sz; @@ -59,4 +66,35 @@ namespace kcp return sz; } }; + + template + class NoMallocVector : public vector { + 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; + } + }; }; \ No newline at end of file diff --git a/kernel/src/Main.cpp b/kernel/src/Main.cpp index 24e79dc..cc51ab6 100644 --- a/kernel/src/Main.cpp +++ b/kernel/src/Main.cpp @@ -75,8 +75,6 @@ extern "C" void kmain() { framebuffer->blue_mask_shift ); - kout << "OS220 research kernel project" << "\n" << "Written by Daniel Hammer" << "\n" << "\n"; - #if defined (__x86_64__) Hal::PrepareGDT(); Hal::BridgeLoadGDT(); @@ -98,6 +96,12 @@ extern "C" void kmain() { #if defined (__x86_64__) Hal::IDTInitialize(); #endif + kcp::NoMallocVector 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(); } diff --git a/kernel/src/Memory/PageFrameAllocator.cpp b/kernel/src/Memory/PageFrameAllocator.cpp index ba3f5a3..461c53c 100644 --- a/kernel/src/Memory/PageFrameAllocator.cpp +++ b/kernel/src/Memory/PageFrameAllocator.cpp @@ -7,6 +7,9 @@ #include "PageFrameAllocator.hpp" #include "HHDM.hpp" +#include +#include + namespace Memory { PageFrameAllocator::PageFrameAllocator(LargestSection section) { /* we need the virtual address rather than the physical address, so we call the helper */ @@ -48,6 +51,21 @@ namespace Memory { 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) { auto prev_next = head.next; head.next = (Page*)ptr; @@ -55,4 +73,10 @@ namespace Memory { head.next->next = prev_next; 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)); + } + } }; \ No newline at end of file diff --git a/kernel/src/Memory/PageFrameAllocator.hpp b/kernel/src/Memory/PageFrameAllocator.hpp index 118eb6d..cab85ef 100644 --- a/kernel/src/Memory/PageFrameAllocator.hpp +++ b/kernel/src/Memory/PageFrameAllocator.hpp @@ -21,7 +21,9 @@ public: PageFrameAllocator(LargestSection section); void* Allocate(); + void* ReallocConsecutive(void* ptr, int n); void Free(void* ptr); + void Free(void* ptr, int n); }; extern PageFrameAllocator* g_pfa;