11 releases

0.2.8 Jul 6, 2023
0.2.7 Jul 6, 2023
0.2.5 Jun 27, 2023
0.1.1 Jun 25, 2023

#164 in Memory management

Download history 20/week @ 2024-02-26 10/week @ 2024-03-04 12/week @ 2024-03-11 13/week @ 2024-03-18 5/week @ 2024-03-25 29/week @ 2024-04-01 17/week @ 2024-04-15

52 downloads per month
Used in 2 crates (via macroassembler)

MIT license

86KB
2K SLoC

jit-allocator

A simple memory allocator for executable code. Use JitAllocator type to allocate/release memory and virtual_memory module functions to enable proper access for executable code. So if you want to allocate a new code to execute it is usually done like this:

use jit_allocator::*;
let compiled_code = ...;
let compiled_code_size = ...;

let mut jit_allocator = JitAllocator::new(Default::default());
let (rx, rw) = jit_allocator.alloc(size)?;

protect_jit_memory(ProtectJitAccess::ReadWrite); // allows to write to RWX code in current thread,
                                                 // it is no-op on all platforms except macOS AArch64
unsafe { copy_nonoverlapping(compiled_code, rw, compiled_code_size);  }
protect_jit_memory(ProtectJitAccess::ReadExecute); // disables writes to RWX code in current thread, 
                                                   // it is no-op on all platforms except macOS AArch64
flush_instruction_cache(rx, compiled_code_size); // flush icache, not required on x86-64 
                                                 // but required on other platforms due 
                                                 // to lack of coherent dcache/icache.

// When you're done with your machine code you can release it:
unsafe { 
    jit_allocator.release(rx)?;
}

Dependencies

~0.8–11MB
~73K SLoC