Function region::alloc[][src]

pub fn alloc(size: usize, protection: Protection) -> Result<Allocation>
Expand description

Allocates one or more pages of memory, with a defined protection.

This function provides a very simple interface for allocating anonymous virtual pages. The allocation address will be decided by the operating system.

Parameters

  • The size may not be zero.
  • The size is rounded up to the closest page boundary.

Errors

  • If an interaction with the underlying operating system fails, an error will be returned.
  • If size is zero, Error::InvalidParameter will be returned.

Examples

use region::Protection;
let ret5 = [0xB8, 0x05, 0x00, 0x00, 0x00, 0xC3u8];

let memory = region::alloc(100, Protection::READ_WRITE_EXECUTE)?;
let slice = unsafe {
  std::slice::from_raw_parts_mut(memory.as_ptr::<u8>() as *mut u8, memory.len())
};

slice[..6].copy_from_slice(&ret5);
let x: extern "C" fn() -> i32 = unsafe { std::mem::transmute(slice.as_ptr()) };

assert_eq!(x(), 5);