Next: Size Clauses, Up: Representation Clauses and Pragmas
GNAT requires that all alignment clauses specify a power of 2, and all default alignments are always a power of 2. The default alignment values are as follows:
Pack
is
used and all fields are packable (see separate section on pragma Pack
),
then the resulting alignment is 1.
A special case is when the size of the record is given explicitly, or a full record representation clause is given, and the size of the record is 2, 4, or 8 bytes. In this case, an alignment is chosen to match the size of the record. For example, if we have:
type Small is record A, B : Character; end record;
then the default alignment of the record type Small
is 2, not 1. This
leads to more efficient code when the record is treated as a unit, and also
allows the type to specified as Atomic
on architectures requiring
strict alignment.
An alignment clause may always specify a larger alignment than the default value, up to some maximum value dependent on the target (obtainable by using the attribute reference System'Maximum_Alignment). The only case in which it is permissible to specify a smaller alignment than the default value is in the case of a record for which a record representation clause is given. In this case, packable fields for which a component clause is given still result in a default alignment corresponding to the original type, but this may be overridden, since these components in fact only require an alignment of one byte. For example, given
type v is record a : integer; end record; for v use record a at 0 range 0 .. 31; end record; for v'alignment use 1;
The default alignment for the type v
is 4, as a result of the
integer field in the record, but since this field is placed with a
component clause, it is permissible, as shown, to override the default
alignment of the record to a smaller value.