From 5c78f83857502df0cccf2a25978166a9e44ba5a8 Mon Sep 17 00:00:00 2001 From: Yota Toyama Date: Sat, 26 Sep 2026 12:48:31 -0700 Subject: [PATCH] Example tests in crate --- .github/workflows/test.yaml | 8 ----- Cargo.lock | 28 --------------- Cargo.toml | 3 +- README.md | 26 +++++++++++++- bdwgc-alloc/Cargo.toml | 4 +++ bdwgc-alloc/tests/allocator.rs | 53 ++++++++++++++++++++++++++++ examples/dynamic_threads/Cargo.toml | 8 ----- examples/dynamic_threads/src/main.rs | 23 ------------ examples/free_by_borrow/Cargo.toml | 8 ----- examples/free_by_borrow/src/main.rs | 14 -------- examples/free_by_gc/Cargo.toml | 8 ----- examples/free_by_gc/src/main.rs | 13 ------- examples/static_threads/Cargo.toml | 8 ----- examples/static_threads/src/main.rs | 19 ---------- tools/example_test.sh | 20 ----------- 15 files changed, 83 insertions(+), 160 deletions(-) create mode 100644 bdwgc-alloc/tests/allocator.rs delete mode 100644 examples/dynamic_threads/Cargo.toml delete mode 100644 examples/dynamic_threads/src/main.rs delete mode 100644 examples/free_by_borrow/Cargo.toml delete mode 100644 examples/free_by_borrow/src/main.rs delete mode 100644 examples/free_by_gc/Cargo.toml delete mode 100644 examples/free_by_gc/src/main.rs delete mode 100644 examples/static_threads/Cargo.toml delete mode 100644 examples/static_threads/src/main.rs delete mode 100755 tools/example_test.sh diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 24ca501..6415b2f 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -21,14 +21,6 @@ jobs: submodules: true - uses: swatinem/rust-cache@6323deb102c322ba6fcbdcafc7e3dddab59af2b6 # v2.9.2 - run: cargo test - examples: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 - with: - submodules: true - - uses: swatinem/rust-cache@6323deb102c322ba6fcbdcafc7e3dddab59af2b6 # v2.9.2 - - run: tools/example_test.sh cmake: runs-on: ubuntu-latest steps: diff --git a/Cargo.lock b/Cargo.lock index 88d3119..0538e82 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -106,33 +106,12 @@ dependencies = [ "cc", ] -[[package]] -name = "dynamic_threads" -version = "0.1.0" -dependencies = [ - "bdwgc-alloc", -] - [[package]] name = "find-msvc-tools" version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3a3076410a55c90011c298b04d0cfa770b00fa04e1e3c97d3f6c9de105a03844" -[[package]] -name = "free_by_borrow" -version = "0.1.0" -dependencies = [ - "bdwgc-alloc", -] - -[[package]] -name = "free_by_gc" -version = "0.1.0" -dependencies = [ - "bdwgc-alloc", -] - [[package]] name = "glob" version = "0.3.4" @@ -258,13 +237,6 @@ 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" diff --git a/Cargo.toml b/Cargo.toml index 251a100..312a4fd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,6 @@ [workspace] resolver = "3" -members = ["bdwgc-alloc", "bdwgc-alloc-sys", "examples/*"] -default-members = ["bdwgc-alloc", "bdwgc-alloc-sys"] +members = ["bdwgc-alloc", "bdwgc-alloc-sys"] [workspace.package] version = "0.6.15" diff --git a/README.md b/README.md index d96a524..d5da1e2 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,31 @@ The raw bindings to the C API of [`bdwgc`][bdwgc] that this crate is built on ar ## Usage -See [`examples`](https://github.com/bdwgc/bdwgc-rust/tree/main/examples) directory. +```rust +use bdwgc_alloc::Allocator; +use std::thread::spawn; + +#[global_allocator] +static GLOBAL_ALLOCATOR: Allocator = Allocator; + +fn main() { + unsafe { Allocator::initialize() } + + // Unreachable objects are collected automatically. + Box::leak(Box::new([0u8; 256])); + + // Threads other than a main thread must be registered to a collector before allocation. + spawn(|| { + unsafe { Allocator::register_current_thread() }.unwrap(); + + Box::leak(Box::new([0u8; 256])); + + unsafe { Allocator::unregister_current_thread() } + }) + .join() + .unwrap(); +} +``` By default [`bdwgc`][bdwgc] is built with autotools. To build with cmake, enable the `cmake` feature: diff --git a/bdwgc-alloc/Cargo.toml b/bdwgc-alloc/Cargo.toml index 676a52d..cf17a24 100644 --- a/bdwgc-alloc/Cargo.toml +++ b/bdwgc-alloc/Cargo.toml @@ -18,3 +18,7 @@ cmake = ["bdwgc-alloc-sys/cmake"] [lints] workspace = true + +[[test]] +name = "allocator" +harness = false diff --git a/bdwgc-alloc/tests/allocator.rs b/bdwgc-alloc/tests/allocator.rs new file mode 100644 index 0000000..29371b8 --- /dev/null +++ b/bdwgc-alloc/tests/allocator.rs @@ -0,0 +1,53 @@ +//! A smoke test of the allocator. + +use bdwgc_alloc::Allocator; +use core::hint::black_box; +use std::thread::spawn; + +const OBJECT_SIZE: usize = 1 << 8; +const TOTAL_COUNT: usize = 1 << 16; +const THREAD_COUNT: usize = 1 << 6; + +#[global_allocator] +static GLOBAL_ALLOCATOR: Allocator = Allocator; + +fn main() { + unsafe { Allocator::initialize() } + + free_by_gc(); + free_by_drop(); + static_thread(); + dynamic_threads(); +} + +fn free_by_gc() { + for _ in 0..TOTAL_COUNT { + black_box(Box::leak(Box::new([0u8; OBJECT_SIZE]))); + } +} + +fn free_by_drop() { + for _ in 0..TOTAL_COUNT { + drop(black_box(Box::new([0u8; OBJECT_SIZE]))); + } +} + +fn static_thread() { + spawn_thread(free_by_gc); +} + +fn dynamic_threads() { + for _ in 0..THREAD_COUNT { + spawn_thread(free_by_gc); + } +} + +fn spawn_thread(allocate: fn()) { + spawn(move || { + unsafe { Allocator::register_current_thread() }.unwrap(); + allocate(); + unsafe { Allocator::unregister_current_thread() } + }) + .join() + .unwrap(); +} diff --git a/examples/dynamic_threads/Cargo.toml b/examples/dynamic_threads/Cargo.toml deleted file mode 100644 index b83beb3..0000000 --- a/examples/dynamic_threads/Cargo.toml +++ /dev/null @@ -1,8 +0,0 @@ -[package] -name = "dynamic_threads" -version = "0.1.0" -edition = "2024" -publish = false - -[dependencies] -bdwgc-alloc = { path = "../../bdwgc-alloc" } diff --git a/examples/dynamic_threads/src/main.rs b/examples/dynamic_threads/src/main.rs deleted file mode 100644 index 915e2e3..0000000 --- a/examples/dynamic_threads/src/main.rs +++ /dev/null @@ -1,23 +0,0 @@ -use bdwgc_alloc::Allocator; -use std::alloc::Layout; - -#[global_allocator] -static GLOBAL_ALLOCATOR: Allocator = Allocator; - -fn main() { - unsafe { Allocator::initialize() } - - loop { - let handle = std::thread::spawn(move || { - unsafe { Allocator::register_current_thread().unwrap() } - - for _ in 0..100 { - let _ = unsafe { std::alloc::alloc(Layout::from_size_align(2 ^ 8, 8).unwrap()) }; - } - - unsafe { Allocator::unregister_current_thread() } - }); - - handle.join().unwrap(); - } -} diff --git a/examples/free_by_borrow/Cargo.toml b/examples/free_by_borrow/Cargo.toml deleted file mode 100644 index 32f2368..0000000 --- a/examples/free_by_borrow/Cargo.toml +++ /dev/null @@ -1,8 +0,0 @@ -[package] -name = "free_by_borrow" -version = "0.1.0" -edition = "2024" -publish = false - -[dependencies] -bdwgc-alloc = { path = "../../bdwgc-alloc" } diff --git a/examples/free_by_borrow/src/main.rs b/examples/free_by_borrow/src/main.rs deleted file mode 100644 index 24bd128..0000000 --- a/examples/free_by_borrow/src/main.rs +++ /dev/null @@ -1,14 +0,0 @@ -use bdwgc_alloc::Allocator; - -#[global_allocator] -static GLOBAL_ALLOCATOR: Allocator = Allocator; - -fn main() { - unsafe { Allocator::initialize() } - - let mut _n: Box<[u8; 2 ^ 8]> = Box::new([0; 2 ^ 8]); - - loop { - _n = Box::new([0; 2 ^ 8]); - } -} diff --git a/examples/free_by_gc/Cargo.toml b/examples/free_by_gc/Cargo.toml deleted file mode 100644 index 81ff4db..0000000 --- a/examples/free_by_gc/Cargo.toml +++ /dev/null @@ -1,8 +0,0 @@ -[package] -name = "free_by_gc" -version = "0.1.0" -edition = "2024" -publish = false - -[dependencies] -bdwgc-alloc = { path = "../../bdwgc-alloc" } diff --git a/examples/free_by_gc/src/main.rs b/examples/free_by_gc/src/main.rs deleted file mode 100644 index 2768d74..0000000 --- a/examples/free_by_gc/src/main.rs +++ /dev/null @@ -1,13 +0,0 @@ -use bdwgc_alloc::Allocator; -use std::alloc::Layout; - -#[global_allocator] -static GLOBAL_ALLOCATOR: Allocator = Allocator; - -fn main() { - unsafe { Allocator::initialize() } - - loop { - 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 deleted file mode 100644 index ac3ad5b..0000000 --- a/examples/static_threads/Cargo.toml +++ /dev/null @@ -1,8 +0,0 @@ -[package] -name = "static_threads" -version = "0.1.0" -edition = "2024" -publish = false - -[dependencies] -bdwgc-alloc = { path = "../../bdwgc-alloc" } diff --git a/examples/static_threads/src/main.rs b/examples/static_threads/src/main.rs deleted file mode 100644 index fad7aa6..0000000 --- a/examples/static_threads/src/main.rs +++ /dev/null @@ -1,19 +0,0 @@ -use bdwgc_alloc::Allocator; -use std::alloc::Layout; - -#[global_allocator] -static GLOBAL_ALLOCATOR: Allocator = Allocator; - -fn main() { - unsafe { Allocator::initialize() } - - let handle = std::thread::spawn(move || { - unsafe { Allocator::register_current_thread().unwrap() } - - loop { - let _ = unsafe { std::alloc::alloc(Layout::from_size_align(2 ^ 8, 8).unwrap()) }; - } - }); - - handle.join().unwrap(); -} diff --git a/tools/example_test.sh b/tools/example_test.sh deleted file mode 100755 index b4282b7..0000000 --- a/tools/example_test.sh +++ /dev/null @@ -1,20 +0,0 @@ -#!/bin/sh - -set -ex - -duration=20 - -cd $(dirname $0)/.. - -for directory in $(ls examples); do - ( - cd examples/$directory - - cargo build - cargo run & - - pid=$! - sleep $duration - kill $pid - ) -done