Add page allocation utility functions
This commit is contained in:
@@ -316,6 +316,25 @@ public:
|
|||||||
/// <param name="ptr">A pointer to the memory block to deallocate.</param>
|
/// <param name="ptr">A pointer to the memory block to deallocate.</param>
|
||||||
static void Free(void* ptr) = delete;
|
static void Free(void* ptr) = delete;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Returns the OS's default page size that can be used with AllocatePages.
|
||||||
|
/// </summary>
|
||||||
|
static uint64 GetDefaultPageSize() = delete;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Allocates pages memory block.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="numPages">The number of pages to allocate.</param>
|
||||||
|
/// <param name="pageSize">The size of single page. Use GetDefaultPageSize() or provide compatible, custom size.</param>
|
||||||
|
/// <returns>The pointer to the allocated pages in memory.</returns>
|
||||||
|
static void* AllocatePages(uint64 numPages, uint64 pageSize) = delete;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Frees allocated pages memory block.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="ptr">The pointer to the pages to deallocate.</param>
|
||||||
|
static void FreePages(void* ptr) = delete;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
@@ -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()
|
uint64 UnixPlatform::GetCurrentProcessId()
|
||||||
{
|
{
|
||||||
return getpid();
|
return getpid();
|
||||||
|
|||||||
@@ -16,6 +16,9 @@ public:
|
|||||||
// [PlatformBase]
|
// [PlatformBase]
|
||||||
static void* Allocate(uint64 size, uint64 alignment);
|
static void* Allocate(uint64 size, uint64 alignment);
|
||||||
static void Free(void* ptr);
|
static void Free(void* ptr);
|
||||||
|
static uint64 GetDefaultPageSize();
|
||||||
|
static void* AllocatePages(uint64 numPages, uint64 pageSize);
|
||||||
|
static void FreePages(void* ptr);
|
||||||
static uint64 GetCurrentProcessId();
|
static uint64 GetCurrentProcessId();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -298,6 +298,29 @@ void Win32Platform::AtomicStore(int64 volatile* dst, int64 value)
|
|||||||
InterlockedExchange64(dst, 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()
|
bool Win32Platform::Is64BitPlatform()
|
||||||
{
|
{
|
||||||
#ifdef PLATFORM_64BITS
|
#ifdef PLATFORM_64BITS
|
||||||
|
|||||||
@@ -43,6 +43,9 @@ public:
|
|||||||
{
|
{
|
||||||
_aligned_free(ptr);
|
_aligned_free(ptr);
|
||||||
}
|
}
|
||||||
|
static uint64 GetDefaultPageSize();
|
||||||
|
static void* AllocatePages(uint64 numPages, uint64 pageSize);
|
||||||
|
static void FreePages(void* ptr);
|
||||||
static bool Is64BitPlatform();
|
static bool Is64BitPlatform();
|
||||||
static BatteryInfo GetBatteryInfo();
|
static BatteryInfo GetBatteryInfo();
|
||||||
static CPUInfo GetCPUInfo();
|
static CPUInfo GetCPUInfo();
|
||||||
|
|||||||
Reference in New Issue
Block a user