From 317f494c72aead3fecd73788569983fd2f8ea8a3 Mon Sep 17 00:00:00 2001 From: Murarth Date: Wed, 7 Nov 2018 10:59:25 -0700 Subject: Fix Rc/Arc allocation layout * Rounds allocation layout up to a multiple of alignment * Adds a convenience method `Layout::pad_to_align` to perform rounding --- src/libcore/alloc.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'src/libcore') diff --git a/src/libcore/alloc.rs b/src/libcore/alloc.rs index 113a85abecb..dd3e8da18a9 100644 --- a/src/libcore/alloc.rs +++ b/src/libcore/alloc.rs @@ -218,6 +218,23 @@ impl Layout { len_rounded_up.wrapping_sub(len) } + /// Creates a layout by rounding the size of this layout up to a multiple + /// of the layout's alignment. + /// + /// Returns `Err` if the padded size would overflow. + /// + /// This is equivalent to adding the result of `padding_needed_for` + /// to the layout's current size. + #[unstable(feature = "alloc_layout_extra", issue = "55724")] + #[inline] + pub fn pad_to_align(&self) -> Result { + let pad = self.padding_needed_for(self.align()); + let new_size = self.size().checked_add(pad) + .ok_or(LayoutErr { private: () })?; + + Layout::from_size_align(new_size, self.align()) + } + /// Creates a layout describing the record for `n` instances of /// `self`, with a suitable amount of padding between each to /// ensure that each instance is given its requested size and -- cgit 1.4.1-3-g733a5