@@ -158,12 +158,17 @@ impl VirtAddr {
158
158
/// Aligns the virtual address upwards to the given alignment.
159
159
///
160
160
/// 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`.
161
166
#[ inline]
162
167
pub fn align_up < U > ( self , align : U ) -> Self
163
168
where
164
169
U : Into < u64 > ,
165
170
{
166
- VirtAddr ( align_up ( self . 0 , align. into ( ) ) )
171
+ VirtAddr :: new_truncate ( align_up ( self . 0 , align. into ( ) ) )
167
172
}
168
173
169
174
/// Aligns the virtual address downwards to the given alignment.
@@ -174,7 +179,7 @@ impl VirtAddr {
174
179
where
175
180
U : Into < u64 > ,
176
181
{
177
- VirtAddr ( align_down ( self . 0 , align. into ( ) ) )
182
+ VirtAddr :: new_truncate ( align_down ( self . 0 , align. into ( ) ) )
178
183
}
179
184
180
185
/// Checks whether the virtual address has the demanded alignment.
@@ -791,4 +796,22 @@ mod tests {
791
796
assert_eq ! ( align_up( 0 , 2 ) , 0 ) ;
792
797
assert_eq ! ( align_up( 0 , 0x8000_0000_0000_0000 ) , 0 ) ;
793
798
}
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
+ }
794
817
}
0 commit comments