From 94f3ce9def8659e281b398c1582036f181d62f44 Mon Sep 17 00:00:00 2001 From: Amanieu d'Antras Date: Sat, 27 Oct 2018 22:38:37 +0100 Subject: [PATCH 1/3] Add a comment about how Layout::extend matches the C struct layout --- src/libcore/alloc.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/libcore/alloc.rs b/src/libcore/alloc.rs index 4efcaae59b0..bc2860a5a2f 100644 --- a/src/libcore/alloc.rs +++ b/src/libcore/alloc.rs @@ -248,6 +248,9 @@ impl Layout { /// will be properly aligned. Note that the result layout will /// satisfy the alignment properties of both `self` and `next`. /// + /// The resulting layout will be the same as that of a C struct containing + /// two fields with the layouts of `self` and `next`, in that order. + /// /// Returns `Some((k, offset))`, where `k` is layout of the concatenated /// record and `offset` is the relative location, in bytes, of the /// start of the `next` embedded within the concatenated record From de027b64f1052cbeed911b5e98aab8803cfe302e Mon Sep 17 00:00:00 2001 From: Amanieu d'Antras Date: Tue, 30 Oct 2018 21:36:43 +0000 Subject: [PATCH 2/3] API changes as discussed in the comments --- src/libcore/alloc.rs | 23 ++++++----------------- 1 file changed, 6 insertions(+), 17 deletions(-) diff --git a/src/libcore/alloc.rs b/src/libcore/alloc.rs index bc2860a5a2f..a5cc58f0c92 100644 --- a/src/libcore/alloc.rs +++ b/src/libcore/alloc.rs @@ -164,15 +164,13 @@ impl Layout { /// alignment. In other words, if `K` has size 16, `K.align_to(32)` /// will *still* have size 16. /// - /// # Panics - /// - /// Panics if the combination of `self.size()` and the given `align` - /// violates the conditions listed in + /// Returns an error if the combination of `self.size()` and the given + /// `align` violates the conditions listed in /// [`Layout::from_size_align`](#method.from_size_align). #[unstable(feature = "allocator_api", issue = "32838")] #[inline] - pub fn align_to(&self, align: usize) -> Self { - Layout::from_size_align(self.size(), cmp::max(self.align(), align)).unwrap() + pub fn align_to(&self, align: usize) -> Result { + Layout::from_size_align(self.size(), cmp::max(self.align(), align)) } /// Returns the amount of padding we must insert after `self` @@ -296,23 +294,14 @@ impl Layout { /// padding is inserted, the alignment of `next` is irrelevant, /// and is not incorporated *at all* into the resulting layout. /// - /// Returns `(k, offset)`, where `k` is layout of the concatenated - /// record and `offset` is the relative location, in bytes, of the - /// start of the `next` embedded within the concatenated record - /// (assuming that the record itself starts at offset 0). - /// - /// (The `offset` is always the same as `self.size()`; we use this - /// signature out of convenience in matching the signature of - /// `extend`.) - /// /// On arithmetic overflow, returns `LayoutErr`. #[unstable(feature = "allocator_api", issue = "32838")] #[inline] - pub fn extend_packed(&self, next: Self) -> Result<(Self, usize), LayoutErr> { + pub fn extend_packed(&self, next: Self) -> Result { let new_size = self.size().checked_add(next.size()) .ok_or(LayoutErr { private: () })?; let layout = Layout::from_size_align(new_size, self.align())?; - Ok((layout, self.size())) + Ok(layout) } /// Creates a layout describing the record for a `[T; n]`. From 02d50de63e50423c9cbaf3daa46dde6c9c5c9dba Mon Sep 17 00:00:00 2001 From: Amanieu d'Antras Date: Thu, 25 Oct 2018 23:48:27 +0100 Subject: [PATCH 3/3] Add a tracking issue for extra Layout methods --- src/liballoc/lib.rs | 1 + src/libcore/alloc.rs | 14 +++++++------- src/libstd/lib.rs | 1 + 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/liballoc/lib.rs b/src/liballoc/lib.rs index 84ca7c4fec9..c082a6d8ef0 100644 --- a/src/liballoc/lib.rs +++ b/src/liballoc/lib.rs @@ -119,6 +119,7 @@ #![feature(const_vec_new)] #![feature(slice_partition_dedup)] #![feature(maybe_uninit)] +#![feature(alloc_layout_extra)] // Allow testing this library diff --git a/src/libcore/alloc.rs b/src/libcore/alloc.rs index a5cc58f0c92..113a85abecb 100644 --- a/src/libcore/alloc.rs +++ b/src/libcore/alloc.rs @@ -167,7 +167,7 @@ impl Layout { /// Returns an error if the combination of `self.size()` and the given /// `align` violates the conditions listed in /// [`Layout::from_size_align`](#method.from_size_align). - #[unstable(feature = "allocator_api", issue = "32838")] + #[unstable(feature = "alloc_layout_extra", issue = "55724")] #[inline] pub fn align_to(&self, align: usize) -> Result { Layout::from_size_align(self.size(), cmp::max(self.align(), align)) @@ -189,7 +189,7 @@ impl Layout { /// to be less than or equal to the alignment of the starting /// address for the whole allocated block of memory. One way to /// satisfy this constraint is to ensure `align <= self.align()`. - #[unstable(feature = "allocator_api", issue = "32838")] + #[unstable(feature = "alloc_layout_extra", issue = "55724")] #[inline] pub fn padding_needed_for(&self, align: usize) -> usize { let len = self.size(); @@ -226,7 +226,7 @@ impl Layout { /// of each element in the array. /// /// On arithmetic overflow, returns `LayoutErr`. - #[unstable(feature = "allocator_api", issue = "32838")] + #[unstable(feature = "alloc_layout_extra", issue = "55724")] #[inline] pub fn repeat(&self, n: usize) -> Result<(Self, usize), LayoutErr> { let padded_size = self.size().checked_add(self.padding_needed_for(self.align())) @@ -255,7 +255,7 @@ impl Layout { /// (assuming that the record itself starts at offset 0). /// /// On arithmetic overflow, returns `LayoutErr`. - #[unstable(feature = "allocator_api", issue = "32838")] + #[unstable(feature = "alloc_layout_extra", issue = "55724")] #[inline] pub fn extend(&self, next: Self) -> Result<(Self, usize), LayoutErr> { let new_align = cmp::max(self.align(), next.align()); @@ -282,7 +282,7 @@ impl Layout { /// aligned. /// /// On arithmetic overflow, returns `LayoutErr`. - #[unstable(feature = "allocator_api", issue = "32838")] + #[unstable(feature = "alloc_layout_extra", issue = "55724")] #[inline] pub fn repeat_packed(&self, n: usize) -> Result { let size = self.size().checked_mul(n).ok_or(LayoutErr { private: () })?; @@ -295,7 +295,7 @@ impl Layout { /// and is not incorporated *at all* into the resulting layout. /// /// On arithmetic overflow, returns `LayoutErr`. - #[unstable(feature = "allocator_api", issue = "32838")] + #[unstable(feature = "alloc_layout_extra", issue = "55724")] #[inline] pub fn extend_packed(&self, next: Self) -> Result { let new_size = self.size().checked_add(next.size()) @@ -307,7 +307,7 @@ impl Layout { /// Creates a layout describing the record for a `[T; n]`. /// /// On arithmetic overflow, returns `LayoutErr`. - #[unstable(feature = "allocator_api", issue = "32838")] + #[unstable(feature = "alloc_layout_extra", issue = "55724")] #[inline] pub fn array(n: usize) -> Result { Layout::new::() diff --git a/src/libstd/lib.rs b/src/libstd/lib.rs index a4db8796805..b3c137f971b 100644 --- a/src/libstd/lib.rs +++ b/src/libstd/lib.rs @@ -310,6 +310,7 @@ #![feature(doc_keyword)] #![feature(panic_info_message)] #![feature(non_exhaustive)] +#![feature(alloc_layout_extra)] #![default_lib_allocator]