xref: /netbsd/lib/libc/sys/brk.2 (revision bf9ec67e)
1.\"	$NetBSD: brk.2,v 1.27 2002/02/08 01:28:16 ross 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. All advertising materials mentioning features or use of this software
15.\"    must display the following acknowledgement:
16.\"	This product includes software developed by the University of
17.\"	California, Berkeley and its contributors.
18.\" 4. Neither the name of the University nor the names of its contributors
19.\"    may be used to endorse or promote products derived from this software
20.\"    without specific prior written permission.
21.\"
22.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32.\" SUCH DAMAGE.
33.\"
34.\"     @(#)brk.2	8.4 (Berkeley) 5/1/95
35.\"
36.Dd July 12, 1999
37.Dt BRK 2
38.Os
39.Sh NAME
40.Nm brk ,
41.Nm sbrk
42.Nd change data segment size
43.Sh LIBRARY
44.Lb libc
45.Sh SYNOPSIS
46.Fd #include \*[Lt]unistd.h\*[Gt]
47.Ft int
48.Fn brk "void *addr"
49.Ft void *
50.Fn sbrk "intptr_t incr"
51.Sh DESCRIPTION
52.Bf -symbolic
53The brk and sbrk functions are legacy interfaces from before the
54advent of modern virtual memory management.
55.Ef
56.Pp
57The
58.Fn brk
59and
60.Fn sbrk
61functions are used to change the amount of memory allocated in a
62process's data segment. They do this by moving the location of the
63.Dq break .
64The break is the first address after the end of the process's
65uninitialized data segment (also known as the
66.Dq BSS ) .
67.Pp
68While the actual process data segment size maintained by the kernel will only
69grow or shrink in page sizes, these functions allow setting the break
70to unaligned values (i.e. it may point to any address inside the last
71page of the data segment).
72.Pp
73The
74.Fn brk
75function sets the break to
76.Fa addr .
77.Pp
78The
79.Fn sbrk
80function raises the break by at least
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.Pp
92.Fn sbrk
93returns the prior address of the break.
94The current value of the program break may be determined by calling
95.Fn sbrk 0 .
96(See also
97.Xr end 3 ) .
98.Pp
99The
100.Xr getrlimit 2
101system call may be used to determine
102the maximum permissible size of the
103.Em data
104segment;
105it will not be possible to set the break
106beyond the
107.Dv RLIMIT_DATA
108.Em rlim_max
109value returned from a call to
110.Xr getrlimit 2 ,
111e.g.
112.Dq etext + rlim.rlim_max .
113(see
114.Xr end 3
115for the definition of
116.Em etext ) .
117.Sh RETURN VALUES
118.Fn brk
119returns 0 if successful;
120otherwise -1 with
121.Va errno
122set to indicate why the allocation failed.
123.Pp
124The
125.Fn sbrk
126function returns the prior break value if successful;
127otherwise ((void *)-1) is returned and
128.Va errno
129is set to indicate why the allocation failed.
130.Sh ERRORS
131.Fn brk
132or
133.Fn sbrk
134will fail and no additional memory will be allocated if
135one of the following are true:
136.Bl -tag -width Er
137.It Bq Er ENOMEM
138The limit, as set by
139.Xr setrlimit 2 ,
140was exceeded.
141.It Bq Er ENOMEM
142The maximum possible size of a data segment (compiled into the
143system) was exceeded.
144.It Bq Er ENOMEM
145Insufficient space existed in the swap area
146to support the expansion.
147.El
148.Sh SEE ALSO
149.Xr execve 2 ,
150.Xr getrlimit 2 ,
151.Xr mmap 2 ,
152.Xr end 3 ,
153.Xr free 3 ,
154.Xr malloc 3 ,
155.Xr sysconf 3
156.Sh HISTORY
157A
158.Fn brk
159function call appeared in
160.At v7 .
161.Sh BUGS
162Note that
163mixing
164.Fn brk
165and
166.Fn sbrk
167with
168.Xr malloc 3 ,
169.Xr free 3 ,
170and similar functions may result in non-portable program
171behavior. Caution is advised.
172.Pp
173Setting the break may fail due to a temporary lack of swap space.
174It is not possible to distinguish this from a failure caused by
175exceeding the maximum size of the data segment without consulting
176.Xr getrlimit 2 .
177