Example tests in crate

This commit is contained in:
Yota Toyama
2026-09-26 12:48:31 -07:00
parent 2f26b40e30
commit 5c78f83857
15 changed files with 83 additions and 160 deletions
+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();
}