views:

194

answers:

3

I've been considering using mmap for filereading, and was wondering how portable that is. I'm developing on a linux platform, but would like my program to work on osx and windows.

Can I assume mmap is working on these platforms?

thanks

+1  A: 

The principle of a memory mapped file is fairly portable, but you don't have mmap() on Windows (but things like MapViewOfFile() exist). You could take a peek at the python mmap modules c code to see how they do it for various platforms.

schlenk
+3  A: 

The 'mmap()' function is a POSIX call. It works fine on MacOS X (and Linux, and HP-UX, and AIX, and Solaris).

The problem area will be Windows. I'm not sure whether there is an _mmap() call in the POSIX 'compatibility' sub-system. It is likely to be there - but will have the name with the leading underscore because Microsoft has an alternative view on namespaces and considers mmap() to intrude on the user name space, even if you ask for POSIX functionality. You can find a definition of an alternative Windows interface MapViewOfFile() and discussion about performance here and in another SO question.

Jonathan Leffler
+1  A: 

Using mmap for reading files isn't portable if you rely on mapping large bits of large files into your address space - 32-bit systems can easily not have a single large usable space - say 1G - of address space available so mmap would fail quite often for a 1G mapping.

MarkR