| 1 | #ifndef _UST_ALIGN_H |
| 2 | #define _UST_ALIGN_H |
| 3 | |
| 4 | /* |
| 5 | * lttng/align.h |
| 6 | * |
| 7 | * (C) Copyright 2010-2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 8 | * |
| 9 | * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED |
| 10 | * OR IMPLIED. ANY USE IS AT YOUR OWN RISK. |
| 11 | * |
| 12 | * Permission is hereby granted to use or copy this program |
| 13 | * for any purpose, provided the above notices are retained on all copies. |
| 14 | * Permission to modify the code and to distribute modified code is granted, |
| 15 | * provided the above notices are retained, and a notice that the code was |
| 16 | * modified is included with the above copyright notice. |
| 17 | */ |
| 18 | |
| 19 | #include <lttng/bug.h> |
| 20 | #include <unistd.h> |
| 21 | |
| 22 | #define PAGE_SIZE sysconf(_SC_PAGE_SIZE) |
| 23 | #define PAGE_MASK (~(PAGE_SIZE - 1)) |
| 24 | #define __ALIGN_MASK(v, mask) (((v) + (mask)) & ~(mask)) |
| 25 | #define ALIGN(v, align) __ALIGN_MASK(v, (typeof(v)) (align) - 1) |
| 26 | #define PAGE_ALIGN(addr) ALIGN(addr, PAGE_SIZE) |
| 27 | |
| 28 | /** |
| 29 | * offset_align - Calculate the offset needed to align an object on its natural |
| 30 | * alignment towards higher addresses. |
| 31 | * @align_drift: object offset from an "alignment"-aligned address. |
| 32 | * @alignment: natural object alignment. Must be non-zero, power of 2. |
| 33 | * |
| 34 | * Returns the offset that must be added to align towards higher |
| 35 | * addresses. |
| 36 | */ |
| 37 | #define offset_align(align_drift, alignment) \ |
| 38 | ({ \ |
| 39 | LTTNG_BUILD_RUNTIME_BUG_ON((alignment) == 0 \ |
| 40 | || ((alignment) & ((alignment) - 1))); \ |
| 41 | (((alignment) - (align_drift)) & ((alignment) - 1)); \ |
| 42 | }) |
| 43 | |
| 44 | /** |
| 45 | * offset_align_floor - Calculate the offset needed to align an object |
| 46 | * on its natural alignment towards lower addresses. |
| 47 | * @align_drift: object offset from an "alignment"-aligned address. |
| 48 | * @alignment: natural object alignment. Must be non-zero, power of 2. |
| 49 | * |
| 50 | * Returns the offset that must be substracted to align towards lower addresses. |
| 51 | */ |
| 52 | #define offset_align_floor(align_drift, alignment) \ |
| 53 | ({ \ |
| 54 | LTTNG_BUILD_RUNTIME_BUG_ON((alignment) == 0 \ |
| 55 | || ((alignment) & ((alignment) - 1))); \ |
| 56 | (((align_drift) - (alignment)) & ((alignment) - 1); \ |
| 57 | }) |
| 58 | |
| 59 | #endif /* _UST_ALIGN_H */ |