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
.Callinvocations - 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_UnwindProtectis active (e.g., inside a#[miniextendr]body)
For long-lived allocations or critical cleanup requirements, consider using Rust’s standard allocator instead.
Re-exports§
use crate::sys::R_PreserveObject_unchecked;🔒use crate::sys::R_ReleaseObject_unchecked;🔒use crate::SEXP;🔒use crate::SEXPTYPE;🔒use crate::SexpExt;🔒use crate::with_r_thread;🔒use core::alloc::GlobalAlloc;🔒use core::alloc::Layout;🔒use core::mem;🔒use core::ptr;🔒
Structs§
- Header 🔒
- Metadata stored immediately before the returned user pointer.
- RAllocator
- R-backed global allocator.
Constants§
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.