Move to kernel style SPDX license identifiers
[lttng-ust.git] / include / lttng / align.h
CommitLineData
8c4127c5 1/*
c0c0989a 2 * SPDX-License-Identifier: MIT
a60d70e6 3 *
c0c0989a 4 * Copyright (C) 2010-2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
8c4127c5
MD
5 */
6
c0c0989a
MJ
7#ifndef _UST_ALIGN_H
8#define _UST_ALIGN_H
9
4318ae1b 10#include <lttng/bug.h>
993e8a49 11#include <unistd.h>
8e3484cf 12#include <limits.h>
8c4127c5 13
b72687b8
MJ
14#ifdef __FreeBSD__
15#include <machine/param.h>
8e3484cf
MD
16#endif
17
b72687b8
MJ
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."
d6324154 24#endif
b72687b8
MJ
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)
8c4127c5
MD
31
32/**
b72687b8
MJ
33 * lttng_ust_offset_align - Calculate the offset needed to align an object on
34 * its natural alignment towards higher addresses.
8c4127c5
MD
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 */
b72687b8 41#define lttng_ust_offset_align(align_drift, alignment) \
8c4127c5 42 ({ \
3f12fcef 43 LTTNG_BUILD_RUNTIME_BUG_ON((alignment) == 0 \
8c4127c5
MD
44 || ((alignment) & ((alignment) - 1))); \
45 (((alignment) - (align_drift)) & ((alignment) - 1)); \
46 })
47
48/**
b72687b8
MJ
49 * lttng_ust_offset_align_floor - Calculate the offset needed to align an
50 * object on its natural alignment towards lower addresses.
8c4127c5
MD
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 */
b72687b8 56#define lttng_ust_offset_align_floor(align_drift, alignment) \
8c4127c5 57 ({ \
3f12fcef 58 LTTNG_BUILD_RUNTIME_BUG_ON((alignment) == 0 \
8c4127c5 59 || ((alignment) & ((alignment) - 1))); \
bf76f1ea 60 (((align_drift) - (alignment)) & ((alignment) - 1)); \
8c4127c5
MD
61 })
62
b72687b8
MJ
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
8c4127c5 95#endif /* _UST_ALIGN_H */
This page took 0.033026 seconds and 4 git commands to generate.