framealloc 0.11.1

Intent-aware, thread-smart memory allocation for Rust game engines
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
# framealloc Troubleshooting Guide


Common issues and their solutions.

## Table of Contents


1. [Compilation Errors]#compilation-errors
2. [Runtime Issues]#runtime-issues
3. [Performance Problems]#performance-problems
4. [Memory Issues]#memory-issues
5. [Threading Issues]#threading-issues
6. [Debugging Tools]#debugging-tools

## Compilation Errors


### "Cannot find type `SmartAlloc`"


```rust
// Error
use framealloc::SmartAlloc; // Error: cannot find type

// Solution
use framealloc::{SmartAlloc, AllocConfig}; // Import both
```

### "FrameBox doesn't implement Send"


```rust
// Error
fn send_frame_data() {
    let data = alloc.frame_box(42);
    std::thread::spawn(move || {
        println!("{}", data); // Error: FrameBox isn't Send
    });
}

// Solution - use TransferHandle
fn send_frame_data() {
    let data = alloc.frame_box(42);
    let handle = alloc.frame_box_for_transfer(data);
    std::thread::spawn(move || {
        let data = handle.receive();
        println!("{}", data);
    });
}
```

### "Cannot return frame allocation"


```rust
// Error
fn get_data() -> FrameBox<i32> {
    alloc.begin_frame();
    let data = alloc.frame_box(42);
    alloc.end_frame();
    data // Error: data dies at end_frame
}

// Solution - use pool or frame retention
fn get_data_pooled() -> PoolBox<i32> {
    alloc.pool_box(42)
}

fn get_data_retained(alloc: &SmartAlloc) -> FrameRetained<i32> {
    alloc.begin_frame();
    let data = alloc.frame_retained(42, RetentionPolicy::PromoteToPool);
    alloc.end_frame();
    data
}
```

## Runtime Issues


### Use After Frame End


```rust
// Problem
fn use_after_frame() {
    alloc.begin_frame();
    let data = alloc.frame_vec::<i32>();
    alloc.end_frame();
    
    data.push(42); // Crash or undefined behavior!
}

// Solution - keep within frame
fn correct_usage() {
    alloc.begin_frame();
    let data = alloc.frame_vec::<i32>();
    data.push(42);
    // Use data here
    alloc.end_frame();
}
```

### Double begin_frame


```rust
// Problem
fn double_begin() {
    alloc.begin_frame();
    alloc.begin_frame(); // May cause issues
    // ...
    alloc.end_frame();
    alloc.end_frame();
}

// Solution - track frame state
fn safe_frames() {
    if !alloc.in_frame() {
        alloc.begin_frame();
    }
    // ...
    alloc.end_frame();
}
```

### Pool Exhaustion


```rust
// Problem - pool runs out
fn exhaust_pool() {
    for _ in 0..1000000 {
        let _data = alloc.pool_alloc::<LargeObject>();
        // Never freed!
    }
}

// Solution - let objects drop
fn correct_pool_usage() {
    {
        let data = alloc.pool_alloc::<LargeObject>();
        // Use data
    } // data automatically returned to pool
}
```

## Performance Problems


### Slow Frame Allocation


```rust
// Problem - too many small allocations
fn slow_allocation() {
    for i in 0..10000 {
        let _data = alloc.frame_alloc::<f32>(); // 10000 separate allocations
    }
}

// Solution - use batch allocation
fn fast_allocation() {
    let batch = unsafe { alloc.frame_alloc_batch::<f32>(10000) };
    for i in 0..10000 {
        let _data = unsafe { batch.add(i) };
    }
}
```

### Excessive Tag Overhead


```rust
// Problem - tag on every tiny allocation
fn tag_heavy() {
    for i in 0..1000 {
        alloc.with_tag("item", |a| a.frame_alloc::<u8>());
    }
}

// Solution - group allocations
fn tag_efficient() {
    alloc.with_tag("items", |a| {
        for i in 0..1000 {
            a.frame_alloc::<u8>();
        }
    });
}
```

### Fragmentation


```rust
// Problem - many different sizes
fn fragmenting_allocations() {
    let sizes = [1, 3, 7, 15, 31, 63, 127, 255, 511, 1023];
    for &size in &sizes {
        let _data = alloc.pool_alloc::<[u8; 1024]>();
        // Wastes most of the allocation
    }
}

// Solution - use appropriate sizes
fn efficient_allocations() {
    // Use size-appropriate pools
    let small = alloc.pool_alloc::<u8>();
    let medium = alloc.pool_alloc::<[u8; 64]>();
    let large = alloc.pool_alloc::<[u8; 1024]>();
}
```

## Memory Issues


### Memory Leaks


