Skip to content

Stabilize alloc_layout_extra #97438

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion library/alloc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@
//
// Library features:
#![cfg_attr(not(no_global_oom_handling), feature(alloc_c_string))]
#![feature(alloc_layout_extra)]
#![feature(allocator_api)]
#![feature(array_chunks)]
#![feature(array_methods)]
Expand Down Expand Up @@ -121,6 +120,7 @@
#![feature(inplace_iteration)]
#![feature(iter_advance_by)]
#![feature(layout_for_ptr)]
#![feature(layout_padding_needed_for)]
#![feature(maybe_uninit_slice)]
#![cfg_attr(test, feature(new_uninit))]
#![feature(nonnull_slice_from_raw_parts)]
Expand Down
2 changes: 1 addition & 1 deletion library/alloc/tests/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#![feature(allocator_api)]
#![feature(alloc_layout_extra)]
#![feature(assert_matches)]
#![feature(box_syntax)]
#![feature(cow_is_borrowed)]
#![feature(const_alloc_layout)]
#![feature(const_box)]
#![feature(const_convert)]
#![feature(const_cow_is_borrowed)]
Expand Down
36 changes: 21 additions & 15 deletions library/core/src/alloc/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,8 @@ impl Layout {
/// which means this must not be used as a "not yet initialized"
/// sentinel value. Types that lazily allocate must track initialization by
/// some other means.
#[unstable(feature = "alloc_layout_extra", issue = "55724")]
#[rustc_const_unstable(feature = "alloc_layout_extra", issue = "55724")]
#[stable(feature = "alloc_layout_extra", since = "1.63.0")]
#[rustc_const_unstable(feature = "const_alloc_layout", issue = "67521")]
#[must_use]
#[inline]
pub const fn dangling(&self) -> NonNull<u8> {
Expand Down Expand Up @@ -232,7 +232,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 = "alloc_layout_extra", issue = "55724")]
#[unstable(feature = "layout_padding_needed_for", issue = "55724")]
#[rustc_const_unstable(feature = "const_alloc_layout", issue = "67521")]
#[must_use = "this returns the padding needed, \
without modifying the `Layout`"]
Expand Down Expand Up @@ -288,24 +288,30 @@ impl Layout {
/// 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
/// alignment. On success, returns `(k, offs)` where `k` is the
/// layout of the array and `offs` is the distance between the start
/// of each element in the array.
/// alignment, but *no trailing padding*. On success, returns `(k, offs)`
/// where `k` is the layout of the repetition and `offs` is the distance
/// between the start of each element in the repetition. This is equivalent
/// to calling `extend` multiple times.
///
/// On arithmetic overflow, returns `LayoutError`.
#[unstable(feature = "alloc_layout_extra", issue = "55724")]
#[stable(feature = "alloc_layout_extra", since = "1.63.0")]
#[inline]
pub fn repeat(&self, n: usize) -> Result<(Self, usize), LayoutError> {
let padding = self.padding_needed_for(self.align());

// This cannot overflow. Quoting from the invariant of Layout:
// > `size`, when rounded up to the nearest multiple of `align`,
// > must not overflow (i.e., the rounded value must be less than
// > `usize::MAX`)
let padded_size = self.size() + self.padding_needed_for(self.align());
let alloc_size = padded_size.checked_mul(n).ok_or(LayoutError)?;

// SAFETY: self.align is already known to be valid and alloc_size has been
// padded already.
unsafe { Ok((Layout::from_size_align_unchecked(alloc_size, self.align()), padded_size)) }
let padded_size = self.size() + padding;
let array_size = padded_size.checked_mul(n).ok_or(LayoutError)?;
let repetition_size = array_size - padding;

Ok((
// SAFETY: self.align is already known to be valid and array_size doesn't overflow.
unsafe { Layout::from_size_align_unchecked(repetition_size, self.align()) },
padded_size,
))
}

/// Creates a layout describing the record for `self` followed by
Expand Down Expand Up @@ -378,7 +384,7 @@ impl Layout {
/// aligned.
///
/// On arithmetic overflow, returns `LayoutError`.
#[unstable(feature = "alloc_layout_extra", issue = "55724")]
#[stable(feature = "alloc_layout_extra", since = "1.63.0")]
#[inline]
pub fn repeat_packed(&self, n: usize) -> Result<Self, LayoutError> {
let size = self.size().checked_mul(n).ok_or(LayoutError)?;
Expand All @@ -391,7 +397,7 @@ impl Layout {
/// and is not incorporated *at all* into the resulting layout.
///
/// On arithmetic overflow, returns `LayoutError`.
#[unstable(feature = "alloc_layout_extra", issue = "55724")]
#[stable(feature = "alloc_layout_extra", since = "1.63.0")]
#[inline]
pub fn extend_packed(&self, next: Self) -> Result<Self, LayoutError> {
let new_size = self.size().checked_add(next.size()).ok_or(LayoutError)?;
Expand Down
2 changes: 1 addition & 1 deletion library/core/tests/lib.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#![feature(alloc_layout_extra)]
#![feature(array_chunks)]
#![feature(array_methods)]
#![feature(array_windows)]
#![feature(bench_black_box)]
#![feature(box_syntax)]
#![feature(cell_update)]
#![feature(const_alloc_layout)]
#![feature(const_assume)]
#![feature(const_black_box)]
#![feature(const_bool_to_option)]
Expand Down
1 change: 0 additions & 1 deletion library/std/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,6 @@
#![feature(strict_provenance)]
//
// Library features (alloc):
#![feature(alloc_layout_extra)]
#![feature(alloc_c_string)]
#![feature(allocator_api)]
#![feature(get_mut_unchecked)]
Expand Down