Next: , Previous: Pragma Pack for Arrays, Up: Representation Clauses and Pragmas

6.11 Pragma Pack for Records

Pragma Pack applied to a record will pack the components to reduce wasted space from alignment gaps and by reducing the amount of space taken by components. We distinguish between packable components and non-packable components. Components of the following types are considered packable:

All packable components occupy the exact number of bits corresponding to their Size value, and are packed with no padding bits, i.e. they can start on an arbitrary bit boundary.

All other types are non-packable, they occupy an integral number of storage units, and are placed at a boundary corresponding to their alignment requirements.

For example, consider the record

        type Rb1 is array (1 .. 13) of Boolean;
        pragma Pack (rb1);
        type Rb2 is array (1 .. 65) of Boolean;
        pragma Pack (rb2);
        type x2 is record
           l1 : Boolean;
           l2 : Duration;
           l3 : Float;
           l4 : Boolean;
           l5 : Rb1;
           l6 : Rb2;
        end record;
        pragma Pack (x2);

The representation for the record x2 is as follows:

     for x2'Size use 224;
     for x2 use record
        l1 at  0 range  0 .. 0;
        l2 at  0 range  1 .. 64;
        l3 at 12 range  0 .. 31;
        l4 at 16 range  0 .. 0;
        l5 at 16 range  1 .. 13;
        l6 at 18 range  0 .. 71;
     end record;

Studying this example, we see that the packable fields l1 and l2 are of length equal to their sizes, and placed at specific bit boundaries (and not byte boundaries) to eliminate padding. But l3 is of a non-packable float type, so it is on the next appropriate alignment boundary.

The next two fields are fully packable, so l4 and l5 are minimally packed with no gaps. However, type Rb2 is a packed array that is longer than 64 bits, so it is itself non-packable. Thus the l6 field is aligned to the next byte boundary, and takes an integral number of bytes, i.e. 72 bits.