memkit 0.2.0-beta.1

Deterministic, intent-driven memory allocation for systems requiring predictable performance
Documentation
use memkit::{MkAllocator, MkConfig};
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};

#[test]
fn test_allocator_deferred_free() {
    let alloc = MkAllocator::new(MkConfig::default());
    
    struct MyData {
        dropped: Arc<AtomicBool>,
    }
    
    impl Drop for MyData {
        fn drop(&mut self) {
            self.dropped.store(true, Ordering::SeqCst);
        }
    }
    
    let dropped = Arc::new(AtomicBool::new(false));
    
    // Allocate on heap
    let box1 = alloc.heap_box(MyData {
        dropped: Arc::clone(&dropped),
    }).unwrap();
    
    let ptr = box1.into_raw();
    
    unsafe {
        // Defer free
        unsafe fn drop_my_data(p: *mut MyData) {
            std::ptr::drop_in_place(p);
        }
        alloc.deferred_free(ptr, Some(drop_my_data));
    }
    
    // Should not be dropped yet
    assert!(!dropped.load(Ordering::SeqCst));
    
    // Reclaim
    let count = alloc.reclaim();
    assert_eq!(count, 1);
    
    // Should be dropped now
    assert!(dropped.load(Ordering::SeqCst));
}

#[test]
fn test_deferred_free_cross_thread() {
    // Verify MkAllocator is Send by moving it to another thread
    fn assert_send<T: Send>(_: &T) {}
    
    let alloc = MkAllocator::new(MkConfig::default());
    assert_send(&alloc);
    
    struct DropTracker {
        dropped: Arc<AtomicBool>,
    }
    
    impl Drop for DropTracker {
        fn drop(&mut self) {
            self.dropped.store(true, Ordering::SeqCst);
        }
    }
    
    let dropped = Arc::new(AtomicBool::new(false));
    
    let ptr = {
        let box1 = alloc.heap_box(DropTracker {
            dropped: Arc::clone(&dropped),
        }).unwrap();
        box1.into_raw()
    };
    
    unsafe {
        unsafe fn drop_tracker(p: *mut DropTracker) {
            std::ptr::drop_in_place(p);
        }
        alloc.deferred_free(ptr, Some(drop_tracker));
    }
    
    // Not dropped yet
    assert!(!dropped.load(Ordering::SeqCst));
    
    // Reclaim
    alloc.reclaim();
    assert!(dropped.load(Ordering::SeqCst));
}