oximedia-accel 0.1.2

Hardware acceleration layer for OxiMedia using Vulkan compute
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
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
//! GPU-style memory arena allocator.
//!
//! Provides a bump-allocator arena that carves sub-allocations from a
//! contiguous slab.  Useful for grouping per-frame GPU buffer allocations
//! so they can be freed in a single operation at the end of the frame.

#![allow(dead_code)]

use std::collections::HashMap;
use std::fmt;

/// Unique identifier for an arena allocation.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct AllocId(u64);

impl fmt::Display for AllocId {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "alloc#{}", self.0)
    }
}

/// Metadata for a single sub-allocation inside the arena.
#[derive(Debug, Clone, Copy)]
pub struct AllocRecord {
    /// Byte offset from the start of the arena.
    pub offset: usize,
    /// Size in bytes.
    pub size: usize,
    /// Alignment that was requested.
    pub alignment: usize,
    /// Allocation id.
    pub id: AllocId,
}

/// Allocation strategy hint.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AllocStrategy {
    /// Simple bump / linear allocation (fastest, no individual free).
    Linear,
    /// Best-fit free-list (allows individual free, slower).
    BestFit,
}

/// Statistics for the arena.
#[derive(Debug, Clone, Default)]
pub struct ArenaStats {
    /// Total capacity in bytes.
    pub capacity: usize,
    /// Currently used bytes (including alignment padding).
    pub used: usize,
    /// Peak used bytes observed.
    pub peak_used: usize,
    /// Total number of allocations performed.
    pub alloc_count: u64,
    /// Total number of resets performed.
    pub reset_count: u64,
}

impl ArenaStats {
    /// Fraction of arena currently in use (0.0 .. 1.0).
    #[must_use]
    #[allow(clippy::cast_precision_loss)]
    pub fn utilization(&self) -> f64 {
        if self.capacity == 0 {
            return 0.0;
        }
        self.used as f64 / self.capacity as f64
    }

    /// Fraction of arena used at peak.
    #[must_use]
    #[allow(clippy::cast_precision_loss)]
    pub fn peak_utilization(&self) -> f64 {
        if self.capacity == 0 {
            return 0.0;
        }
        self.peak_used as f64 / self.capacity as f64
    }

    /// Remaining free bytes.
    #[must_use]
    pub fn free_bytes(&self) -> usize {
        self.capacity.saturating_sub(self.used)
    }
}

/// A bump-allocator memory arena.
///
/// Allocations are served from a contiguous virtual range and
/// freed collectively via [`MemoryArena::reset`].
pub struct MemoryArena {
    /// Total capacity in bytes.
    capacity: usize,
    /// Current write cursor (next free offset).
    cursor: usize,
    /// Running allocation id counter.
    next_id: u64,
    /// Record of live allocations.
    records: HashMap<AllocId, AllocRecord>,
    /// Strategy hint (stored for introspection; behaviour is always linear).
    strategy: AllocStrategy,
    /// Running statistics.
    stats: ArenaStats,
}

impl MemoryArena {
    /// Create a new arena with the given byte capacity.
    #[must_use]
    pub fn new(capacity: usize) -> Self {
        Self {
            capacity,
            cursor: 0,
            next_id: 0,
            records: HashMap::new(),
            strategy: AllocStrategy::Linear,
            stats: ArenaStats {
                capacity,
                ..ArenaStats::default()
            },
        }
    }

    /// Create a new arena with a specific strategy hint.
    #[must_use]
    pub fn with_strategy(capacity: usize, strategy: AllocStrategy) -> Self {
        let mut arena = Self::new(capacity);
        arena.strategy = strategy;
        arena
    }

    /// Total capacity in bytes.
    #[must_use]
    pub fn capacity(&self) -> usize {
        self.capacity
    }

    /// Currently used bytes.
    #[must_use]
    pub fn used(&self) -> usize {
        self.cursor
    }

    /// Remaining free bytes.
    #[must_use]
    pub fn remaining(&self) -> usize {
        self.capacity.saturating_sub(self.cursor)
    }

    /// The strategy hint for this arena.
    #[must_use]
    pub fn strategy(&self) -> AllocStrategy {
        self.strategy
    }

