Skip to main content

Module memory_limiter

Module memory_limiter 

Source
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§

AllocationGuard
RAII guard that releases a reservation from a MemoryLimiter on drop.
MemoryLimiter
A cooperative memory limiter backed by an atomic byte counter.
OomError
Error returned when a MemoryLimiter rejects an allocation because the requested bytes would exceed max_bytes.