Expand description
Resource guard module for preventing system overload.
This module provides safeguards to prevent out-of-memory conditions during large-scale operations.
§Example
use ringkernel_core::resource::{ResourceGuard, MemoryEstimate};
let guard = ResourceGuard::new();
let estimate = MemoryEstimate::new()
.with_primary(1024 * 1024) // 1 MB
.with_auxiliary(512 * 1024); // 512 KB
if !guard.can_allocate(estimate.total_bytes()) {
panic!("Insufficient memory");
}
// Or use the trait-based estimator
struct MyWorkload { elements: usize }
impl ringkernel_core::resource::MemoryEstimator for MyWorkload {
fn estimate(&self) -> MemoryEstimate {
MemoryEstimate::new().with_primary((self.elements * 64) as u64)
}
fn name(&self) -> &str { "MyWorkload" }
}Structs§
- Linear
Estimator - A linear memory estimator (bytes_per_element * count + overhead).
- Memory
Estimate - Memory estimate for a workload.
- Reservation
Guard - RAII guard for memory reservations.
- Resource
Guard - Resource guard for preventing system overload.
Enums§
- Resource
Error - Resource-related errors.
Constants§
- DEFAULT_
MAX_ MEMORY_ BYTES - Default maximum memory usage (4 GB).
- SYSTEM_
MEMORY_ MARGIN - Safety margin for system memory (leave 1 GB free).
Traits§
- Memory
Estimator - Trait for types that can estimate their memory requirements.
Functions§
- get_
available_ memory - Gets available system memory in bytes.
- get_
total_ memory - Gets total system memory in bytes.
- global_
guard - Gets the global resource guard.
Type Aliases§
- Resource
Result - Result type for resource operations.