From 11694905b4c3073b2ce5a3de954139bfaa50681f Mon Sep 17 00:00:00 2001 From: Christopher Durham Date: Mon, 11 Jul 2022 16:20:00 -0400 Subject: [PATCH] Remove duplication of layout size check --- library/core/src/alloc/layout.rs | 27 +++++++++++---------------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/library/core/src/alloc/layout.rs b/library/core/src/alloc/layout.rs index 51e075546fb..59ebe5fbe02 100644 --- a/library/core/src/alloc/layout.rs +++ b/library/core/src/alloc/layout.rs @@ -68,6 +68,13 @@ pub const fn from_size_align(size: usize, align: usize) -> Result Result { // (power-of-two implies align != 0.) // Rounded up size is: @@ -82,23 +89,11 @@ pub const fn from_size_align(size: usize, align: usize) -> Result isize::MAX as usize - (align - 1) { - return Err(LayoutError); - } - - // SAFETY: the conditions for `from_size_align_unchecked` have been - // checked above. - unsafe { Ok(Layout::from_size_align_unchecked(size, align)) } - } - - /// Internal helper constructor to skip revalidating alignment validity. - #[inline] - const fn from_size_valid_align(size: usize, align: ValidAlign) -> Result { - // See above for the correctness of this check. if size > isize::MAX as usize - (align.as_nonzero().get() - 1) { return Err(LayoutError); } - // SAFTEY: as above, this check is sufficient. + + // SAFETY: Layout::size invariants checked above. Ok(Layout { size, align }) } @@ -113,8 +108,8 @@ const fn from_size_valid_align(size: usize, align: ValidAlign) -> Result Self { - // SAFETY: the caller must ensure that `align` is a power of two. - Layout { size, align: unsafe { ValidAlign::new_unchecked(align) } } + // SAFETY: the caller is required to uphold the preconditions. + unsafe { Layout { size, align: ValidAlign::new_unchecked(align) } } } /// The minimum size in bytes for a memory block of this layout.