Following is an example of how to manage storage for a C program
using callable services.
/*Module/File Name: EDCSTOR */
/****************************************************************/
/* */
/* Function : CEE3RPH - Set report heading */
/* : CEECRHP - Create user heap */
/* : CEEGTST - Obtain storage from user heap */
/* : CEECZST - Change size of this piece of storage */
/* : CEEFRST - Free this piece of storage */
/* : CEEDSHP - Discard user heap */
/* */
/* This example illustrates the invocation of the Language */
/* Environment Dynamic Storage Callable Services for a */
/* C program */
/* 1. A report heading is set for display at the beginning */
/* of the storage or options report. */
/* */
/* 2. A user heap is created. */
/* */
/* 3. Storage is allocated from the user heap. */
/* */
/* 4. A change is made to the size of the allocated storage.*/
/* */
/* 5. The allocated storage is freed. */
/* */
/* 6. The user heap is discarded. */
/* */
/*****************************************************************/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <leawi.h>
#include <ceeedcct.h>
void main ()
{
_CHAR80 RPTHEAD;
_INT4 HEAPID;
_INT4 HPSIZE;
_INT4 NBYTES;
_INT4 INCR;
_INT4 OPTS;
_INT4 STORALC;
_POINTER ADDRSS;
_FEEDBACK FC;
printf ( "\n**********************************\n");
printf ( "\nCE90STO C Example is now in motion\n");
printf ( "\n**********************************\n");
memset ( RPTHEAD , ' ' , 80 );
memcpy ( RPTHEAD , "User defined report heading" , 27 );
/*******************************************************
* Call CEE3RPH to set the user defined report heading *
*******************************************************/
CEE3RPH ( RPTHEAD , &FC );
if ( _FBCHECK ( FC , CEE000 ) != 0 )
printf ( "Error in setting report heading\n" );
/*******************************************************
* Call CEECRHP to create a user heap *
*******************************************************/
HEAPID = 0;
HPSIZE = 1;
INCR = 0;
OPTS = 0;
STORALC = 0;
CEECRHP ( &HEAPID , &HPSIZE , &INCR , &OPTS , &FC );
if ( _FBCHECK ( FC , CEE000 ) == 0 )
{
/*******************************************************
* Call CEEGTST to get storage from user heap *
*******************************************************/
NBYTES = 4000;
CEEGTST ( &HEAPID , &NBYTES , &ADDRSS , &FC );
if ( ( _FBCHECK ( FC , CEE000 ) == 0 ) && ADDRSS != 0 )
{
/*******************************************************
* Call CEECZST to change size of heap element *
*******************************************************/
NBYTES = 2000;
CEECZST ( &ADDRSS , &NBYTES , &FC );
if ( _FBCHECK ( FC , CEE000 ) == 0 )
{
STORALC = 1;
}else{
printf ( "Error in changing size of storage\n");
}
}else{
printf ( "Error in getting user storage\n" );
}
}else{
printf ( "Error in creating user heap\n" );
}
if (STORALC != 0)
{
/*******************************************************
* Call CEEFRST to free this piece of storage *
*******************************************************/
CEEFRST ( &ADDRSS , &FC );
if ( _FBCHECK ( FC , CEE000 ) == 0 )
{
/*******************************************************
* Call CEEDSHP to discard user heap *
*******************************************************/
CEEDSHP ( &HEAPID , &FC );
if ( _FBCHECK ( FC , CEE000 ) == 0 )
{
printf ( "C/370 Storage Example ended\n" );
exit(0);
}else{
printf ( "Error discarding user heap\n" );
}
}else{
printf ( "Error freeing storage from heap\n" );
}
}
exit(-1);
}