]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man2/brk.2
216728fbe6469f965ff18c16c42c84b2d21eb773
[thirdparty/man-pages.git] / man2 / brk.2
1 .\" Copyright (c) 1993 Michael Haardt
2 .\" (michael@moria.de),
3 .\" Fri Apr 2 11:32:09 MET DST 1993
4 .\"
5 .\" %%%LICENSE_START(GPLv2+_DOC_FULL)
6 .\" This is free documentation; you can redistribute it and/or
7 .\" modify it under the terms of the GNU General Public License as
8 .\" published by the Free Software Foundation; either version 2 of
9 .\" the License, or (at your option) any later version.
10 .\"
11 .\" The GNU General Public License's references to "object code"
12 .\" and "executables" are to be interpreted as the output of any
13 .\" document formatting or typesetting system, including
14 .\" intermediate and printed output.
15 .\"
16 .\" This manual is distributed in the hope that it will be useful,
17 .\" but WITHOUT ANY WARRANTY; without even the implied warranty of
18 .\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 .\" GNU General Public License for more details.
20 .\"
21 .\" You should have received a copy of the GNU General Public
22 .\" License along with this manual; if not, see
23 .\" <http://www.gnu.org/licenses/>.
24 .\" %%%LICENSE_END
25 .\"
26 .\" Modified Wed Jul 21 19:52:58 1993 by Rik Faith <faith@cs.unc.edu>
27 .\" Modified Sun Aug 21 17:40:38 1994 by Rik Faith <faith@cs.unc.edu>
28 .\"
29 .TH BRK 2 2015-07-23 "Linux" "Linux Programmer's Manual"
30 .SH NAME
31 brk, sbrk \- change data segment size
32 .SH SYNOPSIS
33 .B #include <unistd.h>
34 .sp
35 .BI "int brk(void *" addr );
36 .sp
37 .BI "void *sbrk(intptr_t " increment );
38 .sp
39 .in -4n
40 Feature Test Macro Requirements for glibc (see
41 .BR feature_test_macros (7)):
42 .in
43 .sp
44 .BR brk (),
45 .BR sbrk ():
46 .ad l
47 .RS 4
48 .PD 0
49 .TP 4
50 Since glibc 2.19:
51 .nf
52 _DEFAULT_SOURCE ||
53 (_XOPEN_SOURCE\ >=\ 500) &&
54 .\" (_XOPEN_SOURCE\ >=\ 500 ||
55 .\" _XOPEN_SOURCE\ &&\ _XOPEN_SOURCE_EXTENDED) &&
56 ! (_POSIX_C_SOURCE\ >=\ 200112L)
57 .fi
58 .TP 4
59 From glibc 2.12 to 2.19:
60 .nf
61 _BSD_SOURCE || _SVID_SOURCE ||
62 (_XOPEN_SOURCE\ >=\ 500) &&
63 .\" (_XOPEN_SOURCE\ >=\ 500 ||
64 .\" _XOPEN_SOURCE\ &&\ _XOPEN_SOURCE_EXTENDED) &&
65 ! (_POSIX_C_SOURCE\ >=\ 200112L)
66 .fi
67 .TP 4
68 Before glibc 2.12:
69 _BSD_SOURCE || _SVID_SOURCE || _XOPEN_SOURCE\ >=\ 500
70 .\" || _XOPEN_SOURCE\ &&\ _XOPEN_SOURCE_EXTENDED
71 .PD
72 .RE
73 .ad b
74 .SH DESCRIPTION
75 .BR brk ()
76 and
77 .BR sbrk ()
78 change the location of the
79 .IR "program break" ,
80 which defines the end of the process's data segment
81 (i.e., the program break is the first location after the end of the
82 uninitialized data segment).
83 Increasing the program break has the effect of
84 allocating memory to the process;
85 decreasing the break deallocates memory.
86
87 .BR brk ()
88 sets the end of the data segment to the value specified by
89 .IR addr ,
90 when that value is reasonable, the system has enough memory,
91 and the process does not exceed its maximum data size (see
92 .BR setrlimit (2)).
93
94 .BR sbrk ()
95 increments the program's data space by
96 .I increment
97 bytes.
98 Calling
99 .BR sbrk ()
100 with an
101 .I increment
102 of 0 can be used to find the current location of the program break.
103 .SH RETURN VALUE
104 On success,
105 .BR brk ()
106 returns zero.
107 On error, \-1 is returned, and
108 .I errno
109 is set to
110 .BR ENOMEM .
111
112 On success,
113 .BR sbrk ()
114 returns the previous program break.
115 (If the break was increased,
116 then this value is a pointer to the start of the newly allocated memory).
117 On error,
118 .I "(void\ *)\ \-1"
119 is returned, and
120 .I errno
121 is set to
122 .BR ENOMEM .
123 .SH CONFORMING TO
124 4.3BSD; SUSv1, marked LEGACY in SUSv2, removed in POSIX.1-2001.
125 .\"
126 .\" .BR brk ()
127 .\" and
128 .\" .BR sbrk ()
129 .\" are not defined in the C Standard and are deliberately excluded from the
130 .\" POSIX.1-1990 standard (see paragraphs B.1.1.1.3 and B.8.3.3).
131 .SH NOTES
132 Avoid using
133 .BR brk ()
134 and
135 .BR sbrk ():
136 the
137 .BR malloc (3)
138 memory allocation package is the
139 portable and comfortable way of allocating memory.
140
141 Various systems use various types for the argument of
142 .BR sbrk ().
143 Common are \fIint\fP, \fIssize_t\fP, \fIptrdiff_t\fP, \fIintptr_t\fP.
144 .\" One sees
145 .\" \fIint\fP (e.g., XPGv4, DU 4.0, HP-UX 11, FreeBSD 4.0, OpenBSD 3.2),
146 .\" \fIssize_t\fP (OSF1 2.0, Irix 5.3, 6.5),
147 .\" \fIptrdiff_t\fP (libc4, libc5, ulibc, glibc 2.0, 2.1),
148 .\" \fIintptr_t\fP (e.g., XPGv5, AIX, SunOS 5.8, 5.9, FreeBSD 4.7, NetBSD 1.6,
149 .\" Tru64 5.1, glibc2.2).
150 .SS C library/kernel differences
151 The return value described above for
152 .BR brk ()
153 is the behavior provided by the glibc wrapper function for the Linux
154 .BR brk ()
155 system call.
156 (On most other implementations, the return value from
157 .BR brk ()
158 is the same; this return value was also specified in SUSv2.)
159 However,
160 the actual Linux system call returns the new program break on success.
161 On failure, the system call returns the current break.
162 The glibc wrapper function does some work
163 (i.e., checks whether the new break is less than
164 .IR addr )
165 to provide the 0 and \-1 return values described above.
166
167 On Linux,
168 .BR sbrk ()
169 is implemented as a library function that uses the
170 .BR brk ()
171 system call, and does some internal bookkeeping so that it can
172 return the old break value.
173 .SH SEE ALSO
174 .BR execve (2),
175 .BR getrlimit (2),
176 .BR end (3),
177 .BR malloc (3)