Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using a local clone.
AscendingPageAllocatoris a fast and safe allocator that rounds all allocations to multiples of the system's page size. It reserves a range of virtual addresses (using mmap on Posix and VirtualAlloc on Windows) and allocates memory at consecutive virtual addresses.When a chunk of memory is requested, the allocator finds a range of virtual pages that satisfy the requested size, changing their protection to read/write using OS primitives (mprotect and VirtualProtect, respectively). The physical memory is allocated on demand, when the pages are accessed. Deallocation removes any read/write permissions from the target pages and notifies the OS to reclaim the physical memory, while keeping the virtual memory. Because the allocator does not reuse memory, any dangling references to deallocated memory will always result in deterministically crashing the process.See Also:Project Snoflake for the general approach.
- The allocator receives as a parameter the size in pages of the virtual address range
- Rounds the allocation size to the next multiple of the page size. The allocation only reserves a range of virtual pages but the actual physical memory is allocated on demand, when accessing the memory.Parameters:
allocateReturns:null on failure or if the requested size exceeds the remaining capacity.
- Rounds the allocation size to the next multiple of the page size. The allocation only reserves
arange of virtual pages but the actual physical memory is allocated on demand, when accessing the memory.The allocated memory is aligned to the specified alignment
Bytes to allocate uint
AlignmentReturns:null on failure or if the requested size exceeds the remaining capacity.
- Rounds the requested size to the next multiple of the page size.
- Decommit all physical memory associated with the buffer given as parameter, but keep the range of virtual addresses.On POSIX systems
deallocatecalls mmap with `MAP_FIXED' a second time to decommit the memory. On Windows, it uses VirtualFree with MEM_DECOMMIT.
- Returns Ternary.yes if the passed buffer is inside the range of virtual adresses. Does not guarantee that the passed buffer is still valid.
- Removes the memory mapping causing all physical memory to be decommited and the virtual address space to be reclaimed.
- Returns the available size for further allocations in bytes.
- If the passed buffer is not the last allocation, then
deltacan be at most the number of bytes left on the last page. Otherwise, we can
expandthe last allocation until the end of the virtual address range.
- Returns Ternary.yes if the allocator does not contain any alive objects and Ternary.no otherwise.