Add memory to the heap (_heapadd) or to the based heap (_bheapadd).
#include <malloc.h> | Required only for function declarations |
int _heapadd( void __far *memblock, size_t size );
int _bheapadd( __segment seg, void __based (void) *memblock, size_t size );
seg | Based-heap segment selector | |
buffer | Pointer to heap memory | |
size | Size in bytes of memory to add |
The _heapadd and _bheapadd functions add an unused piece of memory to the heap. The _bheapadd function adds the memory to the based heap specified by seg. The _heapadd function looks at the segment value and, if it is DGROUP, adds the memory to the near heap. Otherwise, _heapadd adds the memory to the far heap.
These functions return 0 if successful, or –1 if an error occurred.
_headadd
Standards:None
16-Bit:DOS
32-Bit:DOS32X
_bheadadd
Standards:None
16-Bit:DOS, QWIN, WIN, WIN DLL
32-Bit:None
free functions, _halloc, _hfree, malloc functions, realloc functions
/* HEAPMIN.C: This program illustrates heap management using
* _heapadd and _heapmin.
*/
#include <stdio.h>
#include <conio.h>
#include <process.h>
#include <malloc.h>
void heapdump( char *msg ); /* Prototype */
char s1[] = { "Here are some strings that we use at first, then don't\n" };
char s2[] = { "need any more. We'll give their space to the heap.\n" };
void main( void )
{
int *p[3], i;
printf( "%s%s", s1, s2 );
heapdump( "Initial heap" );
/* Give space of used strings to heap. */
if ( _heapadd( s1, sizeof( s1 )) == -1 )
printf("Error.\n");
if ( _heapadd( s2, sizeof( s2 )) == -1 )
printf("Error.\n");
heapdump( "After adding used strings" );
/* Allocate some blocks. Some may use string blocks from _heapadd. */
for( i = 0; i < 2; i++ )
if( (p[i] = (int *)calloc( 10 * (i + 1), sizeof( int ) )) == NULL )
{
--i;
break;
}
heapdump( "After allocating memory" );
/* Free some of the blocks. */
free( p[1] );
free( p[2] );
heapdump( "After freeing memory" );
/* Minimize heap. */
_heapmin();
heapdump( "After compacting heap" );
}
/* Walk through heap entries, displaying information about each block. */
void heapdump( char *msg )
{
_HEAPINFO hi;
printf( "%s\n", msg );
hi._pentry = NULL;
while( _heapwalk( &hi ) == _HEAPOK )
printf( "\t%s block at %Fp of size %u\t\n",
hi._useflag == _USEDENTRY ? "USED" : "FREE",
hi._pentry,
hi._size );
printf("Press any key.\n");
_getch();
}
Here are some strings that we use at first, then don't
need any more. We'll give their space to the heap.
Initial heap
USED block at 2D39:0E9C of size 364
USED block at 2D39:100A of size 36
USED block at 2D39:1030 of size 512
FREE block at 2D39:1232 of size 460
After adding used strings
FREE block at 2D39:0044 of size 52
FREE block at 2D39:007A of size 50
USED block at 2D39:00AE of size 3564
USED block at 2D39:0E9C of size 364
USED block at 2D39:100A of size 36
USED block at 2D39:1030 of size 512
FREE block at 2D39:1232 of size 460
After allocating memory
USED block at 2D39:0044 of size 20
USED block at 2D39:005A of size 40
FREE block at 2D39:0084 of size 40
USED block at 2D39:00AE of size 3564
USED block at 2D39:0E9C of size 364
USED block at 2D39:100A of size 36
USED block at 2D39:1030 of size 512
FREE block at 2D39:1232 of size 460
After freeing memory
USED block at 2D39:0044 of size 20
FREE block at 2D39:005A of size 40
FREE block at 2D39:0084 of size 40
USED block at 2D39:00AE of size 3564
USED block at 2D39:0E9C of size 364
USED block at 2D39:100A of size 36
USED block at 2D39:1030 of size 512
FREE block at 2D39:1232 of size 460
After compacting heap
USED block at 2D39:0044 of size 20
FREE block at 2D39:005A of size 82
USED block at 2D39:00AE of size 3564
USED block at 2D39:0E9C of size 364
USED block at 2D39:100A of size 36
USED block at 2D39:1030 of size 512
FREE block at 2D39:1232 of size 12