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 builder(num_bytes: usize) -> AllocationConfig
pub fn builder(num_bytes: usize) -> AllocationConfig
Creates a builder for configuring memory allocation.
§Example
use alloc_madvise::Memory;
let memory = Memory::builder(1024)
.sequential()
.zeroed()
.allocate()
.expect("allocation failed");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(&self) -> *const c_void
👎Deprecated since 0.5.0: Use to_const_ptr or to_ptr instead
pub fn as_ptr(&self) -> *const c_void
Use to_const_ptr or to_ptr instead
See Memory::to_ptr_const or Memory::to_ptr.
Sourcepub fn to_ptr_const(&self) -> *const c_void
pub fn to_ptr_const(&self) -> *const c_void
Sourcepub fn as_ptr_mut(&mut self) -> *mut c_void
👎Deprecated since 0.5.0: Use to_ptr_mut or to_ptr instead
pub fn as_ptr_mut(&mut self) -> *mut c_void
Use to_ptr_mut or to_ptr instead
See Memory::to_ptr_mut or Memory::to_ptr.
Sourcepub fn to_ptr_mut(&mut self) -> *mut c_void
pub fn to_ptr_mut(&mut self) -> *mut c_void
Sourcepub fn to_ptr(&self) -> Option<NonNull<c_void>>
pub fn to_ptr(&self) -> Option<NonNull<c_void>>
Returns a non-null pointer to the data buffer.
§Returns
A pointer that is guaranteed to be non-null if the Memory was properly
initialized (i.e., is non-default) and wasn’t freed.
§Safety
If the memory is freed while the pointer is in use, access to the address pointed at is undefined behavior.
§Example
use alloc_madvise::{Memory, AllocationError};
fn main() -> Result<(), AllocationError> {
// Allocate 1024 bytes aligned to 64 bytes
const SIZE: usize = 1024;
const SEQUENTIAL: bool = true;
const CLEAR: bool = true;
let memory = Memory::allocate(SIZE, SEQUENTIAL, CLEAR)?;
let ptr = memory.to_ptr().expect("pointer was allocated");
// Use the allocated memory...
assert_ne!(ptr.as_ptr(), std::ptr::null_mut());
// Memory is automatically freed when dropped
Ok(())
}