Skip to content

Commit

Permalink
add missing inline's and optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
gnzlbg committed Jun 2, 2018
1 parent 6ff67ee commit 2d7cd70
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/libcore/alloc.rs
Expand Up @@ -244,7 +244,12 @@ impl Layout {
.ok_or(LayoutErr { private: () })?;
let alloc_size = padded_size.checked_mul(n)
.ok_or(LayoutErr { private: () })?;
Ok((Layout::from_size_align(alloc_size, self.align())?, padded_size))

unsafe {
// self.align is already known to be valid and alloc_size has been
// padded already.
Ok((Layout::from_size_align_unchecked(alloc_size, self.align()), padded_size))
}
}

/// Creates a layout describing the record for `self` followed by
Expand All @@ -258,11 +263,10 @@ impl Layout {
/// (assuming that the record itself starts at offset 0).
///
/// On arithmetic overflow, returns `LayoutErr`.
#[inline]
pub fn extend(&self, next: Self) -> Result<(Self, usize), LayoutErr> {
let new_align = cmp::max(self.align(), next.align());
let realigned = Layout::from_size_align(self.size(), new_align)?;

let pad = realigned.padding_needed_for(next.align());
let pad = self.padding_needed_for(next.align());

let offset = self.size().checked_add(pad)
.ok_or(LayoutErr { private: () })?;
Expand All @@ -285,6 +289,7 @@ impl Layout {
/// aligned.
///
/// On arithmetic overflow, returns `LayoutErr`.
#[inline]
pub fn repeat_packed(&self, n: usize) -> Result<Self, LayoutErr> {
let size = self.size().checked_mul(n).ok_or(LayoutErr { private: () })?;
Layout::from_size_align(size, self.align())
Expand All @@ -305,6 +310,7 @@ impl Layout {
/// `extend`.)
///
/// On arithmetic overflow, returns `LayoutErr`.
#[inline]
pub fn extend_packed(&self, next: Self) -> Result<(Self, usize), LayoutErr> {
let new_size = self.size().checked_add(next.size())
.ok_or(LayoutErr { private: () })?;
Expand All @@ -315,6 +321,7 @@ impl Layout {
/// Creates a layout describing the record for a `[T; n]`.
///
/// On arithmetic overflow, returns `LayoutErr`.
#[inline]
pub fn array<T>(n: usize) -> Result<Self, LayoutErr> {
Layout::new::<T>()
.repeat(n)
Expand Down

0 comments on commit 2d7cd70

Please sign in to comment.