mmap(2) BSD System Calls Manual mmap(2)
mmap -- map files or devices into memory
#include <sys/mman.h> void * mmap(void *addr, size_t len, int prot, int flags, int fildes, off_t offset);
The mmap function causes the pages starting at addr and continuing for at most len bytes to be mapped from the object described by fildes, starting at byte offset offset. If offset or len is not a multiple of the page- size, the mapped region may extend past the specified range. If addr is non-zero, it is used as a hint to the system. (As a conve- nience to the system, the actual address of the region may differ from the address supplied.) If addr is zero, an address will be selected by the system. The actual starting address of the region is returned. A successful mmap deletes any previous mapping in the allocated address range. The protections (region accessibility) are specified in the prot argument by or'ing the following values: PROT_EXEC Pages may be executed. PROT_READ Pages may be read. PROT_WRITE Pages may be written. The flags parameter specifies the type of the mapped object, mapping options, and whether modifications made to the mapped copy of the page are private to the process (copy-on-write) or are to be shared with other references. Sharing, mapping type, and options are specified in the flags argument by or'ing the following values: MAP_ANON Map anonymous memory not associated with any specific file. Mac OS X specific: the file descriptor used for creating MAP_ANON regions can be used to pass some Mach VM flags, and can be specified as -1 if no such flags are associated with the region. Mach VM flags are defined in <mach/vm_statistics.h> and the ones that currently apply to mmap are: VM_FLAGS_PURGABLE to create Mach purgable (i.e. volatile) memory VM_MAKE_TAG(tag) to associate an 8-bit tag with the region <mach/vm_statistics.h> defines some preset tags (with a VM_MEMORY_ prefix). Users are encouraged to use tags between 240 and 255. Tags are used by tools such as vmmap(1) to help identify specific memory regions. MAP_FILE Mapped from a regular file or character-special device memory. (This is the default mapping type, and need not be specified.) MAP_FIXED Do not permit the system to select a different address than the one specified. If the specified address can- not be used, mmap will fail. If MAP_FIXED is speci- fied, addr must be a multiple of the pagesize. Use of this option is discouraged. MAP_HASSEMAPHORE Notify the kernel that the region may contain sema- phores and that special handling may be necessary. MAP_PRIVATE Modifications are private (copy-on-write). MAP_SHARED Modifications are shared. MAP_NOCACHE Pages in this mapping are not retained in the kernel's memory cache. If the system runs low on memory, pages in MAP_NOCACHE mappings will be among the first to be reclaimed. This flag is intended for mappings that have little locality and provides a hint to the kernel that pages in this mapping are unlikely to be needed again in the near future. Conforming applications must specify either MAP_PRIVATE or MAP_SHARED. The close(2) function does not unmap pages, see munmap(2) for further information. The current design does not allow a process to specify the location of swap space. In the future we may define an additional mapping type, MAP_SWAP, in which the file descriptor argument specifies a file or device to which swapping should be done.
Upon successful completion, mmap returns a pointer to the mapped region. Otherwise, a value of -1 is returned and errno is set to indicate the error.
Mmap() will fail if: [EACCES] Fildes is not open for reading. [EACCES] The flags PROT_WRITE and MAP_SHARED are specified as part of the flags and prot parameters and fildes is not open for writing. [EBADF] fildes is not a valid file descriptor for an open file. [EINVAL] MAP_FIXED is specified and the parameter is not page aligned. [EINVAL] fildes does not reference a regular or character spe- cial file. [EINVAL] flags does not include either MAP_PRIVATE or MAP_SHARED. [EINVAL] len is not greater than zero. [EINVAL] offset is not a multiple of the page size, as returned by sysconf(3). [EMFILE] The limit on mapped regions (per process or system) is exceeded. [ENODEV] The file type for fildes is not supported for mapping. [ENOMEM] MAP_FIXED is specified and the address range specified exceeds the address space limit for the process. [ENOMEM] MAP_FIXED is specified and the address specified by the addr parameter isn't available. [ENOMEM] MAP_ANON is specified and insufficient memory is available. [ENXIO] Addresses in the specified range are invalid for fildes. [EOVERFLOW] Addresses in the specified range exceed the maximum offset set for fildes.
#include <sys/types.h> #include <sys/mman.h> The include file <sys/types.h> is necessary.
mmap() now returns with errno set to EINVAL in places that historically succeeded. The rules have changed as follows: o The flags parameter must specify either MAP_PRIVATE or MAP_SHARED. o The size parameter must not be 0. o The off parameter must be a multiple of pagesize, as returned by sysconf().
getpagesize(2), madvise(2), mincore(2), mlock(2), mprotect(2), msync(2), munmap(2), sysconf(3), compat(5) 4th Berkeley Distribution June 4, 1993 4th Berkeley Distribution
Mac OS X 10.7 - Generated Mon Nov 7 15:06:48 CST 2011