From 18f05c6d830afd485bdb40442b4a3e06b0778757 Mon Sep 17 00:00:00 2001 From: Alex Date: Mon, 26 Dec 2022 08:40:09 +0200 Subject: [PATCH] Updated memory manager --- Core/Memory/{MemMgr.cpp => MemoryManager.cpp} | 32 +++++++++++++++++++ include/memory.hpp | 2 ++ 2 files changed, 34 insertions(+) rename Core/Memory/{MemMgr.cpp => MemoryManager.cpp} (68%) diff --git a/Core/Memory/MemMgr.cpp b/Core/Memory/MemoryManager.cpp similarity index 68% rename from Core/Memory/MemMgr.cpp rename to Core/Memory/MemoryManager.cpp index 34bde43..5cef757 100644 --- a/Core/Memory/MemMgr.cpp +++ b/Core/Memory/MemoryManager.cpp @@ -11,6 +11,38 @@ namespace Memory return FROM_PAGES(Size); } + bool MemMgr::Add(void *Address, size_t Count) + { + for (size_t i = 0; i < AllocatedPagesList.size(); i++) + { + if (AllocatedPagesList[i].Address == Address) + { + error("Address already exists!"); + return false; + } + + if ((uintptr_t)Address < (uintptr_t)AllocatedPagesList[i].Address) + { + if ((uintptr_t)Address + (Count * PAGE_SIZE) > (uintptr_t)AllocatedPagesList[i].Address) + { + error("Address intersects with an allocated page!"); + return false; + } + } + else + { + if ((uintptr_t)AllocatedPagesList[i].Address + (AllocatedPagesList[i].PageCount * PAGE_SIZE) > (uintptr_t)Address) + { + error("Address intersects with an allocated page!"); + return false; + } + } + } + + AllocatedPagesList.push_back({Address, Count}); + return true; + } + void *MemMgr::RequestPages(size_t Count) { void *Address = KernelAllocator.RequestPages(Count); diff --git a/include/memory.hpp b/include/memory.hpp index 01c8db3..30b8b0d 100644 --- a/include/memory.hpp +++ b/include/memory.hpp @@ -648,6 +648,8 @@ namespace Memory public: uint64_t GetAllocatedMemorySize(); + bool Add(void *Address, size_t Count); + void *RequestPages(size_t Count); void FreePages(void *Address, size_t Count);