Previous Topic Next topic Print topic


Storage Sharing

Open PL/I provides two storage classes that are explicitly designed to permit storage locations to be shared by several variables. These storage classes are BASED and DEFINED. (Use of the UNION attribute also allows variables to explicitly share storage.)

Based variables are allocated no storage when they are declared. Instead, they serve to determine storage to be allocated by an ALLOCATE statement or to describe storage qualified by a pointer reference.

Defined variables use the storage of a specified basis variable. A defined variable is declared with the DEFINED attribute, which also specifies the basis variable.

A defined variable and its basis reference must satisfy one of the following criteria in order to share storage:

  1. All variables sharing storage have identical data types.
  2. All variables sharing storage are nonvarying character strings and therefore are suitable for character-string overlay defining.
  3. All variables sharing storage are unaligned bit strings and therefore are suitable for bit-string overlay defining.

A variable is suitable for overlay defining if it consists entirely of characters or bits that are packed into adjacent storage without gaps. (Gaps can be caused by the alignment of data in user-defined structures.)Variables suited for overlay defining are arrays, structures containing only the required type of string data, or scalar string variables. For example:

DECLARE X(4,4) CHARACTER(1);
DECLARE Y(16) CHARACTER(1) DEFINED(X);
DECLARE Z CHARACTER(5) DEFINED(X);
DECLARE 1 S BASED,
   2 A CHARACTER(8),
   2 B CHARACTER(8);

In this example, X has a block of storage containing 16 characters. The entire block is shared by Y, but Z shares only the first 5 characters of the block. S could be used to access all 16 characters, while S.A could be used to access the first 8, and S.B could be used to access the last 8. A based character string of more than 16 characters would produce undefined results, but a string of 16 or fewer characters could be used. A similar example could be constructed using all unaligned bit data.

Storage sharing of other data types requires that the data types match exactly, but an element of an array can be shared with a nonarray variable of the same data type. Similarly, a member of a structure can be shared with a nonmember of the same data type.

Members of structures that have the attribute UNION also share physical storage locations with one another. A UNION is a variation of a structure in which all immediate members occupy the same physical storage location. Unions provide capabilities similar to those of defined variables. The UNION attribute, which can be used only in conjunction with a level number in a structure declaration, signifies that all immediate members of the major or minor designated structure occupy the same storage. An immediate member is any member having a level number greater than its containing structure but not greater than the level number of its fellow members. Typically, an immediate member has a level number that is one greater than its containing structure. (For more information on this attribute, see the section Union.)

Arrays can share storage with other arrays only if they have the same data type and/or are equivalent left-to-right. Structures can share storage with other structures only if they are equivalent, left-to-right, to the structure whose storage is being shared.

Left-to-right equivalence means that the sharing structures must be valid descriptions of the left part of the storage being shared. To be a valid description of the left part of storage, the sharing structures must have identical members up to and including all members contained anywhere within the last level-two item being shared. If any part of a level-two item is to be shared, all of it must be shared. For example:

DECLARE   1 S,  DECLARE  1 T BASED,  DECLARE  1 U BASED,
          2 A,           2 A,                 2 A,
          2 B,           2 B,                 2 B,
          3 C,           3 C,                 3 C;
          3 D;           3 D;
          2 E;

In this example, a reference to T.B.C is a valid reference to S.B.C, but a reference to U.B.C is not valid, because the declaration of U does not describe the entire level-two item S.B.

A picture variable can share storage only with other picture variables that have identical pictures.

As is the case for argument/parameter matching, the ALIGNED, UNALIGNED, and VARYING attributes and the declared length of a string variable are part of its data type and must match, unless the strings qualify for string overlay sharing.

The base, scale, and precision of arithmetic variables must always match if they are to share storage.

Previous Topic Next topic Print topic