    /// Number of live allocations.
    #[must_use]
    pub fn live_alloc_count(&self) -> usize {
        self.records.len()
    }

    /// Allocate `size` bytes with the given alignment.
    ///
    /// Returns `None` if the arena cannot satisfy the request.
    pub fn allocate(&mut self, size: usize, alignment: usize) -> Option<AllocRecord> {
        let align = alignment.max(1);
        // Round cursor up to alignment
        let aligned_offset = (self.cursor + align - 1) & !(align - 1);
        let end = aligned_offset.checked_add(size)?;
        if end > self.capacity {
            return None;
        }
        let id = AllocId(self.next_id);
        self.next_id += 1;
        let record = AllocRecord {
            offset: aligned_offset,
            size,
            alignment: align,
            id,
        };
        self.cursor = end;
        self.records.insert(id, record);
        self.stats.alloc_count += 1;
        self.stats.used = self.cursor;
        if self.cursor > self.stats.peak_used {
            self.stats.peak_used = self.cursor;
        }
        Some(record)
    }

    /// Convenience: allocate with default alignment of 1.
    pub fn allocate_unaligned(&mut self, size: usize) -> Option<AllocRecord> {
        self.allocate(size, 1)
    }

    /// Look up a record by its id.
    #[must_use]
    pub fn get_record(&self, id: AllocId) -> Option<&AllocRecord> {
        self.records.get(&id)
    }

    /// Reset the arena, freeing all allocations.
    pub fn reset(&mut self) {
        self.cursor = 0;
        self.records.clear();
        self.stats.used = 0;
        self.stats.reset_count += 1;
    }

    /// Snapshot of arena statistics.
    #[must_use]
    pub fn stats(&self) -> &ArenaStats {
        &self.stats
    }

    /// Resize the arena capacity. If the new capacity is smaller than the
    /// current cursor, this effectively invalidates existing allocations.
    pub fn resize(&mut self, new_capacity: usize) {
        self.capacity = new_capacity;
        self.stats.capacity = new_capacity;
        if self.cursor > new_capacity {
            self.cursor = new_capacity;
            self.stats.used = new_capacity;
        }
    }

    /// Returns true if memory pressure exceeds the given threshold (0.0 to 1.0).
    #[must_use]
    pub fn is_under_pressure(&self, threshold: f64) -> bool {
        self.stats.utilization() >= threshold
    }

    /// Attempts to evict oldest allocations until utilization drops below
    /// `target_utilization` (0.0 to 1.0).
    ///
    /// Returns the number of allocations evicted. Only works with
    /// `BestFit` strategy (linear arenas cannot free individual allocations).
    ///
    /// For `Linear` strategy, this performs a full reset if pressure is
    /// above threshold and returns the count of allocations that were present.
    pub fn evict_until_below(&mut self, target_utilization: f64) -> usize {
        if self.capacity == 0 || self.stats.utilization() <= target_utilization {
            return 0;
        }

        // For both strategies, evict from the tail (most recently allocated)
        // backwards, since the arena is a bump allocator. Only tail
        // allocations can actually reclaim cursor space.
        let mut sorted: Vec<AllocRecord> = self.records.values().copied().collect();
        // Sort by offset descending (newest first) for effective reclamation
        sorted.sort_by(|a, b| b.offset.cmp(&a.offset));

        let target_used = (self.capacity as f64 * target_utilization) as usize;
        let mut evicted = 0;

        for record in &sorted {
            if self.cursor <= target_used {
                break;
            }
            // Only reclaim if this record is at the tail of the arena
            if record.offset + record.size == self.cursor {
                self.records.remove(&record.id);
                self.cursor = record.offset;
                self.stats.used = self.cursor;
                evicted += 1;
            } else {
                // Non-tail record: remove from tracking but can't reclaim space
                self.records.remove(&record.id);
                evicted += 1;
            }
        }

        // Recalculate cursor to the end of the highest remaining allocation
        if self.records.is_empty() {
            self.cursor = 0;
            self.stats.used = 0;
        } else {
            let max_end = self
                .records
                .values()
                .map(|r| r.offset + r.size)
                .max()
                .unwrap_or(0);
            // Cursor can only shrink to the highest remaining allocation end
            if max_end < self.cursor {
                self.cursor = max_end;
                self.stats.used = max_end;
            }
        }

        evicted
    }

