Alignment Requirement
A memory address is -byte aligned when is a multiple of (where is a power of 2)
A data is naturally aligned when data’s memory address is a multiple of the data size
The effects of performing an unaligned memory access vary from architecture to architecture:
- On some architectures unaligned accesses may not be supported
- Unaligned accesses may be non-atomic
- Accesses that cross a cache line boundary may cause a performance penalty because multiple lines need to be fetched
- Accesses that cross a page boundary may cause an even bigger performance penalty because multiple pages need to be fetched
- A DRAM typically supports only aligned bursts
Satisfying Alignment Requirements
Alignment is enforced by making sure that every data type is organized and allocated in such a way that every object within the type satisfies its alignment restrictions
- Initial address of the object must be aligned. Starting address for any block generated by a memory allocator must be aligned
- Every field inside the object must be aligned
- Size of the object must be aligned to satisfy alignment for arrays of objects
- Stack frame must be aligned
For example, given the following struct:
The compiler ensures alignment by inserting padding, so that:
- Any pointer
p
of typestruct S*
points to an address that is a multiple of the size of the largest element type - Each field
fi
is aligned, meaning the address offi
is a multiple of the size ofTi
- The total size of
struct S
is a multiple of the size of the largest element type, ensuring proper alignment for arrays ofS
The compiler uses .align
directives in the assembly code to enforce alignment for global data.
References
- Data structure alignment - Wikipedia
- Computer Systems A Programmer’s Perspective, Global Edition (3rd ed). Randal E. Bryant, David R. O’Hallaron
- CS24-Sp18-Introduction-To-Computing-System - YouTube
- ICS-CMU - YouTube
- assembly - Why is memory alignment needed? - Stack Overflow
- Intermation: Data Structure Alignment and Endianness - YouTube
- Dive Into Systems A Gentle Introduction to Computer Systems. Suzanne J. Matthews, Tia Newhall, Kevin C. Webb
- c - CPU and Data alignment - Stack Overflow
- x86 64 - Why do align access and non-align access have same performance? - Stack Overflow
- Unaligned Memory Accesses — The Linux Kernel documentation