有没有可能caching从MapViewOfFile返回的映射区域?

下午好,这是一个众所周知的事实,当处理大文件不能映射到一个视图在Win32中,创build代码仔细地映射和取消映射文件区域,因为它们是需要的。 该pastebinurl是:

我创build并testing了一个cMemoryMappedFile类,用于处理无法映射到Win32中的一个视图的大文件。 我testing了这个类,发现它虽然function正常,但随机访问需要很长时间(即3秒)。 这是因为类必须为每个随机访问取消映射和映射文件区域。 我想知道是否有可能caching从MapViewFile返回的映射区域,以加快随机访问。

昨天,我注意到UnMapViewOfFile使从MapViewOfFile返回的以前映射区域无效。 有没有人有关于如何通过caching或其他方法加速随机访问的想法?

目前的视口是128KB。 我相信,如果我放大视口,它会减less调用UnMapViewOfFile和MapViewOfFile的次数。 但是,我想知道是否可以使用其他方法。 请看这个方法,
char * cMemoryMappedFile :: GetPointer(int,bool)来查看如何使用文件映射移动视口。 谢谢。

该类的pastebin url是
>。 我在这里添加源代码,以防无人可访问的URL。

// cMemoryMappedFile.Cpp #include "cException.h" #include "cMemoryMappedFile.h" #define BUFFER_SIZE 10 #define MEM_BLOCK_SIZE 65536 * 2 /** \class cMemoryMappedFile \brief Encapsulation of the Windows Memory Management API. The cMemoryMapped class makes some memory mapping operations easier. */ /** \brief Constructor for cMemoryMappedFile object. \param FileSize Size of file. \param OpenMode File open mode \param AccessModes File access mode \param ShareMode File sharing mode \param Flags File attributes and flags \param ShareMode File sharing mode \param Flags File attributes and flags \param Security Security Attributes \param Template Extended attributes tp apply to a newly created file */ cMemoryMappedFile::cMemoryMappedFile(long FileSize_, OpenModes OpenMode_,AccessModes AccessMode_, ShareModes ShareMode_,long Flags_,void *Security_,FILEHANDLE Template_) { FileSize = FileSize_; char buffer[BUFFER_SIZE]; DWORD dwRetVal = 0; UINT uRetVal = 0; DWORD dwPtr = 0; BOOL isSetEndOfFile = FALSE; LARGE_INTEGER Distance_; DWORD ErrorCode = 0; char lpTempPathBuffer[MAX_PATH]; PreviousNCopy = 0; PreviousN = 0; // Gets the temp path env string (no guarantee it's a valid path). dwRetVal = GetTempPath(MAX_PATH, // length of the buffer lpTempPathBuffer); // buffer for path if (dwRetVal > MAX_PATH || (dwRetVal == 0)) { throw cException(ERR_MEMORYMAPPING,""); } // Generates a temporary file name. uRetVal = GetTempFileName(lpTempPathBuffer, // directory for tmp files TEXT("DEMO"), // temp file name prefix 0, // create unique name TempFileName); // buffer for name if (uRetVal == 0) { throw cException(ERR_MEMORYMAPPING,lpTempPathBuffer); } // Creates the new file hFile = CreateFile((LPTSTR) TempFileName, // file name AccessMode_, // open for write 0, // do not share (SECURITY_ATTRIBUTES *) Security_, // default security OpenMode_, // CREATE_ALWAYS, Flags_,// normal file Template_); // no template if (hFile == INVALID_HANDLE_VALUE) { throw cException(ERR_MEMORYMAPPING,TempFileName); } Distance_.LowPart = (ULONG)FileSize_; Distance_.HighPart = 0; // (ULONG)(FileSize_ >> 32); dwPtr = ::SetFilePointer(hFile,Distance_.LowPart, &(Distance_.HighPart), FileBegin); if (dwPtr == INVALID_SET_FILE_POINTER){ throw cException(ERR_MEMORYMAPPING,TempFileName); } isSetEndOfFile = SetEndOfFile(hFile); if (!isSetEndOfFile){ ErrorCode = GetLastError(); throw cException(ERR_MEMORYMAPPING,TempFileName); } hMapping=::CreateFileMapping(hFile,(SECURITY_ATTRIBUTES *)Security_,PAGE_READWRITE,0,0,0); if (hMapping==NULL) throw cException(ERR_MEMORYMAPPING,TempFileName); MapPtr = 0; adjustedptr = 0; prevadjustedptr = adjustedptr; FilePath=new char[strlen(TempFileName)+1]; strcpy(FilePath,TempFileName); } char * cMemoryMappedFile::GetPointer(int n, bool Caching){ unsigned int baseoff; if( n < MEM_BLOCK_SIZE / 2) { baseoff = 0; } else { baseoff = ((n + MEM_BLOCK_SIZE / 4) & (~(MEM_BLOCK_SIZE / 2 - 1))) - MEM_BLOCK_SIZE / 2; } // the correct memory mapped view is already mapped in if (adjustedptr != 0 && mappedoffset == baseoff && Caching) return adjustedptr; else if (Caching) { /* retrieve adjustedptr from cache */ } // get a new memory mapped viewport else{ if (MapPtr){ UnmapViewOfFile(MapPtr); PreviousNCopy = PreviousN; prevadjustedptr = adjustedptr; } PreviousN = n; mappedlength = min(FileSize - baseoff, MEM_BLOCK_SIZE); // MapViewOfFile should be aligned to 64K boundary MapPtr = (char*)::MapViewOfFile( hMapping, FILE_MAP_WRITE | FILE_MAP_READ, 0, baseoff, mappedlength); mappedoffset = baseoff; adjustedptr = MapPtr - mappedoffset; printf("Value: %un: %u\n",adjustedptr[n],n); /* cache PreviousNCopy,PreviousN, prevadjustedptr[PreviousNCopy] */ } return adjustedptr; } 

Solutions Collecting From Web of "有没有可能caching从MapViewOfFile返回的映射区域?"

你可以有一个“自由列表”风格的缓存—当你的班级的用户要求取消映射一个你并不真正的地区时,你只需把它添加到列表中即可。 当他们要求映射一个新的区域时,如果可能的话重新使用一个现有的映射,否则你创建一个新的映射,如果你打开了太多的映射或者映射的大小的缓存映射太大。