1
0
mirror of https://github.com/momo5502/hypervisor.git synced 2025-07-05 10:41:50 +00:00

Better memory management

This commit is contained in:
momo5502
2022-03-27 11:57:26 +02:00
parent 520bdf3aea
commit 1bbd9e9c73
5 changed files with 114 additions and 56 deletions

74
src/driver/memory.cpp Normal file
View File

@ -0,0 +1,74 @@
#include "std_include.hpp"
#include "memory.hpp"
namespace memory
{
namespace
{
void* allocate_aligned_memory_internal(const size_t size)
{
PHYSICAL_ADDRESS lowest{}, highest{};
lowest.QuadPart = 0;
highest.QuadPart = lowest.QuadPart - 1;
#if (NTDDI_VERSION >= NTDDI_VISTA)
return MmAllocateContiguousNodeMemory(size,
lowest,
highest,
lowest,
PAGE_READWRITE,
KeGetCurrentNodeNumber());
#else
return MmAllocateContiguousMemory(size, highest);
#endif
}
}
_IRQL_requires_max_(DISPATCH_LEVEL)
void free_aligned_memory(void* memory)
{
if (memory)
{
MmFreeContiguousMemory(memory);
}
}
_Must_inspect_result_
_IRQL_requires_max_(DISPATCH_LEVEL)
void* allocate_aligned_memory(const size_t size)
{
void* memory = allocate_aligned_memory_internal(size);
if (memory)
{
RtlSecureZeroMemory(memory, size);
}
return memory;
}
_Must_inspect_result_
_IRQL_requires_max_(DISPATCH_LEVEL)
void* allocate_non_paged_memory(const size_t size)
{
void* memory = ExAllocatePoolWithTag(NonPagedPool, size, 'MOMO');
if (memory)
{
RtlSecureZeroMemory(memory, size);
}
return memory;
}
_IRQL_requires_max_(DISPATCH_LEVEL)
void free_non_paged_memory(void* memory)
{
if (memory)
{
ExFreePool(memory);
}
}
}