-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Inline mem::size_of & mem::align_of #80631
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
Closed
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,11 @@ | ||
use crate::cmp; | ||
use crate::fmt; | ||
use crate::mem; | ||
use crate::intrinsics::{ | ||
min_align_of as align_of, min_align_of_val as align_of_val, size_of, size_of_val, | ||
}; | ||
use crate::num::NonZeroUsize; | ||
use crate::ptr::NonNull; | ||
|
||
// While this function is used in one place and its implementation | ||
// could be inlined, the previous attempts to do so made rustc | ||
// slower: | ||
// | ||
// * https://github.com/rust-lang/rust/pull/72189 | ||
// * https://github.com/rust-lang/rust/pull/79827 | ||
const fn size_align<T>() -> (usize, usize) { | ||
(mem::size_of::<T>(), mem::align_of::<T>()) | ||
} | ||
|
||
/// Layout of a block of memory. | ||
/// | ||
/// An instance of `Layout` describes a particular layout of memory. | ||
|
@@ -121,7 +113,8 @@ impl Layout { | |
#[rustc_const_stable(feature = "alloc_layout_const_new", since = "1.42.0")] | ||
#[inline] | ||
pub const fn new<T>() -> Self { | ||
let (size, align) = size_align::<T>(); | ||
let size = size_of::<T>(); | ||
let align = align_of::<T>(); | ||
// SAFETY: the align is guaranteed by Rust to be a power of two and | ||
// the size+align combo is guaranteed to fit in our address space. As a | ||
// result use the unchecked constructor here to avoid inserting code | ||
|
@@ -135,7 +128,8 @@ impl Layout { | |
#[stable(feature = "alloc_layout", since = "1.28.0")] | ||
#[inline] | ||
pub fn for_value<T: ?Sized>(t: &T) -> Self { | ||
let (size, align) = (mem::size_of_val(t), mem::align_of_val(t)); | ||
let size = size_of_val(t); | ||
let align = align_of_val(t); | ||
debug_assert!(Layout::from_size_align(size, align).is_ok()); | ||
// SAFETY: see rationale in `new` for why this is using the unsafe variant | ||
unsafe { Layout::from_size_align_unchecked(size, align) } | ||
|
@@ -170,7 +164,8 @@ impl Layout { | |
#[unstable(feature = "layout_for_ptr", issue = "69835")] | ||
pub unsafe fn for_value_raw<T: ?Sized>(t: *const T) -> Self { | ||
// SAFETY: we pass along the prerequisites of these functions to the caller | ||
let (size, align) = unsafe { (mem::size_of_val_raw(t), mem::align_of_val_raw(t)) }; | ||
let size = size_of_val(t); | ||
let align = align_of_val(t); | ||
Comment on lines
166
to
+168
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. There is inconsistency in safety of size_of_val_raw function and intrinsic used to implement it. |
||
debug_assert!(Layout::from_size_align(size, align).is_ok()); | ||
// SAFETY: see rationale in `new` for why this is using the unsafe variant | ||
unsafe { Layout::from_size_align_unchecked(size, align) } | ||
|
@@ -393,7 +388,7 @@ impl Layout { | |
#[inline] | ||
pub fn array<T>(n: usize) -> Result<Self, LayoutError> { | ||
let (layout, offset) = Layout::new::<T>().repeat(n)?; | ||
debug_assert_eq!(offset, mem::size_of::<T>()); | ||
debug_assert_eq!(offset, size_of::<T>()); | ||
Ok(layout.pad_to_align()) | ||
} | ||
} | ||
|
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.
This could be updated to use
usize::BITS