]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man2/mremap.2
getent.1, _syscall.2, acct.2, adjtimex.2, bdflush.2, brk.2, cacheflush.2, getsid...
[thirdparty/man-pages.git] / man2 / mremap.2
CommitLineData
fea681da
MK
1.\" Copyright (c) 1996 Tom Bjorkholm <tomb@mydata.se>
2.\"
6a8d8745 3.\" %%%LICENSE_START(GPLv2+_doc_full)
fea681da
MK
4.\" This is free documentation; you can redistribute it and/or
5.\" modify it under the terms of the GNU General Public License as
6.\" published by the Free Software Foundation; either version 2 of
7.\" the License, or (at your option) any later version.
8.\"
9.\" The GNU General Public License's references to "object code"
10.\" and "executables" are to be interpreted as the output of any
11.\" document formatting or typesetting system, including
12.\" intermediate and printed output.
13.\"
14.\" This manual is distributed in the hope that it will be useful,
15.\" but WITHOUT ANY WARRANTY; without even the implied warranty of
16.\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17.\" GNU General Public License for more details.
18.\"
19.\" You should have received a copy of the GNU General Public
c715f741
MK
20.\" License along with this manual; if not, see
21.\" <http://www.gnu.org/licenses/>.
6a8d8745 22.\" %%%LICENSE_END
fea681da
MK
23.\"
24.\" 1996-04-11 Tom Bjorkholm <tomb@mydata.se>
25.\" First version written (1.3.86)
26.\" 1996-04-12 Tom Bjorkholm <tomb@mydata.se>
27.\" Update for Linux 1.3.87 and later
4f76dae0 28.\" 2005-10-11 mtk: Added NOTES for MREMAP_FIXED; revised EINVAL text.
fea681da 29.\"
ebbb5118 30.TH MREMAP 2 2010-06-10 "Linux" "Linux Programmer's Manual"
fea681da 31.SH NAME
3b777aff 32mremap \- remap a virtual memory address
fea681da 33.SH SYNOPSIS
3747f397 34.nf
b80f966b 35.BR "#define _GNU_SOURCE" " /* See feature_test_macros(7) */"
ceb6743d 36.br
fea681da
MK
37.B #include <sys/mman.h>
38.sp
da8cb51e 39.BI "void *mremap(void *" old_address ", size_t " old_size ,
ebbb5118 40.BI " size_t " new_size ", int " flags ", ... /* void *" new_address " */);"
fea681da
MK
41.fi
42.SH DESCRIPTION
60a90ecd
MK
43.BR mremap ()
44expands (or shrinks) an existing memory mapping, potentially
a8d55537 45moving it at the same time (controlled by the \fIflags\fP argument and
fea681da
MK
46the available virtual address space).
47
a8d55537 48\fIold_address\fP is the old address of the virtual memory block that you
c13182ef 49want to expand (or shrink).
a8d55537 50Note that \fIold_address\fP has to be page
67da5267
MK
51aligned.
52\fIold_size\fP is the old size of the
be7fff26 53virtual memory block.
a8d55537 54\fInew_size\fP is the requested size of the
c13182ef 55virtual memory block after the resize.
ebbb5118
MK
56An optional fifth argument,
57.IR new_address ,
58may be provided; see the description of
59.B MREMAP_FIXED
60below.
fea681da 61
c13182ef
MK
62In Linux the memory is divided into pages.
63A user process has (one or)
64several linear virtual memory segments.
65Each virtual memory segment has one
66or more mappings to real memory pages (in the page table).
67Each virtual memory segment has its own
68protection (access rights), which may cause
fea681da 69a segmentation violation if the memory is accessed incorrectly (e.g.,
c13182ef
MK
70writing to a read-only segment).
71Accessing virtual memory outside of the
fea681da
MK
72segments will also cause a segmentation violation.
73
60a90ecd
MK
74.BR mremap ()
75uses the Linux page table scheme.
76.BR mremap ()
77changes the
c13182ef 78mapping between virtual addresses and memory pages.
60a90ecd
MK
79This can be used to implement a very efficient
80.BR realloc (3).
fea681da 81
a8d55537 82The \fIflags\fP bit-mask argument may be 0, or include the following flag:
fea681da
MK
83.TP
84.B MREMAP_MAYMOVE
ceb6743d
MK
85By default, if there is not sufficient space to expand a mapping
86at its current location, then
87.BR mremap ()
88fails.
c13182ef 89If this flag is specified, then the kernel is permitted to
ceb6743d
MK
90relocate the mapping to a new virtual address, if necessary.
91If the mapping is relocated,
c13182ef
MK
92then absolute pointers into the old mapping location
93become invalid (offsets relative to the starting address of
ceb6743d 94the mapping should be employed).
8107e4b7
MK
95.TP
96.BR MREMAP_FIXED " (since Linux 2.3.31)"
97This flag serves a similar purpose to the
98.B MAP_FIXED
99flag of
100.BR mmap (2).
101If this flag is specified, then
102.BR mremap ()
103accepts a fifth argument,
104.IR "void *new_address" ,
105which specifies a page-aligned address to which the mapping must
106be moved.
107Any previous mapping at the address range specified by
108.I new_address
109and
110.I new_size
111is unmapped.
c13182ef 112If
8107e4b7 113.B MREMAP_FIXED
c13182ef 114is specified, then
0daa9e92 115.B MREMAP_MAYMOVE
8107e4b7 116must also be specified.
ceb6743d 117.PP
c13182ef 118If the memory segment specified by
ceb6743d 119.I old_address
c13182ef 120and
ceb6743d
MK
121.I old_size
122is locked (using
0bfa087b 123.BR mlock (2)
ceb6743d
MK
124or similar), then this lock is maintained when the segment is
125resized and/or relocated.
a85a10cb 126As a consequence, the amount of memory locked by the process may change.
47297adb 127.SH RETURN VALUE
60a90ecd
MK
128On success
129.BR mremap ()
130returns a pointer to the new virtual memory area.
fea681da
MK
131On error, the value
132.B MAP_FAILED
009df872 133(that is, \fI(void\ *)\ \-1\fP) is returned,
a8d55537 134and \fIerrno\fP is set appropriately.
fea681da
MK
135.SH ERRORS
136.TP
137.B EAGAIN
c13182ef 138The caller tried to expand a memory segment that is locked,
ceb6743d 139but this was not possible without exceeding the
682edefb
MK
140.B RLIMIT_MEMLOCK
141resource limit.
fea681da
MK
142.TP
143.B EFAULT
144"Segmentation fault." Some address in the range
145\fIold_address\fP to \fIold_address\fP+\fIold_size\fP is an invalid
146virtual memory address for this process.
682edefb
MK
147You can also get
148.B EFAULT
149even if there exist mappings that cover the
fea681da
MK
150whole address space requested, but those mappings are of different types.
151.TP
152.B EINVAL
4f76dae0 153An invalid argument was given.
a8d55537 154Possible causes are: \fIold_address\fP was not
4f76dae0
MK
155page aligned; a value other than
156.B MREMAP_MAYMOVE
c13182ef 157or
4f76dae0
MK
158.B MREMAP_FIXED
159was specified in
160.IR flags ;
161.I new_size
162was zero;
163.I new_size
c13182ef 164or
4f76dae0
MK
165.I new_address
166was invalid;
167or the new address range specified by
c13182ef
MK
168.I new_address
169and
4f76dae0
MK
170.I new_size
171overlapped the old address range specified by
172.I old_address
173and
174.IR old_size ;
c13182ef 175or
4f76dae0
MK
176.B MREMAP_FIXED
177was specified without also specifying
178.BR MREMAP_MAYMOVE .
fea681da
MK
179.TP
180.B ENOMEM
181The memory area cannot be expanded at the current virtual address, and the
182.B MREMAP_MAYMOVE
c13182ef 183flag is not set in \fIflags\fP.
fea681da 184Or, there is not enough (virtual) memory available.
47297adb 185.SH CONFORMING TO
8382f16d 186This call is Linux-specific, and should not be used in programs
b5d63e49
MK
187intended to be portable.
188.\" 4.2BSD had a (never actually implemented)
189.\" .BR mremap (2)
190.\" call with completely different semantics.
4f76dae0 191.SH NOTES
8107e4b7 192Prior to version 2.4, glibc did not expose the definition of
4f76dae0 193.BR MREMAP_FIXED ,
8107e4b7 194and the prototype for
4f76dae0 195.BR mremap ()
8107e4b7 196did not allow for the
4f76dae0 197.I new_address
8107e4b7 198argument.
47297adb 199.SH SEE ALSO
fea681da
MK
200.BR brk (2),
201.BR getpagesize (2),
ceb6743d
MK
202.BR getrlimit (2),
203.BR mlock (2),
fea681da
MK
204.BR mmap (2),
205.BR sbrk (2),
206.BR malloc (3),
0a4f8b7b 207.BR realloc (3)
173fe7e7
DP
208
209Your favorite text book on operating systems
210for more information on paged memory
211(e.g., \fIModern Operating Systems\fP by Andrew S. Tanenbaum,
a8d55537 212\fIInside Linux\fP by Randolf Bentson,
173fe7e7 213\fIThe Design of the UNIX Operating System\fP by Maurice J. Bach)