xref: /freebsd-13.1/lib/libc/sys/brk.2 (revision 9f9c9b22)
1.\" Copyright (c) 1980, 1991, 1993
2.\"	The Regents of the University of California.  All rights reserved.
3.\"
4.\" Redistribution and use in source and binary forms, with or without
5.\" modification, are permitted provided that the following conditions
6.\" are met:
7.\" 1. Redistributions of source code must retain the above copyright
8.\"    notice, this list of conditions and the following disclaimer.
9.\" 2. Redistributions in binary form must reproduce the above copyright
10.\"    notice, this list of conditions and the following disclaimer in the
11.\"    documentation and/or other materials provided with the distribution.
12.\" 3. Neither the name of the University nor the names of its contributors
13.\"    may be used to endorse or promote products derived from this software
14.\"    without specific prior written permission.
15.\"
16.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26.\" SUCH DAMAGE.
27.\"
28.\"     @(#)brk.2	8.4 (Berkeley) 5/1/95
29.\" $FreeBSD$
30.\"
31.Dd June 2, 2018
32.Dt BRK 2
33.Os
34.Sh NAME
35.Nm brk ,
36.Nm sbrk
37.Nd change data segment size
38.Sh LIBRARY
39.Lb libc
40.Sh SYNOPSIS
41.In unistd.h
42.Ft int
43.Fn brk "const void *addr"
44.Ft void *
45.Fn sbrk "intptr_t incr"
46.Sh DESCRIPTION
47.Bf -symbolic
48The
49.Fn brk
50and
51.Fn sbrk
52functions are legacy interfaces from before the
53advent of modern virtual memory management.
54They are deprecated and not present on the arm64 or riscv architectures.
55The
56.Xr mmap 2
57interface should be used to allocate pages instead.
58.Ef
59.Pp
60The
61.Fn brk
62and
63.Fn sbrk
64functions are used to change the amount of memory allocated in a
65process's data segment.
66They do this by moving the location of the
67.Dq break .
68The break is the first address after the end of the process's
69uninitialized data segment (also known as the
70.Dq BSS ) .
71.Pp
72The
73.Fn brk
74function
75sets the break to
76.Fa addr .
77.Pp
78The
79.Fn sbrk
80function raises the break by
81.Fa incr
82bytes, thus allocating at least
83.Fa incr
84bytes of new memory in the data segment.
85If
86.Fa incr
87is negative,
88the break is lowered by
89.Fa incr
90bytes.
91.Sh NOTES
92While the actual process data segment size maintained by the kernel will only
93grow or shrink in page sizes, these functions allow setting the break
94to unaligned values (i.e., it may point to any address inside the last
95page of the data segment).
96.Pp
97The current value of the program break may be determined by calling
98.Fn sbrk 0 .
99See also
100.Xr end 3 .
101.Pp
102The
103.Xr getrlimit 2
104system call may be used to determine
105the maximum permissible size of the
106data segment.
107It will not be possible to set the break
108beyond
109.Dq Va etext No + Va rlim.rlim_max
110where the
111.Va rlim.rlim_max
112value is returned from a call to
113.Fn getrlimit RLIMIT_DATA &rlim .
114(See
115.Xr end 3
116for the definition of
117.Va etext ) .
118.Sh RETURN VALUES
119.Rv -std brk
120.Pp
121The
122.Fn sbrk
123function returns the prior break value if successful;
124otherwise the value
125.Po Vt "void *" Pc Ns \-1
126is returned and the global variable
127.Va errno
128is set to indicate the error.
129.Sh ERRORS
130The
131.Fn brk
132and
133.Fn sbrk
134functions
135will fail if:
136.Bl -tag -width Er
137.It Bq Er EINVAL
138The requested break value was beyond the beginning of the data segment.
139.It Bq Er ENOMEM
140The data segment size limit, as set by
141.Xr setrlimit 2 ,
142was exceeded.
143.It Bq Er ENOMEM
144Insufficient space existed in the swap area
145to support the expansion of the data segment.
146.El
147.Sh SEE ALSO
148.Xr execve 2 ,
149.Xr getrlimit 2 ,
150.Xr mmap 2 ,
151.Xr end 3 ,
152.Xr free 3 ,
153.Xr malloc 3
154.Sh HISTORY
155The
156.Fn brk
157function appeared in
158.At v7 .
159.Fx 11.0
160introduced the arm64 and riscv architectures which do not support
161.Fn brk
162or
163.Fn sbrk .
164.Sh BUGS
165Mixing
166.Fn brk
167or
168.Fn sbrk
169with
170.Xr malloc 3 ,
171.Xr free 3 ,
172or similar functions will result in non-portable program behavior.
173.Pp
174Setting the break may fail due to a temporary lack of
175swap space.
176It is not possible to distinguish this
177from a failure caused by exceeding the maximum size of
178the data segment without consulting
179.Xr getrlimit 2 .
180.Pp
181.Fn sbrk
182is sometimes used to monitor heap use by calling with an argument of 0.
183The result is unlikely to reflect actual utilization in combination with an
184.Xr mmap 2
185based malloc.
186.Pp
187.Fn brk
188and
189.Fn sbrk
190are not thread-safe.
191