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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
use core::cmp;
use core::fmt;
use core::mem;
use core::num::NonZeroUsize;
use core::usize;

/// Size and alignment constraints for a memory block.
#[derive(Clone, Copy, PartialEq, Eq)]
pub struct Layout {
    /// Required size in bytes of a valid memory block.
    size: usize,
    /// Required power-of-two base address alignment for a valid memory block.
    align: NonZeroUsize,
}

impl Layout {
    /// Returns a zero-sized `Layout` with byte alignment.
    #[inline]
    pub const fn empty() -> Layout {
        unsafe { Layout::from_size_align_unchecked(0, 1) }
    }

    /// Returns a `Layout` with the given size and power-of-two alignment.
    #[inline]
    pub const unsafe fn from_size_align_unchecked(size: usize, align: usize) -> Layout {
        Layout { size: size, align: NonZeroUsize::new_unchecked(align) }
    }

    /// Returns a `Layout` with the given size and power-of-two alignment,
    /// or `None` for invalid constraints.
    #[inline]
    pub fn from_size_align(size: usize, align: usize) -> Result<Layout, LayoutError> {
        if !align.is_power_of_two() {
            return Err(LayoutError::Misaligned);
        }
        if align > 1 << 31 {
            return Err(LayoutError::Misaligned);
        }
        if size > usize::MAX - (align - 1) {
            return Err(LayoutError::Oversized);
        }
        Ok(unsafe { Layout::from_size_align_unchecked(size, align) })
    }

    /// Returns the `Layout` of the parameterized type.
    #[inline]
    pub fn for_type<T>() -> Layout {
        let size = mem::size_of::<T>();
        let align = mem::align_of::<T>();
        unsafe { Layout::from_size_align_unchecked(size, align) }
    }

    /// Returns the `Layout` of the given `value`.
    #[inline]
    pub fn for_value<T: ?Sized>(value: &T) -> Layout {
        let size = mem::size_of_val(value);
        let align = mem::align_of_val(value);
        unsafe { Layout::from_size_align_unchecked(size, align) }
    }

    /// Returns the `Layout` of an array of `len` values of the parameterized type.
    #[inline]
    pub fn for_array<T>(len: usize) -> Result<Layout, LayoutError> {
        let align = mem::align_of::<T>();
        let stride = mem::size_of::<T>().wrapping_add(align).wrapping_sub(1) & !align.wrapping_sub(1);
        let size = match stride.checked_mul(len) {
            Some(size) => size,
            None => return Err(LayoutError::Oversized),
        };
        if size > usize::MAX - (align - 1) {
            return Err(LayoutError::Oversized);
        }
        Ok(unsafe { Layout::from_size_align_unchecked(size, align) })
    }

    #[inline]
    pub unsafe fn for_array_unchecked<T>(len: usize) -> Layout {
        let align = mem::align_of::<T>();
        let stride = mem::size_of::<T>().wrapping_add(align).wrapping_sub(1) & !align.wrapping_sub(1);
        let size = stride.wrapping_mul(len);
        Layout::from_size_align_unchecked(size, align)
    }

    /// Returns the required size in bytes of a valid memory block.
    #[inline]
    pub const fn size(&self) -> usize {
        self.size
    }

    /// Returns the required power-of-two base address alignment for a valid memory block.
    #[inline]
    pub fn align(&self) -> usize {
        self.align.get()
    }

    /// Returns this layout with at least `align` byte alignment.
    #[inline]
    pub fn aligned_to(&self, align: usize) -> Layout {
        if !align.is_power_of_two() {
            panic!();
        }
        let align = cmp::max(self.align.get(), align);
        unsafe { Layout::from_size_align_unchecked(self.size, align) }
    }

    /// Returns this layout with at least the alignment of the parameterized type.
    #[inline]
    pub fn aligned_to_type<T>(&self) -> Layout {
        let align = cmp::max(self.align.get(), mem::align_of::<T>());
        unsafe { Layout::from_size_align_unchecked(self.size, align) }
    }

    /// Returns this layout with at least the alignment of the given value.
    #[inline]
    pub fn aligned_to_value<T: ?Sized>(&self, value: &T) -> Layout {
        let align = cmp::max(self.align.get(), mem::align_of_val(value));
        unsafe { Layout::from_size_align_unchecked(self.size, align) }
    }

    /// Returns this layout with its size rounded up to the given alignment.
    #[inline]
    pub fn padded_to(&self, align: usize) -> Layout {
        if !align.is_power_of_two() {
            panic!();
        }
        let size = self.size.wrapping_add(align).wrapping_sub(1) & !align.wrapping_sub(1);
        unsafe { Layout::from_size_align_unchecked(size, cmp::max(self.align.get(), align)) }
    }

