diff --git a/Source/Engine/Platform/Base/PlatformBase.h b/Source/Engine/Platform/Base/PlatformBase.h index d2d34c88f..e97ce2b34 100644 --- a/Source/Engine/Platform/Base/PlatformBase.h +++ b/Source/Engine/Platform/Base/PlatformBase.h @@ -316,6 +316,20 @@ public: /// A pointer to the memory block to deallocate. static void Free(void* ptr) = delete; + /// + /// Allocates pages memory block. + /// + /// The number of pages to allocate. + /// The size of single page. Use Platform::GetCPUInfo().PageSize or provide compatible, custom size. + /// The pointer to the allocated pages in memory. + static void* AllocatePages(uint64 numPages, uint64 pageSize) = delete; + + /// + /// Frees allocated pages memory block. + /// + /// The pointer to the pages to deallocate. + static void FreePages(void* ptr) = delete; + public: /// diff --git a/Source/Engine/Platform/Unix/UnixPlatform.cpp b/Source/Engine/Platform/Unix/UnixPlatform.cpp index 42b788649..cb734bfcd 100644 --- a/Source/Engine/Platform/Unix/UnixPlatform.cpp +++ b/Source/Engine/Platform/Unix/UnixPlatform.cpp @@ -51,6 +51,20 @@ void UnixPlatform::Free(void* ptr) } } +void* UnixPlatform::AllocatePages(uint64 numPages, uint64 pageSize) +{ + const uint64 numBytes = numPages * pageSize; + + // Fallback to malloc + return malloc(numBytes); +} + +void UnixPlatform::FreePages(void* ptr) +{ + // Fallback to free + free(ptr); +} + uint64 UnixPlatform::GetCurrentProcessId() { return getpid(); diff --git a/Source/Engine/Platform/Unix/UnixPlatform.h b/Source/Engine/Platform/Unix/UnixPlatform.h index e0041c604..12d4e5bc2 100644 --- a/Source/Engine/Platform/Unix/UnixPlatform.h +++ b/Source/Engine/Platform/Unix/UnixPlatform.h @@ -16,6 +16,8 @@ public: // [PlatformBase] static void* Allocate(uint64 size, uint64 alignment); static void Free(void* ptr); + static void* AllocatePages(uint64 numPages, uint64 pageSize); + static void FreePages(void* ptr); static uint64 GetCurrentProcessId(); }; diff --git a/Source/Engine/Platform/Win32/Win32Platform.cpp b/Source/Engine/Platform/Win32/Win32Platform.cpp index b01b96feb..06c9e542b 100644 --- a/Source/Engine/Platform/Win32/Win32Platform.cpp +++ b/Source/Engine/Platform/Win32/Win32Platform.cpp @@ -298,6 +298,20 @@ void Win32Platform::AtomicStore(int64 volatile* dst, int64 value) InterlockedExchange64(dst, value); } +void* Win32Platform::AllocatePages(uint64 numPages, uint64 pageSize) +{ + const uint64 numBytes = numPages * pageSize; + + // Use VirtualAlloc to allocate page-aligned memory + return VirtualAlloc(nullptr, numBytes, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE); +} + +void Win32Platform::FreePages(void* ptr) +{ + // Free page-aligned memory + VirtualFree(ptr, 0, MEM_RELEASE); +} + bool Win32Platform::Is64BitPlatform() { #ifdef PLATFORM_64BITS diff --git a/Source/Engine/Platform/Win32/Win32Platform.h b/Source/Engine/Platform/Win32/Win32Platform.h index cde506278..ffb1d1d87 100644 --- a/Source/Engine/Platform/Win32/Win32Platform.h +++ b/Source/Engine/Platform/Win32/Win32Platform.h @@ -43,6 +43,8 @@ public: { _aligned_free(ptr); } + static void* AllocatePages(uint64 numPages, uint64 pageSize); + static void FreePages(void* ptr); static bool Is64BitPlatform(); static BatteryInfo GetBatteryInfo(); static CPUInfo GetCPUInfo();