Padding
* Data Alignment
Data alignment means putting the data at a memory address equal to some multiple of the word size, which increases the system's performance due to the way the CPU handles memory. To align the data, it may be necessary to insert some unused bytes between the end of the last data structure and the start of the next, which is data structure padding.
** memory access granularity
the number of byte sized chunks a processor reads at a time
2,4,8,1,16,32 - byte chunks
** Alignment fundamentals
*** sample task
first read four bytes from address 0 into the :definition: processor's register. Then read four bytes from address 1 into the same register.
**** Single-byte memory access granularity
***** 1
Reads the first four bytes starting at 0.
***** 2
Reads the first four bytes starting at 1. This means that it reads 3 of the bytes already read in the first operation.
**** Double-byte memory access granularity
***** 1
Read the first four bytes starting at 0. Only two accesses need to be done.
***** 2
Read the first four bytes starting at 1. Falls out of the memory access boundary (not a multiple of the word size).
**** ...
* Processor registers
** Sagamore Inn analogy
I have all of these ingredients that are stored in their containers (cache) -- things that I dont immediately need, but need around because I use them frequently. The food in the walk in freezer refers to memory, and the what stored there, I can go and retrieve stuff from it, but I dont need it right there, like I do the stuff in my cache but registers, registers are my hands, grabbing things from the cache and operating upon the.m
* Memory access boundary
Essentially, if the data being accessed is n bytes long and the address is n bytes aligned.
if n-accessed > n-bytes-aligned or n-accessed < n-bytes-aligned then the data is unaligned.
** missalignment
this happens when data being read isn't a multiple of the word size.
e.g. the data starts at address 14 instead of 16, then the computer has to read two or more 4 byte chunks and do some calculation before the requested data has been read ... Two padding bytes are inserted between the two data structures at addresses 14 and 15 to align the next data structure at address 16.
** single byte access is always aligned.
* Word
natural unit of data used by a processor. Fixed size, handled as a unit.
sometimes called alignment
* Structure alignmnet
#+START_SRC c
struct MyData
{
short Data1; // 2 Bytes
short Data2; // 2 Bytes
short Data3; // 2 Bytes
}; // Total 6 Bytesx
// padding is put in place to keep the spaces data in line with word size
word size = 4 byte chunk
struct MixedData
{
char Data1; // 1 Bytes
short Data2; // 2 Bytes
int Data3; // 4 Bytes
char Data4; // 1 Byte
}; // Expected 8 bytes. Total bytes however: sizeof(MixedData) = 12
// padding has been used to kepe the data aligned.
struct MixedData
{
char Data1; // 1 Bytes
short Data2; // 2 Bytes
char padding[1]; // 1 + 2 = 3 Bytes, the processor is reads words in sizes of 4 bytes, so wee need to insert 1 padding byte
int Data3; // 4 Bytes
// read 4 full bytes.
char Data4; // 1 Byte
char padding[3]; // 1 + 3 = 4 Bytes.
}; 1 + 2 + 1 (padding) + 4 + 1 + 3 (padding) = 12
#+END_SRC
Written on September 15, 2017