1994-05-27 05:00:24 +00:00
|
|
|
.\" Copyright (c) 1980, 1991, 1993
|
|
|
|
.\" The Regents of the University of California. All rights reserved.
|
|
|
|
.\"
|
|
|
|
.\" Redistribution and use in source and binary forms, with or without
|
|
|
|
.\" modification, are permitted provided that the following conditions
|
|
|
|
.\" are met:
|
|
|
|
.\" 1. Redistributions of source code must retain the above copyright
|
|
|
|
.\" notice, this list of conditions and the following disclaimer.
|
|
|
|
.\" 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
.\" notice, this list of conditions and the following disclaimer in the
|
|
|
|
.\" documentation and/or other materials provided with the distribution.
|
|
|
|
.\" 4. Neither the name of the University nor the names of its contributors
|
|
|
|
.\" may be used to endorse or promote products derived from this software
|
|
|
|
.\" without specific prior written permission.
|
|
|
|
.\"
|
|
|
|
.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
.\" SUCH DAMAGE.
|
|
|
|
.\"
|
1997-03-11 11:35:56 +00:00
|
|
|
.\" @(#)brk.2 8.4 (Berkeley) 5/1/95
|
1999-08-28 00:22:10 +00:00
|
|
|
.\" $FreeBSD$
|
1994-05-27 05:00:24 +00:00
|
|
|
.\"
|
2001-09-06 12:11:22 +00:00
|
|
|
.Dd July 12, 1999
|
1994-05-27 05:00:24 +00:00
|
|
|
.Dt BRK 2
|
2001-07-10 13:41:46 +00:00
|
|
|
.Os
|
1994-05-27 05:00:24 +00:00
|
|
|
.Sh NAME
|
|
|
|
.Nm brk ,
|
|
|
|
.Nm sbrk
|
|
|
|
.Nd change data segment size
|
2000-04-21 09:42:15 +00:00
|
|
|
.Sh LIBRARY
|
|
|
|
.Lb libc
|
1994-05-27 05:00:24 +00:00
|
|
|
.Sh SYNOPSIS
|
2002-01-24 12:11:31 +00:00
|
|
|
.In sys/types.h
|
2001-10-01 16:09:29 +00:00
|
|
|
.In unistd.h
|
2002-01-24 12:11:31 +00:00
|
|
|
.Ft int
|
|
|
|
.Fn brk "const void *addr"
|
|
|
|
.Ft void *
|
|
|
|
.Fn sbrk "intptr_t incr"
|
1994-05-27 05:00:24 +00:00
|
|
|
.Sh DESCRIPTION
|
|
|
|
.Bf -symbolic
|
2001-09-06 12:11:22 +00:00
|
|
|
The
|
|
|
|
.Fn brk
|
|
|
|
and
|
|
|
|
.Fn sbrk
|
|
|
|
functions are legacy interfaces from before the
|
|
|
|
advent of modern virtual memory management.
|
1994-05-27 05:00:24 +00:00
|
|
|
.Ef
|
2001-09-06 12:11:22 +00:00
|
|
|
.Pp
|
|
|
|
The
|
|
|
|
.Fn brk
|
|
|
|
and
|
|
|
|
.Fn sbrk
|
|
|
|
functions are used to change the amount of memory allocated in a
|
|
|
|
process's data segment.
|
|
|
|
They do this by moving the location of the
|
|
|
|
.Dq break .
|
|
|
|
The break is the first address after the end of the process's
|
|
|
|
uninitialized data segment (also known as the
|
|
|
|
.Dq BSS ) .
|
|
|
|
.Pp
|
1994-05-27 05:00:24 +00:00
|
|
|
The
|
|
|
|
.Fn brk
|
|
|
|
function
|
2001-09-06 12:11:22 +00:00
|
|
|
sets the break to
|
|
|
|
.Fa addr .
|
|
|
|
.Pp
|
|
|
|
The
|
|
|
|
.Fn sbrk
|
|
|
|
function raises the break by
|
|
|
|
.Fa incr
|
|
|
|
bytes, thus allocating at least
|
|
|
|
.Fa incr
|
|
|
|
bytes of new memory in the data segment.
|
|
|
|
If
|
|
|
|
.Fa incr
|
|
|
|
is negative,
|
|
|
|
the break is lowered by
|
|
|
|
.Fa incr
|
|
|
|
bytes.
|
|
|
|
.Sh NOTES
|
|
|
|
While the actual process data segment size maintained by the kernel will only
|
|
|
|
grow or shrink in page sizes, these functions allow setting the break
|
|
|
|
to unaligned values (i.e., it may point to any address inside the last
|
|
|
|
page of the data segment).
|
|
|
|
.Pp
|
|
|
|
The current value of the program break may be determined by calling
|
|
|
|
.Fn sbrk 0 .
|
|
|
|
See also
|
|
|
|
.Xr end 3 .
|
1994-05-27 05:00:24 +00:00
|
|
|
.Pp
|
|
|
|
The
|
|
|
|
.Xr getrlimit 2
|
|
|
|
system call may be used to determine
|
|
|
|
the maximum permissible size of the
|
2001-09-06 12:11:22 +00:00
|
|
|
data segment.
|
|
|
|
It will not be possible to set the break
|
|
|
|
beyond
|
|
|
|
.Dq Va etext No + Va rlim.rlim_max
|
|
|
|
where the
|
|
|
|
.Va rlim.rlim_max
|
|
|
|
value is returned from a call to
|
|
|
|
.Fn getrlimit RLIMIT_DATA &rlim .
|
2000-12-29 14:08:20 +00:00
|
|
|
(See
|
1994-05-27 05:00:24 +00:00
|
|
|
.Xr end 3
|
|
|
|
for the definition of
|
2001-02-01 16:38:02 +00:00
|
|
|
.Va etext ) .
|
1994-05-27 05:00:24 +00:00
|
|
|
.Sh RETURN VALUES
|
2002-01-24 12:11:31 +00:00
|
|
|
.Rv -std brk
|
2001-08-31 09:57:38 +00:00
|
|
|
.Pp
|
1994-05-27 05:00:24 +00:00
|
|
|
The
|
1996-08-22 22:05:59 +00:00
|
|
|
.Fn sbrk
|
2001-09-06 12:11:22 +00:00
|
|
|
function returns the prior break value if successful;
|
|
|
|
otherwise the value
|
2002-01-24 12:11:31 +00:00
|
|
|
.Po Vt "void *" Pc Ns \-1
|
2001-09-06 12:11:22 +00:00
|
|
|
is returned and the global variable
|
1994-05-27 05:00:24 +00:00
|
|
|
.Va errno
|
2001-08-31 09:57:38 +00:00
|
|
|
is set to indicate the error.
|
1994-05-27 05:00:24 +00:00
|
|
|
.Sh ERRORS
|
2002-12-18 09:22:32 +00:00
|
|
|
The
|
2001-09-06 12:11:22 +00:00
|
|
|
.Fn brk
|
2002-12-18 09:22:32 +00:00
|
|
|
and
|
1997-03-11 11:35:56 +00:00
|
|
|
.Fn sbrk
|
2002-12-18 09:22:32 +00:00
|
|
|
functions
|
2001-09-06 12:11:22 +00:00
|
|
|
will fail if:
|
2000-05-04 13:09:25 +00:00
|
|
|
.Bl -tag -width Er
|
2001-09-06 12:11:22 +00:00
|
|
|
.It Bq Er EINVAL
|
|
|
|
The requested break value was beyond the beginning of the data segment.
|
1994-05-27 05:00:24 +00:00
|
|
|
.It Bq Er ENOMEM
|
2001-09-06 12:11:22 +00:00
|
|
|
The data segment size limit, as set by
|
1994-05-27 05:00:24 +00:00
|
|
|
.Xr setrlimit 2 ,
|
|
|
|
was exceeded.
|
|
|
|
.It Bq Er ENOMEM
|
|
|
|
Insufficient space existed in the swap area
|
2001-09-06 12:11:22 +00:00
|
|
|
to support the expansion of the data segment.
|
1994-05-27 05:00:24 +00:00
|
|
|
.El
|
|
|
|
.Sh SEE ALSO
|
|
|
|
.Xr execve 2 ,
|
|
|
|
.Xr getrlimit 2 ,
|
2001-09-06 12:11:22 +00:00
|
|
|
.Xr mmap 2 ,
|
1997-01-20 23:23:22 +00:00
|
|
|
.Xr end 3 ,
|
2001-09-06 12:11:22 +00:00
|
|
|
.Xr free 3 ,
|
1997-01-20 23:23:22 +00:00
|
|
|
.Xr malloc 3
|
2005-01-20 09:17:07 +00:00
|
|
|
.Sh HISTORY
|
|
|
|
The
|
|
|
|
.Fn brk
|
|
|
|
function appeared in
|
|
|
|
.At v7 .
|
1994-05-27 05:00:24 +00:00
|
|
|
.Sh BUGS
|
2001-09-06 12:11:22 +00:00
|
|
|
Mixing
|
|
|
|
.Fn brk
|
|
|
|
or
|
|
|
|
.Fn sbrk
|
|
|
|
with
|
|
|
|
.Xr malloc 3 ,
|
|
|
|
.Xr free 3 ,
|
|
|
|
or similar functions will result in non-portable program behavior.
|
|
|
|
.Pp
|
1994-05-27 05:00:24 +00:00
|
|
|
Setting the break may fail due to a temporary lack of
|
2001-09-06 12:11:22 +00:00
|
|
|
swap space.
|
|
|
|
It is not possible to distinguish this
|
1994-05-27 05:00:24 +00:00
|
|
|
from a failure caused by exceeding the maximum size of
|
2001-07-15 07:53:42 +00:00
|
|
|
the data segment without consulting
|
1996-03-27 20:49:07 +00:00
|
|
|
.Xr getrlimit 2 .
|