Skip to content

Commit 8f3cb7d

Browse files
committed
Make [A]Rc::allocate_for_layout() use try_allocate_for_layout()
1 parent 589aa8e commit 8f3cb7d

File tree

2 files changed

+5
-23
lines changed

2 files changed

+5
-23
lines changed

library/alloc/src/rc.rs

+3-12
Original file line numberDiff line numberDiff line change
@@ -1107,20 +1107,10 @@ impl<T: ?Sized> Rc<T> {
11071107
// `&*(ptr as *const RcBox<T>)`, but this created a misaligned
11081108
// reference (see #54908).
11091109
let layout = Layout::new::<RcBox<()>>().extend(value_layout).unwrap().0.pad_to_align();
1110-
1111-
// Allocate for the layout.
1112-
let ptr = allocate(layout).unwrap_or_else(|_| handle_alloc_error(layout));
1113-
1114-
// Initialize the RcBox
1115-
let inner = mem_to_rcbox(ptr.as_non_null_ptr().as_ptr());
11161110
unsafe {
1117-
debug_assert_eq!(Layout::for_value(&*inner), layout);
1118-
1119-
ptr::write(&mut (*inner).strong, Cell::new(1));
1120-
ptr::write(&mut (*inner).weak, Cell::new(1));
1111+
Rc::try_allocate_for_layout(value_layout, allocate, mem_to_rcbox)
1112+
.unwrap_or_else(|_| handle_alloc_error(layout))
11211113
}
1122-
1123-
inner
11241114
}
11251115

11261116
/// Allocates an `RcBox<T>` with sufficient space for
@@ -1129,6 +1119,7 @@ impl<T: ?Sized> Rc<T> {
11291119
///
11301120
/// The function `mem_to_rcbox` is called with the data pointer
11311121
/// and must return back a (potentially fat)-pointer for the `RcBox<T>`.
1122+
#[inline]
11321123
unsafe fn try_allocate_for_layout(
11331124
value_layout: Layout,
11341125
allocate: impl FnOnce(Layout) -> Result<NonNull<[u8]>, AllocError>,

library/alloc/src/sync.rs

+2-11
Original file line numberDiff line numberDiff line change
@@ -1085,19 +1085,10 @@ impl<T: ?Sized> Arc<T> {
10851085
// `&*(ptr as *const ArcInner<T>)`, but this created a misaligned
10861086
// reference (see #54908).
10871087
let layout = Layout::new::<ArcInner<()>>().extend(value_layout).unwrap().0.pad_to_align();
1088-
1089-
let ptr = allocate(layout).unwrap_or_else(|_| handle_alloc_error(layout));
1090-
1091-
// Initialize the ArcInner
1092-
let inner = mem_to_arcinner(ptr.as_non_null_ptr().as_ptr());
1093-
debug_assert_eq!(unsafe { Layout::for_value(&*inner) }, layout);
1094-
10951088
unsafe {
1096-
ptr::write(&mut (*inner).strong, atomic::AtomicUsize::new(1));
1097-
ptr::write(&mut (*inner).weak, atomic::AtomicUsize::new(1));
1089+
Arc::try_allocate_for_layout(value_layout, allocate, mem_to_arcinner)
1090+
.unwrap_or_else(|_| handle_alloc_error(layout))
10981091
}
1099-
1100-
inner
11011092
}
11021093

11031094
/// Allocates an `ArcInner<T>` with sufficient space for

0 commit comments

Comments
 (0)