Skip to main content

AllocatedMemory

Struct AllocatedMemory 

Source
pub struct AllocatedMemory<'g> { /* private fields */ }
Expand description

AllocatedMemory itself is needed when allocating memory on the Rust side; It is an RAII wrapper over memory allocated on the heap of the zygote clone via Scope::alloc; Automatically sends a Free request when going out of scope (Drop).

Important: Library has its own methods for working with memory - they are also needed, but only when we are not the creators of the memory region.

To work with raw addresses allocated by the C side (for example, strdup), use the Library methods directly.

'g is the lifetime of the ScopeGuard block of crate::ffi! in which it was created. Until it is 'static — the value physically cannot be returned from crate::ffi! outside.

Implementations§

Source§

impl<'g> AllocatedMemory<'g>

Source

pub const fn address(&self) -> usize

Returns the raw memory address of the allocation.

Source

pub const fn length(&self) -> usize

Returns the size of the allocated memory block in bytes.

Source

pub const fn asPointer(&self) -> Pointer

Wraps the address into a Pointer for FFI calls.

Source

pub fn read(&self) -> Result<Vec<u8>, FFIError>

Reads the entire allocated memory block from the zygote as raw bytes.

Source

pub fn write(&self, value: impl FfiArg) -> Result<(), FFIError>

Writes a value into the allocated memory block in the zygote.

Source

pub fn readStruct<T: Pod + Zeroable>(&self) -> Result<T, FFIError>

Reads the allocated memory as a statically-typed C struct T.

T must be #[repr(C)] and implement Pod (plain old data) from bytemuck. The Pod bound is checked at compile time — if T has padding or non-POD fields, the code will simply refuse to compile instead of silently reading garbage.

This eliminates all manual byte parsing: the struct’s size and field layout are guaranteed correct by the type system rather than by manually calculated offsets.

Source

pub fn writeStruct<T: Pod + Zeroable>(&self, value: &T) -> Result<(), FFIError>

Writes a statically-typed C struct T into the allocated memory buffer.

T must be #[repr(C)] and implement Pod from bytemuck.

The entire struct is serialized to bytes and written to the zygote’s memory.

Trait Implementations§

Source§

impl<'g> Drop for AllocatedMemory<'g>

Source§

fn drop(&mut self)

Automatically frees the allocated memory in the zygote on scope exit.

Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more

Auto Trait Implementations§

§

impl<'g> Freeze for AllocatedMemory<'g>

§

impl<'g> RefUnwindSafe for AllocatedMemory<'g>

§

impl<'g> Send for AllocatedMemory<'g>

§

impl<'g> Sync for AllocatedMemory<'g>

§

impl<'g> Unpin for AllocatedMemory<'g>

§

impl<'g> UnsafeUnpin for AllocatedMemory<'g>

§

impl<'g> UnwindSafe for AllocatedMemory<'g>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.