Skip to content

Glossary

mmap

The POSIX syscall that maps a file (or anonymous memory) into a process's virtual address space. Backbone of malloc, dyld, and zero-copy file access on macOS.

mmap(2) maps a file or anonymous memory into a process's virtual address space. It's the most widely used VM syscall on XNU — malloc reaches for it on large allocations, dyld uses it to map every dylib, every file-backed memory region in every running process arrived via mmap.

The semantics: pass a length, a file descriptor, an offset, and protection flags; get back a pointer. Reads and writes through the pointer go to the file (or zero-fill anonymous memory). The kernel handles paging in and out.

apple-oss-distributions/xnubsd/kern/kern_mman.cThe BSD-side mmap, munmap, mprotect handlers.View on GitHub(line )

Crucially, mmap is lazy. The call returns having allocated zero physical pages — the kernel only creates the vm_map_entry pointing at the file. Real pages arrive via page fault on first touch.

This is why you can mmap a 50 GB file on a Mac with 16 GB of RAM and the call succeeds instantly: nothing was loaded.

See also: vm_map, pmap, and the mmap walkthrough article.