Section (2) mremap
mremap — remap a virtual memory address
#define _GNU_SOURCE /* See feature_test_macros(7) */ #include <sys/mman.h>
|... /* void *new_address
mremap() expands (or
shrinks) an existing memory mapping, potentially moving it at
the same time (controlled by the
flags argument and the
available virtual address space).
old_address is the
old address of the virtual memory block that you want to
expand (or shrink). Note that
old_address has to be page
the old size of the virtual memory block.
new_size is the requested size
of the virtual memory block after the resize. An optional
new_address, may be provided;
see the description of
If the value of
old_size is zero, and
old_address refers to
a shareable mapping (see mmap(2)
mremap() will create a new mapping of the
will be the size of the new mapping and the location of the
new mapping may be specified with
new_address; see the
below. If a new mapping is requested via this method, then
MREMAP_MAYMOVE flag must
also be specified.
In Linux the memory is divided into pages. A user process has (one or) several linear virtual memory segments. Each virtual memory segment has one or more mappings to real memory pages (in the page table). Each virtual memory segment has its own protection (access rights), which may cause a segmentation violation if the memory is accessed incorrectly (e.g., writing to a read-only segment). Accessing virtual memory outside of the segments will also cause a segmentation violation.
mremap() uses the Linux page
the mapping between virtual addresses and memory pages. This
can be used to implement a very efficient realloc(3).
argument may be 0, or include the following flag:
By default, if there is not sufficient space to expand a mapping at its current location, then
mremap() fails. If this flag is specified, then the kernel is permitted to relocate the mapping to a new virtual address, if necessary. If the mapping is relocated, then absolute pointers into the old mapping location become invalid (offsets relative to the starting address of the mapping should be employed).
MREMAP_FIXED(since Linux 2.3.31)
This flag serves a similar purpose to the
MAP_FIXEDflag of mmap(2). If this flag is specified, then
mremap() accepts a fifth argument, void *new_address, which specifies a page-aligned address to which the mapping must be moved. Any previous mapping at the address range specified by
new_sizeis unmapped. If
MREMAP_FIXEDis specified, then
MREMAP_MAYMOVEmust also be specified.
If the memory segment specified by
old_size is locked (using
mlock(2) or similar), then
this lock is maintained when the segment is resized and/or
relocated. As a consequence, the amount of memory locked by
the process may change.
a pointer to the new virtual memory area. On error, the value
MAP_FAILED (that is,
(void *) −1) is
errno is set
The caller tried to expand a memory segment that is locked, but this was not possible without exceeding the
Segmentation fault. Some address in the range
old_sizeis an invalid virtual memory address for this process. You can also get EFAULT even if there exist mappings that cover the whole address space requested, but those mappings are of different types.
An invalid argument was given. Possible causes are:
old_addresswas not page aligned;
a value other than
MREMAP_FIXEDwas specified in
the new address range specified by
new_sizeoverlapped the old address range specified by
MREMAP_FIXEDwas specified without also specifying
old_sizewas zero and
old_addressdoes not refer to a shareable mapping (but see BUGS);
old_sizewas zero and the
MREMAP_MAYMOVEflag was not specified.
The memory area cannot be expanded at the current virtual address, and the
MREMAP_MAYMOVEflag is not set in
flags. Or, there is not enough (virtual) memory available.
This call is Linux-specific, and should not be used in programs intended to be portable.
Prior to version 2.4, glibc did not expose the definition
MREMAP_FIXED, and the
mremap() did not
allow for the
mremap() is used to move
or expand an area locked with mlock(2) or equivalent, the
mremap() call will make a best
effort to populate the new area but will not fail with
ENOMEM if the area cannot be
Before Linux 4.14, if
old_size was zero and the
mapping referred to by
old_address was a private
mremap() created a new private mapping
unrelated to the original mapping. This behavior was
unintended and probably unexpected in user-space applications
(since the intention of
mremap() is to create a new mapping based
on the original mapping). Since Linux 4.14,
mremap() fails with the error EINVAL in this scenario.
Your favorite text book on operating systems for more information on paged memory (e.g., Modern Operating Systems by Andrew S. Tanenbaum, Inside Linux by Randolf Bentson, The Design of the UNIX Operating System by Maurice J. Bach)
This page is part of release 5.04 of the Linux
man-pages project. A
description of the project, information about reporting bugs,
and the latest version of this page, can be found at
Copyright (c) 1996 Tom Bjorkholm <tombmydata.se>
This is free documentation; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of
the License, or (at your option) any later version.
The GNU General Public License_zsingle_quotesz_s references to object code
and executables are to be interpreted as the output of any
document formatting or typesetting system, including
intermediate and printed output.
This manual is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public
License along with this manual; if not, see
1996-04-11 Tom Bjorkholm <tombmydata.se>
First version written (1.3.86)
1996-04-12 Tom Bjorkholm <tombmydata.se>
Update for Linux 1.3.87 and later
2005-10-11 mtk: Added NOTES for MREMAP_FIXED; revised EINVAL text.