summaryrefslogtreecommitdiff
path: root/notes/struct_padding.txt
blob: 16d4e90247aed061a704c849ce21c194cc772b0c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36

struct padding
--------------

We want both of the following:
* binary compatibility when adding fields (as long as the struct is allocated in the new version)
* as little alignment padding as possible.

The algorithm is (for each field in the order they appear):
 1. Find the smallest hole that fits the field.
 2. If found, allocate the field there,
    and continue with the next field.
    If there are multiple holes of the same size,
    use the first one.
 3. Otherwise, allocate the field last,
    and continue with the next field.

assuming these sizes in bits:
(bool=1?)
char=8
short=16
int=32
long=64

{ char, int }
= char, pad24, int

{ char, int, char }
= char, char, pad16, int

{ int, long, char }
= int, char, pad24, long

{ long, char, int, short }
= long, char, pad8, short, int