    /// Returns true if the arena can accommodate an allocation of `size`
    /// bytes with the given `alignment` without eviction.
    #[must_use]
    pub fn can_allocate(&self, size: usize, alignment: usize) -> bool {
        let align = alignment.max(1);
        let aligned_offset = (self.cursor + align - 1) & !(align - 1);
        match aligned_offset.checked_add(size) {
            Some(end) => end <= self.capacity,
            None => false,
        }
    }

    /// Attempts to allocate `size` bytes with `alignment`, automatically
    /// evicting old allocations if needed.
    ///
    /// Uses a target utilization of 0.75 when eviction is triggered.
    ///
    /// Returns `None` if allocation fails even after eviction.
    pub fn allocate_or_evict(&mut self, size: usize, alignment: usize) -> Option<AllocRecord> {
        if let Some(record) = self.allocate(size, alignment) {
            return Some(record);
        }
        // Try evicting down to 50% utilization
        self.evict_until_below(0.5);
        self.allocate(size, alignment)
    }
}

/// Memory pressure policy for automatic management.
#[derive(Debug, Clone)]
pub struct MemoryPressurePolicy {
    /// Utilization threshold (0.0 to 1.0) that triggers a warning.
    pub warning_threshold: f64,
    /// Utilization threshold (0.0 to 1.0) that triggers automatic eviction.
    pub critical_threshold: f64,
    /// Target utilization after eviction.
    pub eviction_target: f64,
    /// Maximum number of allocations before forcing eviction.
    pub max_live_allocations: usize,
}

impl Default for MemoryPressurePolicy {
    fn default() -> Self {
        Self {
            warning_threshold: 0.75,
            critical_threshold: 0.90,
            eviction_target: 0.60,
            max_live_allocations: 10_000,
        }
    }
}

/// Pressure level reported by the monitor.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PressureLevel {
    /// Utilization below warning threshold.
    Normal,
    /// Utilization between warning and critical thresholds.
    Warning,
    /// Utilization above critical threshold.
    Critical,
}

/// Monitors memory pressure on an arena and can trigger eviction.
pub struct MemoryPressureMonitor {
    /// Policy governing thresholds and limits.
    policy: MemoryPressurePolicy,
    /// Number of evictions triggered.
    eviction_count: u64,
    /// Total allocations evicted across all eviction passes.
    total_evicted: u64,
}

impl MemoryPressureMonitor {
    /// Creates a new monitor with the given policy.
    #[must_use]
    pub fn new(policy: MemoryPressurePolicy) -> Self {
        Self {
            policy,
            eviction_count: 0,
            total_evicted: 0,
        }
    }

    /// Creates a monitor with default policy.
    #[must_use]
    pub fn with_defaults() -> Self {
        Self::new(MemoryPressurePolicy::default())
    }

    /// Returns the current pressure level for the given arena.
    #[must_use]
    pub fn pressure_level(&self, arena: &MemoryArena) -> PressureLevel {
        let util = arena.stats().utilization();
        if util >= self.policy.critical_threshold {
            PressureLevel::Critical
        } else if util >= self.policy.warning_threshold {
            PressureLevel::Warning
        } else {
            PressureLevel::Normal
        }
    }

    /// Checks the arena and triggers eviction if needed.
    ///
    /// Returns the number of allocations evicted (0 if no eviction needed).
    pub fn check_and_evict(&mut self, arena: &mut MemoryArena) -> usize {
        let level = self.pressure_level(arena);
        let over_alloc_limit = arena.live_alloc_count() > self.policy.max_live_allocations;

        if level == PressureLevel::Critical || over_alloc_limit {
            let evicted = arena.evict_until_below(self.policy.eviction_target);
            if evicted > 0 {
                self.eviction_count += 1;
                self.total_evicted += evicted as u64;
            }
            evicted
        } else {
            0
        }
    }

    /// Returns the policy in use.
    #[must_use]
    pub fn policy(&self) -> &MemoryPressurePolicy {
        &self.policy
    }