    /// Returns this layout with its size rouneded up to the alignment of a subsequent field
    /// of the given type.
    #[inline]
    pub fn padded_to_type<T>(&self) -> Layout {
        let align = mem::align_of::<T>();
        let size = self.size.wrapping_add(align).wrapping_sub(1) & !align.wrapping_sub(1);
        unsafe { Layout::from_size_align_unchecked(size, cmp::max(self.align.get(), align)) }
    }

    /// Returns this layout with its size rounded up to the the alignment of alignment of the
    /// given field.
    #[inline]
    pub fn padded_to_value<T: ?Sized>(&self, value: &T) -> Layout {
        let align = mem::align_of_val(value);
        let size = self.size.wrapping_add(align).wrapping_sub(1) & !align.wrapping_sub(1);
        unsafe { Layout::from_size_align_unchecked(size, cmp::max(self.align.get(), align)) }
    }

    /// Returns the `Layout` of a struct with this layout as its first member,
    /// and `that` layout as its second member.
    #[inline]
    pub fn extended(&self, that: Layout) -> Result<(Layout, usize), LayoutError> {
        let next_align = that.align.get();
        let align = cmp::max(self.align.get(), next_align);
        let offset = self.size.wrapping_add(next_align).wrapping_sub(1) & !next_align.wrapping_sub(1);
        let size = match offset.checked_add(that.size) {
            Some(size) => size,
            None => return Err(LayoutError::Oversized),
        };
        if size > usize::MAX - (align - 1) {
            return Err(LayoutError::Oversized);
        }
        Ok((unsafe { Layout::from_size_align_unchecked(size, align) }, offset))
    }

    #[inline]
    pub fn extended_by_type<T>(&self) -> Result<(Layout, usize), LayoutError> {
        self.extended(Layout::for_type::<T>())
    }

    #[inline]
    pub fn extended_by_value<T: ?Sized>(&self, value: &T) -> Result<(Layout, usize), LayoutError> {
        self.extended(Layout::for_value(value))
    }

    #[inline]
    pub fn extended_by_array<T>(&self, len: usize) -> Result<(Layout, usize), LayoutError> {
        self.extended(Layout::for_array::<T>(len)?)
    }

    /// Returns the `Layout` of a struct with this layout as its first member,
    /// and `that` layout as its second member, without checking for size overflow.
    #[inline]
    pub unsafe fn extended_unchecked(&self, that: Layout) -> (Layout, usize) {
        let next_align = that.align.get();
        let align = cmp::max(self.align.get(), next_align);
        let offset = self.size.wrapping_add(next_align).wrapping_sub(1) & !next_align.wrapping_sub(1);
        let size = offset.wrapping_add(that.size);
        (Layout::from_size_align_unchecked(size, align), offset)
    }

    #[inline]
    pub unsafe fn extended_by_type_unchecked<T>(&self) -> (Layout, usize) {
        self.extended_unchecked(Layout::for_type::<T>())
    }

    #[inline]
    pub unsafe fn extended_by_value_unchecked<T: ?Sized>(&self, value: &T) -> (Layout, usize) {
        self.extended_unchecked(Layout::for_value(value))
    }

    #[inline]
    pub unsafe fn extended_by_array_unchecked<T>(&self, len: usize) -> (Layout, usize) {
        self.extended_unchecked(Layout::for_array_unchecked::<T>(len))
    }

    /// Returns the `Layout` of an array with `len` elements of this layout.
    #[inline]
    pub fn repeated(&self, len: usize) -> Result<(Layout, usize), LayoutError> {
        let align = self.align.get();
        let stride = self.size.wrapping_add(align).wrapping_sub(1) & !align.wrapping_sub(1);
        let size = match stride.checked_mul(len) {
            Some(size) => size,
            None => return Err(LayoutError::Oversized),
        };
        if size > usize::MAX - (align - 1) {
            return Err(LayoutError::Oversized);
        }
        Ok((unsafe { Layout::from_size_align_unchecked(size, align) }, stride))
    }

    /// Returns the `Layout` of an array with `len` elements of this layout,
    /// without checking for size overflow.
    #[inline]
    pub unsafe fn repeated_unchecked(&self, len: usize) -> (Layout, usize) {
        let align = self.align.get();
        let stride = self.size.wrapping_add(align).wrapping_sub(1) & !align.wrapping_sub(1);
        let size = stride.wrapping_mul(len);
        (Layout::from_size_align_unchecked(size, align), stride)
    }
}

impl fmt::Debug for Layout {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.debug_struct("Layout")
            .field("size", &self.size)
            .field("align", &self.align.get())
            .finish()
    }
}

/// Memory layout error.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum LayoutError {
    /// Improper structure alignment.
    Misaligned,
    /// Structure size overflow.
    Oversized,
}