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
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
use crate::aligned_alloc;
use crate::boxed::AlignedBox;

use alloc::alloc::alloc_zeroed;
use core::fmt::{self, Debug};
use core::ops::{Deref, DerefMut};
use core::slice;

/// A continuous fixed-length byte array with a specified alignment.
pub struct AlignedBytes {
    inner: AlignedBox<[u8]>,
}

impl AlignedBytes {
    /// Allocate a zero-initialized byte array with an exact alignment.
    pub fn new_zeroed(len: usize, align: usize) -> Self {
        let inner = unsafe {
            let ptr = if len == 0 {
                align as *mut u8
            } else {
                aligned_alloc(alloc_zeroed, len, align)
            };
            AlignedBox::from_raw(slice::from_raw_parts_mut(ptr, len), align)
        };
        debug_assert!(inner.as_ptr() as usize % align == 0);
        Self { inner }
    }

    /// Returns the alignment of the byte array.
    pub fn alignment(&self) -> usize {
        AlignedBox::alignment(&self.inner)
    }
}

impl Debug for AlignedBytes {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        <[u8] as Debug>::fmt(&**self, f)
    }
}

impl Deref for AlignedBytes {
    type Target = [u8];
    fn deref(&self) -> &Self::Target {
        &*self.inner
    }
}

impl DerefMut for AlignedBytes {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut *self.inner
    }
}

impl AsRef<[u8]> for AlignedBytes {
    fn as_ref(&self) -> &[u8] {
        &*self
    }
}

impl AsMut<[u8]> for AlignedBytes {
    fn as_mut(&mut self) -> &mut [u8] {
        &mut *self
    }
}

#[cfg(test)]
mod tests {
    use super::AlignedBytes;

    #[test]
    fn check_alignment() {
        let align = 4096;
        let bytes = AlignedBytes::new_zeroed(8, align);
        assert_eq!(bytes.alignment(), align);
        assert!(bytes.as_ptr() as usize % align == 0);
    }

    #[should_panic(expected = "Invalid layout: size = 1, align = 0")]
    #[test]
    fn check_layout_zero_align() {
        AlignedBytes::new_zeroed(1, 0);
    }

    #[should_panic(expected = "Invalid layout: size = 1, align = 3")]
    #[test]
    fn check_layout_align_not_power_of_2() {
        AlignedBytes::new_zeroed(1, 3);
    }

    #[should_panic]
    #[test]
    fn check_layout_overflow() {
        let size = core::mem::size_of::<usize>() * 8;
        AlignedBytes::new_zeroed((1usize << (size - 1)) + 1, 1usize << (size - 1));
    }

    #[test]
    fn check_markers() {
        fn require_send<T: Send>() {}
        fn require_sync<T: Sync>() {}

        require_send::<AlignedBytes>();
        require_sync::<AlignedBytes>();

        #[cfg(feature = "std")]
        {
            use std::panic::{RefUnwindSafe, UnwindSafe};
            fn require_unwind_safe<T: UnwindSafe>() {}
            fn require_ref_unwind_safe<T: RefUnwindSafe>() {}
            require_unwind_safe::<AlignedBytes>();
            require_ref_unwind_safe::<AlignedBytes>();
        }
    }

    #[test]
    fn check_zst() {
        AlignedBytes::new_zeroed(0, 2);
    }
}