use std::{ alloc::{GlobalAlloc, Layout, System}, cell::Cell, }; use weld_core::{Engine, Fit, Listen, Section, Settings}; struct CheckedAllocator; thread_local! { static CHECK:Cell=const {Cell::new(false)}; static OPERATIONS:Cell=const {Cell::new(0)}; } fn record() { if CHECK.try_with(Cell::get).unwrap_or(false) { let _ = OPERATIONS.try_with(|n| n.set(n.get() + 1)); } } unsafe impl GlobalAlloc for CheckedAllocator { unsafe fn alloc(&self, layout: Layout) -> *mut u8 { record(); unsafe { System.alloc(layout) } } unsafe fn dealloc(&self, pointer: *mut u8, layout: Layout) { record(); unsafe { System.dealloc(pointer, layout) } } unsafe fn realloc(&self, pointer: *mut u8, layout: Layout, size: usize) -> *mut u8 { record(); unsafe { System.realloc(pointer, layout, size) } } } #[global_allocator] static ALLOCATOR: CheckedAllocator = CheckedAllocator; #[test] fn processing_fits_crossfades_and_a_full_queue_do_not_allocate_or_free() { let mut engine = Engine::new(48000., 2).unwrap(); let mut settings = Settings::default(); engine.set_settings(settings).unwrap(); let link = engine.link(); let fits: Vec = (0..4) .map(|k| Fit { flip: k % 2 == 1, count: k, sections: [Section { fc: 200. * (k + 1) as f32, q: 0.7, }; 4], }) .collect(); CHECK.with(|v| v.set(true)); // Nothing takes blocks, so the queue fills up and later blocks are dropped. for i in 0..96000 { if i % 4800 == 0 { settings.output = ((i / 4800) % 5) as f32 - 2.; settings.listen = [Listen::Layer, Listen::Sum, Listen::SumDry][(i / 4800) % 3]; engine.set_settings(settings).unwrap(); link.push_fit(fits[(i / 4800) % 4]); } let t = i as f32 / 48000.; let x = (std::f32::consts::TAU * 55. * t).sin() * 0.5; std::hint::black_box(engine.process_sample([x, -x], [x * 0.5, x])); if i % 1600 == 0 { std::hint::black_box(engine.take_analyzer()); } } engine.reset(); CHECK.with(|v| v.set(false)); assert_eq!(OPERATIONS.with(Cell::get), 0); assert!(link.dropped() > 0); }