Commit | Line | Data |
---|---|---|
d79865b9 MD |
1 | #ifndef _BABELTRACE_ALIGN_H |
2 | #define _BABELTRACE_ALIGN_H | |
6dc2ca62 | 3 | |
de0ba614 | 4 | /* |
ccd7e1c8 | 5 | * Copyright 2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
de0ba614 | 6 | * |
ccd7e1c8 MD |
7 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
8 | * of this software and associated documentation files (the "Software"), to deal | |
9 | * in the Software without restriction, including without limitation the rights | |
10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
11 | * copies of the Software, and to permit persons to whom the Software is | |
12 | * furnished to do so, subject to the following conditions: | |
de0ba614 | 13 | * |
ccd7e1c8 MD |
14 | * The above copyright notice and this permission notice shall be included in |
15 | * all copies or substantial portions of the Software. | |
c462e188 MD |
16 | * |
17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
20 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
22 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
23 | * SOFTWARE. | |
de0ba614 MD |
24 | */ |
25 | ||
3d9990ac | 26 | #include <babeltrace/compiler-internal.h> |
3d9990ac | 27 | #include <babeltrace/compat/limits-internal.h> |
c4f467da | 28 | |
6dc2ca62 MD |
29 | #define ALIGN(x, a) __ALIGN_MASK(x, (typeof(x))(a) - 1) |
30 | #define __ALIGN_MASK(x, mask) (((x) + (mask)) & ~(mask)) | |
31 | #define PTR_ALIGN(p, a) ((typeof(p)) ALIGN((unsigned long) (p), a)) | |
32 | #define ALIGN_FLOOR(x, a) __ALIGN_FLOOR_MASK(x, (typeof(x)) (a) - 1) | |
33 | #define __ALIGN_FLOOR_MASK(x, mask) ((x) & ~(mask)) | |
34 | #define PTR_ALIGN_FLOOR(p, a) \ | |
35 | ((typeof(p)) ALIGN_FLOOR((unsigned long) (p), a)) | |
36 | #define IS_ALIGNED(x, a) (((x) & ((typeof(x)) (a) - 1)) == 0) | |
37 | ||
38 | /* | |
39 | * Align pointer on natural object alignment. | |
40 | */ | |
41 | #define object_align(obj) PTR_ALIGN(obj, __alignof__(*(obj))) | |
42 | #define object_align_floor(obj) PTR_ALIGN_FLOOR(obj, __alignof__(*(obj))) | |
43 | ||
44 | /** | |
45 | * offset_align - Calculate the offset needed to align an object on its natural | |
46 | * alignment towards higher addresses. | |
47 | * @align_drift: object offset from an "alignment"-aligned address. | |
48 | * @alignment: natural object alignment. Must be non-zero, power of 2. | |
49 | * | |
50 | * Returns the offset that must be added to align towards higher | |
51 | * addresses. | |
52 | */ | |
53 | #define offset_align(align_drift, alignment) \ | |
54 | ({ \ | |
55 | MAYBE_BUILD_BUG_ON((alignment) == 0 \ | |
56 | || ((alignment) & ((alignment) - 1))); \ | |
57 | (((alignment) - (align_drift)) & ((alignment) - 1)); \ | |
58 | }) | |
59 | ||
60 | /** | |
61 | * offset_align_floor - Calculate the offset needed to align an object | |
62 | * on its natural alignment towards lower addresses. | |
63 | * @align_drift: object offset from an "alignment"-aligned address. | |
64 | * @alignment: natural object alignment. Must be non-zero, power of 2. | |
65 | * | |
66 | * Returns the offset that must be substracted to align towards lower addresses. | |
67 | */ | |
68 | #define offset_align_floor(align_drift, alignment) \ | |
69 | ({ \ | |
70 | MAYBE_BUILD_BUG_ON((alignment) == 0 \ | |
71 | || ((alignment) & ((alignment) - 1))); \ | |
a12af8c1 | 72 | (((align_drift) - (alignment)) & ((alignment) - 1)); \ |
6dc2ca62 MD |
73 | }) |
74 | ||
d79865b9 | 75 | #endif /* _BABELTRACE_ALIGN_H */ |