Skip to main content

Module allocator

Module allocator 

Source
Expand description

R-backed global allocator for Rust.

Allocations are backed by R RAWSXP objects and protected from GC via R_PreserveObject/R_ReleaseObject (R’s precious list).

§Protection Strategy

This allocator uses R_PreserveObject directly because:

  • Allocations may need to survive across multiple .Call invocations
  • The SEXP (RAWSXP) is its own handle — zero Rust-side bookkeeping
  • LIFO release pattern (recently allocated = first freed) means O(1) release in practice (R’s precious list scans from head)

See the crate-level documentation for an overview of miniextendr’s protection mechanisms.

§Layout

Layout inside the RAWSXP (bytes): [optional leading pad][Header][user bytes…]

We always return a pointer aligned to at least: max(requested_align, align_of::<Header>()) so the Header placed immediately before the user pointer is always aligned.

§⚠️ Warning: longjmp Risk

R’s Rf_allocVector can longjmp on allocation failure instead of returning NULL. If this happens, Rust destructors will NOT run, potentially causing:

  • Resource leaks (files, locks, etc.)
  • Corrupted state if allocation happens mid-operation

This allocator is best suited for:

  • Short-lived operations within a single R API call
  • Contexts where R_UnwindProtect is active (e.g., inside run_on_worker)

For long-lived allocations or critical cleanup requirements, consider using Rust’s standard allocator instead.

Structs§

Header 🔒
Metadata stored immediately before the returned user pointer.
RAllocator
R-backed global allocator.

Constants§

HEADER_ALIGN 🔒
HEADER_SIZE 🔒

Functions§

alloc_main_thread 🔒
Allocate memory on the R main thread.
dealloc_main_thread 🔒
Deallocate memory on the R main thread.
realloc_main_thread 🔒
Reallocate memory on the R main thread.
sendable_data_ptr_get 🔒
sendable_data_ptr_is_null 🔒
sendable_data_ptr_new 🔒
sendable_data_ptr_null 🔒
with_r_thread_or_inline 🔒
Routes a closure to the R main thread if not already there.

Type Aliases§

SendableDataPtr 🔒
Wrapper to make *mut u8 pointers Send for cross-thread routing.