    /// Returns the number of eviction events triggered.
    #[must_use]
    pub fn eviction_count(&self) -> u64 {
        self.eviction_count
    }

    /// Returns the total number of allocations evicted.
    #[must_use]
    pub fn total_evicted(&self) -> u64 {
        self.total_evicted
    }

    /// Updates the policy.
    pub fn set_policy(&mut self, policy: MemoryPressurePolicy) {
        self.policy = policy;
    }
}

impl fmt::Debug for MemoryPressureMonitor {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("MemoryPressureMonitor")
            .field("eviction_count", &self.eviction_count)
            .field("total_evicted", &self.total_evicted)
            .finish()
    }
}

impl fmt::Debug for MemoryArena {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("MemoryArena")
            .field("capacity", &self.capacity)
            .field("used", &self.cursor)
            .field("live_allocs", &self.records.len())
            .field("strategy", &self.strategy)
            .finish()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_alloc_id_display() {
        let id = AllocId(42);
        assert_eq!(id.to_string(), "alloc#42");
    }

    #[test]
    fn test_new_arena() {
        let arena = MemoryArena::new(1024);
        assert_eq!(arena.capacity(), 1024);
        assert_eq!(arena.used(), 0);
        assert_eq!(arena.remaining(), 1024);
    }

    #[test]
    fn test_simple_allocation() {
        let mut arena = MemoryArena::new(256);
        let rec = arena.allocate(64, 1).expect("rec should be valid");
        assert_eq!(rec.offset, 0);
        assert_eq!(rec.size, 64);
        assert_eq!(arena.used(), 64);
        assert_eq!(arena.remaining(), 192);
    }

    #[test]
    fn test_aligned_allocation() {
        let mut arena = MemoryArena::new(256);
        arena.allocate(10, 1).expect("allocate should succeed"); // cursor at 10
        let rec = arena.allocate(32, 16).expect("rec should be valid"); // should align to 16
        assert_eq!(rec.offset, 16);
        assert_eq!(rec.size, 32);
    }

    #[test]
    fn test_allocation_overflow() {
        let mut arena = MemoryArena::new(64);
        assert!(arena.allocate(65, 1).is_none());
        assert_eq!(arena.live_alloc_count(), 0);
    }

    #[test]
    fn test_multiple_allocations() {
        let mut arena = MemoryArena::new(1024);
        for i in 0..10 {
            let rec = arena.allocate(32, 1).expect("rec should be valid");
            assert_eq!(rec.offset, i * 32);
        }
        assert_eq!(arena.live_alloc_count(), 10);
        assert_eq!(arena.used(), 320);
    }

    #[test]
    fn test_reset() {
        let mut arena = MemoryArena::new(256);
        arena.allocate(100, 1).expect("allocate should succeed");
        arena.allocate(50, 1).expect("allocate should succeed");
        arena.reset();
        assert_eq!(arena.used(), 0);
        assert_eq!(arena.live_alloc_count(), 0);
        assert_eq!(arena.stats().reset_count, 1);
    }

    #[test]
    fn test_peak_tracking() {
        let mut arena = MemoryArena::new(512);
        arena.allocate(200, 1).expect("allocate should succeed");
        arena.allocate(100, 1).expect("allocate should succeed");
        assert_eq!(arena.stats().peak_used, 300);
        arena.reset();
        arena.allocate(50, 1).expect("allocate should succeed");
        // peak should still be 300
        assert_eq!(arena.stats().peak_used, 300);
    }

    #[test]
    fn test_get_record() {
        let mut arena = MemoryArena::new(256);
        let rec = arena.allocate(16, 1).expect("rec should be valid");
        let found = arena.get_record(rec.id).expect("found should be valid");
        assert_eq!(found.offset, 0);
        assert_eq!(found.size, 16);
    }

    #[test]
    fn test_stats_utilization() {
        let mut arena = MemoryArena::new(100);
        arena.allocate(50, 1).expect("allocate should succeed");
        let s = arena.stats();
        assert!((s.utilization() - 0.5).abs() < 1e-9);
        assert_eq!(s.free_bytes(), 50);
    }

    #[test]
    fn test_strategy_hint() {
        let arena = MemoryArena::with_strategy(1024, AllocStrategy::BestFit);
        assert_eq!(arena.strategy(), AllocStrategy::BestFit);
    }

