memkit 0.2.0-beta.1

Deterministic, intent-driven memory allocation for systems requiring predictable performance
Documentation
//! Basic memkit usage example.
//!
//! Demonstrates core allocator functionality including frame allocation,
//! pool allocation, and scoped allocations.
//!
//! Run with: `cargo run --example basic`

use std::time::Instant;
use memkit::{MkAllocator, MkConfig};

fn main() {
    println!("=== memkit Basic Example ===\n");

    // Create allocator with default config
    let alloc = MkAllocator::new(MkConfig::default());
    
    // Simulate a game loop
    for frame_num in 0..3 {
        println!("--- Frame {} ---", frame_num);
        
        // Begin frame - resets the frame arena
        alloc.begin_frame();
        
        // Frame allocations - ultra fast, reset at end of frame
        if let Some(mut position) = alloc.frame_box([0.0f32, 1.0, 2.0]) {
            println!("  Frame box: position = {:?}", *position);
            position[0] = 10.0;
            println!("  Modified: position = {:?}", *position);
        }
        
        // Frame slice - allocate array of values
        if let Some(mut velocities) = alloc.frame_slice::<[f32; 3]>(4) {
            println!("  Frame slice: {} velocities allocated", velocities.len());
            for (i, v) in velocities.iter_mut().enumerate() {
                *v = [i as f32, 0.0, 0.0];
            }
        }
        
        // Frame vec - dynamic sized with capacity
        if let Some(mut entities) = alloc.frame_vec::<u32>(10) {
            entities.push(100);
            entities.push(200);
            entities.push(300);
            println!("  Frame vec: {} entities", entities.len());
        }
        
        // Scoped allocation - automatically reset when scope drops
        {
            let _scope = alloc.scope();
            let checkpoint = alloc.frame_head();
            println!("  Scope checkpoint at: {}", checkpoint);
            
            // These allocations are freed when scope drops
            let _temp1 = alloc.frame_box(42u64);
            let _temp2 = alloc.frame_box(123u64);
            
            println!("  Inside scope: frame head = {}", alloc.frame_head());
        }
        println!("  After scope: frame head = {}", alloc.frame_head());
        
        // Pool allocation - returned to pool on drop
        if let Some(pooled) = alloc.pool_box(String::from("Hello from pool!")) {
            println!("  Pool box: {}", *pooled);
        }
        
        // End frame - instant reset of all frame allocations
        alloc.end_frame();
        println!("  Frame ended, arena reset\n");
    }
    
    // Print stats
    let stats = alloc.stats();
    println!("=== Stats ===");
    println!("  Frames: {}", stats.frame);
    println!("  Total allocated: {} bytes", stats.total_allocated);
    println!("  Peak allocated: {} bytes", stats.peak_allocated);
    
    println!("\nDone!");
}