1
Fork 0

Set #[no_drop_flag] on Rc<T> and AtomicOption. Add Test

This commit is contained in:
James Miller 2013-06-23 16:13:29 +12:00 committed by James Miller
parent 0cca08a21a
commit d9f6dd263c
4 changed files with 39 additions and 9 deletions

View file

@ -70,10 +70,12 @@ impl<T> Rc<T> {
impl<T> Drop for Rc<T> {
fn finalize(&self) {
unsafe {
(*self.ptr).count -= 1;
if (*self.ptr).count == 0 {
ptr::replace_ptr(self.ptr, intrinsics::uninit());
free(self.ptr as *c_void)
if self.ptr.is_not_null() {
(*self.ptr).count -= 1;
if (*self.ptr).count == 0 {
ptr::replace_ptr(self.ptr, intrinsics::uninit());
free(self.ptr as *c_void)
}
}
}
}
@ -220,10 +222,12 @@ impl<T> RcMut<T> {
impl<T> Drop for RcMut<T> {
fn finalize(&self) {
unsafe {
(*self.ptr).count -= 1;
if (*self.ptr).count == 0 {
ptr::replace_ptr(self.ptr, uninit());
free(self.ptr as *c_void)
if self.ptr.is_not_null() {
(*self.ptr).count -= 1;
if (*self.ptr).count == 0 {
ptr::replace_ptr(self.ptr, uninit());
free(self.ptr as *c_void)
}
}
}
}

View file

@ -3883,7 +3883,7 @@ impl DtorKind {
pub fn ty_dtor(cx: ctxt, struct_id: def_id) -> DtorKind {
match cx.destructor_for_type.find(&struct_id) {
Some(&method_def_id) => {
let flag = has_attr(cx, struct_id, "no_drop_flag");
let flag = !has_attr(cx, struct_id, "no_drop_flag");
TraitDtor(method_def_id, flag)
}

View file

@ -62,6 +62,7 @@ pub struct AtomicPtr<T> {
/**
* An owned atomic pointer. Ensures that only a single reference to the data is held at any time.
*/
#[no_drop_flag]
pub struct AtomicOption<T> {
priv p: *mut c_void
}

View file

@ -0,0 +1,25 @@
// Copyright 2012 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::sys::size_of;
#[no_drop_flag]
struct Test<T> {
a: T
}
#[unsafe_destructor]
impl<T> Drop for Test<T> {
fn finalize(&self) { }
}
fn main() {
assert_eq!(size_of::<int>(), size_of::<Test<int>>());
}