```rust
// Problem - frame data promoted but never freed
fn memory_leak() {
    alloc.begin_frame();
    let data = alloc.frame_retained(large_data(), RetentionPolicy::PromoteToPool);
    alloc.end_frame();
    // data is now in pool but never referenced again
}

// Solution - track promotions
fn track_promotions() {
    alloc.begin_frame();
    let data = alloc.frame_retained(large_data(), RetentionPolicy::PromoteToPool);
    alloc.end_frame();
    
    // Use the data or explicitly drop
    drop(data);
}
```

### Out of Memory


```rust
// Problem - unbounded allocation
fn unbounded() {
    loop {
        let data = alloc.frame_vec::<u8>();
        data.resize(data.len() + 1000000, 0);
        // Grows without bound!
    }
}

// Solution - set budgets
fn bounded() {
    alloc.set_frame_budget(megabytes(100));
    
    loop {
        if alloc.frame_usage() < megabytes(90) {
            let data = alloc.frame_vec::<u8>();
            data.resize(1000000, 0);
        }
    }
}
```

### High Memory Usage


```rust
// Problem - keeping too much data
fn high_usage() {
    alloc.begin_frame();
    let all_data = alloc.frame_vec::<HugeStruct>();
    for _ in 0..100000 {
        all_data.push(HugeStruct::new());
    }
    // Uses lots of memory
    alloc.end_frame();
}

// Solution - process in chunks
fn chunked_processing() {
    const CHUNK_SIZE: usize = 1000;
    
    for chunk in 0..100 {
        alloc.begin_frame();
        let data = alloc.frame_vec::<HugeStruct>();
        for i in 0..CHUNK_SIZE {
            data.push(HugeStruct::new());
        }
        process_chunk(&data);
        alloc.end_frame();
    }
}
```

## Threading Issues


### Data Race


```rust
// Problem - sharing frame data across threads
fn data_race() {
    alloc.begin_frame();
    let data = alloc.frame_vec::<i32>();
    
    let handle = std::thread::spawn(move || {
        data.push(42); // Data race!
    });
    
    handle.join().unwrap();
    alloc.end_frame();
}

// Solution - use thread-local allocators
fn thread_safe() {
    let alloc_clone = alloc.clone();
    
    std::thread::spawn(move || {
        alloc_clone.begin_frame();
        let data = alloc_clone.frame_vec::<i32>();
        data.push(42);
        alloc_clone.end_frame();
    }).join().unwrap();
}
```

### Deadlock with TransferHandle


```rust
// Problem - deadlock waiting for transfer
fn deadlock() {
    let (tx, rx) = std::sync::mpsc::channel();
    
    std::thread::spawn(move || {
        let handle = rx.recv().unwrap(); // Waits forever
        let data = handle.receive();
    });
    
    // Never send anything
}

// Solution - timeout or proper synchronization
fn no_deadlock() {
    let (tx, rx) = std::sync::mpsc::channel();
    
    let handle = std::thread::spawn(move || {
        if let Ok(handle) = rx.recv() {
            let data = handle.receive();
            // Process data
        }
    });
    
    // Send the data
    let data = alloc.frame_box(42);
    let transfer = alloc.frame_box_for_transfer(data);
    tx.send(transfer).unwrap();
    
    handle.join().unwrap();
}
```

## Debugging Tools


### Enable Debug Mode


```toml
# Cargo.toml

framealloc = { version = "0.10", features = ["debug"] }
```

### Behavior Filter


```rust
// Enable runtime detection
alloc.enable_behavior_filter();

// Run your code
run_application();

// Check for issues
let report = alloc.behavior_report();
for issue in &report.issues {
    match issue.code {
        "FA601" => println!("Frame allocation in async function at {:?}", issue.location),
        "FA602" => println!("Allocation in hot loop at {:?}", issue.location),
        "FA603" => println!("Frame data crossing await point at {:?}", issue.location),
        _ => println!("Issue {}: {}", issue.code, issue.message),
    }
}
```

### Memory Poisoning


```rust
#[cfg(feature = "debug")]

fn debug_allocations() {
    alloc.begin_frame();
    
    let data = alloc.frame_alloc::<u32>();
    // Memory is poisoned when allocated
    
    unsafe { *data = 42; } // OK
    // data is automatically checked for corruption at end_frame
    
    alloc.end_frame();
    // Any corruption will be detected
}
```

### Statistics and Metrics


```rust
// Enable statistics
alloc.enable_statistics();

// After running
let stats = alloc.statistics();
println!("Total allocations: {}", stats.total_allocations);
println!("Peak memory: {} bytes", stats.peak_memory);
println!("Frame allocations: {}", stats.frame_allocations);
println!("Pool allocations: {}", stats.pool_allocations);
```

### cargo-fa Static Analysis


```bash
# Install cargo-fa

cargo install cargo-fa

# Check for common issues

cargo fa --all

# Specific checks

cargo fa --dirtymem    # Frame escape issues
cargo fa --threading   # Thread safety
cargo fa --async-safety # Async issues
cargo fa --architecture # Architecture violations

# Explain specific errors

cargo fa explain FA601
```

## Common Error Messages


### "Frame allocation escaped frame boundary"


