Retro video games delivered to your door every month!
Click above to get retro games delivered to your door ever month!
X-Hacker.org- Watcom C Library Reference - <u>synopsis:</u> http://www.X-Hacker.org [<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]
Synopsis:
    #include <stdlib.h>
    void *sbrk( int increment );

Description:
    Under 16-bit DOS and Phar Lap's 386|DOS-Extender, the data segment is
    grown contiguously.  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 will advance when there is no freed
    block large enough to satisfy an allocation request.  The sbrk function
    can be used to set a new "break" value for the program by adding the
    value of increment to the current break value.  This increment may be
    positive or negative.

    Under other systems, heap allocation is discontiguous.  The sbrk
    function can only be used to allocate additional discontiguous blocks of
    memory.  The value of increment is used to determine the minimum size of
    the block to be allocated and may not be zero or negative.  The actual
    size of the block that is allocated is rounded up to a multiple of 4K.

    The variable  _amblksiz defined in <stdlib.h> contains the default
    increment by which the "break" pointer for memory allocation will be
    advanced when there is no freed block large enough to satisfy a request
    to allocate a block of memory.  This value may be changed by a program
    at any time.

    Under 16-bit DOS, a new process started with one of the  spawn...  or
     exec...  functions is loaded following the break value.  Consequently,
    decreasing the break value leaves more space available to the new
    process.  Similarly, for a resident program (a program which remains in
    memory while another program executes), increasing the break value will
    leave more space available to be allocated by the resident program after
    other programs are loaded.

Returns:
    If the call to sbrk succeeds, a pointer to the start of the new block of
    memory is returned.  Under 16-bit DOS, this corresponds to the old break
    value.  If the call to sbrk fails, -1 is returned.  When an error has
    occurred,  errno contains a value indicating the type of error that has
    been detected.

Example:
    #include <stdio.h>
    #include <stdlib.h>

    #if defined(M_I86)
    #define alloc( x, y ) sbrk( x ); y = sbrk( 0 );
    #else
    #define alloc( x, y ) y = sbrk( x );
    #endif

    void main()
     {
        void *brk;

    #if defined(M_I86)
        alloc( 0x0000, brk );
        /* calling printf will cause an allocation */
        printf( "Original break value %p\n", brk );
        printf( "Current amblksiz value %x\n", _amblksiz );
        alloc( 0x0000, brk );
        printf( "New break value after printf \t\t%p\n", brk );
    #endif
        alloc( 0x3100, brk );
        printf( "New break value after sbrk( 0x3100 ) \t%p\n",
                brk );
        alloc( 0x0200, brk );
        printf( "New break value after sbrk( 0x0200 ) \t%p\n",
                brk );
    #if defined(M_I86)
        alloc( -0x0100, brk );
        printf( "New break value after sbrk( -0x0100 ) \t%p\n",
                brk );
    #endif
     }

Classification:
    WATCOM

Systems:
    DOS, Windows, Win386, Win32, QNX, OS/2 1.x, OS/2 1.x(MT), OS/2-32

See Also:
    calloc Functions, _expand Functions, free Functions, halloc, hfree,
    malloc Functions, _msize Functions, realloc Functions

See Also: halloc hfree

Online resources provided by: http://www.X-Hacker.org --- NG 2 HTML conversion by Dave Pearson