xref: /386bsd/usr/src/lib/libc/sys/brk.2 (revision a2142627)
1.\" Copyright (c) 1980, 1991 Regents of the University of California.
2.\" 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. All advertising materials mentioning features or use of this software
13.\"    must display the following acknowledgement:
14.\"	This product includes software developed by the University of
15.\"	California, Berkeley and its contributors.
16.\" 4. Neither the name of the University nor the names of its contributors
17.\"    may be used to endorse or promote products derived from this software
18.\"    without specific prior written permission.
19.\"
20.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30.\" SUCH DAMAGE.
31.\"
32.\"     @(#)brk.2	6.5 (Berkeley) 3/10/91
33.\"
34.Dd March 10, 1991
35.Dt BRK 2
36.Os BSD 4
37.Sh NAME
38.Nm brk ,
39.Nm sbrk
40.Nd change data segment size
41.Sh SYNOPSIS
42.Fd #include <sys/types.h>
43.Ft char
44.Fn *brk "const char *addr"
45.Ft char *
46.Fn *sbrk "int incr"
47.Sh DESCRIPTION
48.Bf -symbolic
49The brk and sbrk functions are historical curiosities
50left over from earlier days before the advent of virtual memory management.
51.Ef
52The
53.Fn brk
54function
55sets the break or lowest address
56of a process's data segment (unilitialized data) to
57.Fa addr
58(immediately above bss).
59Data addressing is restricted between
60.Fa addr
61and the lowest stack pointer to the stack segment.
62Memory is allocated by
63.Fa brk
64in page size pieces;
65if
66.Fa addr
67is not evenly divisible by the system page size, it is
68increased to the next page boundary.
69.Pp
70.\" The
71.\" .Nm sbrk
72.\" function
73.\" allocates chunks of
74.\" .Fa incr
75.\" bytes
76.\" to the process's data space
77.\" and returns an address pointer.
78.\" The
79.\" .Xr malloc 3
80.\" function utilizes
81.\" .Nm sbrk .
82.\" .Pp
83The current value of the program break is reliably returned by
84.Dq Li sbrk(0)
85(see also
86.Xr end 3 ) .
87The
88.Xr getrlimit 2
89system call may be used to determine
90the maximum permissible size of the
91.Em data
92segment;
93it will not be possible to set the break
94beyond the
95.Em rlim_max
96value returned from a call to
97.Xr getrlimit ,
98e.g.
99.Dq qetext + rlp\(->rlim_max.
100(see
101.Xr end 3
102for the definition of
103.Em etext ) .
104.Sh RETURN VALUES
105.Nm Brk
106returns 0 if successful; -1 if the process requests more memory than
107than allowed by the system limit.
108The
109.Nm sbrk
110function returns 0 if successful, otherwise the error
111.Er EOPNOTSUPP
112is returned.
113.\" .Sh ERRORS
114.\" .Xr Sbrk
115.\" returns -1 if the break could not be set.
116.\" will fail and no additional memory will be allocated if
117.\" one of the following are true:
118.\" .Bl -tag -width [ENOMEM]
119.\" .It Bq Er ENOMEM
120.\" The limit, as set by
121.\" .Xr setrlimit 2 ,
122.\" was exceeded.
123.\" .It Bq Er ENOMEM
124.\" The maximum possible size of a data segment (compiled into the
125.\" system) was exceeded.
126.\" .It Bq Er ENOMEM
127.\" Insufficient space existed in the swap area
128.\" to support the expansion.
129.\" .El
130.Sh SEE ALSO
131.Xr execve 2 ,
132.Xr getrlimit 2 ,
133.Xr malloc 3 ,
134.Xr end 3
135.Sh BUGS
136Setting the break may fail due to a temporary lack of
137swap space.  It is not possible to distinguish this
138from a failure caused by exceeding the maximum size of
139the data segment without consulting
140.Xr getrlimit .
141.Sh HISTORY
142A
143.Nm
144function call appeared in Version 7 AT&T UNIX.
145