For the More Curious: Memory-Mapped Files

Memory-mapped files are a blend of the virtual memory system and the file system, where a file’s contents are mapped onto a range of bytes in memory. When you get bytes from that range of memory, you are actually reading from the file. When you change bytes in that range of memory, you are writing to the file. In essence, you are doing I/O without using read() and write(). The virtual memory system gets to do the work of buffering, reading, and writing, only bringing in the blocks from disk that are actually used. To use memory-mapped files, you need to open the file, then call

caddr_t mmap (caddr_t addr, size_t len, int prot, int flags, int fd, off_t offset);

The return value is the address where the ...

Get Advanced Mac OS X Programming: The Big Nerd Ranch Guide now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.