mirror of
https://github.com/bdwgc/bdwgc-rust.git
synced 2026-10-25 09:23:18 -06:00
Example tests in crate (#585)
This commit is contained in:
1 parent
bdd7d6ac0f
commit
b0364797c2
15 files changed
+83
-168
No files matched your search
@@ -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();
|
||||
}
|
||||
Reference in new issue
Block a user