Skip to content

Commit 79d50a0

Browse files
committed
---
yaml --- r: 166767 b: refs/heads/master c: 0d48f76 h: refs/heads/master i: 166765: 2c0d0f2 166763: 92a4717 166759: 0f1ee11 166751: d8294d2 v: v3
1 parent bfc0f49 commit 79d50a0

File tree

2 files changed

+30
-16
lines changed

2 files changed

+30
-16
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
refs/heads/master: bb4473774866a1a9a3965a62db3298f1be874418
2+
refs/heads/master: 0d48f76224371e884496182f6cfb3e2dad7690a2
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
44
refs/heads/snap-stage3: 023dfb0c898d851dee6ace2f8339b73b5287136b
55
refs/heads/try: f5d619caf9f32458680fae55526b99582ca682dd

trunk/src/liballoc/rc.rs

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,7 @@ use core::mem::{transmute, min_align_of, size_of, forget};
153153
use core::ops::{Deref, Drop};
154154
use core::option::Option;
155155
use core::option::Option::{Some, None};
156-
use core::ptr;
157-
use core::ptr::RawPtr;
156+
use core::ptr::{mod, NonZero, RawPtr};
158157
use core::result::Result;
159158
use core::result::Result::{Ok, Err};
160159

@@ -174,7 +173,7 @@ struct RcBox<T> {
174173
pub struct Rc<T> {
175174
// FIXME #12808: strange names to try to avoid interfering with field accesses of the contained
176175
// type via Deref
177-
_ptr: *mut RcBox<T>,
176+
_ptr: NonZero<*mut RcBox<T>>,
178177
_nosend: marker::NoSend,
179178
_noshare: marker::NoSync
180179
}
@@ -196,11 +195,11 @@ impl<T> Rc<T> {
196195
// there is an implicit weak pointer owned by all the strong pointers, which
197196
// ensures that the weak destructor never frees the allocation while the strong
198197
// destructor is running, even if the weak pointer is stored inside the strong one.
199-
_ptr: transmute(box RcBox {
198+
_ptr: NonZero(transmute(box RcBox {
200199
value: value,
201200
strong: Cell::new(1),
202201
weak: Cell::new(1)
203-
}),
202+
})),
204203
_nosend: marker::NoSend,
205204
_noshare: marker::NoSync
206205
}
@@ -281,7 +280,8 @@ pub fn try_unwrap<T>(rc: Rc<T>) -> Result<T, Rc<T>> {
281280
let val = ptr::read(&*rc); // copy the contained object
282281
// destruct the box and skip our Drop
283282
// we can ignore the refcounts because we know we're unique
284-
deallocate(rc._ptr as *mut u8, size_of::<RcBox<T>>(),
283+
let NonZero(ptr) = rc._ptr;
284+
deallocate(ptr as *mut u8, size_of::<RcBox<T>>(),
285285
min_align_of::<RcBox<T>>());
286286
forget(rc);
287287
Ok(val)
@@ -311,7 +311,10 @@ pub fn try_unwrap<T>(rc: Rc<T>) -> Result<T, Rc<T>> {
311311
#[experimental]
312312
pub fn get_mut<'a, T>(rc: &'a mut Rc<T>) -> Option<&'a mut T> {
313313
if is_unique(rc) {
314-
let inner = unsafe { &mut *rc._ptr };
314+
let inner = unsafe {
315+
let NonZero(ptr) = rc._ptr;
316+
&mut *ptr
317+
};
315318
Some(&mut inner.value)
316319
} else {
317320
None
@@ -343,7 +346,10 @@ impl<T: Clone> Rc<T> {
343346
// pointer that will ever be returned to T. Our reference count is guaranteed to be 1 at
344347
// this point, and we required the `Rc<T>` itself to be `mut`, so we're returning the only
345348
// possible reference to the inner value.
346-
let inner = unsafe { &mut *self._ptr };
349+
let inner = unsafe {
350+
let NonZero(ptr) = self._ptr;
351+
&mut *ptr
352+
};
347353
&mut inner.value
348354
}
349355
}
@@ -391,7 +397,8 @@ impl<T> Drop for Rc<T> {
391397
/// ```
392398
fn drop(&mut self) {
393399
unsafe {
394-
if !self._ptr.is_null() {
400+
let NonZero(ptr) = self._ptr;
401+
if !ptr.is_null() {
395402
self.dec_strong();
396403
if self.strong() == 0 {
397404
ptr::read(&**self); // destroy the contained object
@@ -401,7 +408,7 @@ impl<T> Drop for Rc<T> {
401408
self.dec_weak();
402409

403410
if self.weak() == 0 {
404-
deallocate(self._ptr as *mut u8, size_of::<RcBox<T>>(),
411+
deallocate(ptr as *mut u8, size_of::<RcBox<T>>(),
405412
min_align_of::<RcBox<T>>())
406413
}
407414
}
@@ -618,7 +625,7 @@ impl<T: fmt::Show> fmt::Show for Rc<T> {
618625
pub struct Weak<T> {
619626
// FIXME #12808: strange names to try to avoid interfering with
620627
// field accesses of the contained type via Deref
621-
_ptr: *mut RcBox<T>,
628+
_ptr: NonZero<*mut RcBox<T>>,
622629
_nosend: marker::NoSend,
623630
_noshare: marker::NoSync
624631
}
@@ -682,12 +689,13 @@ impl<T> Drop for Weak<T> {
682689
/// ```
683690
fn drop(&mut self) {
684691
unsafe {
685-
if !self._ptr.is_null() {
692+
let NonZero(ptr) = self._ptr;
693+
if !ptr.is_null() {
686694
self.dec_weak();
687695
// the weak count starts at 1, and will only go to zero if all the strong pointers
688696
// have disappeared.
689697
if self.weak() == 0 {
690-
deallocate(self._ptr as *mut u8, size_of::<RcBox<T>>(),
698+
deallocate(ptr as *mut u8, size_of::<RcBox<T>>(),
691699
min_align_of::<RcBox<T>>())
692700
}
693701
}
@@ -742,12 +750,18 @@ trait RcBoxPtr<T> {
742750

743751
impl<T> RcBoxPtr<T> for Rc<T> {
744752
#[inline(always)]
745-
fn inner(&self) -> &RcBox<T> { unsafe { &(*self._ptr) } }
753+
fn inner(&self) -> &RcBox<T> {
754+
let NonZero(ptr) = self._ptr;
755+
unsafe { &(*ptr) }
756+
}
746757
}
747758

748759
impl<T> RcBoxPtr<T> for Weak<T> {
749760
#[inline(always)]
750-
fn inner(&self) -> &RcBox<T> { unsafe { &(*self._ptr) } }
761+
fn inner(&self) -> &RcBox<T> {
762+
let NonZero(ptr) = self._ptr;
763+
unsafe { &(*ptr) }
764+
}
751765
}
752766

753767
#[cfg(test)]

0 commit comments

Comments
 (0)