This commit is contained in:
Yota Toyama
2023-09-28 12:51:38 +10:00
committed by GitHub
parent 7f2429fe25
commit 1e54b65b9e
12 changed files with 42 additions and 17 deletions

View File

@@ -1,11 +1,13 @@
{ {
"words": [ "words": [
"bdwgc",
"dealloc", "dealloc",
"finalizer", "finalizer",
"gcollect", "gcollect",
"libc", "libc",
"realloc", "realloc",
"repr", "repr",
"stackbottom" "stackbottom",
"unregisters"
] ]
} }

View File

@@ -9,7 +9,6 @@ authors = [
repository = "https://github.com/raviqqe/bdwgc-alloc" repository = "https://github.com/raviqqe/bdwgc-alloc"
edition = "2021" edition = "2021"
license = "MIT" license = "MIT"
build = "build.rs"
[features] [features]
default = ["autotools"] default = ["autotools"]

View File

@@ -1,4 +1,5 @@
[workspace] [workspace]
resolver = "2"
members = [ members = [
"dynamic_threads", "dynamic_threads",
"free_by_borrow", "free_by_borrow",

View File

@@ -2,7 +2,7 @@
name = "dynamic_threads" name = "dynamic_threads"
version = "0.1.0" version = "0.1.0"
authors = ["Yota Toyama <raviqqe@gmail.com>"] authors = ["Yota Toyama <raviqqe@gmail.com>"]
edition = "2018" edition = "2021"
publish = false publish = false
[dependencies] [dependencies]

View File

@@ -1,5 +1,3 @@
extern crate bdwgc_alloc;
use bdwgc_alloc::Allocator; use bdwgc_alloc::Allocator;
use std::alloc::Layout; use std::alloc::Layout;
@@ -14,7 +12,7 @@ fn main() {
unsafe { Allocator::register_current_thread().unwrap() } unsafe { Allocator::register_current_thread().unwrap() }
for _ in 0..100 { 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() } unsafe { Allocator::unregister_current_thread() }

View File

@@ -2,7 +2,7 @@
name = "free_by_borrow" name = "free_by_borrow"
version = "0.1.0" version = "0.1.0"
authors = ["Yota Toyama <raviqqe@gmail.com>"] authors = ["Yota Toyama <raviqqe@gmail.com>"]
edition = "2018" edition = "2021"
publish = false publish = false
[dependencies] [dependencies]

View File

@@ -1,5 +1,3 @@
extern crate bdwgc_alloc;
use bdwgc_alloc::Allocator; use bdwgc_alloc::Allocator;
#[global_allocator] #[global_allocator]

View File

@@ -2,7 +2,7 @@
name = "free_by_gc" name = "free_by_gc"
version = "0.1.0" version = "0.1.0"
authors = ["Yota Toyama <raviqqe@gmail.com>"] authors = ["Yota Toyama <raviqqe@gmail.com>"]
edition = "2018" edition = "2021"
publish = false publish = false
[dependencies] [dependencies]

View File

@@ -1,5 +1,3 @@
extern crate bdwgc_alloc;
use bdwgc_alloc::Allocator; use bdwgc_alloc::Allocator;
use std::alloc::Layout; use std::alloc::Layout;
@@ -10,6 +8,6 @@ fn main() {
unsafe { Allocator::initialize() } unsafe { Allocator::initialize() }
loop { 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()) };
} }
} }

View File

@@ -2,7 +2,7 @@
name = "static_threads" name = "static_threads"
version = "0.1.0" version = "0.1.0"
authors = ["Yota Toyama <raviqqe@gmail.com>"] authors = ["Yota Toyama <raviqqe@gmail.com>"]
edition = "2018" edition = "2021"
publish = false publish = false
[dependencies] [dependencies]

View File

@@ -1,5 +1,3 @@
extern crate bdwgc_alloc;
use bdwgc_alloc::Allocator; use bdwgc_alloc::Allocator;
use std::alloc::Layout; use std::alloc::Layout;
@@ -13,7 +11,7 @@ fn main() {
unsafe { Allocator::register_current_thread().unwrap() } unsafe { Allocator::register_current_thread().unwrap() }
loop { 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()) };
} }
}); });

View File

@@ -42,19 +42,31 @@ extern "C" {
pub struct Allocator; pub struct Allocator;
impl Allocator { impl Allocator {
/// Locks a collector.
pub fn lock() { pub fn lock() {
unsafe { GC_alloc_lock() } unsafe { GC_alloc_lock() }
} }
/// Unlocks a collector.
pub fn unlock() { pub fn unlock() {
unsafe { GC_alloc_unlock() } unsafe { GC_alloc_unlock() }
} }
/// Initializes a collector.
///
/// # Safety
///
/// This function must be called in a main thread.
pub unsafe fn initialize() { pub unsafe fn initialize() {
GC_init(); GC_init();
GC_allow_register_threads(); 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> { pub unsafe fn register_current_thread() -> Result<(), error::Error> {
let mut base = GcStackBase { mem_base: null() }; let mut base = GcStackBase { mem_base: null() };
@@ -67,6 +79,14 @@ impl Allocator {
Ok(()) 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) { pub unsafe fn set_stack_bottom(bottom: *const u8) {
GC_set_stackbottom( GC_set_stackbottom(
null(), 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() { pub unsafe fn unregister_current_thread() {
GC_unregister_my_thread() GC_unregister_my_thread()
} }
/// Runs a garbage collection forcibly.
pub fn force_collect() { pub fn force_collect() {
unsafe { GC_gcollect() } 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( pub unsafe fn register_finalizer(
ptr: *const c_void, ptr: *const c_void,
finalizer: extern "C" fn(*mut c_void, *mut c_void), finalizer: extern "C" fn(*mut c_void, *mut c_void),