私は今日VC++2008でメモリマッピングをいじっていますが、それを使用する方法や、それが私の目的に合っているかどうかをまだ完全には理解していません。ここでの私の目標は、非常に大きなバイナリファイルをすばやく読み取ることです。
私は構造体を持っています:
typedef struct _data
{
int number;
char character[512];
float *entries;
}Data;
これは何度もファイルに書き込まれます。「entries」変数は、浮動小数点の小数の配列です。このファイルを書き込んだ後(各「エントリ」配列が90000フロートである10000データ構造体)、データをより速く読み取ることができるように、次の関数を使用してこのファイルをメモリマップしようとしました。これが私がこれまでに持っているものです:
void readDataMmap(char *fname, //name of file containing my data
int arraySize, //number of values in struct Data
int entrySize) //number of values in each "entries" array
{
//Read and mem map the file
HANDLE hFile = INVALID_HANDLE_VALUE;
HANDLE hMapFile;
char* pBuf;
int fd = open(fname, O_RDONLY);
if(fd == -1){
printf("Error: read failed");
exit(-1);
}
hFile = CreateFile((TCHAR*)fname,
GENERIC_READ, // open for reading
0, // do not share
NULL, // default security
OPEN_EXISTING, // existing file only
FILE_ATTRIBUTE_NORMAL, // normal file
NULL); // no template
if (hFile == INVALID_HANDLE_VALUE)
{
printf("First CreateFile failed"));
return (1);
}
hMapFile = CreateFileMapping(hFile,
NULL, // default security
PAGE_READWRITE,
0, // max. object size
0, // buffer size
NULL); // name of mapping object
if(hMapFile == ERROR_FILE_INVALID){
printf("File Mapping failed");
return(2);
}
pBuf = (char*) MapViewOfFile(hMapFile, // handle to map object
FILE_MAP_READ, // read/write permission
0,
0,
0); //Was NULL, 0 should represent full file bytesToMap size
if (pBuf == NULL)
{
printf("Could not map view of file\n");
CloseHandle(hMapFile);
return 1;
}
//Allocate data structure
Data *inData = new Data[arraySize];
for(int i = 0; i<arraySize; i++)inData[i].entries = new float[entrySize];
int pos = 0;
for(int i = 0; i < arraySize; i++)
{
//This is where I'm not sure what to do with the memory block
}
}
関数の最後で、メモリがマップされ、メモリブロック「pBuf」の先頭へのポインタが返された後、このメモリブロックをデータに読み戻すにはどうすればよいかわかりません。構造。したがって、最終的には、このメモリブロックを10000データ構造体エントリの配列に戻したいと思います。もちろん、私はこれを完全に間違っている可能性があります...