Skip to main content

ByteAllocator

Trait ByteAllocator 

Source
pub trait ByteAllocator {
    // Required methods
    fn alloc(&mut self, layout: Layout) -> AllocResult<NonNull<u8>>;
    fn dealloc(&mut self, pos: NonNull<u8>, layout: Layout);
    fn total_bytes(&self) -> usize;
    fn used_bytes(&self) -> usize;
    fn available_bytes(&self) -> usize;
}
Expand description

Byte-granularity allocator for arbitrary-size allocations.

Provides methods for allocating and deallocating memory with byte-level granularity.

Required Methods§

Source

fn alloc(&mut self, layout: Layout) -> AllocResult<NonNull<u8>>

Allocate memory with the given size (in bytes) and alignment.

§Arguments
  • layout - Memory layout specifying size and alignment requirements
§Returns

Returns a pointer to the allocated memory on success, or an error if allocation fails.

§Examples
let mut alloc = MyAllocator;
let layout = Layout::from_size_align(64, 8).unwrap();
let ptr = alloc.alloc(layout)?;
Source

fn dealloc(&mut self, pos: NonNull<u8>, layout: Layout)

Deallocate memory at the given position, size, and alignment.

§Arguments
  • pos - Pointer to the memory to deallocate
  • layout - Memory layout specifying size and alignment requirements
§Safety

The pointer must have been previously allocated from this allocator with the same layout.

Source

fn total_bytes(&self) -> usize

Returns total memory size in bytes managed by this allocator.

Source

fn used_bytes(&self) -> usize

Returns allocated memory size in bytes.

Source

fn available_bytes(&self) -> usize

Returns available memory size in bytes.

Implementors§

Source§

impl<const PAGE_SIZE: usize> ByteAllocator for SlabByteAllocator<PAGE_SIZE>