Home > Coding, hamsterdb, Howto's > howto port unix mmap to win32

howto port unix mmap to win32

November 14th, 2007

I just found a bug which is more than a year old. When reading pages with memory mapped I/O on Windows, the mapping had write-access, and each modification of the mapped page was written to the file. This had no effect so far, but now i had a situation where this caused a test to fail.

On Unix/POSIX platforms, i call mmap() with the flag MAP_PRIVATE, which creates private copies of the pages, and does not modify the file.

fter some searching, i found out how i can mimick this behaviour on Windows:

HANDLE mmaph=CreateFileMapping(fd, 0, PAGE_WRITECOPY, 0, filesize, 0);
... // error checking ommitted
void *page=MapViewOfFile(mmaph, FILE_MAP_COPY, 0, position, size);

The flags PAGE_WRITECOPY and FILE_MAP_COPY use copy-on-write and therefore don’t modify the file, when the page is modified.

hamsterdb 0.4.8 is just around the corner – it features transparent AES encryption and zlib compression! Also, a C++ API and two new samples are in the package. And the mmap bug is fixed, too :)

[edit]
I just noticed that the above code works on Windows 32bit and Windows 64bit, but not on Windows 64bit in Debug mode (an MS Visual Studio project in debug configuration). MapFileOfView returns 5 (ACCESS_DENIED). The release configuration works. This looks like a bug in the Debug runtime libraries. If somebody has a clue what’s going on, please tell me…

[edit #2]
I found the problem; it’s not in the line above, but elsewhere where i calculated the position.  Some nasty casts were involved, which had different results in x64 release mode and x64 debug mode.

chris Coding, hamsterdb, Howto's

  1. No comments yet.
  1. No trackbacks yet.