Memory Mapping
This section provides documentation for the Windows API functions related to memory mapping. Memory mapping allows applications to treat files or devices as if they were in memory, enabling efficient data access and sharing.
Overview
Memory mapping is a powerful technique that facilitates several key functionalities:
- Efficient File Access: Read and write to files directly in memory without explicit read/write calls, significantly improving performance for large files.
- Inter-Process Communication (IPC): Share memory regions between different processes, allowing them to communicate and exchange data efficiently.
- Resource Management: Map system resources like devices into the application's address space for direct manipulation.
Key Concepts
- Memory-Mapped Files: Files whose contents are mapped directly into the process's virtual address space.
- Pages: Memory is managed in fixed-size units called pages. Memory mapping operates at the page level.
- Virtual Address Space: Each process has its own independent virtual address space, which the operating system maps to physical memory.
- File Handles and Mapping Objects: Objects that represent the mapping between a file and a memory region.
Core Functions
CreateFileMapping
Description: Creates or opens a named or unnamed file mapping object. This object defines the size of the memory block to be shared or mapped.
Syntax:
HANDLE CreateFileMapping(
HANDLE hFile,
LPSECURITY_ATTRIBUTES lpFileMappingAttributes,
DWORD flProtect,
DWORD dwMaximumSizeHigh,
DWORD dwMaximumSizeLow,
LPCTSTR lpName
);
Parameters:
hFile: A handle to the file to be backed by the file mapping object.lpFileMappingAttributes: Security attributes.flProtect: Memory protection for the file mapping object.dwMaximumSizeHigh,dwMaximumSizeLow: High and low parts of the maximum size of the mapping object.lpName: Name of the mapping object.
Return Value: A handle to the file mapping object. Returns NULL on failure.
See Also: OpenFileMapping, MapViewOfFile
OpenFileMapping
Description: Opens an existing named file mapping object. This is used by other processes to access a mapping created by CreateFileMapping.
Syntax:
HANDLE OpenFileMapping(
DWORD dwDesiredAccess,
BOOL bInheritHandle,
LPCTSTR lpName
);
Parameters:
dwDesiredAccess: The access to the file mapping object.bInheritHandle: If true, the handle is inheritable.lpName: The name of the file mapping object.
Return Value: A handle to the file mapping object. Returns NULL on failure.
See Also: CreateFileMapping
MapViewOfFile
Description: Maps a view of a file mapping into the calling process's address space. This view is a contiguous range of addresses within the virtual address space.
Syntax:
LPVOID MapViewOfFile(
HANDLE hFileMappingObject,
DWORD dwDesiredAccess,
DWORD dwFileOffsetHigh,
DWORD dwFileOffsetLow,
SIZE_T dwNumberOfBytesToMap
);
Parameters:
hFileMappingObject: Handle to a file mapping object.dwDesiredAccess: Access to the memory-mapped file.dwFileOffsetHigh,dwFileOffsetLow: Starting offset of the view in the mapped file.dwNumberOfBytesToMap: Number of bytes to map.
Return Value: The starting address of the mapped view. Returns NULL on failure.
See Also: UnmapViewOfFile, MapViewOfFileEx
UnmapViewOfFile
Description: Decrements the commit count for the specified range of pages in the virtual address space of the calling process. This eventually unmaps the view.
Syntax:
BOOL UnmapViewOfFile(
LPVOID lpBaseAddress
);
Parameters:
lpBaseAddress: The base address of the memory-mapped view to unmap.
Return Value: TRUE on success, FALSE on failure.
See Also: MapViewOfFile