//to reserve virtual address space //equivalent of VirtualAlloc(NULL, size, MEM_RESERVE, PAGE_NOACCESS) void* ptr = mmap(NULL, size, PROT_NONE, (MAP_PRIVATE | MAP_ANON), -1, 0); msync(ptr, size, (MS_SYNC | MS_INVALIDATE)); //to free ALL virtual address space //equivalent of VirtualFree(addr, 0, MEM_RELEASE) //where "size" is the size of the entire virtual address space and "addr" the starting address msync(addr, size, MS_SYNC); munmap(addr, size); //to allocate physical memory //equivalent of VirtualAlloc(addr, size, MEM_COMMIT, PAGE_READWRITE) void* ptr = mmap(addr, size, (PROT_READ | PROT_WRITE), (MAP_FIXED | MAP_SHARED | MAP_ANON), -1, 0); msync(addr, size, (MS_SYNC | MS_INVALIDATE)); // instead of unmapping the address, we're just gonna trick // the TLB to mark this as a new mapped area which, due to // demand paging, will not be committed until used. VirtualFree(addr, size, MEM_DECOMMIT); mmap(addr, size, PROT_NONE, MAP_FIXED|MAP_PRIVATE|MAP_ANON, -1, 0); msync(addr, size, MS_SYNC|MS_INVALIDATE);
|