forked from GitHub/bdwgc-rust
Example tests in crate
This commit is contained in:
@@ -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:
|
||||
|
||||
Generated
-28
@@ -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
@@ -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"
|
||||
|
||||
@@ -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:
|
||||
|
||||
|
||||
@@ -18,3 +18,7 @@ cmake = ["bdwgc-alloc-sys/cmake"]
|
||||
|
||||
[lints]
|
||||
workspace = true
|
||||
|
||||
[[test]]
|
||||
name = "allocator"
|
||||
harness = false
|
||||
|
||||
@@ -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,8 +0,0 @@
|
||||
[package]
|
||||
name = "dynamic_threads"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
publish = false
|
||||
|
||||
[dependencies]
|
||||
bdwgc-alloc = { path = "../../bdwgc-alloc" }
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
[package]
|
||||
name = "free_by_borrow"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
publish = false
|
||||
|
||||
[dependencies]
|
||||
bdwgc-alloc = { path = "../../bdwgc-alloc" }
|
||||
@@ -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]);
|
||||
}
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
[package]
|
||||
name = "free_by_gc"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
publish = false
|
||||
|
||||
[dependencies]
|
||||
bdwgc-alloc = { path = "../../bdwgc-alloc" }
|
||||
@@ -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()) };
|
||||
}
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
[package]
|
||||
name = "static_threads"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
publish = false
|
||||
|
||||
[dependencies]
|
||||
bdwgc-alloc = { path = "../../bdwgc-alloc" }
|
||||
@@ -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();
|
||||
}
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user