[Previous]
[Contents]
[Next]

_expand(), _bexpand(), _fexpand(), _nexpand()

change the size of a previously allocated block

Synopsis:

#include <malloc.h>
void *_expand( void *mem_blk, 
               size_t size );
void __based(void) *_bexpand( __segment seg,
                      void __based(void) *mem_blk,
                      size_t size );
void __far *_fexpand(void __far  *mem_blk,
                     size_t size );
void __near *_nexpand(void __near *mem_blk,
                      size_t size );

Description:

The _expand() functions change the size of the previously allocated block pointed to by mem_blk by attempting to expand or contract the memory block without changing its location in the heap. The argument size specifies the new desired size for the memory block. The contents of the memory block are unchanged up to the smaller of the new and old sizes.

Each function expands the memory from a particular heap, as listed below:

_expand()
Depends on data model of the program (see below)
_bexpand()
Based heap specified by seg value
_fexpand()
Far heap (outside the default data segment)
_nexpand()
Near heap (inside the default data segment)

In a small data memory model, the _expand() function is equivalent to _nexpand(); in a large data memory model, the _expand() function is equivalent to the _fexpand() function.

Returns:

The value mem_blk if the size of the block was successfully changed.

The return value is NULL (_NULLOFF for _bexpand()) if the memory block couldn't be expanded to the desired size. It's expanded as much as possible in this case.

The appropriate _msize() function can be used to determine the new size of the expanded block.

Examples:

#include <stdio.h>
#include <malloc.h>

void main()
  {
    char *buf;
    char __far *buf2;

    buf = (char *) malloc( 80 );
    printf( "Size of buffer is %u\n", _msize(buf) );
    if( _expand( buf, 100 ) == NULL ) {
      printf( "Unable to expand buffer\n" );
    }
    printf( "New size of buffer is %u\n", _msize(buf) );
    buf2 = (char __far *) _fmalloc( 2000 );
    printf( "Size of far buffer is %u\n", _fmsize(buf2) );
    if( _fexpand( buf2, 8000 ) == NULL ) {
      printf( "Unable to expand far buffer\n" );
    }
    printf( "New size of far buffer is %u\n",
      _fmsize(buf2) );
  }

produces the output

Size of buffer is 80
Unable to expand buffer
New size of buffer is 80
Size of far buffer is 2000
New size of far buffer is 8000

Classification:

WATCOM

Safety:
Interrupt handler No
Signal handler No
Thread Yes

See also:

calloc(), free(), halloc(), hfree(), malloc(), _msize(), realloc(), sbrk()


[Previous]
[Contents]
[Next]