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().
Like asmjit’s JitAllocator, this is a low-level interface: Spans are plain handles that do not own
their memory. Allocations live until they are passed to JitAllocator::release, or until the allocator
is reset or dropped, and it is up to the caller not to use a span past that point.
Implementations§
Source§impl JitAllocator
impl JitAllocator
Sourcepub fn new(params: JitAllocatorOptions) -> Box<Self>
pub fn new(params: JitAllocatorOptions) -> Box<Self>
Creates a new JIT allocator.
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.
§Safety
- The caller must ensure that no code is currently executing from the memory managed by this allocator.
- The caller must ensure that no references to the memory managed by this allocator are used after calling this function.
- The caller must ensure that no other thread is currently accessing the memory managed by this allocator.
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.
The returned Span holds 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 fromallocon this allocator.rx_ptrmust not have been passed torelease/shrinkbefore.rx_ptrmust point to the read-execute mapping returned byalloc, and no pointer into the released area may be used afterwards.
Sourcepub unsafe fn shrink(
&mut self,
span: &mut Span,
new_size: usize,
) -> Result<(), AsmError>
pub unsafe fn shrink( &mut self, span: &mut Span, new_size: usize, ) -> Result<(), AsmError>
Shrinks the memory allocated by alloc and updates span.size() to match.
A new_size of zero releases the allocation.
§Safety
span must describe a live allocation returned by alloc on this
allocator; no pointer into the released tail may be used afterwards.
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 to the start of an allocation and returns a Span
describing its RX and RW mappings and size.
Sourcepub unsafe fn write(
&mut self,
span: &mut Span,
write_func: impl FnOnce(&mut Span),
) -> Result<(), AsmError>
pub unsafe fn write( &mut self, span: &mut Span, write_func: impl FnOnce(&mut Span), ) -> Result<(), AsmError>
Writes through a span and synchronizes the instruction cache.
Makes JIT memory writable for the duration of write_func (MAP_JIT
platforms), then flushes the instruction cache for the whole span.
§Safety
span must describe a live allocation of this allocator,
write_func must only write inside it and leave valid executable code
there, and no thread may execute it while the closure is modifying it.
Sourcepub unsafe fn copy_from_slice(
&mut self,
span: &mut Span,
offset: usize,
slice: &[u8],
) -> Result<(), AsmError>
pub unsafe fn copy_from_slice( &mut self, span: &mut Span, offset: usize, slice: &[u8], ) -> Result<(), AsmError>
Copies slice into span at offset and synchronizes the written
instruction-cache range.
§Safety
span must describe a live allocation of this allocator, and no thread
may execute the written range while it is being modified. The range is
bounds-checked against span.size().