Thursday, January 1, 2009

Why use MAP_ANON with mmap?

If you read the man page for mmap, you'll notice that one of the allowed flags is MAP_ANONYMOUS which creates a mapping which is "not backed by any file". I couldn't think of a reason why that would be useful. I couldn't find anything helpful on the 'net either. Then, at about minutes 48-49 in Bryan Cantrill's talk about Dtrace, he explains that anonymous mmap is used to allocate memory when you're in a context which is too delicate to call malloc. He suggests that it's sometimes incorrectly used for performance reasons.

1 comments:

Mike Spooner said...

MAP_ANON is useful where related processes (eg: parent and child) need to communicate via privately shared memory. For instance, a (parent) process mmaps an area with MAP_ANON, and then forks to create a child process; both processes have the same region of shared memory mapped and can communicate through it
(often faster than pipes for bulk data).