Skip to content

huonw/boehm-rs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Using the Boehm GC from Rust

Build Status

A basic wrapper that provides Gc<T> and GcTracing<T> types (completely conservative and precise-on-heap respectively), implemented by binding to the Boehm-Demers-Weiser garbage collector. See examples/ for some examples.

Warning

This is not correct and shouldn't be used/should only be used very carefully, because I don't think the Rust compiler provides enough hooks yet to make something like ~[Gc<T>] completely work.

To illustrate, the following program is trying to make a vector ~[Gc 0, Gc 1, ..., Gc 1_000_000], so it should print 0... but it prints 895221 for me; the vector doesn't act as a root that Boehm can understand, and so it's free to GC the first one and reuse that memory for one of the later allocations. Oops.

extern mod boehm;

#[start]
fn main(_: int, _: **u8) -> int {
    boehm::init();

    let mut v = std::vec::from_fn(1_000_000, boehm::Gc::new);
    println!("{}", *v[0].borrow());

    0
}

I think this could be somewhat fixed with some trickery with #[no_std] and #[lang="malloc"] and so on, but that's beyond the time I've been able to allocate (no pun intended) to this so far.

Todo

  • Fix the above
  • Stop generating the Boehm type-descriptor for each type on every allocation (requires compiler support to do properly, but we could cache based on the address of the (Rust) type descriptor, or something)

Todo-done

  • Use the typed inferface for more precise collection (at the very least, working out a way to use malloc_atomic where appropriate would be good). See boehm::tracing.

License

Dual Apache v2.0 and MIT, like Rust itself.