Commit | Line | Data |
---|---|---|
6dc2ca62 MD |
1 | #ifndef _CTF_ALIGN_H |
2 | #define _CTF_ALIGN_H | |
3 | ||
4 | #include <ctf/compiler.h> | |
5 | ||
6 | #define ALIGN(x, a) __ALIGN_MASK(x, (typeof(x))(a) - 1) | |
7 | #define __ALIGN_MASK(x, mask) (((x) + (mask)) & ~(mask)) | |
8 | #define PTR_ALIGN(p, a) ((typeof(p)) ALIGN((unsigned long) (p), a)) | |
9 | #define ALIGN_FLOOR(x, a) __ALIGN_FLOOR_MASK(x, (typeof(x)) (a) - 1) | |
10 | #define __ALIGN_FLOOR_MASK(x, mask) ((x) & ~(mask)) | |
11 | #define PTR_ALIGN_FLOOR(p, a) \ | |
12 | ((typeof(p)) ALIGN_FLOOR((unsigned long) (p), a)) | |
13 | #define IS_ALIGNED(x, a) (((x) & ((typeof(x)) (a) - 1)) == 0) | |
14 | ||
15 | /* | |
16 | * Align pointer on natural object alignment. | |
17 | */ | |
18 | #define object_align(obj) PTR_ALIGN(obj, __alignof__(*(obj))) | |
19 | #define object_align_floor(obj) PTR_ALIGN_FLOOR(obj, __alignof__(*(obj))) | |
20 | ||
21 | /** | |
22 | * offset_align - Calculate the offset needed to align an object on its natural | |
23 | * alignment towards higher addresses. | |
24 | * @align_drift: object offset from an "alignment"-aligned address. | |
25 | * @alignment: natural object alignment. Must be non-zero, power of 2. | |
26 | * | |
27 | * Returns the offset that must be added to align towards higher | |
28 | * addresses. | |
29 | */ | |
30 | #define offset_align(align_drift, alignment) \ | |
31 | ({ \ | |
32 | MAYBE_BUILD_BUG_ON((alignment) == 0 \ | |
33 | || ((alignment) & ((alignment) - 1))); \ | |
34 | (((alignment) - (align_drift)) & ((alignment) - 1)); \ | |
35 | }) | |
36 | ||
37 | /** | |
38 | * offset_align_floor - Calculate the offset needed to align an object | |
39 | * on its natural alignment towards lower addresses. | |
40 | * @align_drift: object offset from an "alignment"-aligned address. | |
41 | * @alignment: natural object alignment. Must be non-zero, power of 2. | |
42 | * | |
43 | * Returns the offset that must be substracted to align towards lower addresses. | |
44 | */ | |
45 | #define offset_align_floor(align_drift, alignment) \ | |
46 | ({ \ | |
47 | MAYBE_BUILD_BUG_ON((alignment) == 0 \ | |
48 | || ((alignment) & ((alignment) - 1))); \ | |
49 | (((align_drift) - (alignment)) & ((alignment) - 1); \ | |
50 | }) | |
51 | ||
52 | #endif /* _CTF_ALIGN_H */ |