Commit | Line | Data |
---|---|---|
d79865b9 MD |
1 | #ifndef _BABELTRACE_ALIGN_H |
2 | #define _BABELTRACE_ALIGN_H | |
6dc2ca62 | 3 | |
de0ba614 | 4 | /* |
d79865b9 | 5 | * BabelTrace align.h - alignment header |
de0ba614 | 6 | * |
ccd7e1c8 | 7 | * Copyright 2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
de0ba614 | 8 | * |
ccd7e1c8 MD |
9 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
10 | * of this software and associated documentation files (the "Software"), to deal | |
11 | * in the Software without restriction, including without limitation the rights | |
12 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
13 | * copies of the Software, and to permit persons to whom the Software is | |
14 | * furnished to do so, subject to the following conditions: | |
de0ba614 | 15 | * |
ccd7e1c8 MD |
16 | * The above copyright notice and this permission notice shall be included in |
17 | * all copies or substantial portions of the Software. | |
de0ba614 MD |
18 | */ |
19 | ||
4c8bfb7e | 20 | #include <babeltrace/compiler.h> |
c4f467da MD |
21 | #include <unistd.h> |
22 | #include <limits.h> | |
23 | ||
24 | #ifndef PAGE_SIZE /* Cygwin limits.h defines its own PAGE_SIZE */ | |
25 | #define PAGE_SIZE sysconf(_SC_PAGE_SIZE) | |
26 | #endif | |
6dc2ca62 MD |
27 | |
28 | #define ALIGN(x, a) __ALIGN_MASK(x, (typeof(x))(a) - 1) | |
29 | #define __ALIGN_MASK(x, mask) (((x) + (mask)) & ~(mask)) | |
30 | #define PTR_ALIGN(p, a) ((typeof(p)) ALIGN((unsigned long) (p), a)) | |
31 | #define ALIGN_FLOOR(x, a) __ALIGN_FLOOR_MASK(x, (typeof(x)) (a) - 1) | |
32 | #define __ALIGN_FLOOR_MASK(x, mask) ((x) & ~(mask)) | |
33 | #define PTR_ALIGN_FLOOR(p, a) \ | |
34 | ((typeof(p)) ALIGN_FLOOR((unsigned long) (p), a)) | |
35 | #define IS_ALIGNED(x, a) (((x) & ((typeof(x)) (a) - 1)) == 0) | |
36 | ||
37 | /* | |
38 | * Align pointer on natural object alignment. | |
39 | */ | |
40 | #define object_align(obj) PTR_ALIGN(obj, __alignof__(*(obj))) | |
41 | #define object_align_floor(obj) PTR_ALIGN_FLOOR(obj, __alignof__(*(obj))) | |
42 | ||
43 | /** | |
44 | * offset_align - Calculate the offset needed to align an object on its natural | |
45 | * alignment towards higher addresses. | |
46 | * @align_drift: object offset from an "alignment"-aligned address. | |
47 | * @alignment: natural object alignment. Must be non-zero, power of 2. | |
48 | * | |
49 | * Returns the offset that must be added to align towards higher | |
50 | * addresses. | |
51 | */ | |
52 | #define offset_align(align_drift, alignment) \ | |
53 | ({ \ | |
54 | MAYBE_BUILD_BUG_ON((alignment) == 0 \ | |
55 | || ((alignment) & ((alignment) - 1))); \ | |
56 | (((alignment) - (align_drift)) & ((alignment) - 1)); \ | |
57 | }) | |
58 | ||
59 | /** | |
60 | * offset_align_floor - Calculate the offset needed to align an object | |
61 | * on its natural alignment towards lower addresses. | |
62 | * @align_drift: object offset from an "alignment"-aligned address. | |
63 | * @alignment: natural object alignment. Must be non-zero, power of 2. | |
64 | * | |
65 | * Returns the offset that must be substracted to align towards lower addresses. | |
66 | */ | |
67 | #define offset_align_floor(align_drift, alignment) \ | |
68 | ({ \ | |
69 | MAYBE_BUILD_BUG_ON((alignment) == 0 \ | |
70 | || ((alignment) & ((alignment) - 1))); \ | |
71 | (((align_drift) - (alignment)) & ((alignment) - 1); \ | |
72 | }) | |
73 | ||
d79865b9 | 74 | #endif /* _BABELTRACE_ALIGN_H */ |