Set the allocation break value
#include <unistd.h> void* sbrk( int increment );
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). |
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.
A pointer to the start of the new block of memory, or -1 if an error occurs (errno is set).
#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; }
Safety: | |
---|---|
Cancellation point | No |
Interrupt handler | No |
Signal handler | No |
Thread | Yes |
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 Neutrino, memory is returned to the system by the heap, causing the heap to become sparse. The Neutrino 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.
_amblksiz, brk(), _btext, calloc(), _edata, _end, errno, _etext, free(), malloc(), realloc()