内存映射学习

来源:互联网 发布:java开源工作流引擎 编辑:程序博客网 时间:2024/05/16 10:16

内存映射文件是由一个文件到一块内存的映射。Win32提供了允许应用程序把文件映射到一个进程的函数(CreateFileMapping)。这样,文 件内的数据就可以用内存读/写指令来访问,而不是用ReadFileWriteFile这样的I/O系统函数,从而提高了文件存取速度。

这种函数最适用于需要读取文件并且对文件内包含的信息做语法分析的应用程序,如对输入文件进行语法分析的彩色语法编辑器,编译器等。把文件映射后进行读和分析,能让应用程序使用内存操作来操纵文件,而不必在文件里来回地读、写、移动文件指针。

有些操作,如放弃一个字符,在以前是相当复杂的,用户需要处理缓冲区的刷新问题。在引入了映射文件之后,就简单的多了。应用程序要做的只是使指针减少一个值。

映射文件的另一个重要应用就是用来支持永久命名的共享内存。要在两个应用程序之间共享内存,可以在一个应用程序中创建一个文件并映射之,然后另一个应用程序可以通过打开和映射此文件把它作为共享的内存来使用。

题目:对一个文件使用内存映射文件

1:创建或打开一个文件内核对象:

 // Open the file for reading and writing.
 HANDLE hFile = CreateFile(pszPathname, GENERIC_WRITE | GENERIC_READ, 0,
   NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);

 // 由于hFile即使为INVALID_HANDLE_VALUE,下面的CreateFileMapping仍然可以正常运行,
 //
所以这里一定要对hFile进行检查!
 if (hFile == INVALID_HANDLE_VALUE) {
   chMB("File could not be opened.");
   return(FALSE);
 }

 2:创建一个文件映射内核对象:

 // Get the size of the file (I assume the whole file can be mapped).
 DWORD dwFileSize = GetFileSize(hFile, NULL);

 // Create the file-mapping object. The file-mapping object is 1 character
 // bigger than the file size so that a zero character can be placed at the
 // end of the file to terminate the string (file). Because I don't yet know
 // if the file contains ANSI or Unicode characters, I assume worst case
 // and add the size of a WCHAR instead of CHAR.
 HANDLE hFileMap = CreateFileMapping(hFile, NULL, PAGE_READWRITE,
   0,
   dwFileSize + sizeof(WCHAR),

// 果该文件小于设定的大小,本函数将扩展该文件的大小,
// 使
磁盘上的文件变大。这样当以后将该文件作为内存映射
          //
件使用时,物理存储器就已经存在了。
   NULL f//
个文件映射对象的名字用于与其他进程共享该对象,这里我们还用不到。
   );

 if (hFileMap == NULL) {
   chMB("File map could not be opened.");
   CloseHandle(hFile);
   return(FALSE);
 }

 3:将文件数据映射到进程的地址空间:
 
当创建了一个文件映射对象之后,仍然必须让系统为文件的数据保留一个地址空间区域,
 
并将文件的数据作为映射到该区域的物理存储器进行提交。

 // Get the address where the first byte of the file is mapped into memory.
 // the return value is the starting address of the mapped view:
 PVOID pvFile = MapViewOfFile(hFileMap, FILE_MAP_WRITE, 0, 0, 0);

 if (pvFile == NULL) {
   chMB("Could not map view of file.");
   CloseHandle(hFileMap);
   CloseHandle(hFile);
   return(FALSE);
 }

 4:既然我们通过pvFile得到了映象视图的起始地址,那么可以对视图做一些操作了:
  
 ANSI
版本:
 PSTR pchANSI = (PSTR) pvFile;
 UNICODE
版本:
 PWSTR pchUnicode = (PWSTR) pvFile;

 5:从进程的地址空间中撤销文件数据的映象:

 // Clean up everything before exiting.
 UnmapViewOfFile(pvFile);

 6:关闭文件映射对象和文件对象:

 CloseHandle(hFileMap);
 CloseHandle(hFile);

也可以尽量早地把对象关闭,以消除资源泄漏的可能性,:
HANDLE hFile = CreateFile(...);
HANDLE hFileMapping = CreateFileMapping(hFile,...);
CloseHandle(hFile);

PVOID pvFile = MapViewOfFile(hFileMapping,...);
CloseHandle(hFileMapping);

 // use the memory-mapped file.

UnmapViewOfFile(pvFile);

Definition:

HANDLE CreateFileMapping(
  HANDLE hFile,                       // handle to file
  LPSECURITY_ATTRIBUTES lpAttributes, // security
  DWORD flProtect,                    // protection
  DWORD dwMaximumSizeHigh,            // high-order DWORD of size
  DWORD dwMaximumSizeLow,             // low-order DWORD of size
  LPCTSTR lpName                      // object name
);
LPVOID MapViewOfFile(
  HANDLE hFileMappingObject,   // handle to file-mapping object
  DWORD dwDesiredAccess,       // access mode
  DWORD dwFileOffsetHigh,      // high-order DWORD of offset
  DWORD dwFileOffsetLow,       // low-order DWORD of offset
  SIZE_T dwNumberOfBytesToMap  // number of bytes to map
);
BOOL UnmapViewOfFile(
  LPCVOID lpBaseAddress   // starting address
);