Reserves a contiguous slice of virtual memory committing just a portion of it.
More...
|
|
| VirtualMemory (const VirtualMemory &)=delete |
| |
|
| VirtualMemory (VirtualMemory &&)=delete |
| |
|
VirtualMemory & | operator= (const VirtualMemory &)=delete |
| |
|
VirtualMemory & | operator= (VirtualMemory &&)=delete |
| |
| bool | reserve (size_t maxCapacityInBytes) |
| | Reserves a large block of virtual memory of size maxCapacityInBytes.
|
| |
| void | release () |
| | Reclaims the entire virtual memory block (reserved with VirtualMemory::reserve)
|
| |
| bool | commit (size_t sizeInBytes) |
| | Ensures at least sizeInBytes to be committed / accessible from the large maxCapacityInBytes block.
|
| |
| bool | decommit (size_t sizeInBytes) |
| | Reclaims all unused pages past sizeInBytes (previously committed with VirtualMemory::commit)
|
| |
| size_t | size () const |
| | Returns how many bytes are currently committed / accessible.
|
| |
| size_t | capacity () const |
| | Returns how many bytes are currently reserved.
|
| |
| void * | data () |
| | Returns a pointer to the start of the reserved virtual memory.
|
| |
| const void * | data () const |
| | Returns a pointer to the start of the reserved virtual memory.
|
| |
Reserves a contiguous slice of virtual memory committing just a portion of it.
This class is useful on 64-bit systems where the address space is so large that it's feasible reserving large chunks of memory to commit and de-commit (shrink) as needed.
Reservation ensures that the returned address will not change and will be sized in multiples of system page size.
- Note
- Memory must be committed in order to be read or written, occupying physical memory pages.
- Warning
- This class has no defined destructor so memory MUST be released calling VirtualMemory::release
const size_t moreThanOnePageSize = VirtualMemory::getPageSize() + 1024;
const size_t lessThanOnePageSize = VirtualMemory::getPageSize() - 1024;
void* reference = Memory::allocate(moreThanOnePageSize, 1);
memset(reference, 1, moreThanOnePageSize);
auto releaseLater = MakeDeferred([&] { Memory::release(reference); });
SC_TEST_EXPECT(virtualMemory.reserve(2 * VirtualMemory::getPageSize()));
char* memory = static_cast<char*>(virtualMemory.data());
memset(memory, 1, lessThanOnePageSize);
memset(memory + lessThanOnePageSize, 1, moreThanOnePageSize - lessThanOnePageSize);
virtualMemory.release();