| 1 | /* SPDX-License-Identifier: (GPL-2.0-only or LGPL-2.1-only) |
| 2 | * |
| 3 | * lttng/align.h |
| 4 | * |
| 5 | * Copyright (C) 2010-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 6 | */ |
| 7 | |
| 8 | #ifndef _LTTNG_ALIGN_H |
| 9 | #define _LTTNG_ALIGN_H |
| 10 | |
| 11 | #ifdef __KERNEL__ |
| 12 | |
| 13 | #include <linux/types.h> |
| 14 | #include <lttng/bug.h> |
| 15 | |
| 16 | #define ALIGN_FLOOR(x, a) __ALIGN_FLOOR_MASK(x, (typeof(x)) (a) - 1) |
| 17 | #define __ALIGN_FLOOR_MASK(x, mask) ((x) & ~(mask)) |
| 18 | #define PTR_ALIGN_FLOOR(p, a) \ |
| 19 | ((typeof(p)) ALIGN_FLOOR((unsigned long) (p), a)) |
| 20 | |
| 21 | /* |
| 22 | * Align pointer on natural object alignment. |
| 23 | */ |
| 24 | #define object_align(obj) PTR_ALIGN(obj, __alignof__(*(obj))) |
| 25 | #define object_align_floor(obj) PTR_ALIGN_FLOOR(obj, __alignof__(*(obj))) |
| 26 | |
| 27 | /** |
| 28 | * offset_align - Calculate the offset needed to align an object on its natural |
| 29 | * alignment towards higher addresses. |
| 30 | * @align_drift: object offset from an "alignment"-aligned address. |
| 31 | * @alignment: natural object alignment. Must be non-zero, power of 2. |
| 32 | * |
| 33 | * Returns the offset that must be added to align towards higher |
| 34 | * addresses. |
| 35 | */ |
| 36 | #define offset_align(align_drift, alignment) \ |
| 37 | ({ \ |
| 38 | BUILD_RUNTIME_BUG_ON((alignment) == 0 \ |
| 39 | || ((alignment) & ((alignment) - 1))); \ |
| 40 | (((alignment) - (align_drift)) & ((alignment) - 1)); \ |
| 41 | }) |
| 42 | |
| 43 | /** |
| 44 | * offset_align_floor - Calculate the offset needed to align an object |
| 45 | * on its natural alignment towards lower addresses. |
| 46 | * @align_drift: object offset from an "alignment"-aligned address. |
| 47 | * @alignment: natural object alignment. Must be non-zero, power of 2. |
| 48 | * |
| 49 | * Returns the offset that must be substracted to align towards lower addresses. |
| 50 | */ |
| 51 | #define offset_align_floor(align_drift, alignment) \ |
| 52 | ({ \ |
| 53 | BUILD_RUNTIME_BUG_ON((alignment) == 0 \ |
| 54 | || ((alignment) & ((alignment) - 1))); \ |
| 55 | (((align_drift) - (alignment)) & ((alignment) - 1)); \ |
| 56 | }) |
| 57 | |
| 58 | #endif /* __KERNEL__ */ |
| 59 | |
| 60 | #endif |