| 1 | #ifndef _UST_ALIGN_H |
| 2 | #define _UST_ALIGN_H |
| 3 | |
| 4 | /* |
| 5 | * ust/align.h |
| 6 | * |
| 7 | * (C) Copyright 2010-2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 8 | * |
| 9 | * Dual LGPL v2.1/GPL v2 license. |
| 10 | */ |
| 11 | |
| 12 | #include <ust/bug.h> |
| 13 | |
| 14 | /* |
| 15 | * Align pointer on natural object alignment. |
| 16 | */ |
| 17 | #define object_align(obj) PTR_ALIGN(obj, __alignof__(*(obj))) |
| 18 | #define object_align_floor(obj) PTR_ALIGN_FLOOR(obj, __alignof__(*(obj))) |
| 19 | |
| 20 | /** |
| 21 | * offset_align - Calculate the offset needed to align an object on its natural |
| 22 | * alignment towards higher addresses. |
| 23 | * @align_drift: object offset from an "alignment"-aligned address. |
| 24 | * @alignment: natural object alignment. Must be non-zero, power of 2. |
| 25 | * |
| 26 | * Returns the offset that must be added to align towards higher |
| 27 | * addresses. |
| 28 | */ |
| 29 | #define offset_align(align_drift, alignment) \ |
| 30 | ({ \ |
| 31 | BUILD_RUNTIME_BUG_ON((alignment) == 0 \ |
| 32 | || ((alignment) & ((alignment) - 1))); \ |
| 33 | (((alignment) - (align_drift)) & ((alignment) - 1)); \ |
| 34 | }) |
| 35 | |
| 36 | /** |
| 37 | * offset_align_floor - Calculate the offset needed to align an object |
| 38 | * on its natural alignment towards lower addresses. |
| 39 | * @align_drift: object offset from an "alignment"-aligned address. |
| 40 | * @alignment: natural object alignment. Must be non-zero, power of 2. |
| 41 | * |
| 42 | * Returns the offset that must be substracted to align towards lower addresses. |
| 43 | */ |
| 44 | #define offset_align_floor(align_drift, alignment) \ |
| 45 | ({ \ |
| 46 | BUILD_RUNTIME_BUG_ON((alignment) == 0 \ |
| 47 | || ((alignment) & ((alignment) - 1))); \ |
| 48 | (((align_drift) - (alignment)) & ((alignment) - 1); \ |
| 49 | }) |
| 50 | |
| 51 | #endif /* _UST_ALIGN_H */ |