    #[test]
    fn test_resize_larger() {
        let mut arena = MemoryArena::new(100);
        arena.allocate(80, 1).expect("allocate should succeed");
        arena.resize(200);
        assert_eq!(arena.capacity(), 200);
        assert_eq!(arena.remaining(), 120);
    }

    #[test]
    fn test_resize_smaller_than_cursor() {
        let mut arena = MemoryArena::new(200);
        arena.allocate(150, 1).expect("allocate should succeed");
        arena.resize(100);
        assert_eq!(arena.capacity(), 100);
        // cursor clamped to capacity
        assert_eq!(arena.used(), 100);
    }

    #[test]
    fn test_allocate_unaligned() {
        let mut arena = MemoryArena::new(64);
        let rec = arena.allocate_unaligned(10).expect("rec should be valid");
        assert_eq!(rec.alignment, 1);
        assert_eq!(rec.offset, 0);
    }

    // ── Memory pressure tests ──────────────────────────────────────────────

    #[test]
    fn test_is_under_pressure() {
        let mut arena = MemoryArena::new(100);
        assert!(!arena.is_under_pressure(0.5));
        arena.allocate(75, 1).expect("allocate should succeed");
        assert!(arena.is_under_pressure(0.5));
        assert!(!arena.is_under_pressure(0.8));
    }

    #[test]
    fn test_can_allocate() {
        let mut arena = MemoryArena::new(100);
        assert!(arena.can_allocate(50, 1));
        assert!(arena.can_allocate(100, 1));
        assert!(!arena.can_allocate(101, 1));
        arena.allocate(90, 1).expect("allocate should succeed");
        assert!(!arena.can_allocate(20, 1));
        assert!(arena.can_allocate(10, 1));
    }

    #[test]
    fn test_can_allocate_with_alignment() {
        let mut arena = MemoryArena::new(64);
        arena.allocate(1, 1).expect("allocate should succeed"); // cursor at 1
                                                                // With alignment 16, next alloc starts at 16, needs 16+48=64 total
        assert!(arena.can_allocate(48, 16));
        assert!(!arena.can_allocate(49, 16));
    }

    #[test]
    fn test_evict_linear_arena() {
        let mut arena = MemoryArena::new(100);
        arena.allocate(90, 1).expect("allocate should succeed");
        arena.allocate(5, 1).expect("allocate should succeed");
        // Linear arena: eviction does full reset
        let evicted = arena.evict_until_below(0.5);
        assert_eq!(evicted, 2);
        assert_eq!(arena.used(), 0);
        assert_eq!(arena.live_alloc_count(), 0);
    }

    #[test]
    fn test_evict_bestfit_arena() {
        let mut arena = MemoryArena::with_strategy(100, AllocStrategy::BestFit);
        for _ in 0..5 {
            arena.allocate(18, 1).expect("allocate should succeed");
        }
        assert_eq!(arena.used(), 90);
        let evicted = arena.evict_until_below(0.5);
        assert!(evicted > 0);
        // After eviction, cursor should have moved back
        assert!(arena.used() <= 54, "used={}", arena.used()); // 54 = 3*18
    }

    #[test]
    fn test_evict_not_needed() {
        let mut arena = MemoryArena::new(100);
        arena.allocate(10, 1).expect("allocate should succeed");
        let evicted = arena.evict_until_below(0.5);
        assert_eq!(evicted, 0);
    }

    #[test]
    fn test_allocate_or_evict_success() {
        let mut arena = MemoryArena::with_strategy(100, AllocStrategy::BestFit);
        for _ in 0..9 {
            arena.allocate(10, 1).expect("allocate should succeed");
        }
        // Arena at 90/100, can't fit 20
        assert!(arena.allocate(20, 1).is_none());
        // allocate_or_evict evicts (down to 50%), then allocates the 20 bytes
        let rec = arena.allocate_or_evict(20, 1);
        // After eviction, we should have freed enough space for a 20-byte alloc
        assert!(
            rec.is_some(),
            "allocate_or_evict should succeed after eviction, used={}",
            arena.used()
        );
    }

