munmap() — Unmap pages of memory

Standards

Standards / Extensions C or C++ Dependencies

XPG4.2
Single UNIX Specification, Version 3
z/OS® UNIX

both  

Format

#define _XOPEN_SOURCE_EXTENDED 1
#include <sys/mman.h>

int munmap(void *addr, size_t len);

General description

The munmap() function removes the mappings for pages in the range [addr, addr + len) rounding the len argument up to the next multiple of the page size as returned by sysconf(). If addr is not the address of a mapping established by a prior call to mmap(), the behavior is undefined. After a successful call to munmap() and before any subsequent mapping of the unmapped pages, further references to these pages will result in the delivery of a SIGBUS or SIGSEGV signal to the process.

__MAP_MEGA mapping: The munmap service removes the mapping for pages in the requested range. The requested range may span multiple maps, and the maps may represent the same or different files. The pages in the range may be part of a regular mapping or may be part of a __MAP_MEGA mapping. When unmapping a regular mapping, entire pages are unmapped; when unmapping a __MAP_MEGA mapping, entire segments are unmapped.

Map_address: The value of map address must be a multiple of the page size. The specified value does not have to be the start of a mapping. However, if the value specified for Map_address falls within a __MAP_MEGA map, then the address is rounded down to a megabyte multiple so that an entire segment is included in the unmap operation. It is not possible to unmap a part of a segment when processing a __MAP_MEGA map.

Map_length: The length can be the size of the whole mapping, or a part of it. If the specified length is not in multiples of the page size, it will be rounded up to a page boundary. If the Map_address plus the Map_length falls within a __MAP_MEGA map, then the length is rounded up to a segment boundary, thus including the entire segment (not necessarily the entire __MAP_MEGA mapping).

Returned value

If successful, munmap() returns 0.

If unsuccessful, munmap() returns -1 and sets errno to one of the following values:
Error Code
Description
EINVAL
One of the following error conditions exists:
  • The addr argument is not a multiple of the page size as returned by sysconf.
  • Addresses outside the valid range for the address space of a process.
  • The len argument is 0.

Related information