diff --git a/.cspell.json b/.cspell.json index ddedd2a..11d5e88 100644 --- a/.cspell.json +++ b/.cspell.json @@ -1,11 +1,13 @@ { "words": [ + "bdwgc", "dealloc", "finalizer", "gcollect", "libc", "realloc", "repr", - "stackbottom" + "stackbottom", + "unregisters" ] } diff --git a/Cargo.toml b/Cargo.toml index 96593a9..262a2eb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,7 +9,6 @@ authors = [ repository = "https://github.com/raviqqe/bdwgc-alloc" edition = "2021" license = "MIT" -build = "build.rs" [features] default = ["autotools"] diff --git a/examples/Cargo.toml b/examples/Cargo.toml index 5cb5511..8454b9c 100644 --- a/examples/Cargo.toml +++ b/examples/Cargo.toml @@ -1,4 +1,5 @@ [workspace] +resolver = "2" members = [ "dynamic_threads", "free_by_borrow", diff --git a/examples/dynamic_threads/Cargo.toml b/examples/dynamic_threads/Cargo.toml index dcdd5f2..6ddf443 100644 --- a/examples/dynamic_threads/Cargo.toml +++ b/examples/dynamic_threads/Cargo.toml @@ -2,7 +2,7 @@ name = "dynamic_threads" version = "0.1.0" authors = ["Yota Toyama "] -edition = "2018" +edition = "2021" publish = false [dependencies] diff --git a/examples/dynamic_threads/src/main.rs b/examples/dynamic_threads/src/main.rs index bd3365f..915e2e3 100644 --- a/examples/dynamic_threads/src/main.rs +++ b/examples/dynamic_threads/src/main.rs @@ -1,5 +1,3 @@ -extern crate bdwgc_alloc; - use bdwgc_alloc::Allocator; use std::alloc::Layout; @@ -14,7 +12,7 @@ fn main() { unsafe { Allocator::register_current_thread().unwrap() } for _ in 0..100 { - unsafe { std::alloc::alloc(Layout::from_size_align(2 ^ 8, 8).unwrap()) }; + let _ = unsafe { std::alloc::alloc(Layout::from_size_align(2 ^ 8, 8).unwrap()) }; } unsafe { Allocator::unregister_current_thread() } diff --git a/examples/free_by_borrow/Cargo.toml b/examples/free_by_borrow/Cargo.toml index 9b49a48..79b4828 100644 --- a/examples/free_by_borrow/Cargo.toml +++ b/examples/free_by_borrow/Cargo.toml @@ -2,7 +2,7 @@ name = "free_by_borrow" version = "0.1.0" authors = ["Yota Toyama "] -edition = "2018" +edition = "2021" publish = false [dependencies] diff --git a/examples/free_by_borrow/src/main.rs b/examples/free_by_borrow/src/main.rs index 673df12..24bd128 100644 --- a/examples/free_by_borrow/src/main.rs +++ b/examples/free_by_borrow/src/main.rs @@ -1,5 +1,3 @@ -extern crate bdwgc_alloc; - use bdwgc_alloc::Allocator; #[global_allocator] diff --git a/examples/free_by_gc/Cargo.toml b/examples/free_by_gc/Cargo.toml index 49d09bd..74ca1a5 100644 --- a/examples/free_by_gc/Cargo.toml +++ b/examples/free_by_gc/Cargo.toml @@ -2,7 +2,7 @@ name = "free_by_gc" version = "0.1.0" authors = ["Yota Toyama "] -edition = "2018" +edition = "2021" publish = false [dependencies] diff --git a/examples/free_by_gc/src/main.rs b/examples/free_by_gc/src/main.rs index 94b26ef..2768d74 100644 --- a/examples/free_by_gc/src/main.rs +++ b/examples/free_by_gc/src/main.rs @@ -1,5 +1,3 @@ -extern crate bdwgc_alloc; - use bdwgc_alloc::Allocator; use std::alloc::Layout; @@ -10,6 +8,6 @@ fn main() { unsafe { Allocator::initialize() } loop { - unsafe { std::alloc::alloc(Layout::from_size_align(2 ^ 8, 8).unwrap()) }; + let _ = unsafe { std::alloc::alloc(Layout::from_size_align(2 ^ 8, 8).unwrap()) }; } } diff --git a/examples/static_threads/Cargo.toml b/examples/static_threads/Cargo.toml index aaa22b7..f6ee965 100644 --- a/examples/static_threads/Cargo.toml +++ b/examples/static_threads/Cargo.toml @@ -2,7 +2,7 @@ name = "static_threads" version = "0.1.0" authors = ["Yota Toyama "] -edition = "2018" +edition = "2021" publish = false [dependencies] diff --git a/examples/static_threads/src/main.rs b/examples/static_threads/src/main.rs index dda7581..fad7aa6 100644 --- a/examples/static_threads/src/main.rs +++ b/examples/static_threads/src/main.rs @@ -1,5 +1,3 @@ -extern crate bdwgc_alloc; - use bdwgc_alloc::Allocator; use std::alloc::Layout; @@ -13,7 +11,7 @@ fn main() { unsafe { Allocator::register_current_thread().unwrap() } loop { - unsafe { std::alloc::alloc(Layout::from_size_align(2 ^ 8, 8).unwrap()) }; + let _ = unsafe { std::alloc::alloc(Layout::from_size_align(2 ^ 8, 8).unwrap()) }; } }); diff --git a/src/lib.rs b/src/lib.rs index 5a6a9ad..f84c7cc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -42,19 +42,31 @@ extern "C" { pub struct Allocator; impl Allocator { + /// Locks a collector. pub fn lock() { unsafe { GC_alloc_lock() } } + /// Unlocks a collector. pub fn unlock() { unsafe { GC_alloc_unlock() } } + /// Initializes a collector. + /// + /// # Safety + /// + /// This function must be called in a main thread. pub unsafe fn initialize() { GC_init(); GC_allow_register_threads(); } + /// Registers a current thread to a collector. + /// + /// # Safety + /// + /// This function must not be called in a main thread. pub unsafe fn register_current_thread() -> Result<(), error::Error> { let mut base = GcStackBase { mem_base: null() }; @@ -67,6 +79,14 @@ impl Allocator { Ok(()) } + /// Sets a bottom of a stack. + /// + /// You do not have to call this function in most cases. + /// A collector detects the bottom on initialization automatically. + /// + /// # Safety + /// + /// The bottom address must be valid. pub unsafe fn set_stack_bottom(bottom: *const u8) { GC_set_stackbottom( null(), @@ -76,14 +96,25 @@ impl Allocator { ) } + /// Unregisters a current thread from a collector. + /// + /// # Safety + /// + /// The thread must be registered already. pub unsafe fn unregister_current_thread() { GC_unregister_my_thread() } + /// Runs a garbage collection forcibly. pub fn force_collect() { unsafe { GC_gcollect() } } + /// Registers a finalizer of an object. + /// + /// # Safety + /// + /// The given finalizer must not be null and handle pointers properly. pub unsafe fn register_finalizer( ptr: *const c_void, finalizer: extern "C" fn(*mut c_void, *mut c_void),