VirtualMemory
(ply-system.h)
The VirtualMemory class is a platform-independent wrapper for mapping virtual memory to physical memory.
In C++ applications, memory is represented as a 32-bit or 64-bit address space known as virtual memory, which is divided into fixed-sized pages. Most pages are initially unusable and will cause an access violation or segmentation fault if accessed. To make pages usable, they must be mapped to physical memory by the underlying operating system.
System Information
static VirtualMemory::Properties VirtualMemory::getProperties()Returns information about the system's virtual memory page size and allocation alignment.
VirtualMemory::Propertieshas the following data members:uptr regionAlignmentreserveRegionandallocRegionsizes must be a multiple of this.uptr pageSizecommitPagessizes must be a multiple of this.static VirtualMemory::SystemStats VirtualMemory::getSystemStats()Returns platform-specific statistics about the current process's virtual memory usage.
VirtualMemory::SystemStatshas the following data members on Windows:uptr privateUsagePrivate memory allocated by the process. uptr workingSetSizePhysical memory in the process working set. On other platforms:
uptr virtualSizeVirtual address space used by the process. uptr residentSizePhysical memory resident for the process.
Managing Pages
static void* VirtualMemory::reserveRegion(uptr numBytes)Reserves a region of address space. Memory pages are initially uncommitted. Returns
nullptron failure.numBytesmust be a multiple ofregionAlignment.static void VirtualMemory::unreserveRegion(void* addr, uptr numReservedBytes, uptr numCommittedBytes)Unreserves a region of address space.
numReservedBytesmust match the argument passed toreserveRegion. Caller is responsible for passing the correctnumCommittedBytes, otherwise stats will get out of sync.static void VirtualMemory::commitPages(void* addr, uptr numBytes)Commits a subregion of reserved address space, making it legal to read and write to the subregion.
addrmust be aligned topageSizeandnumBytesmust be a multiple ofpageSize.static void VirtualMemory::decommitPages(void* addr, uptr numBytes)Decommits a subregion of previously committed memory.
addrmust be aligned topageSizeandnumBytesmust be a multiple ofpageSize.
Allocating Large Blocks
static void* VirtualMemory::allocRegion(uptr numBytes)Reserves and commits a region of address space. Returns
nullptron failure. Free usingfreeRegion. Don't decommit any pages in the returned region, otherwise stats will get out of sync.numBytesmust be a multiple ofregionAlignment.static void VirtualMemory::freeRegion(void* addr, uptr numBytes)Decommits and unreserves a region of address space.
numBytesmust match the argument passed toallocRegion.
Usage Stats
static Atomic<uptr> totalReservedBytes |
The current total amount of address space that was reserved using allocRegion or reserveRegion. |
static Atomic<uptr> totalCommittedBytes |
The current total amount of memory that was committed using allocRegion or commitPages. |