sbrk()

Set the allocation break value

Synopsis:

#include <unistd.h>

void* sbrk( int increment );

Arguments:

increment
The amount by which to increase the current break value. This increment may be positive or negative.

Library:

libc

Use the -l c option to qcc to link against this library. This library is usually included automatically.

This function is in libc.a, but not in libc.so (in order to save space).

Description:

The break value is the address of the first byte of unallocated memory. When a program starts execution, the break value is placed following the code and constant data for the program. As memory is allocated, this pointer advances when there is no free block large enough to satisfy an allocation request. The sbrk() function sets a new break value for the program by adding the value of increment to the current break value. Newly allocated space is set to zero.

The variable _amblksiz (defined in <stdlib.h>) contains the default increment. This value may be changed by a program at any time.

Returns:

A pointer to the start of the new block of memory, or -1 if an error occurs ( errno is set).

Errors:

EAGAIN
The total amount of system memory available for allocation to this process is temporarily insufficient. This may occur although the space requested is less than the maximum data segment size.
ENOMEM
The requested change allocated more space than allowed, or it caused a memory allocation conflict.

Examples:

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>

#define alloc( x, y ) y = sbrk( x );

int main( void )
{
    void* brk;

    brk = sbrk( 0x3100 );
    printf( "New break value after sbrk( 0x3100 ) \t%p\n",
            brk );

    brk = sbrk( 0x0200 );
    printf( "New break value after sbrk( 0x0200 ) \t%p\n",
            brk );
            
    return EXIT_SUCCESS;
}

Classification:

Legacy Unix

Safety:  
Cancellation point No
Interrupt handler No
Signal handler No
Thread Yes

Caveats:

Don't use brk() and sbrk() with any other memory functions (such as malloc() , mmap() , and free() ). The brk() function assumes that the heap is contiguous; in BlackBerry 10 OS, memory is returned to the system by the heap, causing the heap to become sparse. The BlackBerry 10 OS malloc() function is based on mmap(), and not on brk().

The sbrk() function has been used in specialized cases where no other memory allocation function provided the same capability. Use mmap() instead because it can be used portably with all other memory allocation functions and with any function that uses other allocation functions.

The value of the argument to sbrk() is rounded up for alignment with eight-byte boundaries.

Last modified: 2013-12-23

comments powered by Disqus