AllocationController

Trait AllocationController 

Source
pub trait AllocationController {
    // Required methods
    fn alloc_align(&self) -> usize;
    fn property(&self) -> AllocationProperty;
    unsafe fn memory_mut(&mut self) -> &mut [MaybeUninit<u8>];
    fn memory(&self) -> &[MaybeUninit<u8>];

    // Provided methods
    fn split(
        &mut self,
        _offset: usize,
    ) -> Result<(Box<dyn AllocationController>, Box<dyn AllocationController>), SplitError> { ... }
    fn duplicate(&self) -> Option<Box<dyn AllocationController>> { ... }
    unsafe fn copy_into(&self, buf: &mut [u8]) { ... }
    fn grow(&mut self, size: usize, align: usize) -> Result<(), AllocationError> { ... }
    fn try_detach(&mut self) -> Option<NonNull<u8>> { ... }
}
Expand description

Defines how an [Allocation] can be controlled.

This trait enables type erasure of the allocator after an [Allocation] is created, while still providing methods to modify or manage an existing [Allocation].

Required Methods§

Source

fn alloc_align(&self) -> usize

The alignment this allocation was created with.

Source

fn property(&self) -> AllocationProperty

Returns memory property for the current allocation.

Source

unsafe fn memory_mut(&mut self) -> &mut [MaybeUninit<u8>]

Returns a mutable view of the memory of the whole allocation

§Safety

Must only write initialized data to the buffer.

Source

fn memory(&self) -> &[MaybeUninit<u8>]

Returns a view of the memory of the whole allocation

Provided Methods§

Source

fn split( &mut self, _offset: usize, ) -> Result<(Box<dyn AllocationController>, Box<dyn AllocationController>), SplitError>

Splits the current allocation in multiple separate allocations.

Source

fn duplicate(&self) -> Option<Box<dyn AllocationController>>

Duplicates the current allocation with a clone on write strategy if the allocation controller supports it.

Source

unsafe fn copy_into(&self, buf: &mut [u8])

Reads the data from the current allocation controller and copy its content into the provided buffer.

§Safety

Ensures the length provided reflect initialized values in the current allocation controller.

Source

fn grow(&mut self, size: usize, align: usize) -> Result<(), AllocationError>

Extends the provided [Allocation] to a new size with specified alignment.

§Errors

Returns an AllocationError if the extension fails (e.g., due to insufficient memory or unsupported operation by the allocator).

Source

fn try_detach(&mut self) -> Option<NonNull<u8>>

Indicates whether the allocation uses the Rust alloc crate and can be safely managed by another data structure.

If true, the allocation is not managed by a memory pool and can be safely deallocated using the alloc crate.

§Notes

This allows the allocation’s pointer to be converted into a native Rust Vec without requiring a new allocation.

Implementing this incorrectly is unsafe and may lead to undefined behavior.

Implementors§