Skip to content

Commit

Permalink
Reuse more internal Rc and Arc methods
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonSapin committed Aug 16, 2019
1 parent ae1e201 commit 810dfd7
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 70 deletions.
42 changes: 7 additions & 35 deletions src/liballoc/rc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,17 +350,11 @@ impl<T> Rc<T> {
/// ```
#[unstable(feature = "new_uninit", issue = "63291")]
pub fn new_uninit() -> Rc<mem::MaybeUninit<T>> {
let layout = Layout::new::<RcBox<mem::MaybeUninit<T>>>();
unsafe {
let mut ptr = Global.alloc(layout)
.unwrap_or_else(|_| handle_alloc_error(layout))
.cast::<RcBox<mem::MaybeUninit<T>>>();
ptr::write(&mut ptr.as_mut().strong, Cell::new(1));
ptr::write(&mut ptr.as_mut().weak, Cell::new(1));
Rc {
ptr,
phantom: PhantomData,
}
Rc::from_ptr(Rc::allocate_for_unsized(
Layout::new::<T>(),
|mem| mem as *mut RcBox<mem::MaybeUninit<T>>,
))
}
}

Expand Down Expand Up @@ -440,24 +434,8 @@ impl<T> Rc<[T]> {
/// ```
#[unstable(feature = "new_uninit", issue = "63291")]
pub fn new_uninit_slice(len: usize) -> Rc<[mem::MaybeUninit<T>]> {
let data_layout = Layout::array::<mem::MaybeUninit<T>>(len).unwrap();
// This relies on `value` being the last field of `RcBox` in memory,
// so that the layout of `RcBox<T>` is the same as that of `RcBox<()>` followed by `T`.
let (layout, offset) = Layout::new::<RcBox<()>>().extend(data_layout).unwrap();
unsafe {
let allocated_ptr = Global.alloc(layout)
.unwrap_or_else(|_| handle_alloc_error(layout))
.as_ptr();
let data_ptr = allocated_ptr.add(offset) as *mut mem::MaybeUninit<T>;
let slice: *mut [mem::MaybeUninit<T>] = from_raw_parts_mut(data_ptr, len);
let wide_ptr = slice as *mut RcBox<[mem::MaybeUninit<T>]>;
let wide_ptr = set_data_ptr(wide_ptr, allocated_ptr);
ptr::write(&mut (*wide_ptr).strong, Cell::new(1));
ptr::write(&mut (*wide_ptr).weak, Cell::new(1));
Rc {
ptr: NonNull::new_unchecked(wide_ptr),
phantom: PhantomData,
}
Rc::from_ptr(Rc::allocate_for_slice(len))
}
}
}
Expand Down Expand Up @@ -497,10 +475,7 @@ impl<T> Rc<mem::MaybeUninit<T>> {
#[unstable(feature = "new_uninit", issue = "63291")]
#[inline]
pub unsafe fn assume_init(self) -> Rc<T> {
Rc {
ptr: mem::ManuallyDrop::new(self).ptr.cast(),
phantom: PhantomData,
}
Rc::from_inner(mem::ManuallyDrop::new(self).ptr.cast())
}
}

Expand Down Expand Up @@ -541,10 +516,7 @@ impl<T> Rc<[mem::MaybeUninit<T>]> {
#[unstable(feature = "new_uninit", issue = "63291")]
#[inline]
pub unsafe fn assume_init(self) -> Rc<[T]> {
Rc {
ptr: NonNull::new_unchecked(mem::ManuallyDrop::new(self).ptr.as_ptr() as _),
phantom: PhantomData,
}
Rc::from_ptr(mem::ManuallyDrop::new(self).ptr.as_ptr() as _)
}
}

Expand Down
42 changes: 7 additions & 35 deletions src/liballoc/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -334,17 +334,11 @@ impl<T> Arc<T> {
/// ```
#[unstable(feature = "new_uninit", issue = "63291")]
pub fn new_uninit() -> Arc<mem::MaybeUninit<T>> {
let layout = Layout::new::<ArcInner<mem::MaybeUninit<T>>>();
unsafe {
let mut ptr = Global.alloc(layout)
.unwrap_or_else(|_| handle_alloc_error(layout))
.cast::<ArcInner<mem::MaybeUninit<T>>>();
ptr::write(&mut ptr.as_mut().strong, atomic::AtomicUsize::new(1));
ptr::write(&mut ptr.as_mut().weak, atomic::AtomicUsize::new(1));
Arc {
ptr,
phantom: PhantomData,
}
Arc::from_ptr(Arc::allocate_for_unsized(
Layout::new::<T>(),
|mem| mem as *mut ArcInner<mem::MaybeUninit<T>>,
))
}
}

Expand Down Expand Up @@ -424,24 +418,8 @@ impl<T> Arc<[T]> {
/// ```
#[unstable(feature = "new_uninit", issue = "63291")]
pub fn new_uninit_slice(len: usize) -> Arc<[mem::MaybeUninit<T>]> {
let data_layout = Layout::array::<mem::MaybeUninit<T>>(len).unwrap();
// This relies on `value` being the last field of `RcBox` in memory,
// so that the layout of `RcBox<T>` is the same as that of `RcBox<()>` followed by `T`.
let (layout, offset) = Layout::new::<ArcInner<()>>().extend(data_layout).unwrap();
unsafe {
let allocated_ptr = Global.alloc(layout)
.unwrap_or_else(|_| handle_alloc_error(layout))
.as_ptr();
let data_ptr = allocated_ptr.add(offset) as *mut mem::MaybeUninit<T>;
let slice: *mut [mem::MaybeUninit<T>] = from_raw_parts_mut(data_ptr, len);
let wide_ptr = slice as *mut ArcInner<[mem::MaybeUninit<T>]>;
let wide_ptr = set_data_ptr(wide_ptr, allocated_ptr);
ptr::write(&mut (*wide_ptr).strong, atomic::AtomicUsize::new(1));
ptr::write(&mut (*wide_ptr).weak, atomic::AtomicUsize::new(1));
Arc {
ptr: NonNull::new_unchecked(wide_ptr),
phantom: PhantomData,
}
Arc::from_ptr(Arc::allocate_for_slice(len))
}
}
}
Expand Down Expand Up @@ -481,10 +459,7 @@ impl<T> Arc<mem::MaybeUninit<T>> {
#[unstable(feature = "new_uninit", issue = "63291")]
#[inline]
pub unsafe fn assume_init(self) -> Arc<T> {
Arc {
ptr: mem::ManuallyDrop::new(self).ptr.cast(),
phantom: PhantomData,
}
Arc::from_inner(mem::ManuallyDrop::new(self).ptr.cast())
}
}

Expand Down Expand Up @@ -525,10 +500,7 @@ impl<T> Arc<[mem::MaybeUninit<T>]> {
#[unstable(feature = "new_uninit", issue = "63291")]
#[inline]
pub unsafe fn assume_init(self) -> Arc<[T]> {
Arc {
ptr: NonNull::new_unchecked(mem::ManuallyDrop::new(self).ptr.as_ptr() as _),
phantom: PhantomData,
}
Arc::from_ptr(mem::ManuallyDrop::new(self).ptr.as_ptr() as _)
}
}

Expand Down

0 comments on commit 810dfd7

Please sign in to comment.