Skip to content
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

Simplify [a]rc code a little #99113

Merged
merged 1 commit into from
Jul 15, 2022
Merged
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
1 change: 1 addition & 0 deletions library/alloc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@
#![cfg_attr(test, feature(new_uninit))]
#![feature(nonnull_slice_from_raw_parts)]
#![feature(pattern)]
#![feature(pointer_byte_offsets)]
#![feature(ptr_internals)]
#![feature(ptr_metadata)]
#![feature(ptr_sub_ptr)]
Expand Down
11 changes: 5 additions & 6 deletions library/alloc/src/rc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -907,8 +907,7 @@ impl<T: ?Sized> Rc<T> {
let offset = unsafe { data_offset(ptr) };

// Reverse the offset to find the original RcBox.
let rc_ptr =
unsafe { (ptr as *mut u8).offset(-offset).with_metadata_of(ptr as *mut RcBox<T>) };
let rc_ptr = unsafe { ptr.byte_sub(offset) as *mut RcBox<T> };

unsafe { Self::from_ptr(rc_ptr) }
}
Expand Down Expand Up @@ -2331,7 +2330,7 @@ impl<T: ?Sized> Weak<T> {
let offset = unsafe { data_offset(ptr) };
// Thus, we reverse the offset to get the whole RcBox.
// SAFETY: the pointer originated from a Weak, so this offset is safe.
unsafe { (ptr as *mut u8).offset(-offset).with_metadata_of(ptr as *mut RcBox<T>) }
unsafe { ptr.byte_sub(offset) as *mut RcBox<T> }
};

// SAFETY: we now have recovered the original Weak pointer, so can create the Weak.
Expand Down Expand Up @@ -2684,7 +2683,7 @@ impl<T: ?Sized> Unpin for Rc<T> {}
///
/// The pointer must point to (and have valid metadata for) a previously
/// valid instance of T, but the T is allowed to be dropped.
unsafe fn data_offset<T: ?Sized>(ptr: *const T) -> isize {
unsafe fn data_offset<T: ?Sized>(ptr: *const T) -> usize {
// Align the unsized value to the end of the RcBox.
// Because RcBox is repr(C), it will always be the last field in memory.
// SAFETY: since the only unsized types possible are slices, trait objects,
Expand All @@ -2695,7 +2694,7 @@ unsafe fn data_offset<T: ?Sized>(ptr: *const T) -> isize {
}

#[inline]
fn data_offset_align(align: usize) -> isize {
fn data_offset_align(align: usize) -> usize {
let layout = Layout::new::<RcBox<()>>();
(layout.size() + layout.padding_needed_for(align)) as isize
layout.size() + layout.padding_needed_for(align)
}
11 changes: 5 additions & 6 deletions library/alloc/src/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -908,8 +908,7 @@ impl<T: ?Sized> Arc<T> {
let offset = data_offset(ptr);

// Reverse the offset to find the original ArcInner.
let arc_ptr =
(ptr as *mut u8).offset(-offset).with_metadata_of(ptr as *mut ArcInner<T>);
let arc_ptr = ptr.byte_sub(offset) as *mut ArcInner<T>;

Self::from_ptr(arc_ptr)
}
Expand Down Expand Up @@ -1942,7 +1941,7 @@ impl<T: ?Sized> Weak<T> {
let offset = unsafe { data_offset(ptr) };
// Thus, we reverse the offset to get the whole RcBox.
// SAFETY: the pointer originated from a Weak, so this offset is safe.
unsafe { (ptr as *mut u8).offset(-offset).with_metadata_of(ptr as *mut ArcInner<T>) }
unsafe { ptr.byte_sub(offset) as *mut ArcInner<T> }
};

// SAFETY: we now have recovered the original Weak pointer, so can create the Weak.
Expand Down Expand Up @@ -2749,7 +2748,7 @@ impl<T: ?Sized> Unpin for Arc<T> {}
///
/// The pointer must point to (and have valid metadata for) a previously
/// valid instance of T, but the T is allowed to be dropped.
unsafe fn data_offset<T: ?Sized>(ptr: *const T) -> isize {
unsafe fn data_offset<T: ?Sized>(ptr: *const T) -> usize {
// Align the unsized value to the end of the ArcInner.
// Because RcBox is repr(C), it will always be the last field in memory.
// SAFETY: since the only unsized types possible are slices, trait objects,
Expand All @@ -2760,7 +2759,7 @@ unsafe fn data_offset<T: ?Sized>(ptr: *const T) -> isize {
}

#[inline]
fn data_offset_align(align: usize) -> isize {
fn data_offset_align(align: usize) -> usize {
let layout = Layout::new::<ArcInner<()>>();
(layout.size() + layout.padding_needed_for(align)) as isize
layout.size() + layout.padding_needed_for(align)
}