p 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 address at which the process's heap ends. This address is known as the .Dq break .
p The .Fn brk function sets the break to .Fa addr .
p The .Fn sbrk function changes the break by .Fa incr bytes. If .Fa incr is positive, this allocates .Fa incr bytes of new memory in the data segment. If .Fa incr is negative, this releases the corresponding number of bytes.
p While the break may be set to any address, actual allocation takes place in page-sized quantities. For allocation and access control purposes the address of the break is always rounded up to the next page boundary. Thus, changes to the break that do not cross a page boundary have no material effect. Any new pages that are allocated, however, always appear freshly zeroed.
p
The
.Xr getrlimit 2
system call may be used to determine
the maximum permissible size of the
.Em data
segment;
it will not be possible to set the break so that the sum of the heap
size and the data segment is greater than the
.Dv RLIMIT_DATA
.Em rlim_max
value returned from a call to
.Xr getrlimit 2 .
One can use the
.Dq _etext
symbol to find the end of the program text and thus the beginning of
the data segment.
XXX is that always true? there are platforms where there's a fairly
large unmapped gap between text and data, plus using etext doesn't
take into account read-only data, which is probably (or should be)
billed against text size and not data size.
See
.Xr end 3
regarding
.Dq _etext .
p Historically and in .Nx the heap immediately follows the data segment, and in fact is considered part of it. Thus the initial break is the first address after the end of the process's uninitialized data (also known as the .Dq BSS ) . This address is provided by the linker as .Dq _end ; see .Xr end 3 .
p There exist implementations in the wild where this is not the case, however, or where the initial break is rounded up to a page boundary, or other minor variations, so the recommended more-portable way to retrieve the initial break is by calling .Fn sbrk 0 at program startup. (This returns the current break without changing it.)
p In any event, the break may not be set to an address below its initial position.
p Note that ordinary application code should use .Xr malloc 3 and related functions to allocate memory, or .Xr mmap 2 for lower-level page-granularity control. While the .Fn brk and/or .Fn sbrk functions exist in most Unix-like environments, their semantics sometimes vary subtly and their use is not particularly portable. Also, one must take care not to mix calls to .Xr malloc 3 or related functions with calls to .Fn brk or .Fn sbrk as this will ordinarily confuse .Xr malloc 3 ; this can be difficult to accomplish given that many things in the C library call .Xr malloc 3 themselves. .Sh RETURN VALUES .Fn brk returns 0 if successful; otherwise -1 with .Va errno set to indicate why the allocation failed.
p The .Fn sbrk function returns the prior break value if successful; otherwise ((void *)-1) is returned and .Va errno is set to indicate why the allocation failed. .Sh ERRORS .Fn brk or .Fn sbrk will fail and no additional memory will be allocated if one of the following are true: l -tag -width Er t Bq Er ENOMEM The limit, as set by .Xr setrlimit 2 , was exceeded; or the maximum possible size of a data segment (compiled into the system) was exceeded; or insufficient space existed in the swap area to support the expansion. .El .Sh SEE ALSO .Xr execve 2 , .Xr getrlimit 2 , .Xr mmap 2 , .Xr end 3 , .Xr free 3 , .Xr malloc 3 , .Xr sysconf 3 .Sh HISTORY An .Fn sbrk function call appeared in .At v4 . A .Fn brk function call appeared in .At v6 . .Sh BUGS 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 .Xr getrlimit 2 .