diff --git a/examples/allocation_loop/src/main.rs b/examples/allocation_loop/src/main.rs index b7c7d82..6ea2fb3 100644 --- a/examples/allocation_loop/src/main.rs +++ b/examples/allocation_loop/src/main.rs @@ -1,14 +1,17 @@ extern crate bdwgc_allocator; +use bdwgc_allocator::Allocator; +use std::alloc::{GlobalAlloc, Layout}; + #[global_allocator] -static GLOBAL_ALLOCATOR: bdwgc_allocator::Allocator = bdwgc_allocator::Allocator; +static GLOBAL_ALLOCATOR: Allocator = Allocator; fn main() { - unsafe { bdwgc_allocator::Allocator::initialize() } + unsafe { Allocator::initialize() } - let mut _n = bdwgc_allocator::Allocator::alloc(2 ^ 8); + let mut _n = unsafe { GLOBAL_ALLOCATOR.alloc(Layout::from_size_align(2 ^ 8, 8).unwrap()) }; loop { - _n = bdwgc_allocator::Allocator::alloc(2 ^ 8) + _n = unsafe { GLOBAL_ALLOCATOR.alloc(Layout::from_size_align(2 ^ 8, 8).unwrap()) }; } } diff --git a/examples/static_threads/src/main.rs b/examples/static_threads/src/main.rs index b6b24c6..26641f2 100644 --- a/examples/static_threads/src/main.rs +++ b/examples/static_threads/src/main.rs @@ -1,18 +1,21 @@ extern crate bdwgc_allocator; +use bdwgc_allocator::Allocator; +use std::alloc::{GlobalAlloc, Layout}; + #[global_allocator] -static GLOBAL_ALLOCATOR: bdwgc_allocator::Allocator = bdwgc_allocator::Allocator; +static GLOBAL_ALLOCATOR: Allocator = Allocator; fn main() { - unsafe { bdwgc_allocator::Allocator::initialize() } + unsafe { Allocator::initialize() } let handle = std::thread::spawn(move || { - bdwgc_allocator::Allocator::register_current_thread().unwrap(); + Allocator::register_current_thread().unwrap(); - let mut _n = bdwgc_allocator::Allocator::alloc(2 ^ 8); + let mut _n = unsafe { GLOBAL_ALLOCATOR.alloc(Layout::from_size_align(2 ^ 8, 8).unwrap()) }; loop { - _n = bdwgc_allocator::Allocator::alloc(2 ^ 8) + _n = unsafe { GLOBAL_ALLOCATOR.alloc(Layout::from_size_align(2 ^ 8, 8).unwrap()) } } }); diff --git a/src/lib.rs b/src/lib.rs index 9c30b61..d4c8aad 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -6,7 +6,6 @@ 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 { @@ -51,10 +50,6 @@ impl Allocator { pub fn unregister_current_thread() { unsafe { GC_unregister_my_thread() } } - - pub fn alloc(size: usize) -> *mut u8 { - unsafe { Allocator.alloc(Layout::from_size_align_unchecked(size, ALLOC_ALIGN)) } - } } unsafe impl GlobalAlloc for Allocator {