Expand description
Memory-use limiter with cooperative back-pressure.
MemoryLimiter tracks a global byte count via an AtomicUsize counter.
Callers must obtain an AllocationGuard via MemoryLimiter::try_allocate;
the guard decrements the counter automatically on drop, ensuring the tracked
usage stays accurate even across panics.
§Design
The compare-exchange loop in MemoryLimiter::try_allocate gives linearisable
semantics: either the allocation is fully visible or it is rejected, with no
window where the counter exceeds max_bytes.
§Example
use amaters_core::memory_limiter::MemoryLimiter;
let limiter = MemoryLimiter::new(1024);
let guard = limiter.try_allocate(512).expect("should fit");
assert_eq!(limiter.current_bytes(), 512);
drop(guard);
assert_eq!(limiter.current_bytes(), 0);Structs§
- Allocation
Guard - RAII guard that releases a reservation from a
MemoryLimiteron drop. - Memory
Limiter - A cooperative memory limiter backed by an atomic byte counter.
- OomError
- Error returned when a
MemoryLimiterrejects an allocation because the requested bytes would exceedmax_bytes.