A3 Alignment

The alignment of data on certain memory addresses is necessary to make the best possible use of processor caches and to boost performance. Some architectures mandate that data types of a particular length have a specific alignment. Even architectures that are able to handle random alignment can read and write certain alignments faster than when access is unaligned. Alignment is typically on byte addresses that are divisible without remainder by the byte length of the data type. In certain situations, the required alignment may be a little larger. Relevant information is given in the architecture documentation of the processor in question. The alignment of a data type on its own length is referred to as natural alignment.

It may be necessary to access non-aligned data types at some points in the kernel. The various architectures must therefore define two macros for this purpose (in <asm-arofr/unaligned.h>):

□ get_unaligned(ptr) de-references a pointer at an unaligned memory location.

□ put_unaligned(val, ptr) writes val to a memory location specified by ptr that is unaligned (and not suitable for direct access).

These support access by, for example, copying the value to another memory location and accessing it there. When the GCC organizes memory for structs or unions, it automatically selects the appropriate alignment so that the programmer is not required to do this.

Continue reading here: A4 Memory Pages

Was this article helpful?

0 0