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
use_multiple_poolsflag 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()andshrink().
Implementations§
Source§impl JitAllocator
impl JitAllocator
Sourcepub fn new(params: JitAllocatorOptions) -> Box<Self>
pub fn new(params: JitAllocatorOptions) -> Box<Self>
Creates a new JitAllocator instance.
Sourcepub unsafe fn reset(&mut self, reset_policy: ResetPolicy)
pub unsafe 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.
Sourcepub fn alloc(&mut self, size: usize) -> Result<Span, AsmError>
pub fn alloc(&mut self, size: usize) -> Result<Span, AsmError>
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.
Sourcepub unsafe fn release(&mut self, rx_ptr: *const u8) -> Result<(), AsmError>
pub unsafe fn release(&mut self, rx_ptr: *const u8) -> Result<(), AsmError>
Releases the memory allocated by alloc.
§SAFETY
rx_ptrmust have been returned fromallocrx_ptrmust have been allocaetd from this allocatorrx_ptrmust not have been passed toreleasebeforerx_ptrmust point to read-execute part of memory returned fromalloc.
Sourcepub unsafe fn shrink(
&mut self,
rx_ptr: *const u8,
new_size: usize,
) -> Result<(), AsmError>
pub unsafe fn shrink( &mut self, rx_ptr: *const u8, new_size: usize, ) -> Result<(), AsmError>
Sourcepub fn query(&self, rx_ptr: *const u8) -> Result<Span, AsmError>
pub fn query(&self, rx_ptr: *const u8) -> Result<Span, AsmError>
Takes a pointer into the JIT memory and tries to query RX, RW mappings and size of the allocation.
Sourcepub unsafe fn write(
&mut self,
span: &mut Span,
write_func: impl FnMut(&mut Span),
) -> Result<(), AsmError>
pub unsafe fn write( &mut self, span: &mut Span, write_func: impl FnMut(&mut Span), ) -> Result<(), AsmError>
Writes to the memory referenced by span using write_func and then flushes the instruction cache for that memory range.
§SAFETY
spanmust reference a valid memory range allocated by this allocator.write_funcmust only write to the memory range referenced byspan.- After
write_funcis called, the memory range referenced byspanmust contain valid executable code. - The caller must ensure that no other threads are executing code in the memory range referenced by
spanwhilewrite_funcis modifying it.