Struct alloc_madvise::Memory
source · pub struct Memory { /* private fields */ }Expand description
Allocated memory.
§Example
const FOUR_MEGABYTES: usize = 4 * 1024 * 1024;
// Allocate 2 MiB of aligned, zeroed-out, sequential read memory.
// The memory will be automatically freed when it leaves scope.
let mut memory = Memory::allocate(FOUR_MEGABYTES, true, true).unwrap();
// Get a reference to a mutable slice.
let data: &mut [f32] = memory.as_mut();
data[0] = 1.234;
data[1] = 5.678;
// Get a reference to an immutable slice.
let reference: &[f32] = memory.as_ref();
assert_eq!(reference[0], 1.234);
assert_eq!(reference[1], 5.678);
assert_eq!(reference[2], 0.0);
assert_eq!(reference.len(), memory.len() / std::mem::size_of::<f32>());Implementations§
source§impl Memory
impl Memory
sourcepub fn allocate(
num_bytes: usize,
sequential: bool,
clear: bool,
) -> Result<Self, AllocationError>
pub fn allocate( num_bytes: usize, sequential: bool, clear: bool, ) -> Result<Self, AllocationError>
Allocates memory of the specified number of bytes.
The optimal alignment will be determined by the number of bytes provided. If the amount of bytes is a multiple of 2MB, Huge/Large Page support is enabled.
§Arguments
num_bytes- The number of bytes to allocate.sequential- Whether or not the memory access pattern is sequential mostly.clear- Whether or not to zero out the allocated memory.
sourcepub fn free(&mut self)
pub fn free(&mut self)
Frees memory of the specified number of bytes.
The memory instance is required to be created by allocate.
sourcepub fn as_ptr_mut(&mut self) -> *mut c_void
pub fn as_ptr_mut(&mut self) -> *mut c_void
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Memory
impl RefUnwindSafe for Memory
impl !Send for Memory
impl !Sync for Memory
impl Unpin for Memory
impl UnwindSafe for Memory
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