c5589c43aca6253a42470cb6b42214d5763ac943
[babeltrace.git] / include / babeltrace / align.h
1 #ifndef _BABELTRACE_ALIGN_H
2 #define _BABELTRACE_ALIGN_H
3
4 /*
5 * BabelTrace align.h - alignment header
6 *
7 * Copyright 2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a copy
10 * of this software and associated documentation files (the "Software"), to deal
11 * in the Software without restriction, including without limitation the rights
12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 * copies of the Software, and to permit persons to whom the Software is
14 * furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
18 */
19
20 #include <babeltrace/compiler.h>
21 #include <unistd.h>
22 #include <limits.h>
23
24 #ifndef PAGE_SIZE /* Cygwin limits.h defines its own PAGE_SIZE */
25 #define PAGE_SIZE sysconf(_SC_PAGE_SIZE)
26 #endif
27
28 #define ALIGN(x, a) __ALIGN_MASK(x, (typeof(x))(a) - 1)
29 #define __ALIGN_MASK(x, mask) (((x) + (mask)) & ~(mask))
30 #define PTR_ALIGN(p, a) ((typeof(p)) ALIGN((unsigned long) (p), a))
31 #define ALIGN_FLOOR(x, a) __ALIGN_FLOOR_MASK(x, (typeof(x)) (a) - 1)
32 #define __ALIGN_FLOOR_MASK(x, mask) ((x) & ~(mask))
33 #define PTR_ALIGN_FLOOR(p, a) \
34 ((typeof(p)) ALIGN_FLOOR((unsigned long) (p), a))
35 #define IS_ALIGNED(x, a) (((x) & ((typeof(x)) (a) - 1)) == 0)
36
37 /*
38 * Align pointer on natural object alignment.
39 */
40 #define object_align(obj) PTR_ALIGN(obj, __alignof__(*(obj)))
41 #define object_align_floor(obj) PTR_ALIGN_FLOOR(obj, __alignof__(*(obj)))
42
43 /**
44 * offset_align - Calculate the offset needed to align an object on its natural
45 * alignment towards higher 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 added to align towards higher
50 * addresses.
51 */
52 #define offset_align(align_drift, alignment) \
53 ({ \
54 MAYBE_BUILD_BUG_ON((alignment) == 0 \
55 || ((alignment) & ((alignment) - 1))); \
56 (((alignment) - (align_drift)) & ((alignment) - 1)); \
57 })
58
59 /**
60 * offset_align_floor - Calculate the offset needed to align an object
61 * on its natural alignment towards lower addresses.
62 * @align_drift: object offset from an "alignment"-aligned address.
63 * @alignment: natural object alignment. Must be non-zero, power of 2.
64 *
65 * Returns the offset that must be substracted to align towards lower addresses.
66 */
67 #define offset_align_floor(align_drift, alignment) \
68 ({ \
69 MAYBE_BUILD_BUG_ON((alignment) == 0 \
70 || ((alignment) & ((alignment) - 1))); \
71 (((align_drift) - (alignment)) & ((alignment) - 1); \
72 })
73
74 #endif /* _BABELTRACE_ALIGN_H */
This page took 0.042586 seconds and 3 git commands to generate.