brk(2)


brk, sbrk -- change data segment space allocation

Synopsis

   #include <unistd.h>
   

int brk(void *endds);

void *sbrk(int incr);

Description

brk and sbrk are used to change dynamically the amount of space allocated for the calling process's data segment [see exec(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.

brk sets the break value to endds and changes the allocated space accordingly.

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.

Return values

On success, brk returns 0 and sbrk returns the old break value. On failure, brk and sbrk return -1 and set errno to identify the error without making any change in the allocated space.

Errors

In the following conditions, brk and sbrk fail and set errno to:

ENOMEM
Such a change would result in more space being allocated than is allowed by the system-imposed maximum process size [see ulimit(2)].

EAGAIN
Total amount of system memory or swap space available is temporarily insufficient [see shmop(2)]. This may occur even though the space requested was less than the system-imposed maximum process size [see ulimit(2)].

References

end(3C), exec(2), malloc(3C), shmop(2), ulimit(2)

Notices

Usage

Applications generally cope with the details of dynamic memory allocation/deallocation by using the malloc(3C) family of library functions. Moreover, use of brk or sbrk can interfere with the dynamic memory used by malloc(3C).

Considerations for threads programming

Threads within a process share (by definition) the same address space; modifications to the address space by one thread can be perceived by sibling threads.

The sbrk function is actually a front end to the brk system call and has a potential race condition that might produce incorrect results if used concurrently by sibling threads. Such usage is not advised. The malloc(3C) family of functions has been made safe for use by threads.


© 2004 The SCO Group, Inc. All rights reserved.
UnixWare 7 Release 7.1.4 - 25 April 2004