#[repr(C)]pub struct AbiBox {
pub data: *mut c_void,
pub len: usize,
pub free: unsafe extern "C" fn(data: *mut c_void, len: usize),
}Expand description
A cross-module data box: memory allocated and freed by the same module.
This is the cdyn mode’s raw-data smart pointer, complementing the
ref-counted instance handle AbiRef<T>:
- Instance objects (with vtables) → ref-counted, use
AbiStableDynRef’sretain/releaseviaAbiRef<T>. - Raw data buffers (payloads, serialized blobs, arrays) → single-owner,
use this struct’s
freefunction pointer.
Both share the same principle: the deallocator lives in the module that allocated the memory, so allocator mismatches across module/CRT boundaries (notably on Windows) can never corrupt the heap.
The box is move-only: there is exactly one owner, and dropping it
(or explicitly calling free) hands the memory back to its origin module.
Ref-counted sharing of data should be modeled as an instance instead.
§C/C++/Zig side
Any language can produce a box: allocate, fill, and export
struct { void* data; size_t len; void (*free)(void*, size_t); }
where free calls the module’s own allocator (C++: operator delete[]
/ std::free, Zig: allocator.free). The layout is #[repr(C)] /
plain C struct.
Fields§
§data: *mut c_voidPointer to the data. Allocated by the producing module.
len: usizeLength in bytes.
free: unsafe extern "C" fn(data: *mut c_void, len: usize)Frees data. Must be a function from the module that allocated it.
Implementations§
Source§impl AbiBox
impl AbiBox
pub fn is_null(&self) -> bool
Sourcepub fn from_vec(v: Vec<u8>) -> Self
pub fn from_vec(v: Vec<u8>) -> Self
Plugin side (Rust) — wrap an owned Vec<u8> into a box.
The buffer is exact-fit (into_boxed_slice), so len == capacity and
the paired abi_box_free_rust can reconstruct and free it. The
returned free pointer executes in the plugin’s code — the module
that owns the allocation.