// JP opened flex table

Click to See Complete Forum and Search --> : memory mapped I/O and private copies


cruppstahl
February 18th, 2007, 06:16 AM
Hi!

I'm porting a library from unix/posix to Win32, and i have some troubles with the memory mapped file access.

I use mmap to read pages from the file, but use the normal write API to write the pages.

On unix, it looks like this:


buffer=mmap(0, size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, position);

On win32, it's a bit longer, as usual:

DWORD fsize=GetFileSize(fd, 0);

mmaph=CreateFileMapping(fd, 0, PAGE_READONLY, 0, fsize, 0);

buffer=MapViewOfFile(mmaph, PAGE_READWRITE, 0, (unsigned long)position, size);

My first problem is that CreateFileMapping is called whenever i read a page from the file. Is it an expensive operation? I just don't see a way to avoid this. If i cache the mmaph handle, and the file is resized because a page is appended, then this page is not covered by the file mapping...

The second problem: it just doesn't work. CreateFileMapping returns 0 and GetLastError is 5 (ACCESS_DENIED) when it's called for the very first time (fd is a valid file handle, and fsize is 4096).

The file handle was created with
CreateFile(filename, GENERIC_WRITE, 0, 0, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL|FILE_FLAG_OVERLAPPED, 0);

Thanks for any help
Chris

//JP added flex table