Skip to content

Commit

Permalink
Set #[no_drop_flag] on Rc<T> and AtomicOption. Add Test
Browse files Browse the repository at this point in the history
  • Loading branch information
James Miller authored and Aatch committed Jun 25, 2013
1 parent 0cca08a commit d9f6dd2
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 9 deletions.
20 changes: 12 additions & 8 deletions src/libextra/rc.rs
Expand Up @@ -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)
}
}
}
}
Expand Down Expand Up @@ -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)
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/middle/ty.rs
Expand Up @@ -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)
}
Expand Down
1 change: 1 addition & 0 deletions src/libstd/unstable/atomics.rs
Expand Up @@ -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
}
Expand Down
25 changes: 25 additions & 0 deletions src/test/run-pass/attr-no-drop-flag-size.rs
@@ -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>>());
}

0 comments on commit d9f6dd2

Please sign in to comment.