IBM AS/400 Frozen Dessert Maker User Manual


 
Managing Dynamically-Allocated Storage
The logic of the subprocedures is as follows:
1. DYNA_INIT creates the heap using the ILE bindable API CEECRHP (Create
Heap), storing the heap Id in a global variable HeapId. It allocates heap storage
based on initial value of the array (in this case 100) by calling the ILE bindable
API CEEGTST (Get Heap Storage).
2. DYNA_TERM destroys the heap using the ILE bindable API CEEDSHP
(Discard Heap).
3. DYNA_SET sets the value of an element in the array.
Before adding an element to the array, the procedure checks to see if there is
sufficient heap storage. If not, it uses operation code REALLOC to acquire
additional storage.
4. DYNA_GET returns the value of a specified element. The procedure returns to
the caller either the element requested, or zeros. The latter occurs if the
requested element has not actually been stored in the array.
To create the module DYNARRAY, type:
CRTRPGMOD MODULE(MYLIB/DYNARRAY) SRCFILE(MYLIB/QRPGLESRC)
The procedure can then be bound with other modules using CRTPGM or
CRTSRVPGM.
Figure 57 shows another module that tests the procedures in DYNARRAY.
*=================================================================
* DYNTEST: Test program for DYNARRAY module.
*=================================================================
/COPY EXAMPLES,DYNARRI
D X S LIKE(DYNA_TYPE)
* Initialize the array
C CALLP DYNA_INIT
* Set a few elements
C CALLP DYNA_SET (25 : 3)
C CALLP DYNA_SET (467252232 : 1)
C CALLP DYNA_SET (-2311 : 750)
* Retrieve a few elements
C EVAL X = DYNA_GET (750)
C '750' DSPLY X
C EVAL X = DYNA_GET (8001)
C '8001' DSPLY X
C EVAL X = DYNA_GET (2)
C '2' DSPLY X
* Clean up
C CALLP DYNA_TERM
C SETON LR
Figure 57. Sample module using procedures in DYNARRAY
126 ILE RPG for AS/400 Programmer's Guide