2011-03-02 49 views
0

下午好,这是一个众所周知的事实,当处理无法映射到Win32中的一个视图的大文件 时,请创建代码,仔细根据需要映射 和unmaps文件区域。该引擎收录的网址是:是否可以缓存从MapViewOfFile返回的映射区域?

我创建和测试,与大文件 不能被映射到Win32的一个观点涉及一个cMemoryMappedFile类。我测试了该课程,发现 虽然功能正常,但对于 随机访问需要很长时间(即3秒)。这是因为该类必须为每个随机访问取消映射和映射文件 区域。我想知道是否有可能通过 缓存从MapViewFile返回的映射区域来加速随机访问。

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

当前视口为128KB。我相信,如果我放大 视口,它将减少对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: %u n: %u\n",adjustedptr[n],n); 

/* 
    cache PreviousNCopy,PreviousN, prevadjustedptr[PreviousNCopy] 
*/ 

} 
return adjustedptr; 
} 
+0

pastebin url是