Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
EFanZh committed Dec 29, 2024
1 parent 9a531fc commit 6e1cefc
Showing 1 changed file with 35 additions and 46 deletions.
81 changes: 35 additions & 46 deletions library/alloc/src/raw_rc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ where
R: RcOps,
{
fn drop(&mut self) {
unsafe { self.weak.drop_unchecked::<R>() };
unsafe { self.weak.assume_non_dangling_drop::<R>() };
}
}

Expand Down Expand Up @@ -343,10 +343,6 @@ where
self.ptr
}

unsafe fn as_ref_unchecked(&self) -> &T {
unsafe { self.ptr.as_ref() }
}

unsafe fn assume_init_drop<R>(&mut self)
where
A: Allocator,
Expand Down Expand Up @@ -383,7 +379,7 @@ where
{
unsafe {
if !self.is_dangling() {
R::increment_ref_count(self.weak_count_unchecked());
R::increment_ref_count(self.assume_non_dangling_weak_count());
}

self.clone_without_increment_weak_count()
Expand Down Expand Up @@ -417,26 +413,22 @@ where
R: RcOps,
{
if !self.is_dangling() {
unsafe { self.drop_unchecked::<R>() };
unsafe { self.assume_non_dangling_drop::<R>() };
}
}

unsafe fn drop_unchecked<R>(&mut self)
unsafe fn assume_non_dangling_drop<R>(&mut self)
where
A: Allocator,
R: RcOps,
{
unsafe {
if R::decrement_ref_count(self.weak_count_unchecked()) {
if R::decrement_ref_count(self.assume_non_dangling_weak_count()) {
self.deallocate();
}
};
}

unsafe fn get_mut_unchecked(&mut self) -> &mut T {
unsafe { self.ptr.as_mut() }
}

pub fn into_raw(self) -> NonNull<T> {
self.ptr
}
Expand All @@ -459,27 +451,27 @@ where

#[cfg(not(no_sync))]
pub fn ref_counts(&self) -> Option<&RefCounts> {
(!self.is_dangling()).then(|| unsafe { self.ref_counts_unchecked() })
(!self.is_dangling()).then(|| unsafe { self.assume_non_dangling_ref_counts() })
}

#[cfg(not(no_sync))]
unsafe fn ref_counts_unchecked(&self) -> &RefCounts {
unsafe fn assume_non_dangling_ref_counts(&self) -> &RefCounts {
unsafe { ref_counts_ptr_from_value_ptr(self.ptr.cast()).as_ref() }
}

pub fn strong_count(&self) -> Option<&UnsafeCell<usize>> {
(!self.is_dangling()).then(|| unsafe { self.strong_count_unchecked() })
(!self.is_dangling()).then(|| unsafe { self.assume_non_dangling_strong_count() })
}

unsafe fn strong_count_unchecked(&self) -> &UnsafeCell<usize> {
unsafe fn assume_non_dangling_strong_count(&self) -> &UnsafeCell<usize> {
unsafe { strong_count_ptr_from_value_ptr(self.ptr.cast()).as_ref() }
}

pub fn weak_count(&self) -> Option<&UnsafeCell<usize>> {
(!self.is_dangling()).then(|| unsafe { self.weak_count_unchecked() })
(!self.is_dangling()).then(|| unsafe { self.assume_non_dangling_weak_count() })
}

unsafe fn weak_count_unchecked(&self) -> &UnsafeCell<usize> {
unsafe fn assume_non_dangling_weak_count(&self) -> &UnsafeCell<usize> {
unsafe { weak_count_ptr_from_value_ptr(self.ptr.cast()).as_ref() }
}

Expand All @@ -488,16 +480,16 @@ where
A: Clone,
R: RcOps,
{
if self.is_dangling() { None } else { unsafe { self.upgrade_unchecked::<R>() } }
if self.is_dangling() { None } else { unsafe { self.assume_non_dangling_upgrade::<R>() } }
}

unsafe fn upgrade_unchecked<R>(&self) -> Option<RawRc<T, A>>
unsafe fn assume_non_dangling_upgrade<R>(&self) -> Option<RawRc<T, A>>
where
A: Clone,
R: RcOps,
{
unsafe {
R::upgrade(self.strong_count_unchecked())
R::upgrade(self.assume_non_dangling_strong_count())
.then(|| RawRc::from_raw_parts(self.ptr, self.alloc.clone()))
}
}
Expand Down Expand Up @@ -600,7 +592,7 @@ impl<T, A> RawWeak<T, A> {
unsafe {
let result = self.ptr.read();

self.drop_unchecked::<R>();
self.assume_non_dangling_drop::<R>();

result
}
Expand Down Expand Up @@ -798,7 +790,7 @@ where
A: Allocator,
R: RcOps,
{
unsafe { self.weak.assume_init_drop::<R>() }
unsafe { self.weak.assume_init_drop::<R>() };
}

pub unsafe fn get_mut<R>(&mut self) -> Option<&mut T>
Expand All @@ -811,7 +803,7 @@ where
}

pub unsafe fn get_mut_unchecked(&mut self) -> &mut T {
unsafe { self.weak.get_mut_unchecked() }
unsafe { self.weak.as_ptr().as_mut() }
}

pub fn into_raw(self) -> NonNull<T> {
Expand Down Expand Up @@ -877,7 +869,7 @@ where
unsafe {
let new_ptr = allocate_for_rc_with_value::<T, A, 1>(ptr.as_ref(), alloc);

RawWeak::from_raw_parts(ptr, &*alloc).drop_unchecked::<R>();
RawWeak::from_raw_parts(ptr, &*alloc).assume_non_dangling_drop::<R>();

*ptr_ref = new_ptr;
}
Expand Down Expand Up @@ -911,15 +903,15 @@ where

#[cfg(all(not(no_global_oom_handling), not(no_sync)))]
pub fn ref_counts(&self) -> &RefCounts {
unsafe { self.weak.ref_counts_unchecked() }
unsafe { self.weak.assume_non_dangling_ref_counts() }
}

pub fn strong_count(&self) -> &UnsafeCell<usize> {
unsafe { self.weak.strong_count_unchecked() }
unsafe { self.weak.assume_non_dangling_strong_count() }
}

pub fn weak_count(&self) -> &UnsafeCell<usize> {
unsafe { self.weak.weak_count_unchecked() }
unsafe { self.weak.assume_non_dangling_weak_count() }
}
}

Expand Down Expand Up @@ -996,7 +988,7 @@ impl<T, A> RawRc<T, A> {
R: RcOps,
{
unsafe {
R::decrement_ref_count(&self.strong_count())
R::decrement_ref_count(self.strong_count())
.then(|| self.weak.assume_init_into_inner::<R>())
}
}
Expand Down Expand Up @@ -1134,13 +1126,10 @@ impl<T, A> RawRc<[T], A> {
{
fn drop(&mut self) {
unsafe {
let length = self.tail.sub_ptr(self.uninit_rc.uninit_rc.as_ptr().cast());
let head = self.uninit_rc.uninit_rc.as_ptr().cast();
let length = self.tail.sub_ptr(head);

NonNull::<[T]>::slice_from_raw_parts(
self.uninit_rc.uninit_rc.as_ptr().cast(),
length,
)
.drop_in_place();
NonNull::<[T]>::slice_from_raw_parts(head, length).drop_in_place();
}
}
}
Expand Down Expand Up @@ -1241,7 +1230,7 @@ where
T: ?Sized,
{
fn as_ref(&self) -> &T {
unsafe { self.weak.as_ref_unchecked() }
unsafe { self.weak.as_ptr().as_ref() }
}
}

Expand Down Expand Up @@ -1308,7 +1297,7 @@ where
T: Default,
{
fn spec_default() -> Self {
unsafe fn spec_default_impl<T, const N: usize>() -> RawRc<T, Global>
fn spec_default_impl<T, const N: usize>() -> RawRc<T, Global>
where
T: Default,
{
Expand Down Expand Up @@ -1354,13 +1343,13 @@ where
($($value:literal,)*) => {
match RefCounts::LAYOUT.padding_needed_for(T::LAYOUT.align()) / RefCounts::LAYOUT.size() {
$($value => spec_default_impl::<T, $value>,)*
_ => panic!("invalid padding"),
_ => panic!("internal error: unsupported RC layout"),
}
};
}

#[cfg(target_pointer_width = "16")]
let selected_impl: unsafe fn() -> Self = const {
let selected_impl: fn() -> Self = const {
select_impl![
0x0000, // (1 << 0) - 1
0x0001, // (1 << 1) - 1
Expand All @@ -1380,7 +1369,7 @@ where
};

#[cfg(target_pointer_width = "32")]
let selected_impl: unsafe fn() -> Self = const {
let selected_impl: fn() -> Self = const {
select_impl![
0x00000000, // (1 << 0) - 1
0x00000001, // (1 << 1) - 1
Expand Down Expand Up @@ -1415,7 +1404,7 @@ where
};

#[cfg(target_pointer_width = "64")]
let selected_impl: unsafe fn() -> Self = const {
let selected_impl: fn() -> Self = const {
select_impl![
0x0000000000000000, // (1 << 0) - 1
0x0000000000000001, // (1 << 1) - 1
Expand Down Expand Up @@ -1480,7 +1469,7 @@ where
]
};

unsafe { selected_impl() }
selected_impl()
}
}

Expand Down Expand Up @@ -1860,7 +1849,7 @@ where
R: RcOps,
{
unsafe {
R::unlock_strong_count(self.weak.strong_count_unchecked());
R::unlock_strong_count(self.weak.assume_non_dangling_strong_count());

RawRc::from_weak(self.weak)
}
Expand Down Expand Up @@ -1897,7 +1886,7 @@ where
T: ?Sized,
{
fn as_ref(&self) -> &T {
unsafe { self.weak.as_ref_unchecked() }
unsafe { self.weak.as_ptr().as_ref() }
}
}

Expand All @@ -1906,7 +1895,7 @@ where
T: ?Sized,
{
fn as_mut(&mut self) -> &mut T {
unsafe { self.weak.get_mut_unchecked() }
unsafe { self.weak.as_ptr().as_mut() }
}
}

Expand Down

0 comments on commit 6e1cefc

Please sign in to comment.