From 4d54e545f16d4a566f3812103ff4e2f1b1223f4a Mon Sep 17 00:00:00 2001 From: Yota Toyama Date: Sun, 14 Apr 2019 06:00:11 +0000 Subject: [PATCH] Refactor lib.rs --- Cargo.lock | 2 +- examples/allocation_loop/src/main.rs | 5 +---- examples/static_threads/src/main.rs | 5 +---- src/lib.rs | 19 ++++--------------- 4 files changed, 7 insertions(+), 24 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c4e887e..2360c08 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,7 +1,7 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. [[package]] -name = "gc" +name = "bdwgc-allocator" version = "0.1.0" dependencies = [ "libc 0.2.51 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/examples/allocation_loop/src/main.rs b/examples/allocation_loop/src/main.rs index 55c843b..b7c7d82 100644 --- a/examples/allocation_loop/src/main.rs +++ b/examples/allocation_loop/src/main.rs @@ -4,10 +4,7 @@ extern crate bdwgc_allocator; static GLOBAL_ALLOCATOR: bdwgc_allocator::Allocator = bdwgc_allocator::Allocator; fn main() { - unsafe { - bdwgc_allocator::Allocator::initialize(); - bdwgc_allocator::Allocator::start_gc(); - } + unsafe { bdwgc_allocator::Allocator::initialize() } let mut _n = bdwgc_allocator::Allocator::alloc(2 ^ 8); diff --git a/examples/static_threads/src/main.rs b/examples/static_threads/src/main.rs index 5ac6929..b6b24c6 100644 --- a/examples/static_threads/src/main.rs +++ b/examples/static_threads/src/main.rs @@ -4,10 +4,7 @@ extern crate bdwgc_allocator; static GLOBAL_ALLOCATOR: bdwgc_allocator::Allocator = bdwgc_allocator::Allocator; fn main() { - unsafe { - bdwgc_allocator::Allocator::initialize(); - bdwgc_allocator::Allocator::start_gc(); - } + unsafe { bdwgc_allocator::Allocator::initialize() } let handle = std::thread::spawn(move || { bdwgc_allocator::Allocator::register_current_thread().unwrap(); diff --git a/src/lib.rs b/src/lib.rs index 1086f09..9c30b61 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -6,6 +6,7 @@ use libc::{c_int, c_void, size_t}; use std::alloc::{GlobalAlloc, Layout}; const GC_SUCCESS: c_int = 0; +const ALLOC_ALIGN: usize = 8; #[repr(C)] struct GCStackBase { @@ -20,12 +21,10 @@ extern "C" { fn GC_get_stack_base(stack_base: *mut GCStackBase) -> c_int; fn GC_init() -> 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_unregister_my_thread(); } -static mut GC_STARTED: bool = false; pub struct Allocator; impl Allocator { @@ -34,10 +33,6 @@ impl Allocator { GC_allow_register_threads(); } - pub unsafe fn start_gc() { - GC_STARTED = true - } - pub fn register_current_thread() -> Result<(), error::Error> { let mut base = GCStackBase { mem_base: std::ptr::null(), @@ -46,9 +41,7 @@ impl Allocator { if unsafe { GC_get_stack_base(&mut base) } != GC_SUCCESS { return Err(error::Error::new("failed to get stack base")); - } - - if unsafe { GC_register_my_thread(&base) } != GC_SUCCESS { + } else if unsafe { GC_register_my_thread(&base) } != GC_SUCCESS { 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 { - 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 fn alloc(&self, layout: Layout) -> *mut u8 { - return if GC_STARTED { - GC_malloc(layout.size()) - } else { - GC_malloc_uncollectable(layout.size()) - } as *mut u8; + GC_malloc(layout.size()) as *mut u8 } unsafe fn dealloc(&self, ptr: *mut u8, _layout: Layout) {