Skip to main content

unwrapped_malloc

Function unwrapped_malloc 

Source
pub unsafe fn unwrapped_malloc(size: usize, alignment: usize) -> *mut u8
Expand description

Allocate size bytes from mimalloc’s raw-OS-layer “unwrapped” path.

Thin wrapper around mi_unwrapped_malloc (include/mimalloc/memory-events.h): backed directly by _mi_os_alloc_aligned, never by the hooked mi_malloc family. Page granular, so this is not meant for hot-path/small allocations — it exists for low-level instrumentation and recursion avoidance (e.g. scratch storage for a memory-change callback that must not recursively enter mimalloc). Excluded from normal mimalloc allocation stats and from the memory-change accounting.

Returns a null pointer on failure (including invalid alignment; see # Safety below).

§Safety

  • alignment must be 0 (treated as align_of::<*const ()>(), i.e. pointer size) or a power of two. A non-power-of-two, non-zero alignment is a validated input on the C side: mi_unwrapped_malloc returns a null pointer rather than invoking undefined behavior, but callers should not rely on that as anything other than a defined-failure contract — treat the alignment argument as a precondition to get right, not a value to probe.
  • The returned pointer, if non-null, must be passed only to unwrapped_free or unwrapped_realloc — never to mi_free, this crate’s MiMalloc allocator, or Rust’s global allocator, and vice versa (a pointer from mi_malloc/the Rust global allocator must never be passed to unwrapped_free/unwrapped_realloc). Mixing these families corrupts allocator-internal bookkeeping.
  • The memory is uninitialized; reading it before writing is undefined behavior, as with any raw allocation.