Skip to main content

Allocator

Trait Allocator 

Source
pub unsafe trait Allocator {
    // Required methods
    fn alloc(&mut self, size: usize) -> *mut u8;
    fn calloc(&mut self, count: usize, size: usize) -> *mut u8;
    unsafe fn dealloc(&mut self, ptr: *mut u8);
    unsafe fn realloc(&mut self, ptr: *mut u8, new_size: usize) -> *mut u8;
    unsafe fn usable_size(ptr: *mut u8) -> usize
       where Self: Sized;
}
Expand description

The allocator interface

§Safety

Failure to implement this trait correctly will result in undefined behavior.

  • alloc must return a either a null pointer or a pointer to an available region of memory atleast size bytes and aligned to the size of usize.
  • realloc must either return a null pointer or return a pointer to an available region of memory atleast new_size bytes and aligned to the size of usize.
  • usable_size must return the amount of available memory for any allocation allocated with this allocator.

Required Methods§

Source

fn alloc(&mut self, size: usize) -> *mut u8

Allocate new memory

Source

fn calloc(&mut self, count: usize, size: usize) -> *mut u8

Allocates memory for an array of num objects of size and initializes all bytes in the allocated storage to zero.

Source

unsafe fn dealloc(&mut self, ptr: *mut u8)

De-allocate previously allocated memory

§Safety

Caller must ensure that the pointer that is being deallocated was allocated by the same Allocator instance.

Source

unsafe fn realloc(&mut self, ptr: *mut u8, new_size: usize) -> *mut u8

Re-allocate previously allocated memory

§Safety

Caller must ensure that the pointer points to an allocation that was allocated by the same Allocator instance.

Source

unsafe fn usable_size(ptr: *mut u8) -> usize
where Self: Sized,

Get usable size of allocated memory region

§Safety

Caller must ensure that the pointer handed to this function points to an allocation allocated by the same allocator instance.

Dyn Compatibility§

This trait is dyn compatible.

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

Implementors§