Common/MemArena: Add function for getting page size and alignment

This commit is contained in:
JosJuice 2023-12-29 16:19:41 +01:00
commit d6405669e3
5 changed files with 62 additions and 0 deletions

View file

@ -115,6 +115,16 @@ public:
///
void UnmapFromMemoryRegion(void* view, size_t size);
///
/// Return the system's page size.
///
size_t GetPageSize() const;
///
/// Return the system's required page alignment.
///
size_t GetPageAlignment() const;
private:
#ifdef _WIN32
WindowsMemoryRegion* EnsureSplitRegionForMapping(void* address, size_t size);

View file

@ -144,6 +144,16 @@ void MemArena::UnmapFromMemoryRegion(void* view, size_t size)
NOTICE_LOG_FMT(MEMMAP, "mmap failed");
}
size_t MemArena::GetPageSize() const
{
return sysconf(_SC_PAGESIZE);
}
size_t MemArena::GetPageAlignment() const
{
return GetPageSize();
}
LazyMemoryRegion::LazyMemoryRegion() = default;
LazyMemoryRegion::~LazyMemoryRegion()

View file

@ -3,6 +3,8 @@
#include "Common/MemArena.h"
#include <unistd.h>
#include "Common/Assert.h"
#include "Common/Logging/Log.h"
@ -163,6 +165,16 @@ void MemArena::UnmapFromMemoryRegion(void* view, size_t size)
}
}
size_t MemArena::GetPageSize() const
{
return getpagesize();
}
size_t MemArena::GetPageAlignment() const
{
return GetPageSize();
}
LazyMemoryRegion::LazyMemoryRegion() = default;
LazyMemoryRegion::~LazyMemoryRegion()

View file

@ -110,6 +110,16 @@ void MemArena::UnmapFromMemoryRegion(void* view, size_t size)
NOTICE_LOG_FMT(MEMMAP, "mmap failed");
}
size_t MemArena::GetPageSize() const
{
return sysconf(_SC_PAGESIZE);
}
size_t MemArena::GetPageAlignment() const
{
return GetPageSize();
}
LazyMemoryRegion::LazyMemoryRegion() = default;
LazyMemoryRegion::~LazyMemoryRegion()

View file

@ -438,6 +438,26 @@ void MemArena::UnmapFromMemoryRegion(void* view, size_t size)
UnmapViewOfFile(view);
}
size_t MemArena::GetPageSize() const
{
SYSTEM_INFO si;
GetSystemInfo(&si);
return si.dwPageSize;
}
size_t MemArena::GetPageAlignment() const
{
SYSTEM_INFO si;
GetSystemInfo(&si);
if (!m_memory_functions.m_address_MapViewOfFile3)
{
// In this case, we can only map pages that are 64K aligned.
// See https://devblogs.microsoft.com/oldnewthing/20031008-00/?p=42223
return std::max<size_t>(si.dwPageSize, 64 * 1024);
}
return si.dwPageSize;
}
LazyMemoryRegion::LazyMemoryRegion()
{
InitWindowsMemoryFunctions(&m_memory_functions);