ADC Home > Reference Library > Reference > Mac OS X > Mac OS X Man Pages

 

This document is a Mac OS X manual page. Manual pages are a command-line technology for providing documentation. You can view these manual pages locally using the man(1) command. These manual pages come from many different sources, and thus, have a variety of writing styles.

This manual page is associated with the Mac OS X developer tools. The software or headers described may not be present on your Mac OS X installation until you install the developer tools package. This package is available on your Mac OS X installation DVD, and the latest versions can be downloaded from developer.apple.com.

For more information about the manual page format, see the manual page for manpages(5).



MMAP(2)                     BSD System Calls Manual                    MMAP(2)

NAME
     mmap -- map files or devices into memory

SYNOPSIS
     #include <sys/mman.h>

     void *
     mmap(void *addr, size_t len, int prot, int flags, int fildes, off_t offset);

DESCRIPTION
     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 multi-ple multiple
     ple of the pagesize, 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 convenience 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 val-ues: values:
     ues:

     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 cannot be used, mmap will fail.  If MAP_FIXED is specified,
                        addr must be a multiple of the pagesize.  Use of this option is discouraged.

     MAP_HASSEMAPHORE   Notify the kernel that the region may contain semaphores 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 pro-vides provides
                        vides 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.

RETURN VALUES
     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.

ERRORS
     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 special 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 avail-able. available.
                        able.

     [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.

LEGACY SYNOPSIS
     #include <sys/types.h>
     #include <sys/mman.h>

     The include file <sys/types.h> is necessary.

COMPATIBILITY
     mmap() now returns with errno set to EINVAL in places that historically succeeded.  The rules have
     changed as follows:

     oo   The flags parameter must specify either MAP_PRIVATE or MAP_SHARED.

     oo   The size parameter must not be 0.

     oo   The off parameter must be a multiple of pagesize, as returned by sysconf().

SEE ALSO
     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

Did this document help you?
Yes: Tell us what works for you.
It’s good, but: Report typos, inaccuracies, and so forth.
It wasn’t helpful: Tell us what would have helped.