SBRK(S) XENIX System V SBRK(S)
Name
sbrk, brk - Changes data segment space allocation.
Syntax
char *sbrk (incr)
int incr;
int brk (addr)
char *addr;
Description
sbrk and brk are used to dynamically change the amount of
space allocated for the data segment of the calling process;
see exec(S). The change is made by resetting the break
value of the process. 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.
sbrk adds incr bytes to the break value and changes the
allocated space accordingly. incr can be negative, in which
case the amount of allocated space is decreased.
In 286 large model programs, if incr is greater than the
number of unallocated bytes remaining in the current data
segment, sbrk automatically allocates all the requested
bytes in a new data segment. This guarantees that the
requested bytes will reside entirely in one segment. If
incr is negative and its absolute value is equal to the
number of allocated bytes in the current data segment, the
segment is automatically freed for other use. If incr is
negative and its absolute value is greater than the number
of allocated bytes in the current segment, the segment is
freed, and the additional bytes are removed from the
previous data segment. (The previous data segment contains
space allocated by the most recent sbrk that did not affect
the current segment.)
sbrk will fail without making any change in the allocated
space if:
A change would result in more space being allocated
than is allowed by a system-imposed maximum (see
ulimit(S)). [ENOMEM]
An attempt is made to remove more space than has
actually been allocated.
An attempt to remove space causes the new break value
to be less than the original break value. The original
break value is always taken to be break value when
process execution began plus any shared data bytes that
Page 1 (printed 8/7/87)
SBRK(S) XENIX System V SBRK(S)
have been allocated since that time.
brk sets the the current break value to addr, and changes
the allocated space accordingly. brk fails if the address
references a data segment that does not exist, or if it
references beyond the maximum possible size of the current
data segment.
Return Value
Upon successful completion, sbrk returns a pointer to the
beginning of the allocated space. brk returns 0 on
successful completion. Otherwise, a value of -1 is returned
and errno is set to indicate the error. In large model
programs, if sbrk allocates a new data segment, the return
value is the starting address of that new segment.
See Also
exec(S)
Notes
In 286 large model programs, the call ``sbrk(0)'' does not
necessarily return the starting address of the next sbrk
call. In particular, if the next call causes an additional
data segment to be allocated, the break values returned by
these two calls will not be the same. The return value from
``sbrk(0)'' should only be regarded as a marker for the
original end of data.
Page 2 (printed 8/7/87)