Cleanup: namespace 'align' macros
[lttng-tools.git] / src / common / align.h
1 /*
2 * Copyright (C) 2010-2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
3 *
4 * SPDX-License-Identifier: MIT
5 *
6 */
7
8 #ifndef _LTTNG_ALIGN_H
9 #define _LTTNG_ALIGN_H
10
11 #include "bug.h"
12 #include <unistd.h>
13 #include <limits.h>
14
15 #ifndef PAGE_SIZE /* Cygwin limits.h defines its own PAGE_SIZE. */
16 #define PAGE_SIZE sysconf(_SC_PAGE_SIZE)
17 #endif
18
19
20 /*
21 * Align value to the next multiple of align. Returns val if it already is a
22 * multiple of align. Align must be a power of two.
23 */
24 #define __lttng_align_ceil_mask(v, mask) (((v) + (mask)) & ~(mask))
25
26 #define lttng_align_ceil(v, align) \
27 __lttng_align_ceil_mask(v, (__typeof__(v)) (align) - 1)
28
29 /*
30 * Align value to the previous multiple of align. Returns val if it already is a
31 * multiple of align. Align must be a power of two.
32 */
33 #define __lttng_align_floor_mask(v, mask) ((v) & ~(mask))
34
35 #define lttng_align_floor(v, align) \
36 __lttng_align_floor_mask(v, (__typeof__(v)) (align) - 1)
37
38 /**
39 * lttng_offset_align - Calculate the offset needed to align an object on its natural
40 * alignment towards higher addresses.
41 * @align_drift: object offset from an "alignment"-aligned address.
42 * @alignment: natural object alignment. Must be non-zero, power of 2.
43 *
44 * Returns the offset that must be added to align towards higher
45 * addresses.
46 */
47 #define lttng_offset_align(align_drift, alignment) \
48 ({ \
49 LTTNG_BUILD_RUNTIME_BUG_ON((alignment) == 0 \
50 || ((alignment) & ((alignment) - 1))); \
51 (((alignment) - (align_drift)) & ((alignment) - 1)); \
52 })
53
54 /**
55 * lttng_offset_align_floor - Calculate the offset needed to align an object
56 * on its natural alignment towards lower addresses.
57 * @align_drift: object offset from an "alignment"-aligned address.
58 * @alignment: natural object alignment. Must be non-zero, power of 2.
59 *
60 * Returns the offset that must be substracted to align towards lower addresses.
61 */
62 #define lttng_offset_align_floor(align_drift, alignment) \
63 ({ \
64 LTTNG_BUILD_RUNTIME_BUG_ON((alignment) == 0 \
65 || ((alignment) & ((alignment) - 1))); \
66 (((align_drift) - (alignment)) & ((alignment) - 1)); \
67 })
68
69 #endif /* _LTTNG_ALIGN_H */
This page took 0.031046 seconds and 4 git commands to generate.