Rollup merge of #51901 - rust-lang:weak-unboxing, r=alexcrichton
Rc: remove unused allocation and fix segfault in Weak::new() Same as https://github.com/rust-lang/rust/pull/50357 did for `Arc`. Fixes https://github.com/rust-lang/rust/issues/48493
This commit is contained in:
commit
9b3ec57c16
6 changed files with 192 additions and 44 deletions
|
@ -253,7 +253,7 @@ use core::hash::{Hash, Hasher};
|
||||||
use core::intrinsics::abort;
|
use core::intrinsics::abort;
|
||||||
use core::marker;
|
use core::marker;
|
||||||
use core::marker::{Unsize, PhantomData};
|
use core::marker::{Unsize, PhantomData};
|
||||||
use core::mem::{self, align_of_val, forget, size_of_val, uninitialized};
|
use core::mem::{self, align_of_val, forget, size_of_val};
|
||||||
use core::ops::Deref;
|
use core::ops::Deref;
|
||||||
use core::ops::CoerceUnsized;
|
use core::ops::CoerceUnsized;
|
||||||
use core::ptr::{self, NonNull};
|
use core::ptr::{self, NonNull};
|
||||||
|
@ -1153,6 +1153,10 @@ impl<T> From<Vec<T>> for Rc<[T]> {
|
||||||
/// [`None`]: ../../std/option/enum.Option.html#variant.None
|
/// [`None`]: ../../std/option/enum.Option.html#variant.None
|
||||||
#[stable(feature = "rc_weak", since = "1.4.0")]
|
#[stable(feature = "rc_weak", since = "1.4.0")]
|
||||||
pub struct Weak<T: ?Sized> {
|
pub struct Weak<T: ?Sized> {
|
||||||
|
// This is a `NonNull` to allow optimizing the size of this type in enums,
|
||||||
|
// but it is not necessarily a valid pointer.
|
||||||
|
// `Weak::new` sets this to a dangling pointer so that it doesn’t need
|
||||||
|
// to allocate space on the heap.
|
||||||
ptr: NonNull<RcBox<T>>,
|
ptr: NonNull<RcBox<T>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1165,8 +1169,8 @@ impl<T: ?Sized> !marker::Sync for Weak<T> {}
|
||||||
impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<Weak<U>> for Weak<T> {}
|
impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<Weak<U>> for Weak<T> {}
|
||||||
|
|
||||||
impl<T> Weak<T> {
|
impl<T> Weak<T> {
|
||||||
/// Constructs a new `Weak<T>`, allocating memory for `T` without initializing
|
/// Constructs a new `Weak<T>`, without allocating any memory.
|
||||||
/// it. Calling [`upgrade`] on the return value always gives [`None`].
|
/// Calling [`upgrade`] on the return value always gives [`None`].
|
||||||
///
|
///
|
||||||
/// [`upgrade`]: struct.Weak.html#method.upgrade
|
/// [`upgrade`]: struct.Weak.html#method.upgrade
|
||||||
/// [`None`]: ../../std/option/enum.Option.html
|
/// [`None`]: ../../std/option/enum.Option.html
|
||||||
|
@ -1181,18 +1185,18 @@ impl<T> Weak<T> {
|
||||||
/// ```
|
/// ```
|
||||||
#[stable(feature = "downgraded_weak", since = "1.10.0")]
|
#[stable(feature = "downgraded_weak", since = "1.10.0")]
|
||||||
pub fn new() -> Weak<T> {
|
pub fn new() -> Weak<T> {
|
||||||
unsafe {
|
Weak {
|
||||||
Weak {
|
ptr: NonNull::dangling(),
|
||||||
ptr: Box::into_raw_non_null(box RcBox {
|
|
||||||
strong: Cell::new(0),
|
|
||||||
weak: Cell::new(1),
|
|
||||||
value: uninitialized(),
|
|
||||||
}),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub(crate) fn is_dangling<T: ?Sized>(ptr: NonNull<T>) -> bool {
|
||||||
|
let address = ptr.as_ptr() as *mut () as usize;
|
||||||
|
let align = align_of_val(unsafe { ptr.as_ref() });
|
||||||
|
address == align
|
||||||
|
}
|
||||||
|
|
||||||
impl<T: ?Sized> Weak<T> {
|
impl<T: ?Sized> Weak<T> {
|
||||||
/// Attempts to upgrade the `Weak` pointer to an [`Rc`], extending
|
/// Attempts to upgrade the `Weak` pointer to an [`Rc`], extending
|
||||||
/// the lifetime of the value if successful.
|
/// the lifetime of the value if successful.
|
||||||
|
@ -1222,13 +1226,25 @@ impl<T: ?Sized> Weak<T> {
|
||||||
/// ```
|
/// ```
|
||||||
#[stable(feature = "rc_weak", since = "1.4.0")]
|
#[stable(feature = "rc_weak", since = "1.4.0")]
|
||||||
pub fn upgrade(&self) -> Option<Rc<T>> {
|
pub fn upgrade(&self) -> Option<Rc<T>> {
|
||||||
if self.strong() == 0 {
|
let inner = self.inner()?;
|
||||||
|
if inner.strong() == 0 {
|
||||||
None
|
None
|
||||||
} else {
|
} else {
|
||||||
self.inc_strong();
|
inner.inc_strong();
|
||||||
Some(Rc { ptr: self.ptr, phantom: PhantomData })
|
Some(Rc { ptr: self.ptr, phantom: PhantomData })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Return `None` when the pointer is dangling and there is no allocated `RcBox`,
|
||||||
|
/// i.e. this `Weak` was created by `Weak::new`
|
||||||
|
#[inline]
|
||||||
|
fn inner(&self) -> Option<&RcBox<T>> {
|
||||||
|
if is_dangling(self.ptr) {
|
||||||
|
None
|
||||||
|
} else {
|
||||||
|
Some(unsafe { self.ptr.as_ref() })
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[stable(feature = "rc_weak", since = "1.4.0")]
|
#[stable(feature = "rc_weak", since = "1.4.0")]
|
||||||
|
@ -1258,12 +1274,14 @@ impl<T: ?Sized> Drop for Weak<T> {
|
||||||
/// assert!(other_weak_foo.upgrade().is_none());
|
/// assert!(other_weak_foo.upgrade().is_none());
|
||||||
/// ```
|
/// ```
|
||||||
fn drop(&mut self) {
|
fn drop(&mut self) {
|
||||||
unsafe {
|
if let Some(inner) = self.inner() {
|
||||||
self.dec_weak();
|
inner.dec_weak();
|
||||||
// the weak count starts at 1, and will only go to zero if all
|
// the weak count starts at 1, and will only go to zero if all
|
||||||
// the strong pointers have disappeared.
|
// the strong pointers have disappeared.
|
||||||
if self.weak() == 0 {
|
if inner.weak() == 0 {
|
||||||
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()));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1284,7 +1302,9 @@ impl<T: ?Sized> Clone for Weak<T> {
|
||||||
/// ```
|
/// ```
|
||||||
#[inline]
|
#[inline]
|
||||||
fn clone(&self) -> Weak<T> {
|
fn clone(&self) -> Weak<T> {
|
||||||
self.inc_weak();
|
if let Some(inner) = self.inner() {
|
||||||
|
inner.inc_weak()
|
||||||
|
}
|
||||||
Weak { ptr: self.ptr }
|
Weak { ptr: self.ptr }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1317,7 +1337,7 @@ impl<T> Default for Weak<T> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// NOTE: We checked_add here to deal with mem::forget safety. In particular
|
// NOTE: We checked_add here to deal with mem::forget safely. In particular
|
||||||
// if you mem::forget Rcs (or Weaks), the ref-count can overflow, and then
|
// if you mem::forget Rcs (or Weaks), the ref-count can overflow, and then
|
||||||
// you can free the allocation while outstanding Rcs (or Weaks) exist.
|
// you can free the allocation while outstanding Rcs (or Weaks) exist.
|
||||||
// We abort because this is such a degenerate scenario that we don't care about
|
// We abort because this is such a degenerate scenario that we don't care about
|
||||||
|
@ -1370,12 +1390,10 @@ impl<T: ?Sized> RcBoxPtr<T> for Rc<T> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: ?Sized> RcBoxPtr<T> for Weak<T> {
|
impl<T: ?Sized> RcBoxPtr<T> for RcBox<T> {
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn inner(&self) -> &RcBox<T> {
|
fn inner(&self) -> &RcBox<T> {
|
||||||
unsafe {
|
self
|
||||||
self.ptr.as_ref()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -34,6 +34,7 @@ use core::convert::From;
|
||||||
|
|
||||||
use alloc::{Global, Alloc, Layout, box_free, handle_alloc_error};
|
use alloc::{Global, Alloc, Layout, box_free, handle_alloc_error};
|
||||||
use boxed::Box;
|
use boxed::Box;
|
||||||
|
use rc::is_dangling;
|
||||||
use string::String;
|
use string::String;
|
||||||
use vec::Vec;
|
use vec::Vec;
|
||||||
|
|
||||||
|
@ -43,9 +44,6 @@ use vec::Vec;
|
||||||
/// necessarily) at _exactly_ `MAX_REFCOUNT + 1` references.
|
/// necessarily) at _exactly_ `MAX_REFCOUNT + 1` references.
|
||||||
const MAX_REFCOUNT: usize = (isize::MAX) as usize;
|
const MAX_REFCOUNT: usize = (isize::MAX) as usize;
|
||||||
|
|
||||||
/// A sentinel value that is used for the pointer of `Weak::new()`.
|
|
||||||
const WEAK_EMPTY: usize = 1;
|
|
||||||
|
|
||||||
/// A thread-safe reference-counting pointer. 'Arc' stands for 'Atomically
|
/// A thread-safe reference-counting pointer. 'Arc' stands for 'Atomically
|
||||||
/// Reference Counted'.
|
/// Reference Counted'.
|
||||||
///
|
///
|
||||||
|
@ -239,9 +237,9 @@ impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<Arc<U>> for Arc<T> {}
|
||||||
#[stable(feature = "arc_weak", since = "1.4.0")]
|
#[stable(feature = "arc_weak", since = "1.4.0")]
|
||||||
pub struct Weak<T: ?Sized> {
|
pub struct Weak<T: ?Sized> {
|
||||||
// This is a `NonNull` to allow optimizing the size of this type in enums,
|
// This is a `NonNull` to allow optimizing the size of this type in enums,
|
||||||
// but it is actually not truly "non-null". A `Weak::new()` will set this
|
// but it is not necessarily a valid pointer.
|
||||||
// to a sentinel value, instead of needing to allocate some space in the
|
// `Weak::new` sets this to a dangling pointer so that it doesn’t need
|
||||||
// heap.
|
// to allocate space on the heap.
|
||||||
ptr: NonNull<ArcInner<T>>,
|
ptr: NonNull<ArcInner<T>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1035,10 +1033,8 @@ impl<T> Weak<T> {
|
||||||
/// ```
|
/// ```
|
||||||
#[stable(feature = "downgraded_weak", since = "1.10.0")]
|
#[stable(feature = "downgraded_weak", since = "1.10.0")]
|
||||||
pub fn new() -> Weak<T> {
|
pub fn new() -> Weak<T> {
|
||||||
unsafe {
|
Weak {
|
||||||
Weak {
|
ptr: NonNull::dangling(),
|
||||||
ptr: NonNull::new_unchecked(WEAK_EMPTY as *mut _),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1074,11 +1070,7 @@ impl<T: ?Sized> Weak<T> {
|
||||||
pub fn upgrade(&self) -> Option<Arc<T>> {
|
pub fn upgrade(&self) -> Option<Arc<T>> {
|
||||||
// We use a CAS loop to increment the strong count instead of a
|
// We use a CAS loop to increment the strong count instead of a
|
||||||
// fetch_add because once the count hits 0 it must never be above 0.
|
// fetch_add because once the count hits 0 it must never be above 0.
|
||||||
let inner = if self.ptr.as_ptr() as *const u8 as usize == WEAK_EMPTY {
|
let inner = self.inner()?;
|
||||||
return None;
|
|
||||||
} else {
|
|
||||||
unsafe { self.ptr.as_ref() }
|
|
||||||
};
|
|
||||||
|
|
||||||
// Relaxed load because any write of 0 that we can observe
|
// Relaxed load because any write of 0 that we can observe
|
||||||
// leaves the field in a permanently zero state (so a
|
// leaves the field in a permanently zero state (so a
|
||||||
|
@ -1109,6 +1101,17 @@ impl<T: ?Sized> Weak<T> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Return `None` when the pointer is dangling and there is no allocated `ArcInner`,
|
||||||
|
/// i.e. 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() })
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[stable(feature = "arc_weak", since = "1.4.0")]
|
#[stable(feature = "arc_weak", since = "1.4.0")]
|
||||||
|
@ -1126,10 +1129,10 @@ impl<T: ?Sized> Clone for Weak<T> {
|
||||||
/// ```
|
/// ```
|
||||||
#[inline]
|
#[inline]
|
||||||
fn clone(&self) -> Weak<T> {
|
fn clone(&self) -> Weak<T> {
|
||||||
let inner = if self.ptr.as_ptr() as *const u8 as usize == WEAK_EMPTY {
|
let inner = if let Some(inner) = self.inner() {
|
||||||
return Weak { ptr: self.ptr };
|
inner
|
||||||
} else {
|
} else {
|
||||||
unsafe { self.ptr.as_ref() }
|
return Weak { ptr: self.ptr };
|
||||||
};
|
};
|
||||||
// See comments in Arc::clone() for why this is relaxed. This can use a
|
// See comments in Arc::clone() for why this is relaxed. This can use a
|
||||||
// fetch_add (ignoring the lock) because the weak count is only locked
|
// fetch_add (ignoring the lock) because the weak count is only locked
|
||||||
|
@ -1204,10 +1207,10 @@ impl<T: ?Sized> Drop for Weak<T> {
|
||||||
// weak count can only be locked if there was precisely one weak ref,
|
// weak count can only be locked if there was precisely one weak ref,
|
||||||
// meaning that drop could only subsequently run ON that remaining weak
|
// meaning that drop could only subsequently run ON that remaining weak
|
||||||
// ref, which can only happen after the lock is released.
|
// ref, which can only happen after the lock is released.
|
||||||
let inner = if self.ptr.as_ptr() as *const u8 as usize == WEAK_EMPTY {
|
let inner = if let Some(inner) = self.inner() {
|
||||||
return;
|
inner
|
||||||
} else {
|
} else {
|
||||||
unsafe { self.ptr.as_ref() }
|
return
|
||||||
};
|
};
|
||||||
|
|
||||||
if inner.weak.fetch_sub(1, Release) == 1 {
|
if inner.weak.fetch_sub(1, Release) == 1 {
|
||||||
|
|
55
src/liballoc/tests/arc.rs
Normal file
55
src/liballoc/tests/arc.rs
Normal file
|
@ -0,0 +1,55 @@
|
||||||
|
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
|
||||||
|
// file at the top-level directory of this distribution and at
|
||||||
|
// http://rust-lang.org/COPYRIGHT.
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||||
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||||
|
// option. This file may not be copied, modified, or distributed
|
||||||
|
// except according to those terms.
|
||||||
|
|
||||||
|
use std::any::Any;
|
||||||
|
use std::sync::{Arc, Weak};
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn uninhabited() {
|
||||||
|
enum Void {}
|
||||||
|
let mut a = Weak::<Void>::new();
|
||||||
|
a = a.clone();
|
||||||
|
assert!(a.upgrade().is_none());
|
||||||
|
|
||||||
|
let mut a: Weak<Any> = a; // Unsizing
|
||||||
|
a = a.clone();
|
||||||
|
assert!(a.upgrade().is_none());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn slice() {
|
||||||
|
let a: Arc<[u32; 3]> = Arc::new([3, 2, 1]);
|
||||||
|
let a: Arc<[u32]> = a; // Unsizing
|
||||||
|
let b: Arc<[u32]> = Arc::from(&[3, 2, 1][..]); // Conversion
|
||||||
|
assert_eq!(a, b);
|
||||||
|
|
||||||
|
// Exercise is_dangling() with a DST
|
||||||
|
let mut a = Arc::downgrade(&a);
|
||||||
|
a = a.clone();
|
||||||
|
assert!(a.upgrade().is_some());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn trait_object() {
|
||||||
|
let a: Arc<u32> = Arc::new(4);
|
||||||
|
let a: Arc<Any> = a; // Unsizing
|
||||||
|
|
||||||
|
// Exercise is_dangling() with a DST
|
||||||
|
let mut a = Arc::downgrade(&a);
|
||||||
|
a = a.clone();
|
||||||
|
assert!(a.upgrade().is_some());
|
||||||
|
|
||||||
|
let mut b = Weak::<u32>::new();
|
||||||
|
b = b.clone();
|
||||||
|
assert!(b.upgrade().is_none());
|
||||||
|
let mut b: Weak<Any> = b; // Unsizing
|
||||||
|
b = b.clone();
|
||||||
|
assert!(b.upgrade().is_none());
|
||||||
|
}
|
|
@ -32,12 +32,14 @@ extern crate rand;
|
||||||
use std::hash::{Hash, Hasher};
|
use std::hash::{Hash, Hasher};
|
||||||
use std::collections::hash_map::DefaultHasher;
|
use std::collections::hash_map::DefaultHasher;
|
||||||
|
|
||||||
|
mod arc;
|
||||||
mod binary_heap;
|
mod binary_heap;
|
||||||
mod btree;
|
mod btree;
|
||||||
mod cow_str;
|
mod cow_str;
|
||||||
mod fmt;
|
mod fmt;
|
||||||
mod heap;
|
mod heap;
|
||||||
mod linked_list;
|
mod linked_list;
|
||||||
|
mod rc;
|
||||||
mod slice;
|
mod slice;
|
||||||
mod str;
|
mod str;
|
||||||
mod string;
|
mod string;
|
||||||
|
|
55
src/liballoc/tests/rc.rs
Normal file
55
src/liballoc/tests/rc.rs
Normal file
|
@ -0,0 +1,55 @@
|
||||||
|
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
|
||||||
|
// file at the top-level directory of this distribution and at
|
||||||
|
// http://rust-lang.org/COPYRIGHT.
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||||
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||||
|
// option. This file may not be copied, modified, or distributed
|
||||||
|
// except according to those terms.
|
||||||
|
|
||||||
|
use std::any::Any;
|
||||||
|
use std::rc::{Rc, Weak};
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn uninhabited() {
|
||||||
|
enum Void {}
|
||||||
|
let mut a = Weak::<Void>::new();
|
||||||
|
a = a.clone();
|
||||||
|
assert!(a.upgrade().is_none());
|
||||||
|
|
||||||
|
let mut a: Weak<Any> = a; // Unsizing
|
||||||
|
a = a.clone();
|
||||||
|
assert!(a.upgrade().is_none());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn slice() {
|
||||||
|
let a: Rc<[u32; 3]> = Rc::new([3, 2, 1]);
|
||||||
|
let a: Rc<[u32]> = a; // Unsizing
|
||||||
|
let b: Rc<[u32]> = Rc::from(&[3, 2, 1][..]); // Conversion
|
||||||
|
assert_eq!(a, b);
|
||||||
|
|
||||||
|
// Exercise is_dangling() with a DST
|
||||||
|
let mut a = Rc::downgrade(&a);
|
||||||
|
a = a.clone();
|
||||||
|
assert!(a.upgrade().is_some());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn trait_object() {
|
||||||
|
let a: Rc<u32> = Rc::new(4);
|
||||||
|
let a: Rc<Any> = a; // Unsizing
|
||||||
|
|
||||||
|
// Exercise is_dangling() with a DST
|
||||||
|
let mut a = Rc::downgrade(&a);
|
||||||
|
a = a.clone();
|
||||||
|
assert!(a.upgrade().is_some());
|
||||||
|
|
||||||
|
let mut b = Weak::<u32>::new();
|
||||||
|
b = b.clone();
|
||||||
|
assert!(b.upgrade().is_none());
|
||||||
|
let mut b: Weak<Any> = b; // Unsizing
|
||||||
|
b = b.clone();
|
||||||
|
assert!(b.upgrade().is_none());
|
||||||
|
}
|
15
src/test/run-pass/weak-new-uninhabited-issue-48493.rs
Normal file
15
src/test/run-pass/weak-new-uninhabited-issue-48493.rs
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
|
||||||
|
// file at the top-level directory of this distribution and at
|
||||||
|
// http://rust-lang.org/COPYRIGHT.
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||||
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||||
|
// option. This file may not be copied, modified, or distributed
|
||||||
|
// except according to those terms.
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
enum Void {}
|
||||||
|
std::rc::Weak::<Void>::new();
|
||||||
|
std::sync::Weak::<Void>::new();
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue