Q: Win32 Creating a File Mapping Using Large Pages

Hello, I have a problem about creating a FileMappedObject using system page file. If i don't use the LargePage flag, it is okay but with LargePage i got errors. I use microsoft docs sample. Link is: https://learn.microsoft.com/en-us/windows/win32/memory/creating-a-file-mapping-using-large-pages First i got "Not all privileges or groups referenced are assigned to the caller." error. And I added the appropriate user(my username) to this policy "(secpol.msc) under Local Policies -> User Rights Assignment -> Lock pages in memory". After that I created the page file successfully but i couldn't get view. The View was NULL and the error was "Access is denied.". I use visual studio with admin permission. So is there anyone has experience about it? Thank you.

I assume the problem is that MapViewOfFile is trying to map size that is not multiple of large page size. The BUF_SIZE there is just 64K, but large page size is 2MB. Try changing BUF_SIZE in MapViewOfFile last argument to size variable.

I'm not sure how that example code was supposed to work. Maybe in older Windows versions MapViewOfFile allowed to map the memory with regular/4k pages even if file mapping object was created for large pages. But no idea...


Edited by Mārtiņš Možeiko on

Thank you, it works now. I passed the size value to the view function.

size = (*pGetLargePageMinimum)();
pBuf = (LPTSTR)MapViewOfFile(hMapFile,          // handle to map object
    FILE_MAP_ALL_ACCESS | FILE_MAP_LARGE_PAGES, // read/write permission
    0,
    0,
    size);

So, if i use the large page, do i have to get view which is multiple of large page size? Or it will be bigger than the pGetLargePageMinimum value? There is no information about it in microsoft docs. So bad.


Edited by c on

You can ask for any multiple of large page size. 2M, 4M, 6M, 8M, ... 1024M, etc.

On 64-bit x86 there are really two fixed sizes for large pages - 2MB and 1GB. Afaik to use 1GB pages you need to use VirtualAlloc2 with MEM_EXTENDED_PARAMETER (probably for MapViewOfFile3 too). For regular "large page" VirtualAlloc's it will never do 1GB pages.


Edited by Mārtiņš Možeiko on