'\" te
.\"  Copyright 1989 AT&T  Copyright (c) 1997 Sun Microsystems, Inc.  All Rights Reserved.
.\" The contents of this file are subject to the terms of the Common Development and Distribution License (the "License").  You may not use this file except in compliance with the License.
.\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE or http://www.opensolaris.org/os/licensing.  See the License for the specific language governing permissions and limitations under the License.
.\" When distributing Covered Code, include this CDDL HEADER in each file and include the License file at usr/src/OPENSOLARIS.LICENSE.  If applicable, add the following below this CDDL HEADER, with the fields enclosed by brackets "[]" replaced with your own identifying information: Portions Copyright [yyyy] [name of copyright owner]
.TH brk 2 "14 Jan 1997" "SunOS 5.11" "System Calls"
.SH NAME
brk, sbrk \- change the amount of space allocated for the calling process's
data segment
.SH SYNOPSIS
.LP
.nf
#include <unistd.h>

\fBint\fR \fBbrk\fR(\fBvoid *\fR\fIendds\fR);
.fi

.LP
.nf
\fBvoid *\fR\fBsbrk\fR(\fBintptr_t\fR \fIincr\fR);
.fi

.SH DESCRIPTION
.sp
.LP
The \fBbrk()\fR and \fBsbrk()\fR functions are used to change dynamically the
amount of space allocated for the calling process's data segment (see
\fBexec\fR(2)). The change is made by resetting the process's break value and
allocating the appropriate amount of space. The break value is the address of
the first location beyond the end of the data segment. The amount of allocated
space increases as the break value increases. Newly allocated space is set to
zero. If, however, the same memory space  is reallocated to the same process
its contents are undefined.
.sp
.LP
When a program begins execution using \fBexecve()\fR the break is set at the
highest location defined by the program and data storage areas.
.sp
.LP
The \fBgetrlimit\fR(2) function may be used to determine the maximum
permissible size of the \fIdata\fR segment; it is not possible to set the break
beyond the \fBrlim_max\fR value returned from a call to \fBgetrlimit()\fR, that
is to say, "\fBend + rlim.rlim_max\fR." See \fBend\fR(3C).
.sp
.LP
The \fBbrk()\fR function sets the break value to \fIendds\fR and changes the
allocated space accordingly.
.sp
.LP
The \fBsbrk()\fR function adds  \fIincr\fR function bytes to the break value
and changes the allocated space accordingly. The \fIincr\fR function can be
negative, in which case the amount of allocated space is decreased.
.SH RETURN VALUES
.sp
.LP
Upon successful completion, \fBbrk()\fR returns \fB0\fR. Otherwise, it returns
\fB\(mi1\fR and sets \fBerrno\fR to indicate the error.
.sp
.LP
Upon successful completion, \fBsbrk()\fR returns the prior break value.
Otherwise, it returns \fB(void *)\(mi1\fR and sets \fBerrno\fR to indicate the
error.
.SH ERRORS
.sp
.LP
The \fBbrk()\fR and \fBsbrk()\fR functions will fail and no additional memory
will be allocated if:
.sp
.ne 2
.mk
.na
\fB\fBENOMEM\fR\fR
.ad
.RS 10n
.rt  
The data segment size limit as set by \fBsetrlimit()\fR (see
\fBgetrlimit\fR(2)) would be exceeded; the maximum possible size of a data
segment (compiled into the system) would be exceeded; insufficient space exists
in the swap area to support the expansion; or the new break value would extend
into an area of the address space defined by some previously established
mapping (see \fBmmap\fR(2)).
.RE

.sp
.ne 2
.mk
.na
\fB\fBEAGAIN\fR\fR
.ad
.RS 10n
.rt  
Total amount of system memory available for private pages is temporarily
insufficient. This may occur even though the space requested was less than the
maximum data segment size (see  \fBulimit\fR(2)).
.RE

.SH USAGE
.sp
.LP
The behavior of \fBbrk()\fR and \fBsbrk()\fR is unspecified if an application
also uses any other memory functions (such as \fBmalloc\fR(3C), \fBmmap\fR(2),
\fBfree\fR(3C)). The \fBbrk()\fR and \fBsbrk()\fR functions have been used in
specialized cases where no other memory allocation function provided the same
capability.  The use of \fBmmap\fR(2) is now preferred because it can be used
portably with all other memory allocation functions and with any function that
uses other allocation functions.
.sp
.LP
It is unspecified whether the pointer returned by \fBsbrk()\fR is aligned
suitably for any purpose.
.SH ATTRIBUTES
.sp
.LP
See \fBattributes\fR(5) for descriptions of the following attributes:
.sp

.sp
.TS
tab() box;
cw(2.75i) |cw(2.75i) 
lw(2.75i) |lw(2.75i) 
.
ATTRIBUTE TYPEATTRIBUTE VALUE
_
MT-LevelMT-Safe
.TE

.SH SEE ALSO
.sp
.LP
\fBexec\fR(2), \fBgetrlimit\fR(2), \fBmmap\fR(2), \fBshmop\fR(2),
\fBulimit\fR(2), \fBend\fR(3C), \fBfree\fR(3C), \fBmalloc\fR(3C)
.SH NOTES
.sp
.LP
The value of \fIincr\fR may be adjusted by the system before setting the new
break value.  Upon successful completion, the implementation guarantees a
minimum of \fIincr\fR bytes will be added to the data segment if \fIincr\fR is
a positive value.  If \fIincr\fR is a negative value, a maximum of \fIincr\fR
bytes will be removed from the data segment.  This adjustment may not be
necessary for all machine architectures.
.sp
.LP
The value of the arguments to both \fBbrk()\fR and \fBsbrk()\fR are rounded up
for alignment with eight-byte boundaries.
.SH BUGS
.sp
.LP
Setting the break may fail due to a temporary lack of swap space. It is not
possible to distinguish this from a failure caused by exceeding the maximum
size of the data segment without consulting \fBgetrlimit()\fR.