Move to kernel style SPDX license identifiers
[lttng-ust.git] / include / lttng / align.h
1 /*
2 * SPDX-License-Identifier: MIT
3 *
4 * Copyright (C) 2010-2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 */
6
7 #ifndef _UST_ALIGN_H
8 #define _UST_ALIGN_H
9
10 #include <lttng/bug.h>
11 #include <unistd.h>
12 #include <limits.h>
13
14 #ifdef __FreeBSD__
15 #include <machine/param.h>
16 #endif
17
18 #ifdef _SC_PAGE_SIZE
19 #define LTTNG_UST_PAGE_SIZE sysconf(_SC_PAGE_SIZE)
20 #elif defined(PAGE_SIZE)
21 #define LTTNG_UST_PAGE_SIZE PAGE_SIZE
22 #else
23 #error "Please add page size detection for your OS."
24 #endif
25
26 #define LTTNG_UST_PAGE_MASK (~(LTTNG_UST_PAGE_SIZE - 1))
27
28 #define __LTTNG_UST_ALIGN_MASK(v, mask) (((v) + (mask)) & ~(mask))
29 #define LTTNG_UST_ALIGN(v, align) __LTTNG_UST_ALIGN_MASK(v, (__typeof__(v)) (align) - 1)
30 #define LTTNG_UST_PAGE_ALIGN(addr) LTTNG_UST_ALIGN(addr, LTTNG_UST_PAGE_SIZE)
31
32 /**
33 * lttng_ust_offset_align - Calculate the offset needed to align an object on
34 * its natural alignment towards higher addresses.
35 * @align_drift: object offset from an "alignment"-aligned address.
36 * @alignment: natural object alignment. Must be non-zero, power of 2.
37 *
38 * Returns the offset that must be added to align towards higher
39 * addresses.
40 */
41 #define lttng_ust_offset_align(align_drift, alignment) \
42 ({ \
43 LTTNG_BUILD_RUNTIME_BUG_ON((alignment) == 0 \
44 || ((alignment) & ((alignment) - 1))); \
45 (((alignment) - (align_drift)) & ((alignment) - 1)); \
46 })
47
48 /**
49 * lttng_ust_offset_align_floor - Calculate the offset needed to align an
50 * object on its natural alignment towards lower addresses.
51 * @align_drift: object offset from an "alignment"-aligned address.
52 * @alignment: natural object alignment. Must be non-zero, power of 2.
53 *
54 * Returns the offset that must be substracted to align towards lower addresses.
55 */
56 #define lttng_ust_offset_align_floor(align_drift, alignment) \
57 ({ \
58 LTTNG_BUILD_RUNTIME_BUG_ON((alignment) == 0 \
59 || ((alignment) & ((alignment) - 1))); \
60 (((align_drift) - (alignment)) & ((alignment) - 1)); \
61 })
62
63 /*
64 * Non-namespaced defines for backwards compatibility,
65 * introduced in 2.13, should be removed in the future.
66 */
67
68 /* Cygwin limits.h defines its own PAGE_SIZE */
69 #ifndef PAGE_SIZE
70 #define PAGE_SIZE LTTNG_UST_PAGE_SIZE
71 #endif
72
73 /* FreeBSD and macOS defines their own PAGE_MASK. */
74 #ifndef PAGE_MASK
75 #define PAGE_MASK LTTNG_UST_PAGE_MASK
76 #endif
77
78 /* FreeBSD machine/param.h defines its own ALIGN */
79 #ifndef ALIGN
80 #define ALIGN LTTNG_UST_ALIGN
81 #endif
82
83 #ifndef PAGE_ALIGN
84 #define PAGE_ALIGN LTTNG_UST_PAGE_ALIGN
85 #endif
86
87 #ifndef offset_align
88 #define offset_align lttng_ust_offset_align
89 #endif
90
91 #ifndef offset_align_floor
92 #define offset_align_floor lttng_ust_offset_align_floor
93 #endif
94
95 #endif /* _UST_ALIGN_H */
This page took 0.030462 seconds and 4 git commands to generate.