Refactor lib.rs

This commit is contained in:
Yota Toyama
2019-04-14 06:00:11 +00:00
parent 8372c90cf8
commit 4d54e545f1
4 changed files with 7 additions and 24 deletions

2
Cargo.lock generated
View File

@@ -1,7 +1,7 @@
# This file is automatically @generated by Cargo. # This file is automatically @generated by Cargo.
# It is not intended for manual editing. # It is not intended for manual editing.
[[package]] [[package]]
name = "gc" name = "bdwgc-allocator"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"libc 0.2.51 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.51 (registry+https://github.com/rust-lang/crates.io-index)",

View File

@@ -4,10 +4,7 @@ extern crate bdwgc_allocator;
static GLOBAL_ALLOCATOR: bdwgc_allocator::Allocator = bdwgc_allocator::Allocator; static GLOBAL_ALLOCATOR: bdwgc_allocator::Allocator = bdwgc_allocator::Allocator;
fn main() { fn main() {
unsafe { unsafe { bdwgc_allocator::Allocator::initialize() }
bdwgc_allocator::Allocator::initialize();
bdwgc_allocator::Allocator::start_gc();
}
let mut _n = bdwgc_allocator::Allocator::alloc(2 ^ 8); let mut _n = bdwgc_allocator::Allocator::alloc(2 ^ 8);

View File

@@ -4,10 +4,7 @@ extern crate bdwgc_allocator;
static GLOBAL_ALLOCATOR: bdwgc_allocator::Allocator = bdwgc_allocator::Allocator; static GLOBAL_ALLOCATOR: bdwgc_allocator::Allocator = bdwgc_allocator::Allocator;
fn main() { fn main() {
unsafe { unsafe { bdwgc_allocator::Allocator::initialize() }
bdwgc_allocator::Allocator::initialize();
bdwgc_allocator::Allocator::start_gc();
}
let handle = std::thread::spawn(move || { let handle = std::thread::spawn(move || {
bdwgc_allocator::Allocator::register_current_thread().unwrap(); bdwgc_allocator::Allocator::register_current_thread().unwrap();

View File

@@ -6,6 +6,7 @@ use libc::{c_int, c_void, size_t};
use std::alloc::{GlobalAlloc, Layout}; use std::alloc::{GlobalAlloc, Layout};
const GC_SUCCESS: c_int = 0; const GC_SUCCESS: c_int = 0;
const ALLOC_ALIGN: usize = 8;
#[repr(C)] #[repr(C)]
struct GCStackBase { struct GCStackBase {
@@ -20,12 +21,10 @@ extern "C" {
fn GC_get_stack_base(stack_base: *mut GCStackBase) -> c_int; fn GC_get_stack_base(stack_base: *mut GCStackBase) -> c_int;
fn GC_init() -> c_void; fn GC_init() -> c_void;
fn GC_malloc(size: size_t) -> *mut c_void; fn GC_malloc(size: size_t) -> *mut c_void;
fn GC_malloc_uncollectable(size: size_t) -> *mut c_void;
fn GC_register_my_thread(stack_base: *const GCStackBase) -> c_int; fn GC_register_my_thread(stack_base: *const GCStackBase) -> c_int;
fn GC_unregister_my_thread(); fn GC_unregister_my_thread();
} }
static mut GC_STARTED: bool = false;
pub struct Allocator; pub struct Allocator;
impl Allocator { impl Allocator {
@@ -34,10 +33,6 @@ impl Allocator {
GC_allow_register_threads(); GC_allow_register_threads();
} }
pub unsafe fn start_gc() {
GC_STARTED = true
}
pub fn register_current_thread() -> Result<(), error::Error> { pub fn register_current_thread() -> Result<(), error::Error> {
let mut base = GCStackBase { let mut base = GCStackBase {
mem_base: std::ptr::null(), mem_base: std::ptr::null(),
@@ -46,9 +41,7 @@ impl Allocator {
if unsafe { GC_get_stack_base(&mut base) } != GC_SUCCESS { if unsafe { GC_get_stack_base(&mut base) } != GC_SUCCESS {
return Err(error::Error::new("failed to get stack base")); return Err(error::Error::new("failed to get stack base"));
} } else if unsafe { GC_register_my_thread(&base) } != GC_SUCCESS {
if unsafe { GC_register_my_thread(&base) } != GC_SUCCESS {
return Err(error::Error::new("failed to register a thread for GC")); return Err(error::Error::new("failed to register a thread for GC"));
} }
@@ -60,17 +53,13 @@ impl Allocator {
} }
pub fn alloc(size: usize) -> *mut u8 { pub fn alloc(size: usize) -> *mut u8 {
unsafe { Allocator.alloc(Layout::from_size_align_unchecked(size, 8)) } unsafe { Allocator.alloc(Layout::from_size_align_unchecked(size, ALLOC_ALIGN)) }
} }
} }
unsafe impl GlobalAlloc for Allocator { unsafe impl GlobalAlloc for Allocator {
unsafe fn alloc(&self, layout: Layout) -> *mut u8 { unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
return if GC_STARTED { GC_malloc(layout.size()) as *mut u8
GC_malloc(layout.size())
} else {
GC_malloc_uncollectable(layout.size())
} as *mut u8;
} }
unsafe fn dealloc(&self, ptr: *mut u8, _layout: Layout) { unsafe fn dealloc(&self, ptr: *mut u8, _layout: Layout) {