From 2f26b40e30444d44616eb9eee096e6bdf5902c65 Mon Sep 17 00:00:00 2001 From: Yota Toyama Date: Fri, 25 Sep 2026 20:43:48 -0700 Subject: [PATCH] `sys` crate (#583) Fix #508. --- .github/workflows/lint.yaml | 2 +- .gitmodules | 4 +- Cargo.lock | 211 +++++++++++++++++- Cargo.toml | 31 +-- README.md | 2 + bdwgc-alloc-sys/Cargo.toml | 25 +++ bdwgc-alloc-sys/README.md | 28 +++ build.rs => bdwgc-alloc-sys/build.rs | 36 ++- bdwgc-alloc-sys/src/lib.rs | 8 + bdwgc-alloc-sys/tests/collector.rs | 76 +++++++ {vendor => bdwgc-alloc-sys/vendor}/bdwgc | 0 .../vendor}/libatomic_ops | 0 bdwgc-alloc/Cargo.toml | 20 ++ bdwgc-alloc/README.md | 1 + {src => bdwgc-alloc/src}/error.rs | 0 {src => bdwgc-alloc/src}/lib.rs | 62 ++--- cspell.json | 2 + examples/dynamic_threads/Cargo.toml | 2 +- examples/free_by_borrow/Cargo.toml | 2 +- examples/free_by_gc/Cargo.toml | 2 +- examples/static_threads/Cargo.toml | 2 +- 21 files changed, 436 insertions(+), 80 deletions(-) create mode 100644 bdwgc-alloc-sys/Cargo.toml create mode 100644 bdwgc-alloc-sys/README.md rename build.rs => bdwgc-alloc-sys/build.rs (73%) create mode 100644 bdwgc-alloc-sys/src/lib.rs create mode 100644 bdwgc-alloc-sys/tests/collector.rs rename {vendor => bdwgc-alloc-sys/vendor}/bdwgc (100%) rename {vendor => bdwgc-alloc-sys/vendor}/libatomic_ops (100%) create mode 100644 bdwgc-alloc/Cargo.toml create mode 120000 bdwgc-alloc/README.md rename {src => bdwgc-alloc/src}/error.rs (100%) rename {src => bdwgc-alloc/src}/lib.rs (66%) diff --git a/.github/workflows/lint.yaml b/.github/workflows/lint.yaml index 78a4b00..009a15c 100644 --- a/.github/workflows/lint.yaml +++ b/.github/workflows/lint.yaml @@ -17,7 +17,7 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 - - run: cargo fmt -- --check + - run: cargo fmt --all -- --check spell-check: runs-on: ubuntu-latest steps: diff --git a/.gitmodules b/.gitmodules index 3a83d65..1dba229 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,6 +1,6 @@ [submodule "vendor/bdwgc"] - path = vendor/bdwgc + path = bdwgc-alloc-sys/vendor/bdwgc url = https://github.com/bdwgc/bdwgc [submodule "vendor/libatomic_ops"] - path = vendor/libatomic_ops + path = bdwgc-alloc-sys/vendor/libatomic_ops url = https://github.com/bdwgc/libatomic_ops diff --git a/Cargo.lock b/Cargo.lock index 8813658..88d3119 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,15 @@ # It is not intended for manual editing. version = 4 +[[package]] +name = "aho-corasick" +version = "1.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c982642fa9e8606056828ee9a8505737230110bb1099153c79efe865c59d12ba" +dependencies = [ + "memchr", +] + [[package]] name = "autotools" version = "0.2.7" @@ -15,11 +24,43 @@ dependencies = [ name = "bdwgc-alloc" version = "0.6.15" dependencies = [ - "autotools", - "cmake", - "libc", + "bdwgc-alloc-sys", ] +[[package]] +name = "bdwgc-alloc-sys" +version = "0.6.15" +dependencies = [ + "autotools", + "bindgen", + "cmake", +] + +[[package]] +name = "bindgen" +version = "0.73.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "787ef8ef523575546b106a58213d6e6b06198a05c2f757258c68a74273670cfa" +dependencies = [ + "bitflags", + "cexpr", + "clang-sys", + "log", + "prettyplease", + "proc-macro2", + "quote", + "regex", + "rustc-hash", + "shlex 2.0.1", + "syn", +] + +[[package]] +name = "bitflags" +version = "2.13.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3ded4057c258ba199e2d26386d3af3780957ecaee6c4ef4041c6b4b8b97c0b06" + [[package]] name = "cc" version = "1.2.49" @@ -27,7 +68,33 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "90583009037521a116abf44494efecd645ba48b6622457080f080b85544e2215" dependencies = [ "find-msvc-tools", - "shlex", + "shlex 1.3.0", +] + +[[package]] +name = "cexpr" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6fac387a98bb7c37292057cffc56d62ecb629900026402633ae9160df93a8766" +dependencies = [ + "nom", +] + +[[package]] +name = "cfg-if" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4e7648175b45a9a48536d676f68d918270699102aa8dab5496df06904c914600" + +[[package]] +name = "clang-sys" +version = "1.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "157a8ba7b480713b56f4c09fd13fc3e0a22a5dfab8097ba61cbc5feef950788a" +dependencies = [ + "glob", + "libc", + "libloading", ] [[package]] @@ -66,21 +133,157 @@ dependencies = [ "bdwgc-alloc", ] +[[package]] +name = "glob" +version = "0.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e4eba85ea1d0a966a983acd07deee566e67395d2d96b6fb39e62b5a833f1eb0b" + [[package]] name = "libc" version = "0.2.189" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3eaf3ede3fee6db1a4c2ee091bf8a8b4dccdc6d17f656fb07896ee72867612f2" +[[package]] +name = "libloading" +version = "0.8.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d7c4b02199fee7c5d21a5ae7d8cfa79a6ef5bb2fc834d6e9058e89c825efdc55" +dependencies = [ + "cfg-if", + "windows-link", +] + +[[package]] +name = "log" +version = "0.4.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f9f8bd3e56ce4dfc153cf470fffbfa98c7620958b312ca5c3a4b8d5181fd13c6" + +[[package]] +name = "memchr" +version = "2.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf8baf1c55e62ffcace7a9f06f4bd9cd3f0c4beb022d3b367256b91b87513d98" + +[[package]] +name = "minimal-lexical" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a" + +[[package]] +name = "nom" +version = "7.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d273983c5a657a70a3e8f2a01329822f3b8c8172b73826411a55751e404a0a4a" +dependencies = [ + "memchr", + "minimal-lexical", +] + +[[package]] +name = "prettyplease" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2bfe0f4c752e450fc2faf62654f1c134747922825d5b04ca717b8874f41a40c0" +dependencies = [ + "proc-macro2", + "syn", +] + +[[package]] +name = "proc-macro2" +version = "1.0.107" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "985e7ec9bb745e6ce6535b544d84d6cd6f7ad8bd711c398938ae983b91a766d9" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.47" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fbf4db142a473a8d80c26bbf18454ed458bf8d26c8219c331daecfdbd079001" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "regex" +version = "1.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f020237b6c8eed93db2e2cb53c00c60a8e1bc73da7d073199a1180401450218d" +dependencies = [ + "aho-corasick", + "memchr", + "regex-automata", + "regex-syntax", +] + +[[package]] +name = "regex-automata" +version = "0.4.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ad8553b9b26413251cbf30e620595c7a41b3887f03da04579c0e6b0d6a06b4b2" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.8.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d6f6ff9a378485b298a5286656da665ba74413d36db0979633275d2e708145d4" + +[[package]] +name = "rustc-hash" +version = "2.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6b1e7f9a428571be2dc5bc0505c13fb6bf936822b894ec87abf8a08a4e51742d" + [[package]] name = "shlex" version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" +[[package]] +name = "shlex" +version = "2.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8fadd59c855ef2080decdef8ff161eb6661b86933c9d82e5ba29dc602a55aba" + [[package]] name = "static_threads" version = "0.1.0" dependencies = [ "bdwgc-alloc", ] + +[[package]] +name = "syn" +version = "3.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8593e8e72159ed2257d083c7a454a85cbf854f37a0966d8d483aff8c8a3ebcee" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "unicode-ident" +version = "1.0.26" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d245f478577f809a851594d02313b640fb437e0bb33866753cff937863096954" + +[[package]] +name = "windows-link" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5" diff --git a/Cargo.toml b/Cargo.toml index b86618b..251a100 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,34 +1,21 @@ -[package] -name = "bdwgc-alloc" -version = "0.6.15" -edition = "2024" -description = "impl GlobalAlloc for bdwgc" -documentation = "https://bdwgc.github.io/bdwgc-rust/bdwgc_alloc/" -repository = "https://github.com/raviqqe/bdwgc-alloc" -license = "MIT" -categories = ["api-bindings", "memory-management", "no-std", "no-std::no-alloc"] - [workspace] resolver = "3" -members = ["examples/*"] +members = ["bdwgc-alloc", "bdwgc-alloc-sys", "examples/*"] +default-members = ["bdwgc-alloc", "bdwgc-alloc-sys"] -[dependencies] -libc = "0.2.186" +[workspace.package] +version = "0.6.15" +edition = "2024" +repository = "https://github.com/bdwgc/bdwgc-rust" +license = "MIT" -[build-dependencies] -autotools = { version = "0.2.7", optional = true } -cmake = { version = "0.1.58", optional = true } - -[features] -default = ["autotools"] - -[lints.clippy] +[workspace.lints.clippy] alloc_instead_of_core = "deny" dbg_macro = "deny" missing_const_for_fn = "deny" std_instead_of_alloc = "deny" std_instead_of_core = "deny" -[lints.rust] +[workspace.lints.rust] missing_docs = "deny" warnings = "deny" diff --git a/README.md b/README.md index e08e12f..d96a524 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,8 @@ This crate is for use cases in which developers need to integrate [`bdwgc`][bdwgc] into their programs written in Rust (e.g. writing a runtime library in Rust for their own programming language whose GC is done by [`bdwgc`][bdwgc].) +The raw bindings to the C API of [`bdwgc`][bdwgc] that this crate is built on are available as the `bdwgc-alloc-sys` crate. + ## Usage See [`examples`](https://github.com/bdwgc/bdwgc-rust/tree/main/examples) directory. diff --git a/bdwgc-alloc-sys/Cargo.toml b/bdwgc-alloc-sys/Cargo.toml new file mode 100644 index 0000000..a1f6a2e --- /dev/null +++ b/bdwgc-alloc-sys/Cargo.toml @@ -0,0 +1,25 @@ +[package] +name = "bdwgc-alloc-sys" +version.workspace = true +edition.workspace = true +description = "Rust bindings to the bdwgc C API" +documentation = "https://bdwgc.github.io/bdwgc-rust/bdwgc_alloc_sys/" +repository.workspace = true +license.workspace = true +categories = ["external-ffi-bindings", "memory-management", "no-std", "no-std::no-alloc"] +links = "gc" + +[build-dependencies] +autotools = { version = "0.2.7", optional = true } +bindgen = "0.73.2" +cmake = { version = "0.1.58", optional = true } + +[features] +default = ["autotools"] + +[lints] +workspace = true + +[[test]] +name = "collector" +harness = false diff --git a/bdwgc-alloc-sys/README.md b/bdwgc-alloc-sys/README.md new file mode 100644 index 0000000..c3c87b4 --- /dev/null +++ b/bdwgc-alloc-sys/README.md @@ -0,0 +1,28 @@ +# bdwgc-alloc-sys + +[![GitHub Action](https://img.shields.io/github/actions/workflow/status/bdwgc/bdwgc-rust/test.yaml?branch=main&style=flat-square)](https://github.com/bdwgc/bdwgc-rust/actions) +[![License](https://img.shields.io/github/license/bdwgc/bdwgc-rust.svg?style=flat-square)](https://github.com/bdwgc/bdwgc-rust/blob/main/LICENSE) + +Rust bindings to the C API of [`bdwgc`][bdwgc], the conservative garbage collector. + +This crate builds [`bdwgc`][bdwgc] from its vendored source and links it statically. The bindings are generated from its headers at build time by [`bindgen`](https://github.com/rust-lang/rust-bindgen), which requires `libclang`. + +See the [`bdwgc-alloc`](https://crates.io/crates/bdwgc-alloc) crate for a [`GlobalAlloc`](https://doc.rust-lang.org/std/alloc/trait.GlobalAlloc.html) implementation on top of these bindings. + +## Install + +```sh +cargo add bdwgc-alloc-sys +``` + +By default [`bdwgc`][bdwgc] is built with autotools. To build with cmake, enable the `cmake` feature: + +```sh +cargo add bdwgc-alloc-sys --no-default-features --features cmake +``` + +## License + +[MIT](https://github.com/bdwgc/bdwgc-rust/blob/main/LICENSE) + +[bdwgc]: https://github.com/bdwgc/bdwgc diff --git a/build.rs b/bdwgc-alloc-sys/build.rs similarity index 73% rename from build.rs rename to bdwgc-alloc-sys/build.rs index 2118225..a2e82bf 100644 --- a/build.rs +++ b/bdwgc-alloc-sys/build.rs @@ -1,18 +1,36 @@ //! A build script. +use core::error::Error; +use std::{env, path::PathBuf}; + const LIB_ATOMIC_OPS_DIR: &str = "vendor/libatomic_ops"; const LIB_GC_DIR: &str = "vendor/bdwgc"; +fn main() -> Result<(), Box> { + build_library()?; + + bindgen::builder() + .header(format!("{LIB_GC_DIR}/include/gc.h")) + .clang_arg(format!("-I{LIB_GC_DIR}/include")) + .clang_arg("-DGC_THREADS") + .use_core() + .allowlist_item("GC_.*") + .default_macro_constant_type(bindgen::MacroTypeVariation::Signed) + .generate()? + .write_to_file(PathBuf::from(env::var("OUT_DIR")?).join("bindings.rs"))?; + + Ok(()) +} + cfg_select! { feature = "cmake" => { - fn main() { + fn build_library() -> Result<(), Box> { use cmake::Config; use std::path::Path; let libatomic_include_path = Path::new(LIB_ATOMIC_OPS_DIR) .join("src") - .canonicalize() - .unwrap() + .canonicalize()? .display() .to_string() .replace(r"\\?\", ""); @@ -28,16 +46,17 @@ cfg_select! { dst.join("lib").display() ); println!("cargo:rustc-link-lib=static=gc"); + + Ok(()) } } feature = "autotools" => { - fn main() { + fn build_library() -> Result<(), Box> { for dir in &[LIB_ATOMIC_OPS_DIR, LIB_GC_DIR] { std::process::Command::new("sh") .arg("-c") .arg(format!("cd {dir} && ./autogen.sh")) - .output() - .unwrap(); + .output()?; } let dst = autotools::Config::new(LIB_ATOMIC_OPS_DIR) @@ -68,9 +87,10 @@ cfg_select! { std::process::Command::new("sh") .arg("-c") .arg(format!("cd {dir} && git clean -dfx")) - .output() - .unwrap(); + .output()?; } + + Ok(()) } } } diff --git a/bdwgc-alloc-sys/src/lib.rs b/bdwgc-alloc-sys/src/lib.rs new file mode 100644 index 0000000..6d9a9d6 --- /dev/null +++ b/bdwgc-alloc-sys/src/lib.rs @@ -0,0 +1,8 @@ +#![doc = include_str!("../README.md")] +#![no_std] +#![allow(missing_docs)] +#![allow(non_camel_case_types)] +#![allow(non_snake_case)] +#![allow(non_upper_case_globals)] + +include!(concat!(env!("OUT_DIR"), "/bindings.rs")); diff --git a/bdwgc-alloc-sys/tests/collector.rs b/bdwgc-alloc-sys/tests/collector.rs new file mode 100644 index 0000000..a017dc8 --- /dev/null +++ b/bdwgc-alloc-sys/tests/collector.rs @@ -0,0 +1,76 @@ +//! A smoke test of the bindings. + +use bdwgc_alloc_sys::{ + GC_SUCCESS, GC_allow_register_threads, GC_free, GC_gcollect, GC_get_stack_base, GC_init, + GC_malloc, GC_realloc, GC_register_finalizer, GC_register_my_thread, GC_stack_base, + GC_unregister_my_thread, +}; +use core::{ + ffi::c_void, + ptr::null_mut, + sync::atomic::{AtomicUsize, Ordering}, +}; +use std::thread::spawn; + +const TOTAL_COUNT: usize = 1 << 10; + +static FINALIZED_COUNT: AtomicUsize = AtomicUsize::new(0); + +fn main() { + unsafe { + GC_init(); + GC_allow_register_threads(); + } + + allocate(); + finalize(); + register_thread(); +} + +fn allocate() { + let ptr = unsafe { GC_malloc(42) }; + assert!(!ptr.is_null()); + + let ptr = unsafe { GC_realloc(ptr, 84) }; + assert!(!ptr.is_null()); + + unsafe { GC_free(ptr) }; +} + +fn finalize() { + extern "C" fn count(_object: *mut c_void, _client_data: *mut c_void) { + FINALIZED_COUNT.fetch_add(1, Ordering::Relaxed); + } + + for _ in 0..TOTAL_COUNT { + unsafe { + GC_register_finalizer( + GC_malloc(42), + Some(count), + null_mut(), + null_mut(), + null_mut(), + ); + } + } + + unsafe { GC_gcollect() }; + + // TODO Is there any way to collect all? + assert!(FINALIZED_COUNT.load(Ordering::Relaxed) > TOTAL_COUNT / 2); +} + +fn register_thread() { + spawn(|| { + let mut base = GC_stack_base { + mem_base: null_mut(), + }; + + assert_eq!(unsafe { GC_get_stack_base(&mut base) }, GC_SUCCESS); + assert_eq!(unsafe { GC_register_my_thread(&base) }, GC_SUCCESS); + assert!(!unsafe { GC_malloc(42) }.is_null()); + assert_eq!(unsafe { GC_unregister_my_thread() }, GC_SUCCESS); + }) + .join() + .unwrap(); +} diff --git a/vendor/bdwgc b/bdwgc-alloc-sys/vendor/bdwgc similarity index 100% rename from vendor/bdwgc rename to bdwgc-alloc-sys/vendor/bdwgc diff --git a/vendor/libatomic_ops b/bdwgc-alloc-sys/vendor/libatomic_ops similarity index 100% rename from vendor/libatomic_ops rename to bdwgc-alloc-sys/vendor/libatomic_ops diff --git a/bdwgc-alloc/Cargo.toml b/bdwgc-alloc/Cargo.toml new file mode 100644 index 0000000..676a52d --- /dev/null +++ b/bdwgc-alloc/Cargo.toml @@ -0,0 +1,20 @@ +[package] +name = "bdwgc-alloc" +version.workspace = true +edition.workspace = true +description = "impl GlobalAlloc for bdwgc" +documentation = "https://bdwgc.github.io/bdwgc-rust/bdwgc_alloc/" +repository.workspace = true +license.workspace = true +categories = ["api-bindings", "memory-management", "no-std", "no-std::no-alloc"] + +[dependencies] +bdwgc-alloc-sys = { version = "0.6.15", path = "../bdwgc-alloc-sys", default-features = false } + +[features] +default = ["autotools"] +autotools = ["bdwgc-alloc-sys/autotools"] +cmake = ["bdwgc-alloc-sys/cmake"] + +[lints] +workspace = true diff --git a/bdwgc-alloc/README.md b/bdwgc-alloc/README.md new file mode 120000 index 0000000..32d46ee --- /dev/null +++ b/bdwgc-alloc/README.md @@ -0,0 +1 @@ +../README.md \ No newline at end of file diff --git a/src/error.rs b/bdwgc-alloc/src/error.rs similarity index 100% rename from src/error.rs rename to bdwgc-alloc/src/error.rs diff --git a/src/lib.rs b/bdwgc-alloc/src/lib.rs similarity index 66% rename from src/lib.rs rename to bdwgc-alloc/src/lib.rs index 144c0ab..017df7b 100644 --- a/src/lib.rs +++ b/bdwgc-alloc/src/lib.rs @@ -3,42 +3,16 @@ mod error; +use bdwgc_alloc_sys::{ + GC_SUCCESS, GC_alloc_lock, GC_alloc_unlock, GC_allow_register_threads, GC_free, GC_gcollect, + GC_get_stack_base, GC_init, GC_malloc, GC_realloc, GC_register_finalizer, + GC_register_my_thread, GC_set_stackbottom, GC_stack_base, GC_unregister_my_thread, +}; use core::{ alloc::{GlobalAlloc, Layout}, - ptr::null, + ffi::c_void, + ptr::null_mut, }; -use libc::{c_int, c_void, size_t}; - -const GC_SUCCESS: c_int = 0; - -#[repr(C)] -struct GcStackBase { - mem_base: *const c_void, - // TODO: Add reg_base field to support IA64. -} - -#[link(name = "gc", kind = "static")] -unsafe extern "C" { - fn GC_allow_register_threads(); - fn GC_alloc_lock(); - fn GC_alloc_unlock(); - fn GC_free(ptr: *mut c_void); - fn GC_get_stack_base(stack_base: *mut GcStackBase) -> c_int; - fn GC_init(); - fn GC_malloc(size: size_t) -> *mut c_void; - fn GC_realloc(ptr: *mut c_void, size: size_t) -> *mut c_void; - fn GC_register_my_thread(stack_base: *const GcStackBase) -> c_int; - fn GC_set_stackbottom(thread: *const c_void, stack_bottom: *const GcStackBase); - fn GC_unregister_my_thread(); - fn GC_gcollect(); - fn GC_register_finalizer( - ptr: *const c_void, - finalizer: extern "C" fn(*mut c_void, *mut c_void), - client_data: *const c_void, - opt_old_finalizer: *const c_void, - opt_old_client_data: *const c_void, - ) -> *mut c_void; -} /// An allocator. pub struct Allocator; @@ -72,7 +46,9 @@ impl Allocator { /// /// 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() }; + let mut base = GC_stack_base { + mem_base: null_mut(), + }; if unsafe { GC_get_stack_base(&mut base) } != GC_SUCCESS { return Err(error::Error::new("failed to get stack base")); @@ -94,9 +70,9 @@ impl Allocator { pub unsafe fn set_stack_bottom(bottom: *const u8) { unsafe { GC_set_stackbottom( - null(), - &GcStackBase { - mem_base: bottom as *const libc::c_void, + null_mut(), + &GC_stack_base { + mem_base: bottom.cast_mut().cast(), }, ) } @@ -108,7 +84,7 @@ impl Allocator { /// /// The thread must be registered already. pub unsafe fn unregister_current_thread() { - unsafe { GC_unregister_my_thread() } + unsafe { GC_unregister_my_thread() }; } /// Runs a garbage collection forcibly. @@ -126,7 +102,15 @@ impl Allocator { finalizer: extern "C" fn(*mut c_void, *mut c_void), client_data: *const c_void, ) { - unsafe { GC_register_finalizer(ptr, finalizer, client_data, null(), null()) }; + unsafe { + GC_register_finalizer( + ptr.cast_mut(), + Some(finalizer), + client_data.cast_mut(), + null_mut(), + null_mut(), + ) + }; } } diff --git a/cspell.json b/cspell.json index 831e11e..162eef4 100644 --- a/cspell.json +++ b/cspell.json @@ -3,6 +3,7 @@ "autogen", "autotools", "bdwgc", + "bindgen", "canonicalize", "cflag", "clippy", @@ -11,6 +12,7 @@ "gcollect", "libatomic", "libc", + "libclang", "realloc", "repr", "rustc", diff --git a/examples/dynamic_threads/Cargo.toml b/examples/dynamic_threads/Cargo.toml index e09628c..b83beb3 100644 --- a/examples/dynamic_threads/Cargo.toml +++ b/examples/dynamic_threads/Cargo.toml @@ -5,4 +5,4 @@ edition = "2024" publish = false [dependencies] -bdwgc-alloc = { path = "../.." } +bdwgc-alloc = { path = "../../bdwgc-alloc" } diff --git a/examples/free_by_borrow/Cargo.toml b/examples/free_by_borrow/Cargo.toml index 2a2b32a..32f2368 100644 --- a/examples/free_by_borrow/Cargo.toml +++ b/examples/free_by_borrow/Cargo.toml @@ -5,4 +5,4 @@ edition = "2024" publish = false [dependencies] -bdwgc-alloc = { path = "../.." } +bdwgc-alloc = { path = "../../bdwgc-alloc" } diff --git a/examples/free_by_gc/Cargo.toml b/examples/free_by_gc/Cargo.toml index 1e64439..81ff4db 100644 --- a/examples/free_by_gc/Cargo.toml +++ b/examples/free_by_gc/Cargo.toml @@ -5,4 +5,4 @@ edition = "2024" publish = false [dependencies] -bdwgc-alloc = { path = "../.." } +bdwgc-alloc = { path = "../../bdwgc-alloc" } diff --git a/examples/static_threads/Cargo.toml b/examples/static_threads/Cargo.toml index 0124deb..ac3ad5b 100644 --- a/examples/static_threads/Cargo.toml +++ b/examples/static_threads/Cargo.toml @@ -5,4 +5,4 @@ edition = "2024" publish = false [dependencies] -bdwgc-alloc = { path = "../.." } +bdwgc-alloc = { path = "../../bdwgc-alloc" }