Skip to content

Commit 361f668

Browse files
committed
Use alloc_zeroed in {Rc,Arc}::new_zeroed
1 parent 119d2a1 commit 361f668

File tree

2 files changed

+48
-30
lines changed

2 files changed

+48
-30
lines changed

library/alloc/src/rc.rs

+24-15
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ use core::pin::Pin;
250250
use core::ptr::{self, NonNull};
251251
use core::slice::from_raw_parts_mut;
252252

253-
use crate::alloc::{box_free, handle_alloc_error, AllocRef, Global, Layout};
253+
use crate::alloc::{box_free, handle_alloc_error, AllocErr, AllocRef, Global, Layout};
254254
use crate::borrow::{Cow, ToOwned};
255255
use crate::string::String;
256256
use crate::vec::Vec;
@@ -352,9 +352,11 @@ impl<T> Rc<T> {
352352
#[unstable(feature = "new_uninit", issue = "63291")]
353353
pub fn new_uninit() -> Rc<mem::MaybeUninit<T>> {
354354
unsafe {
355-
Rc::from_ptr(Rc::allocate_for_layout(Layout::new::<T>(), |mem| {
356-
mem as *mut RcBox<mem::MaybeUninit<T>>
357-
}))
355+
Rc::from_ptr(Rc::allocate_for_layout(
356+
Layout::new::<T>(),
357+
|layout| Global.alloc(layout),
358+
|mem| mem as *mut RcBox<mem::MaybeUninit<T>>,
359+
))
358360
}
359361
}
360362

@@ -381,9 +383,11 @@ impl<T> Rc<T> {
381383
#[unstable(feature = "new_uninit", issue = "63291")]
382384
pub fn new_zeroed() -> Rc<mem::MaybeUninit<T>> {
383385
unsafe {
384-
let mut uninit = Self::new_uninit();
385-
ptr::write_bytes::<T>(Rc::get_mut_unchecked(&mut uninit).as_mut_ptr(), 0, 1);
386-
uninit
386+
Rc::from_ptr(Rc::allocate_for_layout(
387+
Layout::new::<T>(),
388+
|layout| Global.alloc_zeroed(layout),
389+
|mem| mem as *mut RcBox<mem::MaybeUninit<T>>,
390+
))
387391
}
388392
}
389393

@@ -919,6 +923,7 @@ impl<T: ?Sized> Rc<T> {
919923
/// and must return back a (potentially fat)-pointer for the `RcBox<T>`.
920924
unsafe fn allocate_for_layout(
921925
value_layout: Layout,
926+
allocate: impl FnOnce(Layout) -> Result<NonNull<[u8]>, AllocErr>,
922927
mem_to_rcbox: impl FnOnce(*mut u8) -> *mut RcBox<T>,
923928
) -> *mut RcBox<T> {
924929
// Calculate layout using the given value layout.
@@ -928,7 +933,7 @@ impl<T: ?Sized> Rc<T> {
928933
let layout = Layout::new::<RcBox<()>>().extend(value_layout).unwrap().0.pad_to_align();
929934

930935
// Allocate for the layout.
931-
let ptr = Global.alloc(layout).unwrap_or_else(|_| handle_alloc_error(layout));
936+
let ptr = allocate(layout).unwrap_or_else(|_| handle_alloc_error(layout));
932937

933938
// Initialize the RcBox
934939
let inner = mem_to_rcbox(ptr.as_non_null_ptr().as_ptr());
@@ -946,9 +951,11 @@ impl<T: ?Sized> Rc<T> {
946951
unsafe fn allocate_for_ptr(ptr: *const T) -> *mut RcBox<T> {
947952
// Allocate for the `RcBox<T>` using the given value.
948953
unsafe {
949-
Self::allocate_for_layout(Layout::for_value(&*ptr), |mem| {
950-
set_data_ptr(ptr as *mut T, mem) as *mut RcBox<T>
951-
})
954+
Self::allocate_for_layout(
955+
Layout::for_value(&*ptr),
956+
|layout| Global.alloc(layout),
957+
|mem| set_data_ptr(ptr as *mut T, mem) as *mut RcBox<T>,
958+
)
952959
}
953960
}
954961

@@ -979,9 +986,11 @@ impl<T> Rc<[T]> {
979986
/// Allocates an `RcBox<[T]>` with the given length.
980987
unsafe fn allocate_for_slice(len: usize) -> *mut RcBox<[T]> {
981988
unsafe {
982-
Self::allocate_for_layout(Layout::array::<T>(len).unwrap(), |mem| {
983-
ptr::slice_from_raw_parts_mut(mem as *mut T, len) as *mut RcBox<[T]>
984-
})
989+
Self::allocate_for_layout(
990+
Layout::array::<T>(len).unwrap(),
991+
|layout| Global.alloc(layout),
992+
|mem| ptr::slice_from_raw_parts_mut(mem as *mut T, len) as *mut RcBox<[T]>,
993+
)
985994
}
986995
}
987996
}
@@ -2090,7 +2099,7 @@ impl<T: ?Sized> AsRef<T> for Rc<T> {
20902099
#[stable(feature = "pin", since = "1.33.0")]
20912100
impl<T: ?Sized> Unpin for Rc<T> {}
20922101

2093-
/// Get the offset within an `ArcInner` for
2102+
/// Get the offset within an `RcBoRcBox` for
20942103
/// a payload of type described by a pointer.
20952104
///
20962105
/// # Safety

library/alloc/src/sync.rs

+24-15
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use core::slice::from_raw_parts_mut;
2323
use core::sync::atomic;
2424
use core::sync::atomic::Ordering::{Acquire, Relaxed, Release, SeqCst};
2525

26-
use crate::alloc::{box_free, handle_alloc_error, AllocRef, Global, Layout};
26+
use crate::alloc::{box_free, handle_alloc_error, AllocErr, AllocRef, Global, Layout};
2727
use crate::borrow::{Cow, ToOwned};
2828
use crate::boxed::Box;
2929
use crate::rc::is_dangling;
@@ -352,9 +352,11 @@ impl<T> Arc<T> {
352352
#[unstable(feature = "new_uninit", issue = "63291")]
353353
pub fn new_uninit() -> Arc<mem::MaybeUninit<T>> {
354354
unsafe {
355-
Arc::from_ptr(Arc::allocate_for_layout(Layout::new::<T>(), |mem| {
356-
mem as *mut ArcInner<mem::MaybeUninit<T>>
357-
}))
355+
Arc::from_ptr(Arc::allocate_for_layout(
356+
Layout::new::<T>(),
357+
|layout| Global.alloc(layout),
358+
|mem| mem as *mut ArcInner<mem::MaybeUninit<T>>,
359+
))
358360
}
359361
}
360362

@@ -381,9 +383,11 @@ impl<T> Arc<T> {
381383
#[unstable(feature = "new_uninit", issue = "63291")]
382384
pub fn new_zeroed() -> Arc<mem::MaybeUninit<T>> {
383385
unsafe {
384-
let mut uninit = Self::new_uninit();
385-
ptr::write_bytes::<T>(Arc::get_mut_unchecked(&mut uninit).as_mut_ptr(), 0, 1);
386-
uninit
386+
Arc::from_ptr(Arc::allocate_for_layout(
387+
Layout::new::<T>(),
388+
|layout| Global.alloc_zeroed(layout),
389+
|mem| mem as *mut ArcInner<mem::MaybeUninit<T>>,
390+
))
387391
}
388392
}
389393

@@ -437,7 +441,7 @@ impl<T> Arc<T> {
437441
}
438442

439443
impl<T> Arc<[T]> {
440-
/// Constructs a new reference-counted slice with uninitialized contents.
444+
/// Constructs a new atomically reference-counted slice with uninitialized contents.
441445
///
442446
/// # Examples
443447
///
@@ -875,6 +879,7 @@ impl<T: ?Sized> Arc<T> {
875879
/// and must return back a (potentially fat)-pointer for the `ArcInner<T>`.
876880
unsafe fn allocate_for_layout(
877881
value_layout: Layout,
882+
allocate: impl FnOnce(Layout) -> Result<NonNull<[u8]>, AllocErr>,
878883
mem_to_arcinner: impl FnOnce(*mut u8) -> *mut ArcInner<T>,
879884
) -> *mut ArcInner<T> {
880885
// Calculate layout using the given value layout.
@@ -883,7 +888,7 @@ impl<T: ?Sized> Arc<T> {
883888
// reference (see #54908).
884889
let layout = Layout::new::<ArcInner<()>>().extend(value_layout).unwrap().0.pad_to_align();
885890

886-
let ptr = Global.alloc(layout).unwrap_or_else(|_| handle_alloc_error(layout));
891+
let ptr = allocate(layout).unwrap_or_else(|_| handle_alloc_error(layout));
887892

888893
// Initialize the ArcInner
889894
let inner = mem_to_arcinner(ptr.as_non_null_ptr().as_ptr());
@@ -901,9 +906,11 @@ impl<T: ?Sized> Arc<T> {
901906
unsafe fn allocate_for_ptr(ptr: *const T) -> *mut ArcInner<T> {
902907
// Allocate for the `ArcInner<T>` using the given value.
903908
unsafe {
904-
Self::allocate_for_layout(Layout::for_value(&*ptr), |mem| {
905-
set_data_ptr(ptr as *mut T, mem) as *mut ArcInner<T>
906-
})
909+
Self::allocate_for_layout(
910+
Layout::for_value(&*ptr),
911+
|layout| Global.alloc(layout),
912+
|mem| set_data_ptr(ptr as *mut T, mem) as *mut ArcInner<T>,
913+
)
907914
}
908915
}
909916

@@ -934,9 +941,11 @@ impl<T> Arc<[T]> {
934941
/// Allocates an `ArcInner<[T]>` with the given length.
935942
unsafe fn allocate_for_slice(len: usize) -> *mut ArcInner<[T]> {
936943
unsafe {
937-
Self::allocate_for_layout(Layout::array::<T>(len).unwrap(), |mem| {
938-
ptr::slice_from_raw_parts_mut(mem as *mut T, len) as *mut ArcInner<[T]>
939-
})
944+
Self::allocate_for_layout(
945+
Layout::array::<T>(len).unwrap(),
946+
|layout| Global.alloc(layout),
947+
|mem| ptr::slice_from_raw_parts_mut(mem as *mut T, len) as *mut ArcInner<[T]>,
948+
)
940949
}
941950
}
942951
}

0 commit comments

Comments
 (0)