Re-organize sources
[babeltrace.git] / src / common / align.h
1 #ifndef _BABELTRACE_ALIGN_H
2 #define _BABELTRACE_ALIGN_H
3
4 /*
5 * Copyright 2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
6 *
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:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
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.
24 */
25
26 #include "compat/compiler.h"
27 #include "compat/limits.h"
28
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))); \
72 (((align_drift) - (alignment)) & ((alignment) - 1)); \
73 })
74
75 #endif /* _BABELTRACE_ALIGN_H */
This page took 0.030693 seconds and 4 git commands to generate.