pub trait McjitMemoryManager: Debug {
// Required methods
fn allocate_code_section(
&mut self,
size: uintptr_t,
alignment: c_uint,
section_id: c_uint,
section_name: &str,
) -> *mut u8;
fn allocate_data_section(
&mut self,
size: uintptr_t,
alignment: c_uint,
section_id: c_uint,
section_name: &str,
is_read_only: bool,
) -> *mut u8;
fn finalize_memory(&mut self) -> Result<(), String>;
fn destroy(&mut self);
}Expand description
A trait for user-defined memory management in MCJIT.
Implementors can override how LLVM’s MCJIT engine allocates memory for code and data sections. This is sometimes needed for:
- custom allocators,
- sandboxed or restricted environments,
- capturing stack map sections (e.g., for garbage collection),
- or other specialized JIT memory management requirements.
§StackMap and GC Integration
By examining the section_name argument in McjitMemoryManager::allocate_data_section,
you can detect sections such as .llvm_stackmaps (on ELF) or __llvm_stackmaps
(on Mach-O). Recording the location of these sections may be useful for
custom garbage collectors. For more information, refer to the LLVM
StackMaps documentation.
Typically, on Darwin (Mach-O), the stack map section name is __llvm_stackmaps,
and on Linux (ELF), it is .llvm_stackmaps.
Required Methods§
Sourcefn allocate_code_section(
&mut self,
size: uintptr_t,
alignment: c_uint,
section_id: c_uint,
section_name: &str,
) -> *mut u8
fn allocate_code_section( &mut self, size: uintptr_t, alignment: c_uint, section_id: c_uint, section_name: &str, ) -> *mut u8
Allocates a block of memory for a code section.
§Parameters
size- The size in bytes for the code section.alignment- The required alignment in bytes.section_id- A numeric ID that LLVM uses to identify this section.section_name- A name for this section, if provided by LLVM.
§Returns
Returns a pointer to the allocated memory. Implementors must ensure it is
at least size bytes long and meets alignment requirements.
Sourcefn allocate_data_section(
&mut self,
size: uintptr_t,
alignment: c_uint,
section_id: c_uint,
section_name: &str,
is_read_only: bool,
) -> *mut u8
fn allocate_data_section( &mut self, size: uintptr_t, alignment: c_uint, section_id: c_uint, section_name: &str, is_read_only: bool, ) -> *mut u8
Allocates a block of memory for a data section.
§Parameters
size- The size in bytes for the data section.alignment- The required alignment in bytes.section_id- A numeric ID that LLVM uses to identify this section.section_name- A name for this section, if provided by LLVM.is_read_only- Whether this data section should be read-only.
§Returns
Returns a pointer to the allocated memory. Implementors must ensure it is
at least size bytes long and meets alignment requirements.
Sourcefn finalize_memory(&mut self) -> Result<(), String>
fn finalize_memory(&mut self) -> Result<(), String>
Finalizes memory permissions for all allocated sections.
This is called once all sections have been allocated. Implementors can set permissions such as making code sections executable or data sections read-only.
§Errors
If any error occurs (for example, failing to set page permissions),
return an Err(String). This error is reported back to LLVM as a C string.