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
#[crate_id="boehm#0.1"];
#[crate_type="lib"];
#[feature(globs, macro_rules)];

use std::{libc, mem};
use std::unstable::intrinsics;

#[allow(dead_code)]
pub mod ffi;

pub mod tracing;

/// Initialise the GC. This should be called before using any other
/// functions and on the main thread for maximum portability (some
/// platforms don't require this to be called at all).
///
/// FIXME: initialise automagically somehow
/// FIXME: this should be doing the full equivalent of the GC_INIT()
/// macro.
pub fn init() {
    unsafe { ffi::GC_init(); }
}

/// Number of bytes in the garbage collection heap.
pub fn heap_size() -> uint {
    unsafe { ffi::GC_get_heap_size() as uint }
}

/// Force a garbage collection.
pub fn collect() {
    unsafe { ffi::GC_gcollect(); }
}

/// Dump some debugging/diagnostic information to stdout.
pub fn debug_dump() {
    unsafe { ffi::GC_dump(); }
}

/// A garbage collected pointer.
#[no_send]
#[deriving(Clone)]
pub struct Gc<T> {
    priv ptr: *mut T
}

impl<T: 'static> Gc<T> {
    pub fn new(value: T) -> Gc<T> {
        unsafe {
            let size = mem::size_of::<T>() as libc::size_t;
            let p = if cfg!(debug) {
                ffi::GC_debug_malloc(size, bytes!("Gc", 0).as_ptr() as *i8, 0)
            } else {
                ffi::GC_malloc(size)
            } as *mut T;
            if p.is_null() {
                fail!("Could not allocate")
            }
            intrinsics::move_val_init(&mut *p, value);
            Gc { ptr: p }
        }
    }

    pub fn borrow<'r>(&'r self) -> &'r T {
        unsafe {
            &*self.ptr
        }
    }
}