From 161e9bd37305d10a18256bf28b5607a5113cba49 Mon Sep 17 00:00:00 2001 From: Damian Korczowski Date: Sun, 31 Jan 2021 01:03:32 +0100 Subject: [PATCH 1/4] Add page allocation utility functions --- Source/Engine/Platform/Base/PlatformBase.h | 19 +++++++++++++++ Source/Engine/Platform/Unix/UnixPlatform.cpp | 19 +++++++++++++++ Source/Engine/Platform/Unix/UnixPlatform.h | 3 +++ .../Engine/Platform/Win32/Win32Platform.cpp | 23 +++++++++++++++++++ Source/Engine/Platform/Win32/Win32Platform.h | 3 +++ 5 files changed, 67 insertions(+) diff --git a/Source/Engine/Platform/Base/PlatformBase.h b/Source/Engine/Platform/Base/PlatformBase.h index d2d34c88f..7c200172d 100644 --- a/Source/Engine/Platform/Base/PlatformBase.h +++ b/Source/Engine/Platform/Base/PlatformBase.h @@ -316,6 +316,25 @@ public: /// A pointer to the memory block to deallocate. static void Free(void* ptr) = delete; + /// + /// Returns the OS's default page size that can be used with AllocatePages. + /// + static uint64 GetDefaultPageSize() = delete; + + /// + /// Allocates pages memory block. + /// + /// The number of pages to allocate. + /// The size of single page. Use GetDefaultPageSize() 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..6255e3588 100644 --- a/Source/Engine/Platform/Unix/UnixPlatform.cpp +++ b/Source/Engine/Platform/Unix/UnixPlatform.cpp @@ -51,6 +51,25 @@ void UnixPlatform::Free(void* ptr) } } +uint64 Win32Platform::GetDefaultPageSize() +{ + return 16; +} + +void* Win32Platform::AllocatePages(uint64 numPages, uint64 pageSize) +{ + const uint64 numBytes = numPages * pageSize; + + // Fallback to malloc + return malloc(numBytes); +} + +void Win32Platform::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..9996e3d7e 100644 --- a/Source/Engine/Platform/Unix/UnixPlatform.h +++ b/Source/Engine/Platform/Unix/UnixPlatform.h @@ -16,6 +16,9 @@ public: // [PlatformBase] static void* Allocate(uint64 size, uint64 alignment); static void Free(void* ptr); + static uint64 GetDefaultPageSize(); + 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..39d52a64c 100644 --- a/Source/Engine/Platform/Win32/Win32Platform.cpp +++ b/Source/Engine/Platform/Win32/Win32Platform.cpp @@ -298,6 +298,29 @@ void Win32Platform::AtomicStore(int64 volatile* dst, int64 value) InterlockedExchange64(dst, value); } +uint64 Win32Platform::GetDefaultPageSize() +{ + SYSTEM_INFO systemInfo; + GetSystemInfo(&systemInfo); + + // Return the page size obtained from system + return systemInfo.dwPageSize; +} + +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..a1930fad8 100644 --- a/Source/Engine/Platform/Win32/Win32Platform.h +++ b/Source/Engine/Platform/Win32/Win32Platform.h @@ -43,6 +43,9 @@ public: { _aligned_free(ptr); } + static uint64 GetDefaultPageSize(); + static void* AllocatePages(uint64 numPages, uint64 pageSize); + static void FreePages(void* ptr); static bool Is64BitPlatform(); static BatteryInfo GetBatteryInfo(); static CPUInfo GetCPUInfo(); From 15028c60ba7f5e19745fa054008c9274972aefe6 Mon Sep 17 00:00:00 2001 From: Damian Korczowski Date: Sun, 21 Feb 2021 14:44:16 +0100 Subject: [PATCH 2/4] Remove GetDefaultPageSize Duplicate. We already have the API for this: `Platform::GetCPUInfo().PageSize`. --- Source/Engine/Platform/Base/PlatformBase.h | 5 ----- Source/Engine/Platform/Unix/UnixPlatform.cpp | 5 ----- Source/Engine/Platform/Unix/UnixPlatform.h | 1 - Source/Engine/Platform/Win32/Win32Platform.cpp | 9 --------- Source/Engine/Platform/Win32/Win32Platform.h | 1 - 5 files changed, 21 deletions(-) diff --git a/Source/Engine/Platform/Base/PlatformBase.h b/Source/Engine/Platform/Base/PlatformBase.h index 7c200172d..d640eb252 100644 --- a/Source/Engine/Platform/Base/PlatformBase.h +++ b/Source/Engine/Platform/Base/PlatformBase.h @@ -316,11 +316,6 @@ public: /// A pointer to the memory block to deallocate. static void Free(void* ptr) = delete; - /// - /// Returns the OS's default page size that can be used with AllocatePages. - /// - static uint64 GetDefaultPageSize() = delete; - /// /// Allocates pages memory block. /// diff --git a/Source/Engine/Platform/Unix/UnixPlatform.cpp b/Source/Engine/Platform/Unix/UnixPlatform.cpp index 6255e3588..8cda61cc8 100644 --- a/Source/Engine/Platform/Unix/UnixPlatform.cpp +++ b/Source/Engine/Platform/Unix/UnixPlatform.cpp @@ -51,11 +51,6 @@ void UnixPlatform::Free(void* ptr) } } -uint64 Win32Platform::GetDefaultPageSize() -{ - return 16; -} - void* Win32Platform::AllocatePages(uint64 numPages, uint64 pageSize) { const uint64 numBytes = numPages * pageSize; diff --git a/Source/Engine/Platform/Unix/UnixPlatform.h b/Source/Engine/Platform/Unix/UnixPlatform.h index 9996e3d7e..12d4e5bc2 100644 --- a/Source/Engine/Platform/Unix/UnixPlatform.h +++ b/Source/Engine/Platform/Unix/UnixPlatform.h @@ -16,7 +16,6 @@ public: // [PlatformBase] static void* Allocate(uint64 size, uint64 alignment); static void Free(void* ptr); - static uint64 GetDefaultPageSize(); 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 39d52a64c..06c9e542b 100644 --- a/Source/Engine/Platform/Win32/Win32Platform.cpp +++ b/Source/Engine/Platform/Win32/Win32Platform.cpp @@ -298,15 +298,6 @@ void Win32Platform::AtomicStore(int64 volatile* dst, int64 value) InterlockedExchange64(dst, value); } -uint64 Win32Platform::GetDefaultPageSize() -{ - SYSTEM_INFO systemInfo; - GetSystemInfo(&systemInfo); - - // Return the page size obtained from system - return systemInfo.dwPageSize; -} - void* Win32Platform::AllocatePages(uint64 numPages, uint64 pageSize) { const uint64 numBytes = numPages * pageSize; diff --git a/Source/Engine/Platform/Win32/Win32Platform.h b/Source/Engine/Platform/Win32/Win32Platform.h index a1930fad8..ffb1d1d87 100644 --- a/Source/Engine/Platform/Win32/Win32Platform.h +++ b/Source/Engine/Platform/Win32/Win32Platform.h @@ -43,7 +43,6 @@ public: { _aligned_free(ptr); } - static uint64 GetDefaultPageSize(); static void* AllocatePages(uint64 numPages, uint64 pageSize); static void FreePages(void* ptr); static bool Is64BitPlatform(); From 5bdf4dc74e29a8b82f5f82508faf1e07fd2ed93c Mon Sep 17 00:00:00 2001 From: Damian Korczowski Date: Sun, 21 Feb 2021 14:44:47 +0100 Subject: [PATCH 3/4] Fix Unix platform --- Source/Engine/Platform/Unix/UnixPlatform.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Source/Engine/Platform/Unix/UnixPlatform.cpp b/Source/Engine/Platform/Unix/UnixPlatform.cpp index 8cda61cc8..cb734bfcd 100644 --- a/Source/Engine/Platform/Unix/UnixPlatform.cpp +++ b/Source/Engine/Platform/Unix/UnixPlatform.cpp @@ -51,7 +51,7 @@ void UnixPlatform::Free(void* ptr) } } -void* Win32Platform::AllocatePages(uint64 numPages, uint64 pageSize) +void* UnixPlatform::AllocatePages(uint64 numPages, uint64 pageSize) { const uint64 numBytes = numPages * pageSize; @@ -59,7 +59,7 @@ void* Win32Platform::AllocatePages(uint64 numPages, uint64 pageSize) return malloc(numBytes); } -void Win32Platform::FreePages(void* ptr) +void UnixPlatform::FreePages(void* ptr) { // Fallback to free free(ptr); From 1b234fd32b019841b1c3b46deb589feff944985c Mon Sep 17 00:00:00 2001 From: Damian Korczowski Date: Sun, 21 Feb 2021 14:45:31 +0100 Subject: [PATCH 4/4] Update AllocatePages comment to point to the correct page size API --- Source/Engine/Platform/Base/PlatformBase.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Source/Engine/Platform/Base/PlatformBase.h b/Source/Engine/Platform/Base/PlatformBase.h index d640eb252..e97ce2b34 100644 --- a/Source/Engine/Platform/Base/PlatformBase.h +++ b/Source/Engine/Platform/Base/PlatformBase.h @@ -320,7 +320,7 @@ public: /// Allocates pages memory block. /// /// The number of pages to allocate. - /// The size of single page. Use GetDefaultPageSize() or provide compatible, custom size. + /// 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;