OSDN Git Service

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