BRK(2)                    NetBSD System Calls Manual                    BRK(2)

NAME
     brk, sbrk -- change data segment size

LIBRARY
     Standard C Library (libc, -lc)

SYNOPSIS
     #include <unistd.h>

     int
     brk(void *addr);

     void *
     sbrk(intptr_t incr);

DESCRIPTION
     The brk and sbrk functions are legacy interfaces from before the advent
     of modern virtual memory management.

     The brk() and 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
     ``break''.

     The brk() function sets the break to addr.

     The sbrk() function changes the break by incr bytes.  If incr is posi-
     tive, this allocates incr bytes of new memory in the data segment.  If
     incr is negative, this releases the corresponding number of bytes.

     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.

     The getrlimit(2) system call may be used to determine the maximum permis-
     sible size of the 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
     RLIMIT_DATA rlim_max value returned from a call to getrlimit(2).  One can
     use the ``_etext'' symbol to find the end of the program text and thus
     the beginning of the data segment.  See end(3) regarding ``_etext''.

     Historically and in NetBSD 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 ``BSS'').  This address is provided by the linker as
     ``_end''; see end(3).

     There exist implementations in the wild where this is not the case, how-
     ever, 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 sbrk(0) at program startup.  (This
     returns the current break without changing it.)

     In any event, the break may not be set to an address below its initial
     position.

     Note that ordinary application code should use malloc(3) and related
     functions to allocate memory, or mmap(2) for lower-level page-granularity
     control.  While the brk() and/or 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
     malloc(3) or related functions with calls to brk() or sbrk() as this will
     ordinarily confuse malloc(3); this can be difficult to accomplish given
     that many things in the C library call malloc(3) themselves.

RETURN VALUES
     brk() returns 0 if successful; otherwise -1 with errno set to indicate
     why the allocation failed.

     The sbrk() function returns the prior break value if successful; other-
     wise ((void *)-1) is returned and errno is set to indicate why the allo-
     cation failed.

ERRORS
     brk() or sbrk() will fail and no additional memory will be allocated if
     one of the following are true:

     [ENOMEM]           The limit, as set by 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.

SEE ALSO
     execve(2), getrlimit(2), mmap(2), end(3), free(3), malloc(3), sysconf(3)

HISTORY
     A brk() function call appeared in Version 7 AT&T UNIX.

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 getrlimit(2).

NetBSD 7.0                      August 27, 2016                     NetBSD 7.0

You can also request any man page by name and (optionally) by section:

Command: 
Section: 
Architecture: 
Collection: 
 

Use the DEFAULT collection to view manual pages for third-party software.


©1994 Man-cgi 1.15, Panagiotis Christias <christia@softlab.ntua.gr>
©1996-2016 Modified for NetBSD by Kimmo Suominen