Skip to main content

JitMemory

Struct JitMemory 

Source
pub struct JitMemory { /* private fields */ }
Expand description

A buffer for JIT code execution.

§Example

use gaia_jit::JitMemory;

let mut jit = JitMemory::new(4096).unwrap();
// Write some machine code...
jit.write(&[0x90, 0xc3]).unwrap(); // nop; ret
jit.make_executable().unwrap();

unsafe {
    let f: unsafe extern "C" fn() = jit.as_fn();
    f();
}

Implementations§

Source§

impl JitMemory

Source

pub fn new(capacity: usize) -> Result<Self>

Allocate a new JIT memory block with the given capacity.

Source

pub fn write(&mut self, code: &[u8]) -> Result<()>

Write code into the buffer.

This method automatically ensures the memory is writable before writing, and restores the previous protection after if possible.

Source

pub fn make_executable(&self) -> Result<*const u8>

Make the memory executable and return a pointer to the start.

Source

pub fn make_writable(&self) -> Result<()>

Make the memory readable and writable again (e.g. for updates).

Source

pub fn len(&self) -> usize

Get the current length of the code in the buffer.

Source

pub fn capacity(&self) -> usize

Get the capacity of the buffer.

Source

pub fn is_empty(&self) -> bool

Is the buffer empty?

Source

pub fn as_slice(&self) -> &[u8]

As a slice of bytes (read-only).

Source

pub unsafe fn as_fn<S>(&self) -> S

Cast the JIT memory to a function pointer of type S.

Trait Implementations§

Source§

impl Drop for JitMemory

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl Send for JitMemory

Source§

impl Sync for JitMemory

Auto Trait Implementations§

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.