pub struct MemoryPressure { /* private fields */ }Expand description
A memory-pressure guard that holds size_bytes of allocated memory
for its entire lifetime.
Drop the guard to release the memory.
§Example
use dev_chaos::memory_pressure::MemoryPressure;
{
let _hold = MemoryPressure::allocate(1_024 * 1_024); // 1 MiB
// ... run code under memory pressure ...
} // memory released hereImplementations§
Source§impl MemoryPressure
impl MemoryPressure
Sourcepub fn allocate(size_bytes: usize) -> Self
pub fn allocate(size_bytes: usize) -> Self
Allocate and hold size_bytes of zeroed memory.
Always succeeds for sizes the allocator can satisfy. Returns the guard; drop it to release the memory.
For testing OOM / allocation-failure paths specifically, see
MemoryPressure::try_allocate.
Sourcepub fn try_allocate(size_bytes: usize) -> Result<Self, TryReserveError>
pub fn try_allocate(size_bytes: usize) -> Result<Self, TryReserveError>
Attempt to allocate size_bytes, returning the error path
rather than panicking on allocator failure.
This catches try_reserve_exact errors. For genuinely OOM
scenarios on most platforms the kernel may kill the process
before this returns; honest behavior is platform-specific.
Sourcepub fn size_bytes(&self) -> usize
pub fn size_bytes(&self) -> usize
The number of bytes this guard is holding.
Sourcepub fn allocate_kib(kib: usize) -> Self
pub fn allocate_kib(kib: usize) -> Self
Convenience: allocate kib KiB.
Sourcepub fn allocate_mib(mib: usize) -> Self
pub fn allocate_mib(mib: usize) -> Self
Convenience: allocate mib MiB.
Auto Trait Implementations§
impl Freeze for MemoryPressure
impl RefUnwindSafe for MemoryPressure
impl Send for MemoryPressure
impl Sync for MemoryPressure
impl Unpin for MemoryPressure
impl UnsafeUnpin for MemoryPressure
impl UnwindSafe for MemoryPressure
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more