pub fn alloc<VM: VMBinding>(
mutator: &mut Mutator<VM>,
size: usize,
align: usize,
offset: usize,
semantics: AllocationSemantics,
) -> Address
Expand description
Allocate memory for an object.
When the allocation is successful, it returns the starting address of the new object. The
memory range for the new object is size
bytes starting from the returned address, and
RETURNED_ADDRESS + offset
is guaranteed to be aligned to the align
parameter. The returned
address of a successful allocation will never be zero.
If MMTk fails to allocate memory, it will attempt a GC to free up some memory and retry the
allocation. After triggering GC, it will call crate::vm::Collection::block_for_gc
to suspend
the current thread that is allocating. Callers of alloc
must be aware of this behavior.
For example, JIT compilers that support
precise stack scanning need to make the call site of alloc
a GC-safe point by generating stack maps. See
alloc_with_options
if it is undesirable to trigger GC at this allocation site.
If MMTk has attempted at least one GC, and still cannot free up enough memory, it will call
crate::vm::Collection::out_of_memory
to inform the binding. The VM binding
can implement that method to handle the out-of-memory event in a VM-specific way, including but
not limited to throwing exceptions or errors. If crate::vm::Collection::out_of_memory
returns
normally without panicking or throwing exceptions, this function will return zero.
For performance reasons, a VM should implement the allocation fast-path on their side rather than just calling this function.
Arguments:
mutator
: The mutator to perform this allocation request.size
: The number of bytes required for the object.align
: Required alignment for the object.offset
: Offset associated with the alignment.semantics
: The allocation semantic required for the allocation.