Commit | Line | Data |
---|---|---|
953192ba | 1 | /* |
ab5be9fa | 2 | * Copyright (C) 2010-2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
953192ba | 3 | * |
ab5be9fa | 4 | * SPDX-License-Identifier: MIT |
953192ba | 5 | * |
953192ba MD |
6 | */ |
7 | ||
ab5be9fa MJ |
8 | #ifndef _LTTNG_ALIGN_H |
9 | #define _LTTNG_ALIGN_H | |
10 | ||
8564ccbd | 11 | #include "bug.h" |
953192ba MD |
12 | #include <unistd.h> |
13 | #include <limits.h> | |
14 | ||
d575a7f8 | 15 | #ifndef PAGE_SIZE /* Cygwin limits.h defines its own PAGE_SIZE. */ |
953192ba MD |
16 | #define PAGE_SIZE sysconf(_SC_PAGE_SIZE) |
17 | #endif | |
18 | ||
d575a7f8 | 19 | |
1cbd136b MJ |
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)) | |
d575a7f8 | 25 | |
1cbd136b MJ |
26 | #define lttng_align_ceil(v, align) \ |
27 | __lttng_align_ceil_mask(v, (__typeof__(v)) (align) - 1) | |
d4ad24a3 | 28 | |
1cbd136b MJ |
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)) | |
d4ad24a3 | 34 | |
1cbd136b MJ |
35 | #define lttng_align_floor(v, align) \ |
36 | __lttng_align_floor_mask(v, (__typeof__(v)) (align) - 1) | |
953192ba MD |
37 | |
38 | /** | |
1cbd136b | 39 | * lttng_offset_align - Calculate the offset needed to align an object on its natural |
953192ba MD |
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 | */ | |
1cbd136b | 47 | #define lttng_offset_align(align_drift, alignment) \ |
953192ba MD |
48 | ({ \ |
49 | LTTNG_BUILD_RUNTIME_BUG_ON((alignment) == 0 \ | |
50 | || ((alignment) & ((alignment) - 1))); \ | |
51 | (((alignment) - (align_drift)) & ((alignment) - 1)); \ | |
52 | }) | |
53 | ||
54 | /** | |
1cbd136b | 55 | * lttng_offset_align_floor - Calculate the offset needed to align an object |
953192ba MD |
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 | */ | |
1cbd136b | 62 | #define lttng_offset_align_floor(align_drift, alignment) \ |
953192ba MD |
63 | ({ \ |
64 | LTTNG_BUILD_RUNTIME_BUG_ON((alignment) == 0 \ | |
65 | || ((alignment) & ((alignment) - 1))); \ | |
d07ceecd | 66 | (((align_drift) - (alignment)) & ((alignment) - 1)); \ |
953192ba MD |
67 | }) |
68 | ||
69 | #endif /* _LTTNG_ALIGN_H */ |