Move to kernel style SPDX license identifiers
[babeltrace.git] / src / common / align.h
CommitLineData
de0ba614 1/*
0235b0db 2 * SPDX-License-Identifier: MIT
de0ba614 3 *
0235b0db 4 * Copyright 2010 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
de0ba614
MD
5 */
6
0235b0db
MJ
7#ifndef _BABELTRACE_ALIGN_H
8#define _BABELTRACE_ALIGN_H
9
578e048b
MJ
10#include "compat/compiler.h"
11#include "compat/limits.h"
c4f467da 12
6dc2ca62
MD
13#define ALIGN(x, a) __ALIGN_MASK(x, (typeof(x))(a) - 1)
14#define __ALIGN_MASK(x, mask) (((x) + (mask)) & ~(mask))
15#define PTR_ALIGN(p, a) ((typeof(p)) ALIGN((unsigned long) (p), a))
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#define IS_ALIGNED(x, a) (((x) & ((typeof(x)) (a) - 1)) == 0)
21
22/*
23 * Align pointer on natural object alignment.
24 */
25#define object_align(obj) PTR_ALIGN(obj, __alignof__(*(obj)))
26#define object_align_floor(obj) PTR_ALIGN_FLOOR(obj, __alignof__(*(obj)))
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 MAYBE_BUILD_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 MAYBE_BUILD_BUG_ON((alignment) == 0 \
55 || ((alignment) & ((alignment) - 1))); \
a12af8c1 56 (((align_drift) - (alignment)) & ((alignment) - 1)); \
6dc2ca62
MD
57 })
58
d79865b9 59#endif /* _BABELTRACE_ALIGN_H */
This page took 0.085745 seconds and 4 git commands to generate.