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