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