Skip to main content

AllocationController

Trait AllocationController 

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

    // Provided methods
    fn capacity(&self) -> usize { ... }
    fn split(
        &mut self,
        _offset: usize,
    ) -> Result<(Box<dyn AllocationController>, Box<dyn AllocationController>), SplitError> { ... }
    fn view(
        &self,
        _start: usize,
        _end: usize,
    ) -> Option<Box<dyn AllocationController>> { ... }
    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, policy: AccessPolicy, ) -> Result<&mut [MaybeUninit<u8>], AccessError>

Returns a mutable view of the memory of the whole allocation, subject to policy.

Returns AccessError::WouldCopy if satisfying the access would require a copy (e.g. copy-on-write of shared storage) that policy forbids, or AccessError::Read if materializing lazy storage fails.

§Safety

Must only write initialized data to the buffer.

Source

fn memory( &self, policy: AccessPolicy, ) -> Result<&[MaybeUninit<u8>], AccessError>

Returns a view of the memory of the whole allocation, subject to policy.

Returns AccessError::WouldCopy if satisfying the access would require a copy (e.g. materializing lazy storage) that policy forbids, or AccessError::Read if materializing fails.

Provided Methods§

Source

fn capacity(&self) -> usize

The total byte capacity of the allocation, WITHOUT materializing lazy storage.

The default is correct for already-resident controllers (where memory() is free); lazy controllers must override it to report their size cheaply so that querying the capacity never triggers a copy.

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 view( &self, _start: usize, _end: usize, ) -> Option<Box<dyn AllocationController>>

Returns a zero-copy controller over the sub-range [start, end) of this allocation when the backend supports cheap sub-views (files and shared buffers), borrowing &self. Returns None otherwise.

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.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§