-
Notifications
You must be signed in to change notification settings - Fork 14.1k
Stabilize alloc_layout_extra
#148769
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
Open
GrigorenkoPV
wants to merge
1
commit into
rust-lang:main
Choose a base branch
from
GrigorenkoPV:stabilize/alloc_layout_extra
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+56
−49
Open
Stabilize alloc_layout_extra
#148769
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -230,10 +230,11 @@ impl Layout { | |||||||||||||||||
| /// be that of a valid pointer, 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")] | ||||||||||||||||||
| #[stable(feature = "alloc_layout_extra", since = "CURRENT_RUSTC_VERSION")] | ||||||||||||||||||
| #[rustc_const_stable(feature = "alloc_layout_extra", since = "CURRENT_RUSTC_VERSION")] | ||||||||||||||||||
| #[must_use] | ||||||||||||||||||
| #[inline] | ||||||||||||||||||
| pub const fn dangling(&self) -> NonNull<u8> { | ||||||||||||||||||
| pub const fn dangling_ptr(&self) -> NonNull<u8> { | ||||||||||||||||||
| NonNull::without_provenance(self.align.as_nonzero()) | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
|
|
@@ -263,29 +264,23 @@ impl Layout { | |||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| /// Returns the amount of padding we must insert after `self` | ||||||||||||||||||
| /// to ensure that the following address will satisfy `align` | ||||||||||||||||||
| /// (measured in bytes). | ||||||||||||||||||
| /// to ensure that the following address will satisfy `alignment`. | ||||||||||||||||||
| /// | ||||||||||||||||||
| /// e.g., if `self.size()` is 9, then `self.padding_needed_for(4)` | ||||||||||||||||||
| /// e.g., if `self.size()` is 9, then `self.padding_needed_for(alignment4)` | ||||||||||||||||||
| /// (where `alignment4.as_usize() == 4`) | ||||||||||||||||||
| /// returns 3, because that is the minimum number of bytes of | ||||||||||||||||||
| /// padding required to get a 4-aligned address (assuming that the | ||||||||||||||||||
| /// corresponding memory block starts at a 4-aligned address). | ||||||||||||||||||
| /// | ||||||||||||||||||
| /// The return value of this function has no meaning if `align` is | ||||||||||||||||||
| /// not a power-of-two. | ||||||||||||||||||
| /// | ||||||||||||||||||
| /// Note that the utility of the returned value requires `align` | ||||||||||||||||||
| /// Note that the utility of the returned value requires `alignment` | ||||||||||||||||||
| /// 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")] | ||||||||||||||||||
| #[must_use = "this returns the padding needed, \ | ||||||||||||||||||
| without modifying the `Layout`"] | ||||||||||||||||||
| /// satisfy this constraint is to ensure `alignment.as_usize() <= self.align()`. | ||||||||||||||||||
| #[unstable(feature = "ptr_alignment_type", issue = "102070")] | ||||||||||||||||||
| #[must_use = "this returns the padding needed, without modifying the `Layout`"] | ||||||||||||||||||
| #[inline] | ||||||||||||||||||
| pub const fn padding_needed_for(&self, align: usize) -> usize { | ||||||||||||||||||
| // FIXME: Can we just change the type on this to `Alignment`? | ||||||||||||||||||
| let Some(align) = Alignment::new(align) else { return usize::MAX }; | ||||||||||||||||||
| let len_rounded_up = self.size_rounded_up_to_custom_align(align); | ||||||||||||||||||
| pub const fn padding_needed_for(&self, alignment: Alignment) -> usize { | ||||||||||||||||||
| let len_rounded_up = self.size_rounded_up_to_custom_align(alignment); | ||||||||||||||||||
| // SAFETY: Cannot overflow because the rounded-up value is never less | ||||||||||||||||||
| unsafe { unchecked_sub(len_rounded_up, self.size) } | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
@@ -348,14 +343,15 @@ impl Layout { | |||||||||||||||||
| /// layout of the array and `offs` is the distance between the start | ||||||||||||||||||
| /// of each element in the array. | ||||||||||||||||||
| /// | ||||||||||||||||||
| /// Does not include padding after the trailing element. | ||||||||||||||||||
| /// | ||||||||||||||||||
| /// (That distance between elements is sometimes known as "stride".) | ||||||||||||||||||
| /// | ||||||||||||||||||
| /// On arithmetic overflow, returns `LayoutError`. | ||||||||||||||||||
| /// | ||||||||||||||||||
| /// # Examples | ||||||||||||||||||
| /// | ||||||||||||||||||
| /// ``` | ||||||||||||||||||
| /// #![feature(alloc_layout_extra)] | ||||||||||||||||||
| /// use std::alloc::Layout; | ||||||||||||||||||
| /// | ||||||||||||||||||
| /// // All rust types have a size that's a multiple of their alignment. | ||||||||||||||||||
|
|
@@ -366,17 +362,32 @@ impl Layout { | |||||||||||||||||
| /// // But you can manually make layouts which don't meet that rule. | ||||||||||||||||||
| /// let padding_needed = Layout::from_size_align(6, 4).unwrap(); | ||||||||||||||||||
| /// let repeated = padding_needed.repeat(3).unwrap(); | ||||||||||||||||||
| /// assert_eq!(repeated, (Layout::from_size_align(24, 4).unwrap(), 8)); | ||||||||||||||||||
| /// assert_eq!(repeated, (Layout::from_size_align(22, 4).unwrap(), 8)); | ||||||||||||||||||
| /// | ||||||||||||||||||
| /// // Repeating an element zero times has zero size, but keeps the alignment (like `[T; 0]`) | ||||||||||||||||||
| /// let repeated = normal.repeat(0).unwrap(); | ||||||||||||||||||
| /// assert_eq!(repeated, (Layout::from_size_align(0, 4).unwrap(), 12)); | ||||||||||||||||||
| /// let repeated = padding_needed.repeat(0).unwrap(); | ||||||||||||||||||
| /// assert_eq!(repeated, (Layout::from_size_align(0, 4).unwrap(), 8)); | ||||||||||||||||||
| /// ``` | ||||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. While you're here and this is changing, can you please add a test for Maybe something like
Suggested change
|
||||||||||||||||||
| #[unstable(feature = "alloc_layout_extra", issue = "55724")] | ||||||||||||||||||
| #[stable(feature = "alloc_layout_extra", since = "CURRENT_RUSTC_VERSION")] | ||||||||||||||||||
| #[rustc_const_stable(feature = "alloc_layout_extra", since = "CURRENT_RUSTC_VERSION")] | ||||||||||||||||||
| #[inline] | ||||||||||||||||||
| pub const fn repeat(&self, n: usize) -> Result<(Self, usize), LayoutError> { | ||||||||||||||||||
| // FIXME(const-hack): the following could be way shorter with `?` | ||||||||||||||||||
| let padded = self.pad_to_align(); | ||||||||||||||||||
| if let Ok(repeated) = padded.repeat_packed(n) { | ||||||||||||||||||
| Ok((repeated, padded.size())) | ||||||||||||||||||
| let Ok(result) = (if let Some(k) = n.checked_sub(1) { | ||||||||||||||||||
| let Ok(repeated) = padded.repeat_packed(k) else { | ||||||||||||||||||
| return Err(LayoutError); | ||||||||||||||||||
| }; | ||||||||||||||||||
| repeated.extend_packed(*self) | ||||||||||||||||||
| } else { | ||||||||||||||||||
| Err(LayoutError) | ||||||||||||||||||
| } | ||||||||||||||||||
| debug_assert!(n == 0); | ||||||||||||||||||
| self.repeat_packed(0) | ||||||||||||||||||
| }) else { | ||||||||||||||||||
| return Err(LayoutError); | ||||||||||||||||||
| }; | ||||||||||||||||||
| Ok((result, padded.size())) | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| /// Creates a layout describing the record for `self` followed by | ||||||||||||||||||
|
|
@@ -456,7 +467,8 @@ impl Layout { | |||||||||||||||||
| /// aligned. | ||||||||||||||||||
| /// | ||||||||||||||||||
| /// On arithmetic overflow, returns `LayoutError`. | ||||||||||||||||||
| #[unstable(feature = "alloc_layout_extra", issue = "55724")] | ||||||||||||||||||
| #[stable(feature = "alloc_layout_extra", since = "CURRENT_RUSTC_VERSION")] | ||||||||||||||||||
| #[rustc_const_stable(feature = "alloc_layout_extra", since = "CURRENT_RUSTC_VERSION")] | ||||||||||||||||||
| #[inline] | ||||||||||||||||||
| pub const fn repeat_packed(&self, n: usize) -> Result<Self, LayoutError> { | ||||||||||||||||||
| if let Some(size) = self.size.checked_mul(n) { | ||||||||||||||||||
|
|
@@ -473,7 +485,8 @@ 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 = "CURRENT_RUSTC_VERSION")] | ||||||||||||||||||
| #[rustc_const_stable(feature = "alloc_layout_extra", since = "CURRENT_RUSTC_VERSION")] | ||||||||||||||||||
| #[inline] | ||||||||||||||||||
| pub const fn extend_packed(&self, next: Self) -> Result<Self, LayoutError> { | ||||||||||||||||||
| // SAFETY: each `size` is at most `isize::MAX == usize::MAX/2`, so the | ||||||||||||||||||
|
|
||||||||||||||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, no
Alignment::of_valorAlignment::of_val_raw. Huh, guess this is best then.