:
:
:The man pages state that the fd must be -1 for MAP_ANON.
:an open() /dev/zero will return an valid file descriptor. So how would I
:mmap using /dev/zero?
:
:> Too bad there isn't an "Advance UNIX programming for Windows
:> Programmers" book. 8-(.
:
:Sounds like their could be a good market for one ;-)
:
:Jason
:
:Jason Mawdsley ~ [EMAIL PROTECTED]
With /dev/zero you open() /dev/zero and do a MAP_PRIVATE mapping of
the descriptor.
I recommend you use MAP_ANON on systems that support it. /dev/zero
might be slightly more portable but I learned the hard way (while
writing Diablo) that it isn't guarenteed to work as you might expect
across all platforms either. The big problem with /dev/zero is that
there is no easy way to tell whether a platform supports anonymous
memory allocation via /dev/zero without actually running code to find
out, and then there is no easy way to tell what the fork() characteristics
are again without actually running code to find out. Ick.
If #ifdef MAP_ANON is true then you at least know that MAP_ANON is
supported and you should use it. If it isn't true then you can fall
back to using a small temporary file, or /dev/zero. In Diablo I finally
gave up entirely on /dev/zero and just had three methods: MAP_ANON, the
mmap-a-temporary-file trick, and a standard malloc().
The small-temporary-file trick is simple: create a small temporary
file, get a file descriptor to it, remove() the file, then ftruncate()
the descriptor to the amount of space you need, mmap() it
MAP_PRIVATE, and close the descriptor. Since it is a private map no
actual space is allocated for the file, only an inode. You can
leave the descriptor open if you need to 'allocate' more space using
additional calls to mmap().
-Matt
Matthew Dillon
<[EMAIL PROTECTED]>
To Unsubscribe: send mail to [EMAIL PROTECTED]
with "unsubscribe freebsd-hackers" in the body of the message