Linux – No mremap for Windows?

No mremap for Windows?… here is a solution to the problem.

No mremap for Windows?

As I understand it, to keep a block of virtual memory in Linux, you can call mmap using MAP_ANONYMOUS and MAP_PRIVATE, and the equivalent system call on Windows is VirtualAlloc.

However, Linux provides MreMap to resize the memory map, the man page says

changes the mapping between virtual addresses and memory pages

I can’t find the equivalent Windows system call. It seems that to reallocate memory, HeapAlloc will be used instead of VirtualAlloc, and then HeapReAlloc. Regarding HeapReAlloc, msdn said

The process of preserving the memory content involves a memory copy operation that is potentially very time-consuming.

So is there a way to remap virtual memory in Windows? If not, why not?

Solution

Fine-grained control of virtual memory on Windows can be achieved through AWE family functions in the Win32 API.

The initial allocation is via AllocateUserPhysicalPages Done, as the name suggests, it assigns you the actual physical page. You can then use MapUserPhysicalPages Continue to map these physical pages to virtual pages. to the range of virtual address space that you previously reserved with VirtualAlloc. Note that you can remap a mapped physical page to a different virtual page.

When you’re dealing with both physical and virtual memory, this does introduce a different set of semantics. Among some drawbacks that might be worth mentioning, you need to make sure that aliases don’t exist; And you are effectively limited to using native page sizes, i.e. you will not be able to use large pages.

Related Problems and Solutions