Data alignment

Data alignment is very import for fast execution of code. Local and global variables are aligned by default with some exceptions. The ExStorage, and therefore the Storage module, return addresses that will properly align variables.

RECORD types

The fields of record types are by default packed together with no space in between adjacent fields. Unless you properly order your fields such that they naturally fall of the proper alignment boundaries some of your fields will not be aligned and accesses to these fields will be slow. Warnings will be generated, unless suppressed, informing you when variables and/or types are not aligned.

The compiler can be told to align all fields in records. In this case the compiler will insert space in between fields if necessary to align the data fields. The compiler remembers the largest alignment value necessary and makes sure the total size of the record is an even multiple of this size. This makes sure that an array of these records will have each record be properly aligned and therefore all fields within the record will be aligned. To conserve memory it is generally best for you to arrange your record fields to fall naturally on proper alignment boundaries.

For records that are written to disk you generally should not have the compiler align the data fields. If these records are not naturally aligned and they are used in performance critical code, then you will probably want to do one of the following.

See the topic Optimal disk and memory records for further information.

ARRAY types

The elements of an array are always adjacent without any space in between each element. Therefore an array can only be aligned if the array element is aligned. This really only applies when an array type has a record type as it's element.

Floating point types

The double precision floating point type is a special animal. For Modula-2 this is the LONGREAL type. For Ada95 this is the long_float type. It wants 8 byte alignment. No other type in the language needs such an alignment. As such special consideration must be given to align variables of this type. Let the other instances be as they are. Select eight byte alignment in your compiler options to have local and global variables of this type be aligned properly.

Aligning memory allocations

To align a memory allocation on any value other than the default, use the AllocateAligned procedure to get memory aligned on the boundary you require. For Modula-2 this procedure comes from the ExStorage module. For Ada95 this procedure comes from the SbsStorage package.