diff options
Diffstat (limited to 'rust/kernel')
| -rw-r--r-- | rust/kernel/alloc/allocator.rs | 56 | 
1 files changed, 56 insertions, 0 deletions
| diff --git a/rust/kernel/alloc/allocator.rs b/rust/kernel/alloc/allocator.rs index f4ae0cf0a594..b561e7a57bb8 100644 --- a/rust/kernel/alloc/allocator.rs +++ b/rust/kernel/alloc/allocator.rs @@ -190,3 +190,59 @@ unsafe impl Allocator for KVmalloc {          unsafe { ReallocFunc::KVREALLOC.call(ptr, layout, old_layout, flags, nid) }      }  } + +#[macros::kunit_tests(rust_allocator)] +mod tests { +    use super::*; +    use core::mem::MaybeUninit; +    use kernel::prelude::*; + +    #[test] +    fn test_alignment() -> Result { +        const TEST_SIZE: usize = 1024; +        const TEST_LARGE_ALIGN_SIZE: usize = kernel::page::PAGE_SIZE * 4; + +        // These two structs are used to test allocating aligned memory. +        // they don't need to be accessed, so they're marked as dead_code. +        #[expect(dead_code)] +        #[repr(align(128))] +        struct Blob([u8; TEST_SIZE]); +        #[expect(dead_code)] +        #[repr(align(8192))] +        struct LargeAlignBlob([u8; TEST_LARGE_ALIGN_SIZE]); + +        struct TestAlign<T, A: Allocator>(Box<MaybeUninit<T>, A>); +        impl<T, A: Allocator> TestAlign<T, A> { +            fn new() -> Result<Self> { +                Ok(Self(Box::<_, A>::new_uninit(GFP_KERNEL)?)) +            } + +            fn is_aligned_to(&self, align: usize) -> bool { +                assert!(align.is_power_of_two()); + +                let addr = self.0.as_ptr() as usize; +                addr & (align - 1) == 0 +            } +        } + +        let ta = TestAlign::<Blob, Kmalloc>::new()?; +        assert!(ta.is_aligned_to(128)); + +        let ta = TestAlign::<LargeAlignBlob, Kmalloc>::new()?; +        assert!(ta.is_aligned_to(8192)); + +        let ta = TestAlign::<Blob, Vmalloc>::new()?; +        assert!(ta.is_aligned_to(128)); + +        let ta = TestAlign::<LargeAlignBlob, Vmalloc>::new()?; +        assert!(ta.is_aligned_to(8192)); + +        let ta = TestAlign::<Blob, KVmalloc>::new()?; +        assert!(ta.is_aligned_to(128)); + +        let ta = TestAlign::<LargeAlignBlob, KVmalloc>::new()?; +        assert!(ta.is_aligned_to(8192)); + +        Ok(()) +    } +} | 
