我正在尝试创建一个内存管理端口,其中一些分配器使用虚拟内存机制来保留地址空间,而无需(在开始时)分配任何物理内存,稍后只在需要时分配内存。
代码基于Windows的VirtualAlloc和VirtualFree使事情有效,现在我正在尝试将此代码移植到Apple OS X,据我所知,过了一段时间没有这样的API我想出了以下代码:
//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));
我唯一想通知的是如何移植使用VirtualFree来释放/取消仅仅部分物理内存,模仿VirtualFree调用:
VirtualFree(addr, size, MEM_DECOMMIT);
我试图用所需的地址和大小调用munmap,但它不会释放内存......而调用它来释放所有虚拟空间的效果非常好。
有人可以帮助我完成这项任务吗?
答案 0 :(得分:3)
感谢此博客,我找到了自己问题的答案: http://blog.nervus.org/managing-virtual-address-spaces-with-mmap/
我也在这里发布他的解决方案,以防链接死亡:
void DecommitMemory(void* addr, size_t size)
{
// 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.
mmap(addr, size, PROT_NONE, MAP_FIXED|MAP_PRIVATE|MAP_ANON, -1, 0);
msync(addr, size, MS_SYNC|MS_INVALIDATE);
}