diff options
| author | Mark Rousskov <mark.simulacrum@gmail.com> | 2019-12-22 17:42:04 -0500 |
|---|---|---|
| committer | Mark Rousskov <mark.simulacrum@gmail.com> | 2019-12-22 17:42:47 -0500 |
| commit | a06baa56b95674fc626b3c3fd680d6a65357fe60 (patch) | |
| tree | cd9d867c2ca3cff5c1d6b3bd73377c44649fb075 /src/liballoc/sync.rs | |
| parent | 8eb7c58dbb7b32701af113bc58722d0d1fefb1eb (diff) | |
| download | rust-a06baa56b95674fc626b3c3fd680d6a65357fe60.tar.gz rust-a06baa56b95674fc626b3c3fd680d6a65357fe60.zip | |
Format the world
Diffstat (limited to 'src/liballoc/sync.rs')
| -rw-r--r-- | src/liballoc/sync.rs | 151 |
1 files changed, 56 insertions, 95 deletions
diff --git a/src/liballoc/sync.rs b/src/liballoc/sync.rs index 3343384754f..dc53ad28407 100644 --- a/src/liballoc/sync.rs +++ b/src/liballoc/sync.rs @@ -8,24 +8,24 @@ use core::any::Any; use core::array::LengthAtMost32; -use core::sync::atomic; -use core::sync::atomic::Ordering::{Acquire, Relaxed, Release, SeqCst}; use core::borrow; -use core::fmt; use core::cmp::Ordering; -use core::iter; +use core::convert::{From, TryFrom}; +use core::fmt; +use core::hash::{Hash, Hasher}; use core::intrinsics::abort; +use core::iter; +use core::marker::{PhantomData, Unpin, Unsize}; use core::mem::{self, align_of, align_of_val, size_of_val}; -use core::ops::{Deref, Receiver, CoerceUnsized, DispatchFromDyn}; +use core::ops::{CoerceUnsized, Deref, DispatchFromDyn, Receiver}; use core::pin::Pin; use core::ptr::{self, NonNull}; -use core::marker::{Unpin, Unsize, PhantomData}; -use core::hash::{Hash, Hasher}; -use core::{isize, usize}; -use core::convert::{From, TryFrom}; use core::slice::{self, from_raw_parts_mut}; +use core::sync::atomic; +use core::sync::atomic::Ordering::{Acquire, Relaxed, Release, SeqCst}; +use core::{isize, usize}; -use crate::alloc::{Global, Alloc, Layout, box_free, handle_alloc_error}; +use crate::alloc::{box_free, handle_alloc_error, Alloc, Global, Layout}; use crate::boxed::Box; use crate::rc::is_dangling; use crate::string::String; @@ -211,10 +211,7 @@ impl<T: ?Sized + Unsize<U>, U: ?Sized> DispatchFromDyn<Arc<U>> for Arc<T> {} impl<T: ?Sized> Arc<T> { fn from_inner(ptr: NonNull<ArcInner<T>>) -> Self { - Self { - ptr, - phantom: PhantomData, - } + Self { ptr, phantom: PhantomData } } unsafe fn from_ptr(ptr: *mut ArcInner<T>) -> Self { @@ -334,10 +331,9 @@ impl<T> Arc<T> { #[unstable(feature = "new_uninit", issue = "63291")] pub fn new_uninit() -> Arc<mem::MaybeUninit<T>> { unsafe { - Arc::from_ptr(Arc::allocate_for_layout( - Layout::new::<T>(), - |mem| mem as *mut ArcInner<mem::MaybeUninit<T>>, - )) + Arc::from_ptr(Arc::allocate_for_layout(Layout::new::<T>(), |mem| { + mem as *mut ArcInner<mem::MaybeUninit<T>> + })) } } @@ -446,9 +442,7 @@ impl<T> Arc<[T]> { /// ``` #[unstable(feature = "new_uninit", issue = "63291")] pub fn new_uninit_slice(len: usize) -> Arc<[mem::MaybeUninit<T>]> { - unsafe { - Arc::from_ptr(Arc::allocate_for_slice(len)) - } + unsafe { Arc::from_ptr(Arc::allocate_for_slice(len)) } } } @@ -772,18 +766,15 @@ impl<T: ?Sized> Arc<T> { /// and must return back a (potentially fat)-pointer for the `ArcInner<T>`. unsafe fn allocate_for_layout( value_layout: Layout, - mem_to_arcinner: impl FnOnce(*mut u8) -> *mut ArcInner<T> + mem_to_arcinner: impl FnOnce(*mut u8) -> *mut ArcInner<T>, ) -> *mut ArcInner<T> { // Calculate layout using the given value layout. // Previously, layout was calculated on the expression // `&*(ptr as *const ArcInner<T>)`, but this created a misaligned // reference (see #54908). - let layout = Layout::new::<ArcInner<()>>() - .extend(value_layout).unwrap().0 - .pad_to_align(); + let layout = Layout::new::<ArcInner<()>>().extend(value_layout).unwrap().0.pad_to_align(); - let mem = Global.alloc(layout) - .unwrap_or_else(|_| handle_alloc_error(layout)); + let mem = Global.alloc(layout).unwrap_or_else(|_| handle_alloc_error(layout)); // Initialize the ArcInner let inner = mem_to_arcinner(mem.as_ptr()); @@ -798,10 +789,9 @@ impl<T: ?Sized> Arc<T> { /// Allocates an `ArcInner<T>` with sufficient space for an unsized inner value. unsafe fn allocate_for_ptr(ptr: *const T) -> *mut ArcInner<T> { // Allocate for the `ArcInner<T>` using the given value. - Self::allocate_for_layout( - Layout::for_value(&*ptr), - |mem| set_data_ptr(ptr as *mut T, mem) as *mut ArcInner<T>, - ) + Self::allocate_for_layout(Layout::for_value(&*ptr), |mem| { + set_data_ptr(ptr as *mut T, mem) as *mut ArcInner<T> + }) } fn from_box(v: Box<T>) -> Arc<T> { @@ -816,7 +806,8 @@ impl<T: ?Sized> Arc<T> { ptr::copy_nonoverlapping( bptr as *const T as *const u8, &mut (*ptr).data as *mut _ as *mut u8, - value_size); + value_size, + ); // Free the allocation without dropping its contents box_free(box_unique); @@ -829,10 +820,9 @@ impl<T: ?Sized> Arc<T> { impl<T> Arc<[T]> { /// Allocates an `ArcInner<[T]>` with the given length. unsafe fn allocate_for_slice(len: usize) -> *mut ArcInner<[T]> { - Self::allocate_for_layout( - Layout::array::<T>(len).unwrap(), - |mem| ptr::slice_from_raw_parts_mut(mem as *mut T, len) as *mut ArcInner<[T]>, - ) + Self::allocate_for_layout(Layout::array::<T>(len).unwrap(), |mem| { + ptr::slice_from_raw_parts_mut(mem as *mut T, len) as *mut ArcInner<[T]> + }) } } @@ -852,10 +842,7 @@ impl<T> Arc<[T]> { unsafe fn copy_from_slice(v: &[T]) -> Arc<[T]> { let ptr = Self::allocate_for_slice(v.len()); - ptr::copy_nonoverlapping( - v.as_ptr(), - &mut (*ptr).data as *mut [T] as *mut T, - v.len()); + ptr::copy_nonoverlapping(v.as_ptr(), &mut (*ptr).data as *mut [T] as *mut T, v.len()); Self::from_ptr(ptr) } @@ -893,12 +880,7 @@ impl<T> Arc<[T]> { // Pointer to first element let elems = &mut (*ptr).data as *mut [T] as *mut T; - let mut guard = Guard { - mem: NonNull::new_unchecked(mem), - elems, - layout, - n_elems: 0, - }; + let mut guard = Guard { mem: NonNull::new_unchecked(mem), elems, layout, n_elems: 0 }; for (i, item) in iter.enumerate() { ptr::write(elems.add(i), item); @@ -920,9 +902,7 @@ trait ArcFromSlice<T> { impl<T: Clone> ArcFromSlice<T> for Arc<[T]> { #[inline] default fn from_slice(v: &[T]) -> Self { - unsafe { - Self::from_iter_exact(v.iter().cloned(), v.len()) - } + unsafe { Self::from_iter_exact(v.iter().cloned(), v.len()) } } } @@ -1079,9 +1059,7 @@ impl<T: Clone> Arc<T> { // As with `get_mut()`, the unsafety is ok because our reference was // either unique to begin with, or became one upon cloning the contents. - unsafe { - &mut this.ptr.as_mut().data - } + unsafe { &mut this.ptr.as_mut().data } } } @@ -1121,9 +1099,7 @@ impl<T: ?Sized> Arc<T> { // reference count is guaranteed to be 1 at this point, and we required // the Arc itself to be `mut`, so we're returning the only possible // reference to the inner data. - unsafe { - Some(Arc::get_mut_unchecked(this)) - } + unsafe { Some(Arc::get_mut_unchecked(this)) } } else { None } @@ -1317,9 +1293,7 @@ impl<T> Weak<T> { /// ``` #[stable(feature = "downgraded_weak", since = "1.10.0")] pub fn new() -> Weak<T> { - Weak { - ptr: NonNull::new(usize::MAX as *mut ArcInner<T>).expect("MAX is not 0"), - } + Weak { ptr: NonNull::new(usize::MAX as *mut ArcInner<T>).expect("MAX is not 0") } } /// Returns a raw pointer to the object `T` pointed to by this `Weak<T>`. @@ -1458,9 +1432,7 @@ impl<T> Weak<T> { let offset = data_offset(ptr); let fake_ptr = ptr as *mut ArcInner<T>; let ptr = set_data_ptr(fake_ptr, (ptr as *mut u8).offset(-offset)); - Weak { - ptr: NonNull::new(ptr).expect("Invalid pointer passed to from_raw"), - } + Weak { ptr: NonNull::new(ptr).expect("Invalid pointer passed to from_raw") } } } } @@ -1531,11 +1503,7 @@ impl<T: ?Sized> Weak<T> { /// [`Weak::new`]: #method.new #[stable(feature = "weak_counts", since = "1.41.0")] pub fn strong_count(&self) -> usize { - if let Some(inner) = self.inner() { - inner.strong.load(SeqCst) - } else { - 0 - } + if let Some(inner) = self.inner() { inner.strong.load(SeqCst) } else { 0 } } /// Gets an approximation of the number of `Weak` pointers pointing to this @@ -1553,31 +1521,29 @@ impl<T: ?Sized> Weak<T> { /// [`Weak::new`]: #method.new #[stable(feature = "weak_counts", since = "1.41.0")] pub fn weak_count(&self) -> usize { - self.inner().map(|inner| { - let weak = inner.weak.load(SeqCst); - let strong = inner.strong.load(SeqCst); - if strong == 0 { - 0 - } else { - // Since we observed that there was at least one strong pointer - // after reading the weak count, we know that the implicit weak - // reference (present whenever any strong references are alive) - // was still around when we observed the weak count, and can - // therefore safely subtract it. - weak - 1 - } - }).unwrap_or(0) + self.inner() + .map(|inner| { + let weak = inner.weak.load(SeqCst); + let strong = inner.strong.load(SeqCst); + if strong == 0 { + 0 + } else { + // Since we observed that there was at least one strong pointer + // after reading the weak count, we know that the implicit weak + // reference (present whenever any strong references are alive) + // was still around when we observed the weak count, and can + // therefore safely subtract it. + weak - 1 + } + }) + .unwrap_or(0) } /// Returns `None` when the pointer is dangling and there is no allocated `ArcInner`, /// (i.e., when this `Weak` was created by `Weak::new`). #[inline] fn inner(&self) -> Option<&ArcInner<T>> { - if is_dangling(self.ptr) { - None - } else { - Some(unsafe { self.ptr.as_ref() }) - } + if is_dangling(self.ptr) { None } else { Some(unsafe { self.ptr.as_ref() }) } } /// Returns `true` if the two `Weak`s point to the same allocation (similar to @@ -1722,17 +1688,11 @@ impl<T: ?Sized> Drop for Weak<T> { // weak count can only be locked if there was precisely one weak ref, // meaning that drop could only subsequently run ON that remaining weak // ref, which can only happen after the lock is released. - let inner = if let Some(inner) = self.inner() { - inner - } else { - return - }; + let inner = if let Some(inner) = self.inner() { inner } else { return }; if inner.weak.fetch_sub(1, Release) == 1 { atomic::fence(Acquire); - unsafe { - Global.dealloc(self.ptr.cast(), Layout::for_value(self.ptr.as_ref())) - } + unsafe { Global.dealloc(self.ptr.cast(), Layout::for_value(self.ptr.as_ref())) } } } } @@ -2110,7 +2070,8 @@ impl<T, I: iter::TrustedLen<Item = T>> ArcFromIter<T, I> for Arc<[T]> { let (low, high) = iter.size_hint(); if let Some(high) = high { debug_assert_eq!( - low, high, + low, + high, "TrustedLen iterator's size hint is not exact: {:?}", (low, high) ); @@ -2155,7 +2116,7 @@ impl<T: ?Sized> AsRef<T> for Arc<T> { } #[stable(feature = "pin", since = "1.33.0")] -impl<T: ?Sized> Unpin for Arc<T> { } +impl<T: ?Sized> Unpin for Arc<T> {} /// Computes the offset of the data field within `ArcInner`. unsafe fn data_offset<T: ?Sized>(ptr: *const T) -> isize { |
