Author SHA1 Message Date
Yota Toyama 5c78f83857 Example tests in crate 2026-09-26 12:48:31 -07:00
20 changed files with 113 additions and 234 deletions
-11
View File
@@ -1,11 +0,0 @@
name: setup
description: Sets up a build environment
inputs: {}
outputs: {}
runs:
using: composite
steps:
- uses: swatinem/rust-cache@6323deb102c322ba6fcbdcafc7e3dddab59af2b6 # v2.9.2
- uses: homebrew/actions/setup-homebrew@e99025eb970cfa124b8284a35463d87426917478 # main
- run: brew install autoconf automake libtool
shell: sh
+1 -1
View File
@@ -9,7 +9,7 @@ permissions:
pages: write
id-token: write
jobs:
document:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
+1 -11
View File
@@ -5,7 +5,7 @@ on:
- main
pull_request:
jobs:
clippy:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
@@ -30,13 +30,3 @@ jobs:
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
- uses: lycheeverse/lychee-action@e7477775783ea5526144ba13e8db5eec57747ce8 # v2.9.0
lint:
needs:
- clippy
- format
- spell-check
- readme
if: always()
runs-on: ubuntu-latest
steps:
- run: for result in ${{ join(needs.*.result, ' ') }}; do [ $result = success ]; done
+21 -54
View File
@@ -6,59 +6,26 @@ on:
pull_request:
jobs:
build:
strategy:
fail-fast: false
matrix:
os:
- ubuntu-latest
- ubuntu-26.04-arm
- macos-latest
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
submodules: true
- uses: ./.github/actions/setup
- run: cargo build
unit_test:
strategy:
fail-fast: false
matrix:
os:
- ubuntu-latest
- ubuntu-26.04-arm
- macos-latest
options:
-
- --no-default-features --features cmake
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
submodules: true
- uses: ./.github/actions/setup
- run: cargo test ${{ matrix.options }}
examples:
strategy:
fail-fast: false
matrix:
os:
- ubuntu-latest
- ubuntu-26.04-arm
- macos-latest
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
submodules: true
- uses: ./.github/actions/setup
- run: tools/example_test.sh
test:
needs:
- build
- unit_test
- examples
if: always()
runs-on: ubuntu-latest
steps:
- run: for result in ${{ join(needs.*.result, ' ') }}; do [ $result = success ]; done
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
submodules: true
- uses: swatinem/rust-cache@6323deb102c322ba6fcbdcafc7e3dddab59af2b6 # v2.9.2
- run: cargo build
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
submodules: true
- uses: swatinem/rust-cache@6323deb102c322ba6fcbdcafc7e3dddab59af2b6 # v2.9.2
- run: cargo test
cmake:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
submodules: true
- uses: swatinem/rust-cache@6323deb102c322ba6fcbdcafc7e3dddab59af2b6 # v2.9.2
- run: cargo build --no-default-features --features cmake
Generated
-28
View File
@@ -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"
+1 -2
View File
@@ -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"
+25 -1
View File
@@ -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:
+7 -4
View File
@@ -3,8 +3,6 @@
use core::error::Error;
use std::{env, path::PathBuf};
// cspell: ignore reconf
const LIB_ATOMIC_OPS_DIR: &str = "vendor/libatomic_ops";
const LIB_GC_DIR: &str = "vendor/bdwgc";
@@ -54,8 +52,14 @@ cfg_select! {
}
feature = "autotools" => {
fn build_library() -> Result<(), Box<dyn Error>> {
for dir in &[LIB_ATOMIC_OPS_DIR, LIB_GC_DIR] {
std::process::Command::new("sh")
.arg("-c")
.arg(format!("cd {dir} && ./autogen.sh"))
.output()?;
}
let dst = autotools::Config::new(LIB_ATOMIC_OPS_DIR)
.reconf("-i")
.cflag("-fPIC")
.build();
@@ -66,7 +70,6 @@ cfg_select! {
println!("cargo:rustc-link-lib=static=atomic_ops");
let dst = autotools::Config::new(LIB_GC_DIR)
.reconf("-i")
.cflag(format!(
// spell-checker: disable-next-line
"-I{} -L/lib/x86_64-linux-gnu -lpthread -fPIC",
+4
View File
@@ -18,3 +18,7 @@ cmake = ["bdwgc-alloc-sys/cmake"]
[lints]
workspace = true
[[test]]
name = "allocator"
harness = false
+53
View File
@@ -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();
}
-1
View File
@@ -10,7 +10,6 @@
"dealloc",
"finalizer",
"gcollect",
"gitsubmodule",
"libatomic",
"libc",
"libclang",
-8
View File
@@ -1,8 +0,0 @@
[package]
name = "dynamic_threads"
version = "0.1.0"
edition = "2024"
publish = false
[dependencies]
bdwgc-alloc = { path = "../../bdwgc-alloc" }
-23
View File
@@ -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();
}
}
-8
View File
@@ -1,8 +0,0 @@
[package]
name = "free_by_borrow"
version = "0.1.0"
edition = "2024"
publish = false
[dependencies]
bdwgc-alloc = { path = "../../bdwgc-alloc" }
-14
View File
@@ -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]);
}
}
-8
View File
@@ -1,8 +0,0 @@
[package]
name = "free_by_gc"
version = "0.1.0"
edition = "2024"
publish = false
[dependencies]
bdwgc-alloc = { path = "../../bdwgc-alloc" }
-13
View File
@@ -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()) };
}
}
-8
View File
@@ -1,8 +0,0 @@
[package]
name = "static_threads"
version = "0.1.0"
edition = "2024"
publish = false
[dependencies]
bdwgc-alloc = { path = "../../bdwgc-alloc" }
-19
View File
@@ -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();
}
-20
View File
@@ -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