Memory

Struct 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

Source

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.
Source

pub fn free(&mut self)

Frees memory of the specified number of bytes.

The memory instance is required to be created by allocate.

Source

pub fn len(&self) -> usize

Returns the number of bytes allocated.

Source

pub fn is_empty(&self) -> bool

Returns whether this instance has zero bytes allocated.

Source

pub fn as_ptr(&self) -> *const c_void

👎Deprecated since 0.5.0: Use to_const_ptr or to_ptr instead
Source

pub fn to_ptr_const(&self) -> *const c_void

Returns a pointer to the constant data buffer.

§Returns

A valid pointer.

§Safety

If the memory is freed while the pointer is in use, access to the address pointed at is undefined behavior.

Source

pub fn as_ptr_mut(&mut self) -> *mut c_void

👎Deprecated since 0.5.0: Use to_ptr_mut or to_ptr instead
Source

pub fn to_ptr_mut(&mut self) -> *mut c_void

Returns a mutable pointer to the mutable data buffer.

§Returns

A valid pointer.

§Safety

If the memory is freed while the pointer is in use, access to the address pointed at is undefined behavior.

Source

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(())
}

Trait Implementations§

Source§

impl AsMut<[c_void]> for Memory

Source§

fn as_mut(&mut self) -> &mut [c_void]

Converts this type into a mutable reference of the (usually inferred) input type.
Source§

impl AsMut<[f32]> for Memory

Source§

fn as_mut(&mut self) -> &mut [f32]

Converts this type into a mutable reference of the (usually inferred) input type.
Source§

impl AsMut<[f64]> for Memory

Source§

fn as_mut(&mut self) -> &mut [f64]

Converts this type into a mutable reference of the (usually inferred) input type.
Source§

impl AsMut<[i16]> for Memory

Source§

fn as_mut(&mut self) -> &mut [i16]

Converts this type into a mutable reference of the (usually inferred) input type.
Source§

impl AsMut<[i32]> for Memory

Source§

fn as_mut(&mut self) -> &mut [i32]

Converts this type into a mutable reference of the (usually inferred) input type.
Source§

impl AsMut<[i64]> for Memory

Source§

fn as_mut(&mut self) -> &mut [i64]

Converts this type into a mutable reference of the (usually inferred) input type.
Source§

impl AsMut<[i8]> for Memory

Source§

fn as_mut(&mut self) -> &mut [i8]

Converts this type into a mutable reference of the (usually inferred) input type.
Source§

impl AsMut<[isize]> for Memory

Source§

fn as_mut(&mut self) -> &mut [isize]

Converts this type into a mutable reference of the (usually inferred) input type.
Source§

impl AsMut<[u16]> for Memory

Source§

fn as_mut(&mut self) -> &mut [u16]

Converts this type into a mutable reference of the (usually inferred) input type.
Source§

impl AsMut<[u32]> for Memory

Source§

fn as_mut(&mut self) -> &mut [u32]

Converts this type into a mutable reference of the (usually inferred) input type.
Source§

impl AsMut<[u64]> for Memory

Source§

fn as_mut(&mut self) -> &mut [u64]

Converts this type into a mutable reference of the (usually inferred) input type.
Source§

impl AsMut<[u8]> for Memory

Source§

fn as_mut(&mut self) -> &mut [u8]

Converts this type into a mutable reference of the (usually inferred) input type.
Source§

impl AsMut<[usize]> for Memory

Source§

fn as_mut(&mut self) -> &mut [usize]

Converts this type into a mutable reference of the (usually inferred) input type.
Source§

impl AsRef<[c_void]> for Memory

Source§

fn as_ref(&self) -> &[c_void]

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl AsRef<[f32]> for Memory

Source§

fn as_ref(&self) -> &[f32]

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl AsRef<[f64]> for Memory

Source§

fn as_ref(&self) -> &[f64]

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl AsRef<[i16]> for Memory

Source§

fn as_ref(&self) -> &[i16]

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl AsRef<[i32]> for Memory

Source§

fn as_ref(&self) -> &[i32]

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl AsRef<[i64]> for Memory

Source§

fn as_ref(&self) -> &[i64]

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl AsRef<[i8]> for Memory

Source§

fn as_ref(&self) -> &[i8]

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl AsRef<[isize]> for Memory

Source§

fn as_ref(&self) -> &[isize]

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl AsRef<[u16]> for Memory

Source§

fn as_ref(&self) -> &[u16]

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl AsRef<[u32]> for Memory

Source§

fn as_ref(&self) -> &[u32]

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl AsRef<[u64]> for Memory

Source§

fn as_ref(&self) -> &[u64]

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl AsRef<[u8]> for Memory

Source§

fn as_ref(&self) -> &[u8]

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl AsRef<[usize]> for Memory

Source§

fn as_ref(&self) -> &[usize]

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl Debug for Memory

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Memory

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Drop for Memory

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.