pub struct JitAllocator { /* private fields */ }
Expand description

A simple implementation of memory manager that uses virtual_memory. functions to manage virtual memory for JIT compiled code.

Implementation notes:

  • Granularity of allocated blocks is different than granularity for a typical C malloc. In addition, the allocator can use several memory pools having a different granularity to minimize the maintenance overhead. Multiple pools feature requires kFlagUseMultiplePools flag to be set.

  • The allocator doesn’t store any information in executable memory, instead, the implementation uses two bit-vectors to manage allocated memory of each allocator-block. The first bit-vector called ‘used’ is used to track used memory (where each bit represents memory size defined by granularity) and the second bit vector called ‘stop’ is used as a sentinel to mark where the allocated area ends.

  • Internally, the allocator also uses RB tree to keep track of all blocks across all pools. Each inserted block is added to the tree so it can be matched fast during release() and shrink().

Implementations§

source§

impl JitAllocator

source

pub fn new(params: JitAllocatorOptions) -> Box<Self>

Creates a new JitAllocator instance.

source

pub fn reset(&mut self, reset_policy: ResetPolicy)

Resets current allocator by emptying all pools and blocks.

Frees all memory is ResetPolicy::Hard is specified or immediate_release in JitAllocatorOptions is specific.

source

pub fn alloc(&mut self, size: usize) -> Result<(*const u8, *mut u8), Error>

Allocates size bytes in the executable memory region. Returns two pointers. One points to Read-Execute mapping and another to Read-Write mapping. All code writes must go to the Read-Write mapping.

source

pub fn release(&mut self, rx_ptr: *const u8) -> Result<(), Error>

Releases the memory allocated by alloc.

source

pub fn shrink( &mut self, rx_ptr: *const u8, new_size: usize ) -> Result<(), Error>

Shrinks the memory allocated by alloc.

source

pub fn query( &mut self, rx_ptr: *const u8 ) -> Result<(*const u8, *mut u8, usize), Error>

Takes a pointer into the JIT memory and tries to query RX, RW mappings and size of the allocation.

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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 Twhere 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 Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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.