1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
|
.\" $NetBSD: brk.2,v 1.34 2016/08/28 05:07:50 wiz Exp $
.\"
.\" Copyright (c) 1980, 1991, 1993
.\" The Regents of the University of California. All rights reserved.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\" notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\" notice, this list of conditions and the following disclaimer in the
.\" documentation and/or other materials provided with the distribution.
.\" 3. Neither the name of the University nor the names of its contributors
.\" may be used to endorse or promote products derived from this software
.\" without specific prior written permission.
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
.\" @(#)brk.2 8.4 (Berkeley) 5/1/95
.\"
.Dd August 27, 2016
.Dt BRK 2
.Os
.Sh NAME
.Nm brk ,
.Nm sbrk
.Nd change data segment size
.Sh LIBRARY
.Lb libc
.Sh SYNOPSIS
.In unistd.h
.Ft int
.Fn brk "void *addr"
.Ft void *
.Fn sbrk "intptr_t incr"
.Sh DESCRIPTION
.Bf -symbolic
The brk and sbrk functions are legacy interfaces from before the
advent of modern virtual memory management.
.Ef
.Pp
The
.Fn brk
and
.Fn sbrk
functions are used to change the amount of memory allocated in a
process's data segment.
They do this by moving the address at which the process's heap ends.
This address is known as the
.Dq break .
.Pp
The
.Fn brk
function sets the break to
.Fa addr .
.Pp
The
.Fn sbrk
function changes the break by
.Fa incr
bytes.
If
.Fa incr
is positive, this allocates
.Fa incr
bytes of new memory in the data segment.
If
.Fa incr
is negative,
this releases the corresponding number of bytes.
.Pp
While the break may be set to any address, actual allocation takes
place in page-sized quantities.
For allocation and access control purposes the address of the break is
always rounded up to the next page boundary.
Thus, changes to the break that do not cross a page boundary have no
material effect.
Any new pages that are allocated, however, always appear freshly
zeroed.
.Pp
The
.Xr getrlimit 2
system call may be used to determine
the maximum permissible size of the
.Em data
segment;
it will not be possible to set the break so that the sum of the heap
size and the data segment is greater than the
.Dv RLIMIT_DATA
.Em rlim_max
value returned from a call to
.Xr getrlimit 2 .
One can use the
.Dq _etext
symbol to find the end of the program text and thus the beginning of
the data segment.
.\" XXX is that always true? there are platforms where there's a fairly
.\" large unmapped gap between text and data, plus using etext doesn't
.\" take into account read-only data, which is probably (or should be)
.\" billed against text size and not data size.
See
.Xr end 3
regarding
.Dq _etext .
.Pp
Historically and in
.Nx
the heap immediately follows the data segment, and in fact is
considered part of it.
Thus the initial break is the first address after the end of the
process's uninitialized data (also known as the
.Dq BSS ) .
This address is provided by the linker as
.Dq _end ;
see
.Xr end 3 .
.Pp
There exist implementations in the wild where this is not the case,
however, or where the initial break is rounded up to a page boundary,
or other minor variations, so the recommended more-portable way to
retrieve the initial break is by calling
.Fn sbrk 0
at program startup.
(This returns the current break without changing it.)
.Pp
In any event, the break may not be set to an address below its initial
position.
.Pp
Note that ordinary application code should use
.Xr malloc 3
and related functions to allocate memory, or
.Xr mmap 2
for lower-level page-granularity control.
While the
.Fn brk
and/or
.Fn sbrk
functions exist in most Unix-like environments, their semantics
sometimes vary subtly and their use is not particularly portable.
Also, one must take care not to mix calls to
.Xr malloc 3
or related functions with calls to
.Fn brk
or
.Fn sbrk
as this will ordinarily confuse
.Xr malloc 3 ;
this can be difficult to accomplish given that many things in the
C library call
.Xr malloc 3
themselves.
.Sh RETURN VALUES
.Fn brk
returns 0 if successful;
otherwise \-1 with
.Va errno
set to indicate why the allocation failed.
.Pp
The
.Fn sbrk
function returns the prior break value if successful;
otherwise ((void *)\-1) is returned and
.Va errno
is set to indicate why the allocation failed.
.Sh ERRORS
.Fn brk
or
.Fn sbrk
will fail and no additional memory will be allocated if
one of the following are true:
.Bl -tag -width Er
.It Bq Er ENOMEM
The limit, as set by
.Xr setrlimit 2 ,
was exceeded;
or the maximum possible size of a data segment (compiled into the
system) was exceeded;
or insufficient space existed in the swap area
to support the expansion.
.El
.Sh SEE ALSO
.Xr execve 2 ,
.Xr getrlimit 2 ,
.Xr mmap 2 ,
.Xr end 3 ,
.Xr free 3 ,
.Xr malloc 3 ,
.Xr sysconf 3
.Sh HISTORY
A
.Fn brk
function call appeared in
.At v7 .
.Sh BUGS
Setting the break may fail due to a temporary lack of swap space.
It is not possible to distinguish this from a failure caused by
exceeding the maximum size of the data segment without consulting
.Xr getrlimit 2 .
|