about summary refs log tree commit diff
path: root/src/libstd/rt/rc.rs
blob: 18a5dd4a1145d1fc287665d8aa98380134a7097d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// Copyright 2013 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.

//! An owned, task-local, reference counted type
//!
//! # Safety note
//!
//! XXX There is currently no type-system mechanism for enforcing that
//! reference counted types are both allocated on the exchange heap
//! and also non-sendable
//!
//! This doesn't prevent borrowing multiple aliasable mutable pointers

use ops::Drop;
use clone::Clone;
use libc::c_void;
use cast;

pub struct RC<T> {
    p: *c_void // ~(uint, T)
}

impl<T> RC<T> {
    pub fn new(val: T) -> RC<T> {
        unsafe {
            let v = ~(1, val);
            let p: *c_void = cast::transmute(v);
            RC { p: p }
        }
    }

    fn get_mut_state(&mut self) -> *mut (uint, T) {
        unsafe {
            let p: &mut ~(uint, T) = cast::transmute(&mut self.p);
            let p: *mut (uint, T) = &mut **p;
            return p;
        }
    }

    fn get_state(&self) -> *(uint, T) {
        unsafe {
            let p: &~(uint, T) = cast::transmute(&self.p);
            let p: *(uint, T) = &**p;
            return p;
        }
    }

    pub fn unsafe_borrow_mut(&mut self) -> *mut T {
        unsafe {
            match *self.get_mut_state() {
                (_, ref mut p) => {
                    let p: *mut T = p;
                    return p;
                }
            }
        }
    }

    pub fn refcount(&self) -> uint {
        unsafe {
            match *self.get_state() {
                (count, _) => count
            }
        }
    }
}

#[unsafe_destructor]
impl<T> Drop for RC<T> {
    fn drop(&self) {
        assert!(self.refcount() > 0);

        unsafe {
            // FIXME(#4330) Need self by value to get mutability.
            let this: &mut RC<T> = cast::transmute_mut(self);

            match *this.get_mut_state() {
                (ref mut count, _) => {
                    *count = *count - 1
                }
            }

            if this.refcount() == 0 {
                let _: ~(uint, T) = cast::transmute(this.p);
            }
        }
    }
}

impl<T> Clone for RC<T> {
    fn clone(&self) -> RC<T> {
        unsafe {
            // XXX: Mutable clone
            let this: &mut RC<T> = cast::transmute_mut(self);

            match *this.get_mut_state() {
                (ref mut count, _) => {
                    *count = *count + 1;
                }
            }
        }

        RC { p: self.p }
    }
}

#[cfg(test)]
mod test {
    use super::RC;

    #[test]
    fn smoke_test() {
        unsafe {
            let mut v1 = RC::new(100);
            assert!(*v1.unsafe_borrow_mut() == 100);
            assert!(v1.refcount() == 1);

            let mut v2 = v1.clone();
            assert!(*v2.unsafe_borrow_mut() == 100);
            assert!(v2.refcount() == 2);

            *v2.unsafe_borrow_mut() = 200;
            assert!(*v2.unsafe_borrow_mut() == 200);
            assert!(*v1.unsafe_borrow_mut() == 200);

            let v3 = v2.clone();
            assert!(v3.refcount() == 3);
            {
                let _v1 = v1;
                let _v2 = v2;
            }
            assert!(v3.refcount() == 1);
        }
    }
}