Platform SDK: Memory |
File mapping is the association of a file's contents with a portion of the virtual address space of a process. The system creates a file-mapping object to maintain this association. A file view is the portion of virtual address space that the process uses to access the file's contents. Processes read from and write to the file view using pointers, just as they would with dynamically allocated memory. Processes can also manipulate the file view with the virtual memory function VirtualProtect.
The file-mapping functions allow a process to create file-mapping objects and file views to easily access and share data. The following illustration shows the relationship between the file on disk, a file-mapping object, and a file view.
The file on disk can be any file that you want to map into memory, or it can be the system pagefile.
The file-mapping object can consist of all or only part of the file. It is backed by the file on disk. This means that when the system swaps out pages of the file-mapping object, any changes made to the file-mapping object are written to the file. When the pages of the file-mapping object are swapped back in, they are restored from the file.
A file view can consist of all or only part of the file-mapping object. A process manipulates the file through the file views. A process can create multiple views for a file-mapping object.
When multiple processes use the same file-mapping object to create views for a local file, the data is coherent. That is, the views contain identical copies of the file on disk. The file cannot reside on a remote computer if you want to share memory between multiple processes.