**Cause**: Storing frame allocation in a place that outlives the frame.

```rust
// Bad
static mut GLOBAL_DATA: Option<FrameBox<i32>> = None;

fn store_frame_data() {
    alloc.begin_frame();
    unsafe { GLOBAL_DATA = Some(alloc.frame_box(42)); } // Error
    alloc.end_frame();
}

// Good
fn store_frame_data() {
    alloc.begin_frame();
    let data = alloc.frame_box(42);
    use_data(&data);
    alloc.end_frame();
}
```

### "Attempted to use frame data after end_frame"


**Cause**: Using frame allocation after the frame has ended.

```rust
// Bad
fn late_usage() -> &'static i32 {
    alloc.begin_frame();
    let data = alloc.frame_alloc::<i32>();
    alloc.end_frame();
    unsafe { &*data } // Error
}

// Good
fn timely_usage() {
    alloc.begin_frame();
    let data = alloc.frame_alloc::<i32>();
    // Use data here
    unsafe { *data = 42; }
    alloc.end_frame();
}
```

### "Pool exhausted"


**Cause**: Pool ran out of objects for a size class.

```rust
// Bad - never returning to pool
fn exhaust_pool() {
    let mut handles = Vec::new();
    for _ in 0..10000 {
        handles.push(alloc.pool_box::<MyObject>());
    }
    // Pool is empty
}

// Good - let objects drop
fn use_pool_correctly() {
    for _ in 0..10000 {
        let obj = alloc.pool_box::<MyObject>();
        // Use object
        // Automatically returned to pool
    }
}
```

## Performance Debugging


### Profile Allocation Patterns


```rust
use std::time::Instant;

fn profile_allocations() {
    let mut times = Vec::new();
    
    for _ in 0..1000 {
        let start = Instant::now();
        alloc.begin_frame();
        
        // Your allocation pattern
        let data = alloc.frame_vec::<u32>();
        for i in 0..1000 {
            data.push(i);
        }
        
        alloc.end_frame();
        times.push(start.elapsed());
    }
    
    let avg = times.iter().sum::<Duration>() / times.len() as u32;
    println!("Average frame time: {:?}", avg);
}
```

### Memory Usage Analysis


```rust
fn analyze_memory() {
    alloc.begin_frame();
    
    // Track allocations by size
    let mut small_count = 0;
    let mut medium_count = 0;
    let mut large_count = 0;
    
    // Simulate allocations
    for i in 0..1000 {
        if i < 500 {
            let _ = alloc.frame_alloc::<u8>();
            small_count += 1;
        } else if i < 800 {
            let _ = alloc.frame_alloc::<[u8; 64]>();
            medium_count += 1;
        } else {
            let _ = alloc.frame_alloc::<[u8; 1024]>();
            large_count += 1;
        }
    }
    
    let stats = alloc.frame_stats();
    println!("Small: {}, Medium: {}, Large: {}", small_count, medium_count, large_count);
    println!("Total bytes: {}", stats.bytes_allocated);
    
    alloc.end_frame();
}
```

## Getting Help


### Check the Documentation


- [Getting Started]getting-started.md - Basic concepts
- [Patterns Guide]patterns.md - Common patterns
- [Performance Guide]performance.md - Optimization

### Use Debug Features


```rust
// Comprehensive debugging
#[cfg(debug_assertions)]

{
    alloc.enable_behavior_filter();
    alloc.enable_statistics();
    alloc.enable_memory_poisoning();
}
```

### Report Issues


When reporting issues, include:

1. framealloc version
2. Rust version
3. Platform (OS, architecture)
4. Minimal reproducible example
5. Error message or backtrace
6. Debug output (if available)

### Example Issue Report


```
framealloc version: 0.10.0
Rust version: 1.70.0
Platform: Windows 11, x86_64

Issue: Frame allocation crashes when used in async function

Code:
async fn problem() {
    let alloc = SmartAlloc::new(Default::default());
    alloc.begin_frame();
    let data = alloc.frame_vec::<u8>();
    await something(); // Crashes here
    alloc.end_frame();
}

Error: "Attempted to use frame data after end_frame"
```

## Quick Fixes


| Problem | Quick Fix |
|---------|-----------|
| Frame data escapes | Use `pool_box()` instead |
| Cross-thread issues | Use `TransferHandle` |
| Slow allocations | Use `frame_alloc_batch()` |
| Memory leaks | Enable debug mode |
| Compilation errors | Check imports |
| Performance issues | Run `cargo fa` |

## Prevention Checklist


- [ ] Always match `begin_frame` with `end_frame`
- [ ] Don't store frame allocations across frames
- [ ] Use `TransferHandle` for cross-thread data
- [ ] Enable debug features during development
- [ ] Run `cargo fa` regularly
- [ ] Set memory budgets
- [ ] Profile before optimizing
- [ ] Test with realistic data sizes

Remember: Most issues are caught by `cargo fa`! Use it early and often. 🚀