Skip to content

Instantly share code, notes, and snippets.

@swgillespie
Last active August 28, 2015 22:49
Show Gist options
  • Save swgillespie/a9a4a80fab7d206af8f3 to your computer and use it in GitHub Desktop.
Save swgillespie/a9a4a80fab7d206af8f3 to your computer and use it in GitHub Desktop.
$ cargo build
Compiling libc v0.1.10
Compiling boehm_gc v0.1.0 (file:///Users/sean/Documents/workspace/rust/gc_test)
Compiling gc_test v0.1.0 (file:///Users/sean/Documents/workspace/rust/gc_test)
error: the allocator crate `boehm_gc` cannot depend on a crate that needs an allocator, but it depends on `alloc`
error: the allocator crate `boehm_gc` cannot depend on a crate that needs an allocator, but it depends on `alloc`
error: the allocator crate `boehm_gc` cannot depend on a crate that needs an allocator, but it depends on `alloc`
error: the allocator crate `boehm_gc` cannot depend on a crate that needs an allocator, but it depends on `alloc`
error: the allocator crate `boehm_gc` cannot depend on a crate that needs an allocator, but it depends on `alloc`
error: the allocator crate `boehm_gc` cannot depend on a crate that needs an allocator, but it depends on `alloc`
error: the allocator crate `boehm_gc` cannot depend on a crate that needs an allocator, but it depends on `alloc`
error: the allocator crate `boehm_gc` cannot depend on a crate that needs an allocator, but it depends on `alloc`
error: aborting due to 8 previous errors
Could not compile `gc_test`.
#![feature(allocator, no_std)]
#![allocator]
#![crate_type = "rlib"]
#![no_std]
extern crate libc;
use core::mem;
use libc::{size_t, c_void};
#[link(name = "gc")]
extern {
pub fn GC_malloc(nbytes: size_t) -> *mut c_void;
pub fn GC_malloc_atomic(nbytes: size_t) -> *mut c_void;
pub fn GC_malloc_uncollectable(nbytes: size_t) -> *mut c_void;
pub fn GC_realloc(old: *mut c_void, new_size: size_t) -> *mut c_void;
pub fn GC_free(dead: *mut c_void);
}
#[no_mangle]
pub extern "C" fn __rust_allocate(size: usize, _: usize) -> *mut u8 {
unsafe { GC_malloc_uncollectable(size as size_t) as *mut u8 }
}
#[no_mangle]
pub extern "C" fn __rust_deallocate(ptr: *mut u8, _: usize, _: usize) {
unsafe { GC_free(ptr as *mut c_void) }
}
#[no_mangle]
pub extern "C" fn __rust_reallocate(ptr: *mut u8, _: usize, size: usize, _: usize) -> *mut u8 {
unsafe { GC_realloc(ptr as *mut c_void, size as size_t) as *mut u8 }
}
#[no_mangle]
pub extern "C" fn __rust_reallocate_inplace(ptr: *mut u8, _: usize, size: usize, _: usize) -> *mut u8 {
unsafe { GC_realloc(ptr as *mut c_void, size as size_t) as *mut u8 }
}
#[no_mangle]
pub extern "C" fn __rust_usable_size(size: usize, _: usize) -> usize {
size
}
pub fn gc_allocate<T>() -> *mut T {
let size = mem::size_of::<T>();
unsafe {
GC_malloc(size as size_t) as *mut T
}
}
#![feature(allocator)]
extern crate boehm_gc;
fn main() {
println!("hello, world!");
let int_ptr = boehm_gc::gc_allocate::<i32>();
unsafe { *int_ptr = 42; }
let data = unsafe { *int_ptr };
println!("data: {}", data);
let boxed_ptr = Box::new(42);
println!("boxed data: {}", *boxed_ptr);
}
#![feature(allocator)]
extern crate boehm_gc;
fn main() {
println!("hello, world!");
let int_ptr = boehm_gc::gc_allocate::<i32>();
unsafe { *int_ptr = 42; }
let data = unsafe { *int_ptr };
println!("data: {}", data);
let boxed_ptr = Box::new(42);
println!("boxed data: {}", *boxed_ptr);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment