BRK(2) COMMAND REFERENCE BRK(2)
NAME
brk, sbrk - change data segment size
SYNOPSIS
#include <sys/types.h>
caddr_t brk(addr)
caddr_t addr;
newaddr = sbrk(incr)
caddr_t newaddr;
int incr;
DESCRIPTION
The commands brk and sbrk are used to change dynamically the
amount of space allocated for the calling process's
contiguous heap. The change is made by resetting the
process's break value. The break value is the address of
the first location beyond the end of the contiguous heap.
The amount of allocated space increases as the break value
increases: brk sets the break value to addr (rounded up to
the next multiple of the system's page size) and changes the
allocated space accordingly. Locations greater than addr
and below the stack pointer are not in the address space and
will thus cause a memory violation if accessed.
The command sbrk adds incr more bytes to the break value and
changes the allocated space accordingly. A pointer to the
start of the new area is returned in newaddr.
When a program begins execution via execve the break is set
at the highest location defined by the program and data
storage areas. Ordinarily, therefore, only programs with
growing data areas need to use sbrk.
The getrlimit(2) system call may be used to determine the
maximum permissible size of the data segment; it will not be
possible to set the break beyond the rlim_max value returned
from a call to getrlimit, e.g. "etext + rlp->rlim_max." (See
end(3c) for the definition of etext.)
DIAGNOSTICS
The sbrk and brk commands will fail and no additional memory
will be allocated if one of the following are true:
[ENOMEM] The limit, as set by setrlimit(2), would be
exceeded.
[ENOMEM] The maximum possible size of a data segment,
text segment or stack would be exceeded. These
limits are MAXTSIZ, MAXDSIZ and MAXSSIZ,
defined in <machine/vmparam.h>.
Printed 4/6/89 1
BRK(2) COMMAND REFERENCE BRK(2)
[ENOMEM] Insufficient space exists in the swap area to
support the expansion.
RETURN VALUE
The brk command returns 0 if the break could be set,
otherwise it returns -1; sbrk returns a pointer to the new
data area in newaddr if the break could be set, otherwise it
returns -1. Both brk and sbrk set errno if there is an
error.
CAVEATS
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 getrlimit.
SEE ALSO
execve(2), getrlimit(2), end(3c), and malloc(3c).
Printed 4/6/89 2
%%index%%
na:216,88;
sy:304,1125;
de:1429,1836;
di:3265,695;4248,106;
rv:4354,549;
ca:4903,325;
se:5228,228;
%%index%%000000000132