Example tests in crate (#585)

This commit is contained in:
Yota Toyama authored and GitHub committed 2026-09-30 13:44:53 +08:00
1 parent bdd7d6ac0f
commit b0364797c2
15 files changed
+83 -168

No files matched your search

+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();
}