Fix duplicate arcinner_layout_for_value_layout calls

This commit is contained in:
marc0246 2023-05-16 08:53:05 +02:00
parent 8006510ab0
commit 2a466466c7
No known key found for this signature in database
GPG Key ID: 689CB88C1090C684
2 changed files with 47 additions and 6 deletions

View File

@ -502,6 +502,7 @@ pub fn new_cyclic<F>(data_fn: F) -> Arc<T>
/// assert_eq!(*five, 5)
/// ```
#[cfg(not(no_global_oom_handling))]
#[inline]
#[unstable(feature = "new_uninit", issue = "63291")]
#[must_use]
pub fn new_uninit() -> Arc<mem::MaybeUninit<T>> {
@ -535,6 +536,7 @@ pub fn new_uninit() -> Arc<mem::MaybeUninit<T>> {
///
/// [zeroed]: mem::MaybeUninit::zeroed
#[cfg(not(no_global_oom_handling))]
#[inline]
#[unstable(feature = "new_uninit", issue = "63291")]
#[must_use]
pub fn new_zeroed() -> Arc<mem::MaybeUninit<T>> {
@ -844,6 +846,7 @@ impl<T> Arc<[T]> {
/// assert_eq!(*values, [1, 2, 3])
/// ```
#[cfg(not(no_global_oom_handling))]
#[inline]
#[unstable(feature = "new_uninit", issue = "63291")]
#[must_use]
pub fn new_uninit_slice(len: usize) -> Arc<[mem::MaybeUninit<T>]> {
@ -871,6 +874,7 @@ pub fn new_uninit_slice(len: usize) -> Arc<[mem::MaybeUninit<T>]> {
///
/// [zeroed]: mem::MaybeUninit::zeroed
#[cfg(not(no_global_oom_handling))]
#[inline]
#[unstable(feature = "new_uninit", issue = "63291")]
#[must_use]
pub fn new_zeroed_slice(len: usize) -> Arc<[mem::MaybeUninit<T>]> {
@ -1300,10 +1304,10 @@ unsafe fn allocate_for_layout(
mem_to_arcinner: impl FnOnce(*mut u8) -> *mut ArcInner<T>,
) -> *mut ArcInner<T> {
let layout = arcinner_layout_for_value_layout(value_layout);
unsafe {
Arc::try_allocate_for_layout(value_layout, allocate, mem_to_arcinner)
.unwrap_or_else(|_| handle_alloc_error(layout))
}
let ptr = allocate(layout).unwrap_or_else(|_| handle_alloc_error(layout));
unsafe { Self::initialize_arcinner(ptr, layout, mem_to_arcinner) }
}
/// Allocates an `ArcInner<T>` with sufficient space for
@ -1321,7 +1325,16 @@ unsafe fn try_allocate_for_layout(
let ptr = allocate(layout)?;
// Initialize the ArcInner
let inner = unsafe { Self::initialize_arcinner(ptr, layout, mem_to_arcinner) };
Ok(inner)
}
unsafe fn initialize_arcinner(
ptr: NonNull<[u8]>,
layout: Layout,
mem_to_arcinner: impl FnOnce(*mut u8) -> *mut ArcInner<T>,
) -> *mut ArcInner<T> {
let inner = mem_to_arcinner(ptr.as_non_null_ptr().as_ptr());
debug_assert_eq!(unsafe { Layout::for_value(&*inner) }, layout);
@ -1330,7 +1343,7 @@ unsafe fn try_allocate_for_layout(
ptr::write(&mut (*inner).weak, atomic::AtomicUsize::new(1));
}
Ok(inner)
inner
}
/// Allocates an `ArcInner<T>` with sufficient space for an unsized inner value.

View File

@ -0,0 +1,28 @@
// compile-flags: -O
#![crate_type = "lib"]
#![feature(get_mut_unchecked, new_uninit)]
use std::sync::Arc;
// CHECK-LABEL: @new_uninit
#[no_mangle]
pub fn new_uninit(x: u64) -> Arc<[u64; 1000]> {
// CHECK: call alloc::sync::arcinner_layout_for_value_layout
// CHECK-NOT: call alloc::sync::arcinner_layout_for_value_layout
let mut arc = Arc::new_uninit();
unsafe { Arc::get_mut_unchecked(&mut arc) }.write([x; 1000]);
unsafe { arc.assume_init() }
}
// CHECK-LABEL: @new_uninit_slice
#[no_mangle]
pub fn new_uninit_slice(x: u64) -> Arc<[u64]> {
// CHECK: call alloc::sync::arcinner_layout_for_value_layout
// CHECK-NOT: call alloc::sync::arcinner_layout_for_value_layout
let mut arc = Arc::new_uninit_slice(1000);
for elem in unsafe { Arc::get_mut_unchecked(&mut arc) } {
elem.write(x);
}
unsafe { arc.assume_init() }
}