Reputation: 568
I have a read-only Win32 memory mapped file with no sharing open. Is there any way to tell Windows to load some page into memory, making sure that it was loaded successfully, and keep it in memory until I release it?
The reason I want this is because reading from memory mapped memory can throw an exception in case the page can't be loaded because of disk/network failure. Handling this case everywhere in code is impractical, so I would like to ensure that at least some block is safe to read, and then read block by block.
I know of PrefetchVirtualMemory
but that's strictly a performance optimization; it doesn't guarantee that the pages will successfully load or still be in memory when I get to reading them.
P.S. I don't necessarily want to prevent paging, paging is fine. My main concern are network drives. So VirtualLock
doesn't seem like the right thing.
Upvotes: 0
Views: 283
Reputation:
If you need to just guarantee there will be no network access, you can read required regions to a private memory using ReadFile
(may be done asynchronously).
Another (hacky) way is to use copy-on-write (FILE_MAP_COPY) mapping and cause fake page modification. This allows you to have a contiguous memory mapping that might be easier to navigate, but with certain pages being kept in a private memory. This has disadvantage of causing increased memory pressure as OS reserves memory for the whole mapping.
Upvotes: 2
Reputation: 15162
Unless you are running in an account with the SE_LOCK_MEMORY_NAME privilege, no. That is the only way to ensure that a particular piece of memory is not paged out.
Upvotes: 1