Trait Allocator

Source
pub trait Allocator: Clone {
    // Required methods
    fn alloc(&self, layout: Layout) -> Result<GlobalPtr<u8>, AllocError>;
    unsafe fn dealloc(&self, ptr: GlobalPtr<u8>, layout: Layout);

    // Provided methods
    fn alloc_with<T>(
        &self,
        f: impl FnOnce(RefMut<'_, MaybeUninit<T>>) -> Result<RefMut<'_, T>, AllocError>,
    ) -> Result<GlobalPtr<u8>, AllocError> { ... }
    unsafe fn realloc(
        &self,
        ptr: GlobalPtr<u8>,
        layout: Layout,
        newsize: usize,
    ) -> Result<GlobalPtr<u8>, AllocError> { ... }
}
Expand description

Basic allocation trait.

Required Methods§

Source

fn alloc(&self, layout: Layout) -> Result<GlobalPtr<u8>, AllocError>

Allocate based on layout within this allocator. Returns a global pointer to the start of the allocation.

Note: Using this function by itself can leak memory, particularly on failure. Users should consider using InvBox instead.

Source

unsafe fn dealloc(&self, ptr: GlobalPtr<u8>, layout: Layout)

Free an allocation.

§Safety

Caller must ensure that the pointer is valid and was allocated by this allocator, and refers to memory that matches the provided layout.

Provided Methods§

Source

fn alloc_with<T>( &self, f: impl FnOnce(RefMut<'_, MaybeUninit<T>>) -> Result<RefMut<'_, T>, AllocError>, ) -> Result<GlobalPtr<u8>, AllocError>

Allocate based on layout within this allocator. Returns a global pointer to the start of the allocation.

Note: Using this function by itself can leak memory, particularly on failure. Users should consider using InvBox instead.

Source

unsafe fn realloc( &self, ptr: GlobalPtr<u8>, layout: Layout, newsize: usize, ) -> Result<GlobalPtr<u8>, AllocError>

Reallocate an allocation.

§Safety

Caller must ensure that the pointer is valid and was allocated by this allocator, and refers to memory that matches the provided layout.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§