-
Notifications
You must be signed in to change notification settings - Fork 0
/
layout.c
34 lines (25 loc) · 889 Bytes
/
layout.c
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
#include "layout.h"
Layout layout_empty(void) {
return (Layout){.size = 0, .alignment = 1};
}
size_t layout_add(Layout* layout, Layout member_layout) {
size_t member_size = member_layout.size;
size_t member_alignment = member_layout.alignment != 0 ? member_layout.alignment : 1;
size_t member_offset = (layout->size / member_alignment) * member_alignment;
while (member_offset < layout->size) {
member_offset += member_alignment;
}
layout->size = member_offset + member_size;
if (member_alignment > layout->alignment) {
layout->alignment = member_alignment;
}
return member_offset;
}
size_t layout_pad(Layout* layout) {
size_t alignment = layout->alignment != 0 ? layout->alignment : 1;
size_t new_size = (layout->size / alignment) * alignment;
while (new_size < layout->size) {
new_size += alignment;
}
return layout->size = new_size;
}