COBOL: Dynamically Allocating memory in COBOL batch program

COBOL language does not have any features to dynamically allocate memory.  So, we use the Language environment (LE) programs to allocate and free the storage.

LE program
Function
CEECRHP
Allocates user heap
CEEGTST
Allocates storage in the user heap and returns the address of that storage
CEEMSG
If the invocation of any LE program is not successful, then CEEMSG can be invoked immediately to display the detailed diagnostic message about the last LE program invocation
CEEFRST
Releases storage back to the user heap
CEEDSHP
Releases the user heap allocated by CEECRHP

           
Following are the steps needs to be followed when dynamically allocating memory in batch programs.

  1. Invoke CEECRHP to create a new user heap
  2. 2.If return code of CEECRHP is not successful, then call CEEMSG to display the diagnostic message and abend the program.
  3. Invoke CEEGTST to allocate the storage within the user heap allocated in step 1
  4. 4.If return code of CEEGTST is not successful, then call CEEMSG to display the diagnostic message and abend the program.
  5. 5.Once we are done with the newly allocated memory, then free the storage by calling CEEFRST
  6. 6.If return code of CEEFRST is not successful, then call CEEMSG to display the diagnostic message and abend the program.
  7. Finally we can free the user heap by invoking CEEDSHP
  8. 8.If return code of CEEDSHP is not successful, then call CEEMSG to display the diagnostic message and abend the program.

Note that the steps from 5 to 8 are not mandatory. They are executed automatically when the job step completes. But executing those steps within the program when the storage is no longer needed reduces the total amount of storage needed to run the application.

Sample code is below.

Variable declaration:-

01  LE-AREA.                                               
    05   LE-HEAP-ID                 PIC S9(9)   COMP.       
    05   LE-HEAP-INIT-SIZE          PIC S9(9)   COMP-5.     
    05   LE-HEAP-INCREMENT          PIC S9(9)   COMP.       
    05   LE-HEAP-OPTIONS            PIC S9(9)   COMP.       
    05   LE-STORAGE-SIZE            PIC S9(9)   COMP-5.     
    05   LE-STORAGE-POINTER         USAGE POINTER.          
    05   LE-FEEDBACK.                                  
        10   LE-SEVERITY            PIC S9(04) COMP.   
        10   LE-MSG-NO              PIC S9(04) COMP.   
        10   LE-CASE-SEV-CTL        PIC X(01).         
        10   LE-FACILITY-ID         PIC X(03).         
        10   LE-IS-INFO             PIC S9(09) COMP.   
    05   LE-FEEDBACK-ERROR.                            
        10   LE-ERR-SEVERITY        PIC S9(04) COMP.      
        10   LE-ERR-MSG-NO          PIC S9(04) COMP.      
        10   LE-ERR-CASE-SEV-CTL    PIC X(01).            
        10   LE-ERR-FACILITY-ID     PIC X(03).            
        10   LE-ERR-IS-INFO         PIC S9(09) COMP.      
    05   LE-MSGDEST                 PIC S9(09) COMP.      


Procedure division code:-

       5200-GET-MEMORY-USING-LE.                                              
                                                                       
           MOVE +65536                 TO LE-HEAP-INIT-SIZE.       
           MOVE +4096                  TO LE-HEAP-INCREMENT.       
           MOVE +72                    TO LE-HEAP-OPTIONS.         
                                                                       
           CALL PGM-CEECRHP      USING LE-HEAP-ID                      
                                       LE-HEAP-INIT-SIZE               
                                       LE-HEAP-INCREMENT               
                                       LE-HEAP-OPTIONS                 
                                       LE-FEEDBACK.                    
                                                                        
           IF   LE-SEVERITY = ZERO                                     
           AND  LE-MSG-NO   = ZERO                                   
               DISPLAY ' ===> GOT HEAP SUCCESSFULLY '                       
           ELSE                                                        
               DISPLAY ' ===> GET HEAP FAILED '                            
               PERFORM 8000-CEEMSG THRU 8000-EXIT                      
               GO TO 9999-ABEND                                        
           END-IF.                                                     
                                                                       
           MOVE +65536                 TO LE-STORAGE-SIZE.         
                                                                       
           CALL PGM-CEEGTST      USING LE-HEAP-ID                      
                                       LE-STORAGE-SIZE                 
                                       LE-STORAGE-POINTER              
                                       LE-FEEDBACK.                    
                                                                       
           IF   LE-SEVERITY = ZERO                                      
           AND  LE-MSG-NO     = ZERO                                   
               DISPLAY ' ===> GOT STORAGE SUCCESSFULLY'
           ELSE                                                        
               DISPLAY ' ===> GET STORAGE FAILED '
               PERFORM 8000-CEEMSG THRU 8000-EXIT                      
               GO TO 9999-ABEND                                        
           END-IF.                                                     

* Set Address of the LINKAGE data item to the newly allocated memory

           SET ADDRESS OF MY-NEWLY-ALLOCATED-DATA   TO LE-STORAGE-POINTER.          
                                                                       
       5200-EXIT.  EXIT.                                                
                                                                       
                                                                       
       8000-CEEMSG.                                                    
                                                                        
           MOVE 2                      TO  LE-MSGDEST.                 
                                                                       
           CALL PGM-CEEMSG   USING  LE-FEEDBACK                         
                                    LE-MSGDEST                         
                                    LE-FEEDBACK-ERROR.                 
                                                                       
           IF   LE-ERR-SEVERITY = ZERO                                 
           AND  LE-ERR-MSG-NO     = ZERO                               
               CONTINUE                                                
           ELSE                                                         
               DISPLAY ' ===> ERROR CALLING CEEMSG'                        
               GO TO 9999-ABEND                                        
           END-IF.                                                     
                                                                        
       8000-EXIT.  EXIT.                                               
                                                                       
                                                                        
       8100-FREE-HEAP-STORAGE.                                         
                                                                       
           CALL PGM-CEEFRST      USING LE-STORAGE-POINTER              
                                       LE-FEEDBACK.                    
                                                                       
           IF   LE-SEVERITY = ZERO                                     
           AND  LE-MSG-NO     = ZERO                                    
               DISPLAY ' ===> FREE STORAGE SUCCESSFULLY'                   
           ELSE                                                        
               DISPLAY    ' ===> FREE STORAGE FAILED'                         
               GO TO 9999-ABEND                                        
           END-IF.                                                     
                                                                       
           CALL PGM-CEEDSHP      USING LE-HEAP-ID                      
                                       LE-FEEDBACK.                    
                                                                       
           IF   LE-SEVERITY = ZERO                                     
           AND  LE-MSG-NO     = ZERO                                   
               DISPLAY ' ===> DISCARD HEAP SUCCESSFULLY '                  
           ELSE                                                        
               DISPLAY ' ===> DISCARD HEAP FAILED '                        
               GO TO 9999-ABEND                               
           END-IF.                                            
                                                              
       8100-EXIT.  EXIT.                                      

No comments :

Post a Comment

Note: Only a member of this blog may post a comment.