pub struct ResourceGuard { /* private fields */ }Expand description
Resource guard for preventing system overload.
Tracks memory allocation and provides checks before large allocations.
Implementations§
Source§impl ResourceGuard
impl ResourceGuard
Sourcepub fn with_max_memory(max_bytes: u64) -> Self
pub fn with_max_memory(max_bytes: u64) -> Self
Creates a resource guard with a specific memory limit.
Sourcepub fn with_safety_margin(self, margin: f32) -> Self
pub fn with_safety_margin(self, margin: f32) -> Self
Creates a resource guard with custom safety margin.
Sourcepub fn set_max_memory(&self, max_bytes: u64)
pub fn set_max_memory(&self, max_bytes: u64)
Sets the maximum memory limit.
Sourcepub fn max_memory(&self) -> u64
pub fn max_memory(&self) -> u64
Gets the maximum memory limit.
Sourcepub fn current_memory(&self) -> u64
pub fn current_memory(&self) -> u64
Gets the current tracked memory usage.
Sourcepub fn reserved_memory(&self) -> u64
pub fn reserved_memory(&self) -> u64
Gets the currently reserved memory.
Sourcepub fn available_memory(&self) -> u64
pub fn available_memory(&self) -> u64
Gets the effective available memory (max - current - reserved).
Sourcepub fn can_allocate(&self, bytes: u64) -> bool
pub fn can_allocate(&self, bytes: u64) -> bool
Checks if a given allocation can proceed.
Sourcepub fn can_allocate_safe(&self, bytes: u64) -> ResourceResult<()>
pub fn can_allocate_safe(&self, bytes: u64) -> ResourceResult<()>
Checks if an allocation can proceed, also checking system memory.
Sourcepub fn record_allocation(&self, bytes: u64)
pub fn record_allocation(&self, bytes: u64)
Records a memory allocation.
Sourcepub fn record_deallocation(&self, bytes: u64)
pub fn record_deallocation(&self, bytes: u64)
Records a memory deallocation.
Sourcepub fn reserve(&self, bytes: u64) -> ResourceResult<ReservationGuard<'_>>
pub fn reserve(&self, bytes: u64) -> ResourceResult<ReservationGuard<'_>>
Reserves memory for a future allocation.
Returns a guard that releases the reservation on drop.
Sourcepub fn validate(&self, estimate: &MemoryEstimate) -> ResourceResult<()>
pub fn validate(&self, estimate: &MemoryEstimate) -> ResourceResult<()>
Validates a memory estimate before allocation.
Sourcepub fn max_safe_elements(&self, bytes_per_element: usize) -> usize
pub fn max_safe_elements(&self, bytes_per_element: usize) -> usize
Returns the maximum safe element count for a given per-element byte cost.
Sourcepub fn set_enforce_limits(&self, enforce: bool)
pub fn set_enforce_limits(&self, enforce: bool)
Enables or disables limit enforcement.
Sourcepub fn is_enforcing(&self) -> bool
pub fn is_enforcing(&self) -> bool
Returns whether limits are being enforced.