The following routine calls CEEHDLR to register a user-written condition handler for the out-of-storage condition, calls CEEGTST to allocate heap storage, and calls CEECZST to alter the size of the heap storage requested.
/*Module/File Name: EDCOOSR */
/*********************************************************/
/* */
/* Function : CEEHDLR - Register user condition handler */
/* : CEEGTST - Get Heap Storage */
/* : CEECZST - Change the size of heap element */
/* */
/* 1. A user condition handler CECNDHD is registered. */
/* 2. A large amount of HEAP storage is allocated. */
/* 3. A function sub() is called that is known to */
/* require a large amount of storage. It is not */
/* known whether the storage for sub() is */
/* available during this run of the application. */
/* 4. If sufficient storage for sub() is not available, */
/* a storage condition is generated by Language */
/* Environment. */
/* 5. CECNDHD gets control and sets resume at the */
/* next instruction following the call to sub(). */
/* 6. A test for completion of sub() is made after */
/* the function call. If sub() did not complete, a */
/* large amount of storage is freed, and sub() is */
/* invoked a second time. */
/* 7. sub() runs successfully once it has enough storage */
/* available. */
/* */
/* Note: In order for this example to complete */
/* successfully, the FREE suboption of the HEAP */
/* runtime option must be in effect. */
/* */
/*********************************************************/
#include <stdio.h>
#include <string.h>
#include <leawi.h>
#include <ceeedcct.h>
#define BIGSTOR 300000
#define BIGINDX BIGSTOR-1
#ifdef __cplusplus
extern "C" {
#endif
void CECNDHD(_FEEDBACK *, _INT4 *, _INT4 *, _FEEDBACK *);
#ifdef __cplusplus
}
#endif
char *sub( );
void main ()
{
_FEEDBACK feedback;
_ENTRY pgmptr;
_POINTER addrss;
_INT4 token;
_INT4 hpsize;
_INT4 heapid;
_INT4 newsize;
char *RAN;
/***********************************************************/
/* Call CEEHDLR to register user condition handler CECNDHD.*/
/***********************************************************/
pgmptr.address = (_POINTER)&CECNDHD;
pgmptr.nesting = NULL;
token = 97;
CEEHDLR(&pgmptr, &token, &feedback);
if ( _FBCHECK ( feedback , CEE000 ) != 0 )
printf( "CEEHDLR failed with message number %d\n",
feedback.tok_msgno);
else
printf( "Condition handler registered\n" );
/***********************************************************/
/* Call function sub(). When it becomes active, an out- */
/* of-storage condition arises if the region is too small. */
/***********************************************************/
heapid = 0;
hpsize = BIGSTOR;
CEEGTST ( &heapid , &hpsize , &addrss , &feedback );
if ( _FBCHECK ( feedback , CEE000 ) != 0 )
printf("CEEGTST failed with message number %d\n",
feedback.tok_msgno);
RAN = sub ( );
if (RAN != "r")
{
/*******************************************************/
/* If sub() did not run, reduce the size of allocated */
/* storage and call it a second time. */
/*******************************************************/
newsize = 2000;
CEECZST ( &addrss, &newsize, &feedback );
if ( _FBCHECK ( feedback , CEE000 ) != 0 )
printf( "CEECZST failed with message number %d\n",
feedback.tok_msgno);
printf("Function sub is called for the 2nd time\n");
RAN = sub ( );
printf("Function sub ran successfully\n", *RAN);
};
} /* end of main */
char *sub( )
{
char w2[BIGSTOR];
w2[BIGINDX] = 'B';
return( "r" );
} /* end of sub */
When any condition occurs in the main routine, user condition handler CECNDHD in the following routine receives control and tests for the out-of-storage condition. If the out-of-storage condition has occurred, then CECNDHD calls CEEMRCR to return to the instruction in the main routine after the call to function sub() that produced the out-of-storage condition.
/*Module/File Name: EDCOOSH */
/**********************************************************/
/* */
/* Function : CEEMRCR - Move resume cursor relative */
/* to handle cursor. */
/* */
/* CECNDHD is a user condition handler that is registered */
/* by a main routine. CECNDHD gets control from the */
/* condition manager and tests for the STORAGE CONDITION. */
/* If a STORAGE CONDITION is detected, the resume cursor */
/* is moved so that control is returned to the caller of */
/* the routine encountering the STORAGE CONDITION. */
/* */
/**********************************************************/
#include <stdio.h>
#include <string.h>
#include <leawi.h>
#include <ceeedcct.h>
#define RESUME 10
#define PERCOLATE 20
#define PROMOTE 30
#define PROMOTE_STACK_FRAME 31
#ifdef __cplusplus
extern "C" {
#endif
void CECNDHD (_FEEDBACK *, _INT4 *, _INT4 *, _FEEDBACK *);
#ifdef __cplusplus
}
#endif
void CECNDHD (_FEEDBACK *cond, _INT4 *input_token,
_INT4 *result, _FEEDBACK *new_cond)
{
_FEEDBACK feedback;
_INT4 movetyp;
/**********************************************************/
/* Determine if entry was for OUT-OF-STORAGE condition. */
/**********************************************************/
if ( _FBCHECK (*cond , CEE0PD) == 0 )
{
printf("SUB not run because of storage condition.\n");
/******************************************************/
/* Call CEEMRCR to move resume cursor. */
/******************************************************/
movetyp = 0;
CEEMRCR ( &movetyp , &feedback );
if ( _FBCHECK ( feedback , CEE000) != 0 )
{
*result = PERCOLATE;
}
else
{
*result = RESUME;
}
}
else
{
/*******************************************************/
/* Percolate all conditions except for OUT-OF-STORAGE. */
/*******************************************************/
*result = PERCOLATE;
}
}