    #[test]
    fn test_allocate_or_evict_no_eviction_needed() {
        let mut arena = MemoryArena::new(100);
        let rec = arena.allocate_or_evict(50, 1);
        assert!(rec.is_some());
        assert_eq!(arena.used(), 50);
    }

    #[test]
    fn test_pressure_monitor_levels() {
        let monitor = MemoryPressureMonitor::with_defaults();
        let mut arena = MemoryArena::new(100);

        assert_eq!(monitor.pressure_level(&arena), PressureLevel::Normal);

        arena.allocate(80, 1).expect("allocate should succeed");
        assert_eq!(monitor.pressure_level(&arena), PressureLevel::Warning);

        arena.allocate(15, 1).expect("allocate should succeed");
        assert_eq!(monitor.pressure_level(&arena), PressureLevel::Critical);
    }

    #[test]
    fn test_pressure_monitor_check_and_evict() {
        let policy = MemoryPressurePolicy {
            warning_threshold: 0.75,
            critical_threshold: 0.85,
            eviction_target: 0.50,
            max_live_allocations: 10_000,
        };
        let mut monitor = MemoryPressureMonitor::new(policy);
        let mut arena = MemoryArena::with_strategy(100, AllocStrategy::BestFit);

        for _ in 0..8 {
            arena.allocate(10, 1).expect("allocate should succeed");
        }
        assert_eq!(arena.used(), 80);
        // 80% is below critical (85%), should not evict
        let evicted = monitor.check_and_evict(&mut arena);
        assert_eq!(evicted, 0);

        // Push above critical (90%)
        arena.allocate(10, 1).expect("allocate should succeed");
        assert_eq!(arena.used(), 90);
        let evicted = monitor.check_and_evict(&mut arena);
        assert!(evicted > 0);
        assert_eq!(monitor.eviction_count(), 1);
        assert!(monitor.total_evicted() > 0);
    }

    #[test]
    fn test_pressure_monitor_alloc_limit() {
        let policy = MemoryPressurePolicy {
            max_live_allocations: 5,
            warning_threshold: 0.75,
            critical_threshold: 0.90,
            eviction_target: 0.0, // evict everything
        };
        let mut monitor = MemoryPressureMonitor::new(policy);
        let mut arena = MemoryArena::with_strategy(10000, AllocStrategy::BestFit);

        for _ in 0..6 {
            arena.allocate(1, 1).expect("allocate should succeed");
        }
        // 6 allocs > max_live_allocations(5), should trigger eviction
        let evicted = monitor.check_and_evict(&mut arena);
        assert!(evicted > 0);
    }

    #[test]
    fn test_pressure_policy_default() {
        let policy = MemoryPressurePolicy::default();
        assert!((policy.warning_threshold - 0.75).abs() < 1e-9);
        assert!((policy.critical_threshold - 0.90).abs() < 1e-9);
        assert!((policy.eviction_target - 0.60).abs() < 1e-9);
        assert_eq!(policy.max_live_allocations, 10_000);
    }

    #[test]
    fn test_pressure_monitor_set_policy() {
        let mut monitor = MemoryPressureMonitor::with_defaults();
        let policy = MemoryPressurePolicy {
            warning_threshold: 0.5,
            critical_threshold: 0.7,
            eviction_target: 0.3,
            max_live_allocations: 100,
        };
        monitor.set_policy(policy);
        assert!((monitor.policy().warning_threshold - 0.5).abs() < 1e-9);
    }

    #[test]
    fn test_pressure_monitor_debug() {
        let monitor = MemoryPressureMonitor::with_defaults();
        let debug = format!("{:?}", monitor);
        assert!(debug.contains("MemoryPressureMonitor"));
    }

    #[test]
    fn test_evict_empty_arena() {
        let mut arena = MemoryArena::new(100);
        let evicted = arena.evict_until_below(0.5);
        assert_eq!(evicted, 0);
    }

    #[test]
    fn test_evict_bestfit_to_empty() {
        let mut arena = MemoryArena::with_strategy(100, AllocStrategy::BestFit);
        arena.allocate(90, 1).expect("allocate should succeed");
        let evicted = arena.evict_until_below(0.0);
        assert_eq!(evicted, 1);
        assert_eq!(arena.used(), 0);
    }
}