Rounds the mapping size to the next multiple of the page size and calls the OS primitive responsible for creating memory mappings: mmap on POSIX and VirtualAlloc on Windows.
Unmaps the whole virtual address range on destruction.
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.
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.
Decommit all physical memory associated with the buffer given as parameter, but keep the range of virtual addresses.
Removes the memory mapping causing all physical memory to be decommited and the virtual address space to be reclaimed.
Returns Ternary.yes if the allocator does not contain any alive objects and Ternary.no otherwise.
If the passed buffer is not the last allocation, then delta can be at most the number of bytes left on the last page. Otherwise, we can expand the last allocation until the end of the virtual address range.
Returns the available size for further allocations in bytes.
Rounds the requested size to the next multiple of the page size.
Returns Ternary.yes if the passed buffer is inside the range of virtual adresses. Does not guarantee that the passed buffer is still valid.
import core.memory : pageSize; size_t numPages = 100; void[] buf; void[] prevBuf = null; AscendingPageAllocator a = AscendingPageAllocator(numPages * pageSize); foreach (i; 0 .. numPages) { // Allocation is rounded up to page size buf = a.allocate(pageSize - 100); assert(buf.length == pageSize - 100); // Allocations are served at increasing addresses if (prevBuf) assert(prevBuf.ptr + pageSize == buf.ptr); assert(a.deallocate(buf)); prevBuf = buf; }
Simple Fast and Safe Manual Memory Management for the general approach.
AscendingPageAllocator is 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.