Skip to main content

leak_zeroed_pages

Function leak_zeroed_pages 

Source
pub fn leak_zeroed_pages(size: usize) -> Option<NonNull<u8>>
Expand description

Reserve size bytes of zero-initialised anonymous virtual memory and leak it for the process lifetime, returning the base pointer.

Folds the leaked-zeroed-sidecar pattern (used by allocators for pre-main bookkeeping structures that must not route through the very allocator they implement) into one helper:

  • size is rounded up to a multiple of PAGE internally (any non-zero size is accepted; a zero size returns None). On some platforms (e.g. macOS with 16 KiB pages, or 64 KiB Windows allocation granularity), the OS may round further beyond PAGE, so the actual granularity consumed can exceed PAGE.
  • the span is guaranteed all-zero on every backend, INCLUDING the miri fallback (std::alloc does not zero; this helper zeroes explicitly under miri), so the returned memory is a valid all-zero initial state.
  • the reservation is mem::forget-leaked: it lives for the process lifetime and is never released.

Returns None on OOM or a zero size. The returned pointer is non-null, PAGE-aligned, and valid for the rounded-up size for the whole process lifetime. Because the reservation is leaked, the returned pointer may be safely turned into a &'static by the caller (subject to the caller’s own aliasing discipline).