序
Microsoft Windows 提供了三种机制来对内存进行操作。1、堆-----------最适合用来管理大量的小型对象。
2、虚拟内存-----最适合用来管理大型对象数组或大型结构数组。
3、内存映射文件-最适合用来管理大型数据流(通常是文件),以及在同一机器上运行的多个进程之间共享数据。
接下来我给大家介绍一下内存映射文件的相关知识。
内存映射文件相关知识介绍
与虚拟内存相似,内存映射文件允许开发人员预定一块地址空间区域并给区域调拨物理存储器。不同之处在于,内存映射文件的物理存储器来自磁盘上已有的文件,而不是来自系统的页交换文件。
一旦把文件映射到地址空间,我们就可以对它进行访问,就好像整个文件都已经被载入内存一样。
内存映射文件主要用于以下三种情况。
1、系统使用内存映射文件来载入并运行.exe和动态链接库(DLL)文件。这大量节省了页交换文件的
空间以及应用程序启动的时间。
2、开发人员可以用内存映射文件来访问磁盘上的数据文件。这使得我们可以避免直接对文件进行
I/O操作和对文件内容进行缓存。
3、通过使用内存映射文件,我们可以在同一台机器的不同进程之间共享数据。Windows的确提供了
其他的一些方法来在进程间传送数据,但这些方法都是通过内存映射文件来实现的。因此,如果在
同一台机器的不同进程之间共享数据,内存映射文件是最高效的方法。
使用内存映射文件的步骤:
1、创建或打开一个文件内核对象,该对象标识了我们想要用作内存映射文件的那个磁盘文件。
CreateFile();
2、创建一个文件映射内核对象,来告诉系统文件的大小以及我们打算如何访问文件。
CreateFileMapping();
3、告诉系统把文件映射对象的部分或全部映射到进程的地址空间中。
MapViewOfFile();
用完内存映射文件之后,必须执行以下三步进行清理。
1、告诉系统从进程地址空间中取消对文件映射内核对象的映射。
UnmapViewOfFile();
2、关闭文件映射内核对象。
CloseHandle();
3、关闭文件内核对象。
CloseHandle();
上边说的很复杂,其实使用起来很简单的,下边是我根据windows核心编程改写的一个事例。
事例
/************************************************************************/
/* 内存映射一 */
/*程序演示了如何把一个已存在的文件映射到内存中,并倒转文件内容 */
/*转载请注明文章来自:http://blog.csdn.net/windows_nt */
//************************************************************************/
#include <Windows.h>
#include <tchar.h>
#include <string.h> // For _strrev
#include <iostream>
#include <CommDlg.h>
typedef TCHAR *PTSTR;
#define FILENAME TEXT("FILEREV.DAT")
BOOL FileReverse(PCTSTR pszPathname, PBOOL pfIsTextUnicode)
{
*pfIsTextUnicode = FALSE; // Assume text is Unicode
// Open the file for reading and writing.
HANDLE hFile = CreateFile(pszPathname, GENERIC_WRITE | GENERIC_READ, 0,
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE)
{
printf("File could not be opened.");
return(FALSE);
}
// 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);
if (hFileMap == NULL)
{
printf("File map could not be opened.");
CloseHandle(hFile);
return(FALSE);
}
// Get the address where the first byte of the file is mapped into memory.
PVOID pvFile = MapViewOfFile(hFileMap, FILE_MAP_WRITE, 0, 0, 0);
if (pvFile == NULL)
{
printf("Could not map view of file.");
CloseHandle(hFileMap);
CloseHandle(hFile);
return(FALSE);
}
// Does the buffer contain ANSI or Unicode?
int iUnicodeTestFlags = -1; // Try all tests
*pfIsTextUnicode = IsTextUnicode(pvFile, dwFileSize, &iUnicodeTestFlags);
if (!*pfIsTextUnicode)
{
// For all the file manipulations below, we explicitly use ANSI
// functions because we are processing an ANSI file.
// Put a zero character at the very end of the file.
PSTR pchANSI = (PSTR) pvFile;
pchANSI[dwFileSize / sizeof(CHAR)] = 0;
// Reverse the contents of the file.
_strrev(pchANSI);
// Convert all "\n\r" combinations back to "\r\n" to
// preserve the normal end-of-line sequence.
pchANSI = strchr(pchANSI, '\n'); // Find first '\n'.
while (pchANSI != NULL)
{
// We have found an occurrence....
*pchANSI++ = '\r'; // Change '\n' to '\r'.
*pchANSI++ = '\n'; // Change '\r' to '\n'.
pchANSI = strchr(pchANSI, '\n'); // Find the next occurrence.
}
}
else
{
// For all the file manipulations below, we explicitly use Unicode
// functions because we are processing a Unicode file.
// Put a zero character at the very end of the file.
PWSTR pchUnicode = (PWSTR) pvFile;
pchUnicode[dwFileSize / sizeof(WCHAR)] = 0;
if ((iUnicodeTestFlags & IS_TEXT_UNICODE_SIGNATURE) != 0) {
// If the first character is the Unicode BOM (byte-order-mark),
// 0xFEFF, keep this character at the beginning of the file.
pchUnicode++;
}
// Reverse the contents of the file.
_wcsrev(pchUnicode);
// Convert all "\n\r" combinations back to "\r\n" to
// preserve the normal end-of-line sequence.
pchUnicode = wcschr(pchUnicode, L'\n'); // Find first '\n'.
while (pchUnicode != NULL) {
// We have found an occurrence....
*pchUnicode++ = L'\r'; // Change '\n' to '\r'.
*pchUnicode++ = L'\n'; // Change '\r' to '\n'.
pchUnicode = wcschr(pchUnicode, L'\n'); // Find the next occurrence.
}
}
// Clean up everything before exiting.
UnmapViewOfFile(pvFile);
CloseHandle(hFileMap);
// Remove trailing zero character added earlier.
SetFilePointer(hFile, dwFileSize, NULL, FILE_BEGIN);
SetEndOfFile(hFile);
CloseHandle(hFile);
return(TRUE);
}
void main()
{
//当前目录下,必须是数字或字母且为回车结束,不然,转换出来是乱码
PTCHAR szPathname = _T("123.txt");
// Make copy of input file so that we don't destroy it
if (!CopyFile(szPathname, FILENAME, FALSE))
{
printf("New file could not be created.");
return;
}
BOOL fIsTextUnicode;
if (FileReverse(FILENAME, &fIsTextUnicode))
{
// Spawn Notepad to see the fruits of our labors.
STARTUPINFO si = { sizeof(si) };
PROCESS_INFORMATION pi;
TCHAR sz[] = TEXT("Notepad ") FILENAME;
if (CreateProcess(NULL, sz,
NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi))
{
CloseHandle(pi.hThread);
CloseHandle(pi.hProcess);
}
}
DeleteFile(FILENAME);
return;
}