[ACCEPTED]-How portable is mmap?-mmap

Accepted answer
Score: 14

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

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

If 6 you try to map large files on a 32-bit system, you 5 may find there isn't enough contiguous space 4 to allocate the whole file in memory, so 3 the memory mapping will fail. Do not assume 2 it will work; decide what your fallback 1 strategy is if it fails.

Score: 3

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

Score: 2

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

Score: 1

I consider memory mapped io on UNIXs as 16 not useable for interactive applications, as 15 it may result in a SIGSEGV/SIGBUS (in case 14 of the file has been truncated meanwhile 13 by some other process). Ignoring such sick 12 "solutions" as setjmp/longjmp there is nothing 11 one can do other than to terminate the process 10 after getting SIGSEGV/SIGBUS. The new G++ feature 9 to convert such signals into exceptions seems 8 to be intended mainly for apples OS, since 7 the description states, that one needs runtime 6 support for this G++ feature and there is 5 no information to be found about this G++ feature 4 anywhere. We probably have to wait a couple 3 of years, until structured exception handling 2 like it can be found on windows since more 1 than 20 years makes its way into UNIXs.

More Related questions