Mainframes 360
The one stop destination for System Z professionals

Wednesday, April 2, 2014

ODO Tables and Dynamic Arrays

ODO Tables

Fixed-length tables are good, if the table size is known to the programmer. At times, it is hard to predict an accurate table size. In such situations, COBOL's ODO clause or OCCURS DEPENDING ON may be used to create a table of size X, where X is a variable. Further, OCCURS DEPENDING ON does not offer any memory savings. ODO tables are always allocated at the maximum size N at compile time, similar to fixed tables. But during SEARCH or SEARCH ALL, ODO tables limit the access to just X out of N total occurrences (X < N), thereby saving processing time.
       DATA DIVISION.                                                   
  WORKING-STORAGE SECTION.
  *______________________________________________________________*
  * MISCALLANEOUS VARIABLES *
  *______________________________________________________________*
  01 WS-VARIABLES.
  05 X PIC S9(04) COMP.
  *______________________________________________________________*
  * INTERNAL TABLES/ARRAYS *
  *______________________________________________________________*
  01 CUSTOMER-ACCOUNTS-TABLE.
  05 CUSTOMER-ACCOUNT OCCURS 1 TO 10 TIMES
  DEPENDING ON X.
  10 CASH-ACCOUNT-NO PIC X(10).
  10 BALANCE PIC 9(07)V99.
  10 CUSTOMER-NAME PIC X(30).
CUSTOMER-ACCOUNT is said to be the ODO subject, and X the ODO object. If X is assigned a value 5 at run time, it limits access to 5 occurrences of CUSTOMER-ACCOUNT.


Dynamically allocated Arrays

In COBOL, the compiler estimates the memory needs for all WORKING STORAGE variables before execution. But, there may be cases when the memory needs cannot be established at compile-time. For example, you may want an array large enough to hold a file's records. In such cases, programs need to dynamically allocate memory.

Dynamic memory is allocated using a special LE(Language environment) service CEEGTST (Get Storage). The dynamic memory requested by the program comes is allocated by the system from the heap. Memory allocated using CEEGTST can be grown, resized through CEECZST (Change Storage). When memory is no longer needed, it is cleaned up, recycled using CEEFRST (Free Storage).

A dynamic array is allocated at run-time. First, the array layout is defined in LINKAGE SECTION. Like all LINKAGE SECTION items, the array layout does not occupy memory space. Rather, it will be used to address blocks of memory defined elsewhere in external storage.
       LINKAGE SECTION.                                                 
  01 LS-PARM-DATA.
  05 LS-PARM-LEN PIC S9(04) COMP.
  05 LS-ARRAY-LEN PIC S9(04) COMP.
 
  *______________________________________________________________*
  * DYNAMIC ARRAY *
  *______________________________________________________________*
  01 CUSTOMER-ACCOUNTS-TABLE.
  05 CUSTOMER-ACCOUNT OCCURS 1 TO 10 TIMES
  DEPENDING ON LS-ARRAY-LEN.
  10 CASH-ACCOUNT-NO PIC X(10).
  10 BALANCE PIC 9(07)V99.
  10 CUSTOMER-NAME PIC X(30).
Next, CEEGTST routine is called for dynamic memory allocation. CEEGTST accepts the HEAP-ID-0: the heap id and HEAP-SIZE: the amount of memory needed as inputs. If the memory requested is allocated, the HEAP-ADDRESS that points to the chunk of space allocated and FEEDBACK code indicating success or error is are returned as outputs.
       1100-ALLOC-MEMORY.                                              
  ****
  * THIS ROUTINE USES CEEGTST LE SERVICE TO ACQUIRE STORAGE *
  * DYNAMICALLY. ALL REQUESTS FOR STORAGE ARE CONDITIONAL. IF *
  * THE STORAGE IS NOT AVAILABLE, THE FEEDBACK CODE(FC) IS SET & *
  * RETURNED TO YOU, BUT THE PROGRAM DOES NO ABEND. *
  * *
  * CALL LE-GET-STORAGE USING HEAP-ID-0 SIZE ADDRESS FEEDBACK *
  ****
  MOVE ZERO TO HEAP-ID-0
  COMPUTE HEAP-SIZE = WS-ELM-SIZE * LS-ARRAY-LEN
 
  CALL LE-GET-STORAGE USING HEAP-ID-0
  HEAP-SIZE
  HEAP-ADDRESS
  FEEDBACK
  IF CEE000 OF FEEDBACK
  MOVE 'Y' TO WS-STORAGE-REQUEST-SW
  ELSE
  MOVE 'N' TO WS-STORAGE-REQUEST-SW
  END-IF.
  1100-EXIT. EXIT.

HEAP-SIZE is computed as Array length x size of 1 array element. The HEAP-ADDRESS contains the address of the newly allocated memory. The last step would be to establish addressability, by setting CUSTOMER-ACCOUNTS-TABLE to point to the newly allocated memory at HEAP-ADDRESS. CUSTOMER-ACCOUNTS-TABLE layout effectively is simply a redefinition for that memory block.
       1000-INITIALIZE.                                              
  PERFORM 1100-ALLOC-MEMORY THRU 1100-EXIT.
  IF STORAGE-REQUEST-SUCC
  SET ADDRESS OF CUSTOMER-ACCOUNTS-TABLE TO HEAP-ADDRESS
  END-IF.
  1000-EXIT. EXIT.

To many people who are thrown to work at a mainframe computer on their first job, they feel lost. Mainframe people seem to speak a completely different language and that doesn't make life easy. What's more, the books and manuals are incredibly hard to comprehend.

"What on earth is a Mainframe?" is an absolute beginner's guide to mainframe computers. We'll introduce you to the hardware and peripherals. We'll talk about the operating system, the software installed on a mainframe. We'll also talk about the different people who work on a mainframe. In a nutshell, we'll de-mystify the mainframe.

Readers based in India, can buy the e-book for Rs. 50 only or the print book. International readers based in the US and other countries can click here to purchase the e-book.