mirror of
https://github.com/rust-lang/rust.git
synced 2024-12-18 03:25:55 +00:00
parent
d8d1b8f8be
commit
82f5f65076
@ -17,6 +17,13 @@ use ptr::to_unsafe_ptr;
|
|||||||
pub static RC_MANAGED_UNIQUE : uint = (-2) as uint;
|
pub static RC_MANAGED_UNIQUE : uint = (-2) as uint;
|
||||||
pub static RC_IMMORTAL : uint = 0x77777777;
|
pub static RC_IMMORTAL : uint = 0x77777777;
|
||||||
|
|
||||||
|
/// Returns the refcount of a shared box (as just before calling this)
|
||||||
|
#[inline]
|
||||||
|
pub fn refcount<T>(t: @T) -> uint {
|
||||||
|
use unstable::raw::Repr;
|
||||||
|
unsafe { (*t.repr()).ref_count }
|
||||||
|
}
|
||||||
|
|
||||||
/// Determine if two shared boxes point to the same object
|
/// Determine if two shared boxes point to the same object
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn ptr_eq<T>(a: @T, b: @T) -> bool {
|
pub fn ptr_eq<T>(a: @T, b: @T) -> bool {
|
||||||
|
@ -13,22 +13,12 @@
|
|||||||
#[allow(missing_doc)];
|
#[allow(missing_doc)];
|
||||||
|
|
||||||
use c_str::ToCStr;
|
use c_str::ToCStr;
|
||||||
use cast;
|
|
||||||
use libc::size_t;
|
use libc::size_t;
|
||||||
use libc;
|
use libc;
|
||||||
use repr;
|
use repr;
|
||||||
use rt::task;
|
use rt::task;
|
||||||
use str;
|
use str;
|
||||||
|
|
||||||
/// Returns the refcount of a shared box (as just before calling this)
|
|
||||||
#[inline]
|
|
||||||
pub fn refcount<T>(t: @T) -> uint {
|
|
||||||
unsafe {
|
|
||||||
let ref_ptr: *uint = cast::transmute_copy(&t);
|
|
||||||
*ref_ptr - 1
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn log_str<T>(t: &T) -> ~str {
|
pub fn log_str<T>(t: &T) -> ~str {
|
||||||
use rt::io;
|
use rt::io;
|
||||||
use rt::io::Decorator;
|
use rt::io::Decorator;
|
||||||
|
@ -14,11 +14,11 @@
|
|||||||
enum t { make_t(@int), clam, }
|
enum t { make_t(@int), clam, }
|
||||||
|
|
||||||
fn foo(s: @int) {
|
fn foo(s: @int) {
|
||||||
info2!("{:?}", ::std::sys::refcount(s));
|
info2!("{:?}", ::std::managed::refcount(s));
|
||||||
let count = ::std::sys::refcount(s);
|
let count = ::std::managed::refcount(s);
|
||||||
let x: t = make_t(s); // ref up
|
let x: t = make_t(s); // ref up
|
||||||
assert_eq!(::std::sys::refcount(s), count + 1u);
|
assert_eq!(::std::managed::refcount(s), count + 1u);
|
||||||
info2!("{:?}", ::std::sys::refcount(s));
|
info2!("{:?}", ::std::managed::refcount(s));
|
||||||
|
|
||||||
match x {
|
match x {
|
||||||
make_t(y) => {
|
make_t(y) => {
|
||||||
@ -27,19 +27,19 @@ fn foo(s: @int) {
|
|||||||
}
|
}
|
||||||
_ => { info2!("?"); fail2!(); }
|
_ => { info2!("?"); fail2!(); }
|
||||||
}
|
}
|
||||||
info2!("{:?}", ::std::sys::refcount(s));
|
info2!("{:?}", ::std::managed::refcount(s));
|
||||||
assert_eq!(::std::sys::refcount(s), count + 1u);
|
assert_eq!(::std::managed::refcount(s), count + 1u);
|
||||||
let _ = ::std::sys::refcount(s); // don't get bitten by last-use.
|
let _ = ::std::managed::refcount(s); // don't get bitten by last-use.
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn main() {
|
pub fn main() {
|
||||||
let s: @int = @0; // ref up
|
let s: @int = @0; // ref up
|
||||||
|
|
||||||
let count = ::std::sys::refcount(s);
|
let count = ::std::managed::refcount(s);
|
||||||
|
|
||||||
foo(s); // ref up then down
|
foo(s); // ref up then down
|
||||||
|
|
||||||
info2!("{}", ::std::sys::refcount(s));
|
info2!("{}", ::std::managed::refcount(s));
|
||||||
let count2 = ::std::sys::refcount(s);
|
let count2 = ::std::managed::refcount(s);
|
||||||
assert_eq!(count, count2);
|
assert_eq!(count, count2);
|
||||||
}
|
}
|
||||||
|
@ -10,14 +10,14 @@
|
|||||||
|
|
||||||
#[allow(unused_variable)];
|
#[allow(unused_variable)];
|
||||||
|
|
||||||
use std::sys;
|
use std::managed;
|
||||||
|
|
||||||
pub fn main() {
|
pub fn main() {
|
||||||
let i = ~@1;
|
let i = ~@1;
|
||||||
let j = ~@2;
|
let j = ~@2;
|
||||||
let rc1 = sys::refcount(*i);
|
let rc1 = managed::refcount(*i);
|
||||||
let j = i.clone();
|
let j = i.clone();
|
||||||
let rc2 = sys::refcount(*i);
|
let rc2 = managed::refcount(*i);
|
||||||
error2!("rc1: {} rc2: {}", rc1, rc2);
|
error2!("rc1: {} rc2: {}", rc1, rc2);
|
||||||
assert_eq!(rc1 + 1u, rc2);
|
assert_eq!(rc1 + 1u, rc2);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user