2 * SPDX-License-Identifier: MIT
4 * Copyright 2010 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 #ifndef _BABELTRACE_ALIGN_H
8 #define _BABELTRACE_ALIGN_H
10 #include "compat/compiler.h"
11 #include "compat/limits.h"
13 #define BT_ALIGN(x, a) __BT_ALIGN_MASK(x, (__typeof__(x))(a) - 1)
14 #define __BT_ALIGN_MASK(x, mask) (((x) + (mask)) & ~(mask))
15 #define BT_PTR_ALIGN(p, a) ((__typeof__(p)) BT_ALIGN((unsigned long) (p), a))
16 #define BT_ALIGN_FLOOR(x, a) __BT_ALIGN_FLOOR_MASK(x, (__typeof__(x)) (a) - 1)
17 #define __BT_ALIGN_FLOOR_MASK(x, mask) ((x) & ~(mask))
18 #define BT_PTR_ALIGN_FLOOR(p, a) \
19 ((__typeof__(p)) BT_ALIGN_FLOOR((unsigned long) (p), a))
20 #define BT_IS_ALIGNED(x, a) (((x) & ((__typeof__(x)) (a) - 1)) == 0)
23 * Align pointer on natural object alignment.
25 #define bt_object_align(obj) BT_PTR_ALIGN(obj, __alignof__(*(obj)))
26 #define bt_object_align_floor(obj) BT_PTR_ALIGN_FLOOR(obj, __alignof__(*(obj)))
29 * bt_offset_align - Calculate the offset needed to align an object on its
30 * natural alignment towards higher addresses.
31 * @align_drift: object offset from an "alignment"-aligned address.
32 * @alignment: natural object alignment. Must be non-zero, power of 2.
34 * Returns the offset that must be added to align towards higher
37 #define offset_align(align_drift, alignment) \
39 MAYBE_BUILD_BUG_ON((alignment) == 0 \
40 || ((alignment) & ((alignment) - 1))); \
41 (((alignment) - (align_drift)) & ((alignment) - 1)); \
45 * bt_offset_align_floor - Calculate the offset needed to align an object
46 * on its natural alignment towards lower addresses.
47 * @align_drift: object offset from an "alignment"-aligned address.
48 * @alignment: natural object alignment. Must be non-zero, power of 2.
50 * Returns the offset that must be substracted to align towards lower addresses.
52 #define bt_offset_align_floor(align_drift, alignment) \
54 MAYBE_BUILD_BUG_ON((alignment) == 0 \
55 || ((alignment) & ((alignment) - 1))); \
56 (((align_drift) - (alignment)) & ((alignment) - 1)); \
59 #endif /* _BABELTRACE_ALIGN_H */
This page took 0.058225 seconds and 5 git commands to generate.