alloc_traits

Trait LocalAlloc

Source
pub unsafe trait LocalAlloc<'alloc> {
    // Required methods
    fn alloc(&'alloc self, layout: NonZeroLayout) -> Option<Allocation<'alloc>>;
    unsafe fn dealloc(&'alloc self, alloc: Allocation<'alloc>);

    // Provided methods
    fn alloc_zeroed(
        &'alloc self,
        layout: NonZeroLayout,
    ) -> Option<Allocation<'alloc>> { ... }
    unsafe fn realloc(
        &'alloc self,
        alloc: Allocation<'alloc>,
        layout: NonZeroLayout,
    ) -> Option<Allocation<'alloc>> { ... }
}
Expand description

An allocator providing memory regions valid for a particular lifetime.

It is useful to compare this trait to std::alloc::GlobalAlloc. Similar to the trait it is required that the implementors adhere to the contract of the methods.

Required Methods§

Source

fn alloc(&'alloc self, layout: NonZeroLayout) -> Option<Allocation<'alloc>>

Allocate one block of memory.

The callee guarantees that a successful return contains a pointer that is valid for at least the layout requested by the caller.

Source

unsafe fn dealloc(&'alloc self, alloc: Allocation<'alloc>)

Deallocate a block previously allocated.

§Safety

The caller must ensure that:

  • alloc has been previously returned from a call to alloc.
  • There are no more pointer to the allocation.

Provided Methods§

Source

fn alloc_zeroed( &'alloc self, layout: NonZeroLayout, ) -> Option<Allocation<'alloc>>

Allocate a block of memory initialized with zeros.

The callee guarantees that a successful return contains a pointer that is valid for at least the layout requested by the caller and the contiguous region of bytes, starting at the pointer and with the size of the returned layout, is initialized and zeroed.

Source

unsafe fn realloc( &'alloc self, alloc: Allocation<'alloc>, layout: NonZeroLayout, ) -> Option<Allocation<'alloc>>

Change the layout of a block previously allocated.

The callee guarantees that a successful return contains a pointer that is valid for at least the layout requested by the caller and the contiguous region of bytes, starting at the pointer and with the size of the returned layout, is initialized with the prefix of the previous allocation that is still valid.

Note that it is NOT safe to elide the methods call for changing the alignment of the layout to a less strict one, or to an incidentally fulfilled stricter version. The allocator might make use of the alignment during deallocation.

Implementors§