Move types.c to types/
[babeltrace.git] / include / ctf / align.h
CommitLineData
6dc2ca62
MD
1#ifndef _CTF_ALIGN_H
2#define _CTF_ALIGN_H
3
de0ba614
MD
4/*
5 * align.h - alignment header
6 *
7 * Copyright (c) 2010 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
6dc2ca62
MD
24#include <ctf/compiler.h>
25
26#define ALIGN(x, a) __ALIGN_MASK(x, (typeof(x))(a) - 1)
27#define __ALIGN_MASK(x, mask) (((x) + (mask)) & ~(mask))
28#define PTR_ALIGN(p, a) ((typeof(p)) ALIGN((unsigned long) (p), a))
29#define ALIGN_FLOOR(x, a) __ALIGN_FLOOR_MASK(x, (typeof(x)) (a) - 1)
30#define __ALIGN_FLOOR_MASK(x, mask) ((x) & ~(mask))
31#define PTR_ALIGN_FLOOR(p, a) \
32 ((typeof(p)) ALIGN_FLOOR((unsigned long) (p), a))
33#define IS_ALIGNED(x, a) (((x) & ((typeof(x)) (a) - 1)) == 0)
34
35/*
36 * Align pointer on natural object alignment.
37 */
38#define object_align(obj) PTR_ALIGN(obj, __alignof__(*(obj)))
39#define object_align_floor(obj) PTR_ALIGN_FLOOR(obj, __alignof__(*(obj)))
40
41/**
42 * offset_align - Calculate the offset needed to align an object on its natural
43 * alignment towards higher addresses.
44 * @align_drift: object offset from an "alignment"-aligned address.
45 * @alignment: natural object alignment. Must be non-zero, power of 2.
46 *
47 * Returns the offset that must be added to align towards higher
48 * addresses.
49 */
50#define offset_align(align_drift, alignment) \
51 ({ \
52 MAYBE_BUILD_BUG_ON((alignment) == 0 \
53 || ((alignment) & ((alignment) - 1))); \
54 (((alignment) - (align_drift)) & ((alignment) - 1)); \
55 })
56
57/**
58 * offset_align_floor - Calculate the offset needed to align an object
59 * on its natural alignment towards lower addresses.
60 * @align_drift: object offset from an "alignment"-aligned address.
61 * @alignment: natural object alignment. Must be non-zero, power of 2.
62 *
63 * Returns the offset that must be substracted to align towards lower addresses.
64 */
65#define offset_align_floor(align_drift, alignment) \
66 ({ \
67 MAYBE_BUILD_BUG_ON((alignment) == 0 \
68 || ((alignment) & ((alignment) - 1))); \
69 (((align_drift) - (alignment)) & ((alignment) - 1); \
70 })
71
72#endif /* _CTF_ALIGN_H */
This page took 0.034711 seconds and 4 git commands to generate.