-
Notifications
You must be signed in to change notification settings - Fork 161
/
Copy pathUtils.h
33 lines (27 loc) · 1.03 KB
/
Utils.h
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
#ifndef UTILS_H
#define UTILS_H
class Utils {
public:
static const std::size_t CalculatePadding(const std::size_t baseAddress, const std::size_t alignment) {
const std::size_t multiplier = (baseAddress / alignment) + 1;
const std::size_t alignedAddress = multiplier * alignment;
const std::size_t padding = alignedAddress - baseAddress;
return padding;
}
static const std::size_t CalculatePaddingWithHeader(const std::size_t baseAddress, const std::size_t alignment, const std::size_t headerSize) {
std::size_t padding = CalculatePadding(baseAddress, alignment);
std::size_t neededSpace = headerSize;
if (padding < neededSpace){
// Header does not fit - Calculate next aligned address that header fits
neededSpace -= padding;
// How many alignments I need to fit the header
if(neededSpace % alignment > 0){
padding += alignment * (1+(neededSpace / alignment));
}else {
padding += alignment * (neededSpace / alignment);
}
}
return padding;
}
};
#endif /* UTILS_H */