1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// Copyright © 2024 Institute of Software, CAS. All rights reserved.
// SPDX-License-Identifier: BSD-3-Clause
//! Miscellaneous macros related to align addresses
/// Aligns the given address downwards to the nearest boundary specified by
/// `alignment`.
///
/// This macro ensures that the result is the largest address less than or equal
/// to `addr` that is aligned to `alignment`.
///
/// # Arguments
///
/// * `$addr` - The address or value to align downwards.
/// * `$alignment` - The alignment boundary, which must be a power of two.
///
/// # Panics
///
/// The macro will panic if `$alignment` is not a power of two.
///
/// # Example
///
/// ```rust
/// use vmm_sys_util::align_downwards;
///
/// let addr: usize = 10;
/// let alignment: usize = 4; // Must be a power of two
/// let aligned = align_downwards!(addr, alignment);
/// assert_eq!(aligned, 8);
/// ```
/// Aligns the given address upwards to the nearest boundary specified by `alignment`.
///
/// This macro ensures that the result is the smallest address greater than or equal to `addr`
/// that is aligned to `alignment`.
///
/// # Arguments
///
/// * `$addr` - The address or value to align upwards.
/// * `$alignment` - The alignment boundary, which must be a power of two.
///
/// # Panics
///
/// The macro will panic if `$alignment` is not a power of two.
///
/// # Example
///
/// ```rust
/// use vmm_sys_util::align_upwards;
///
/// let addr: usize = 10;
/// let alignment: usize = 4; // Must be a power of two
/// let aligned = align_upwards!(addr, alignment);
/// assert_eq!(aligned, 12);
/// ```