1MREMAP(2)                  Linux Programmer's Manual                 MREMAP(2)
2
3
4

NAME

6       mremap - remap a virtual memory address
7

SYNOPSIS

9       #define _GNU_SOURCE         /* See feature_test_macros(7) */
10       #include <sys/mman.h>
11
12       void *mremap(void *old_address, size_t old_size,
13                    size_t new_size, int flags, ... /* void *new_address */);
14

DESCRIPTION

16       mremap()  expands  (or shrinks) an existing memory mapping, potentially
17       moving it at the same time (controlled by the flags  argument  and  the
18       available virtual address space).
19
20       old_address  is  the  old  address of the virtual memory block that you
21       want to expand (or shrink).  Note  that  old_address  has  to  be  page
22       aligned.   old_size  is  the  old  size  of  the  virtual memory block.
23       new_size is the requested size of the virtual memory  block  after  the
24       resize.   An optional fifth argument, new_address, may be provided; see
25       the description of MREMAP_FIXED below.
26
27       In Linux the memory is divided into pages.  A user process has (one or)
28       several  linear  virtual  memory segments.  Each virtual memory segment
29       has one or more mappings to real memory  pages  (in  the  page  table).
30       Each  virtual  memory  segment  has its own protection (access rights),
31       which may cause a segmentation violation  if  the  memory  is  accessed
32       incorrectly  (e.g., writing to a read-only segment).  Accessing virtual
33       memory outside of the segments will also cause  a  segmentation  viola‐
34       tion.
35
36       mremap()  uses  the Linux page table scheme.  mremap() changes the map‐
37       ping between virtual addresses and memory pages.  This can be  used  to
38       implement a very efficient realloc(3).
39
40       The flags bit-mask argument may be 0, or include the following flag:
41
42       MREMAP_MAYMOVE
43              By default, if there is not sufficient space to expand a mapping
44              at its current location, then mremap() fails.  If this  flag  is
45              specified,  then the kernel is permitted to relocate the mapping
46              to a new virtual address, if necessary.  If the mapping is relo‐
47              cated,  then  absolute  pointers  into  the old mapping location
48              become invalid (offsets relative to the starting address of  the
49              mapping should be employed).
50
51       MREMAP_FIXED (since Linux 2.3.31)
52              This  flag  serves  a  similar  purpose to the MAP_FIXED flag of
53              mmap(2).  If this flag is specified,  then  mremap()  accepts  a
54              fifth  argument,  void *new_address,  which  specifies  a  page-
55              aligned address to which the mapping must be moved.  Any  previ‐
56              ous  mapping  at  the address range specified by new_address and
57              new_size  is  unmapped.   If  MREMAP_FIXED  is  specified,  then
58              MREMAP_MAYMOVE must also be specified.
59
60       If  the  memory segment specified by old_address and old_size is locked
61       (using mlock(2) or similar), then this lock is maintained when the seg‐
62       ment is resized and/or relocated.  As a consequence, the amount of mem‐
63       ory locked by the process may change.
64

RETURN VALUE

66       On success mremap() returns a pointer to the new virtual  memory  area.
67       On  error, the value MAP_FAILED (that is, (void *) -1) is returned, and
68       errno is set appropriately.
69

ERRORS

71       EAGAIN The caller tried to expand a memory segment that is locked,  but
72              this  was  not  possible  without  exceeding  the RLIMIT_MEMLOCK
73              resource limit.
74
75       EFAULT "Segmentation fault." Some address in the range  old_address  to
76              old_address+old_size  is  an  invalid virtual memory address for
77              this process.  You can also get EFAULT even if there exist  map‐
78              pings  that  cover  the whole address space requested, but those
79              mappings are of different types.
80
81       EINVAL An invalid argument was given.  Possible causes are: old_address
82              was  not  page  aligned;  a  value  other than MREMAP_MAYMOVE or
83              MREMAP_FIXED was specified in flags; new_size was zero; new_size
84              or  new_address  was invalid; or the new address range specified
85              by new_address and new_size overlapped  the  old  address  range
86              specified by old_address and old_size; or MREMAP_FIXED was spec‐
87              ified without also specifying MREMAP_MAYMOVE.
88
89       ENOMEM The memory area  cannot  be  expanded  at  the  current  virtual
90              address,  and  the MREMAP_MAYMOVE flag is not set in flags.  Or,
91              there is not enough (virtual) memory available.
92

CONFORMING TO

94       This call is  Linux-specific,  and  should  not  be  used  in  programs
95       intended to be portable.
96

NOTES

98       Prior   to  version  2.4,  glibc  did  not  expose  the  definition  of
99       MREMAP_FIXED, and the prototype for mremap()  did  not  allow  for  the
100       new_address argument.
101

SEE ALSO

103       brk(2),  getpagesize(2), getrlimit(2), mlock(2), mmap(2), sbrk(2), mal‐
104       loc(3), realloc(3)
105
106       Your favorite text book on operating systems for  more  information  on
107       paged  memory  (e.g.,  Modern Operating Systems by Andrew S. Tanenbaum,
108       Inside Linux by Randolf Bentson, The Design of the UNIX Operating  Sys‐
109       tem by Maurice J. Bach)
110

COLOPHON

112       This  page  is  part of release 3.53 of the Linux man-pages project.  A
113       description of the project, and information about reporting  bugs,  can
114       be found at http://www.kernel.org/doc/man-pages/.
115
116
117
118Linux                             2010-06-10                         MREMAP(2)
Impressum