Skip to content

Commit e58e5e1

Browse files
committed
fix VirtAddr::align_up & VirtAddr::align_down
1 parent 3cae23e commit e58e5e1

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed

src/addr.rs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,12 +158,17 @@ impl VirtAddr {
158158
/// Aligns the virtual address upwards to the given alignment.
159159
///
160160
/// See the `align_up` function for more information.
161+
///
162+
/// # Panics
163+
///
164+
/// This function panics if the resulting address is higher than
165+
/// `0xffff_ffff_ffff_ffff`.
161166
#[inline]
162167
pub fn align_up<U>(self, align: U) -> Self
163168
where
164169
U: Into<u64>,
165170
{
166-
VirtAddr(align_up(self.0, align.into()))
171+
VirtAddr::new_truncate(align_up(self.0, align.into()))
167172
}
168173

169174
/// Aligns the virtual address downwards to the given alignment.
@@ -174,7 +179,7 @@ impl VirtAddr {
174179
where
175180
U: Into<u64>,
176181
{
177-
VirtAddr(align_down(self.0, align.into()))
182+
VirtAddr::new_truncate(align_down(self.0, align.into()))
178183
}
179184

180185
/// Checks whether the virtual address has the demanded alignment.
@@ -791,4 +796,22 @@ mod tests {
791796
assert_eq!(align_up(0, 2), 0);
792797
assert_eq!(align_up(0, 0x8000_0000_0000_0000), 0);
793798
}
799+
800+
#[test]
801+
fn test_virt_addr_align_up() {
802+
// Make sure the 47th bit is extended.
803+
assert_eq!(
804+
VirtAddr::new(0x7fff_ffff_ffff).align_up(2u64),
805+
VirtAddr::new(0xffff_8000_0000_0000)
806+
);
807+
}
808+
809+
#[test]
810+
fn test_virt_addr_align_down() {
811+
// Make sure the 47th bit is extended.
812+
assert_eq!(
813+
VirtAddr::new(0xffff_8000_0000_0000).align_down(1u64 << 48),
814+
VirtAddr::new(0)
815+
);
816+
}
794817
}

0 commit comments

Comments
 (0)