diff options
| author | Pietro Albini <pietro@pietroalbini.org> | 2018-11-11 00:21:13 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2018-11-11 00:21:13 +0100 |
| commit | d2aeef06f2f41c0b7f4d13cd59cd07ad8a377e83 (patch) | |
| tree | e690a983ab0484984882a62328b5d4c212b58fa9 /src/libcore | |
| parent | f0a6e3a583bc19bdb21e4403d7235488d02b883d (diff) | |
| parent | 317f494c72aead3fecd73788569983fd2f8ea8a3 (diff) | |
| download | rust-d2aeef06f2f41c0b7f4d13cd59cd07ad8a377e83.tar.gz rust-d2aeef06f2f41c0b7f4d13cd59cd07ad8a377e83.zip | |
Rollup merge of #55764 - murarth:fix-rc-alloc, r=RalfJung
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 Closes #55747 cc #55724
Diffstat (limited to 'src/libcore')
| -rw-r--r-- | src/libcore/alloc.rs | 17 |
1 files changed, 17 insertions, 0 deletions
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<Layout, LayoutErr> { + 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 |
