5b91ae87410bdff7c031dbae5b8feb6a52b0c7ec
[lttng-modules.git] / lib / align.h
1 #ifndef _LTTNG_ALIGN_H
2 #define _LTTNG_ALIGN_H
3
4 /*
5 * lib/align.h
6 *
7 * Copyright (C) 2010-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; only
12 * version 2.1 of the License.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
24 #ifdef __KERNEL__
25
26 #include <linux/types.h>
27 #include <lib/bug.h>
28
29 #define ALIGN_FLOOR(x, a) __ALIGN_FLOOR_MASK(x, (typeof(x)) (a) - 1)
30 #define __ALIGN_FLOOR_MASK(x, mask) ((x) & ~(mask))
31 #define PTR_ALIGN_FLOOR(p, a) \
32 ((typeof(p)) ALIGN_FLOOR((unsigned long) (p), a))
33
34 /*
35 * Align pointer on natural object alignment.
36 */
37 #define object_align(obj) PTR_ALIGN(obj, __alignof__(*(obj)))
38 #define object_align_floor(obj) PTR_ALIGN_FLOOR(obj, __alignof__(*(obj)))
39
40 /**
41 * offset_align - Calculate the offset needed to align an object on its natural
42 * alignment towards higher addresses.
43 * @align_drift: object offset from an "alignment"-aligned address.
44 * @alignment: natural object alignment. Must be non-zero, power of 2.
45 *
46 * Returns the offset that must be added to align towards higher
47 * addresses.
48 */
49 #define offset_align(align_drift, alignment) \
50 ({ \
51 BUILD_RUNTIME_BUG_ON((alignment) == 0 \
52 || ((alignment) & ((alignment) - 1))); \
53 (((alignment) - (align_drift)) & ((alignment) - 1)); \
54 })
55
56 /**
57 * offset_align_floor - Calculate the offset needed to align an object
58 * on its natural alignment towards lower addresses.
59 * @align_drift: object offset from an "alignment"-aligned address.
60 * @alignment: natural object alignment. Must be non-zero, power of 2.
61 *
62 * Returns the offset that must be substracted to align towards lower addresses.
63 */
64 #define offset_align_floor(align_drift, alignment) \
65 ({ \
66 BUILD_RUNTIME_BUG_ON((alignment) == 0 \
67 || ((alignment) & ((alignment) - 1))); \
68 (((align_drift) - (alignment)) & ((alignment) - 1)); \
69 })
70
71 #endif /* __KERNEL__ */
72
73 #endif
This page took 0.033534 seconds and 3 git commands to generate.