Module pool_allocator

Module pool_allocator 

Source
Expand description

A threadsafe, lock-free bucket Allocator which partitions the provided memory into buckets of equal size with a given alignment. The memory chunks cannot be resized or greater than the maximum bucket size.

§Example


use iceoryx2_bb_memory::pool_allocator::*;

const BUCKET_SIZE: usize = 128;
const BUCKET_ALIGNMENT: usize = 8;
const MEMORY_SIZE: usize = 1024;
const MAX_NUMBER_OF_BUCKETS: usize = 512;
let mut memory: [u8; MEMORY_SIZE] = [0; MEMORY_SIZE];
let mut allocator = FixedSizePoolAllocator::<MAX_NUMBER_OF_BUCKETS>
                        ::new(unsafe{ Layout::from_size_align_unchecked(BUCKET_SIZE,
                        BUCKET_ALIGNMENT) }, NonNull::new(memory.as_mut_ptr()).unwrap(), MEMORY_SIZE );

let mut memory = allocator.allocate(unsafe{Layout::from_size_align_unchecked(48, 4)})
                          .expect("failed to allocate");

let mut grown_memory = unsafe { allocator.grow_zeroed(
                            NonNull::new(memory.as_mut().as_mut_ptr()).unwrap(),
                            Layout::from_size_align_unchecked(48, 4),
                            Layout::from_size_align_unchecked(64, 4)
                        ).expect("failed to grow memory")};

let mut shrink_memory = unsafe { allocator.shrink(
                            NonNull::new(grown_memory.as_mut().as_mut_ptr()).unwrap(),
                            Layout::from_size_align_unchecked(64, 4),
                            Layout::from_size_align_unchecked(32, 4)
                        ).expect("failed to shrink memory")};

unsafe{ allocator.deallocate(NonNull::new(shrink_memory.as_mut().as_mut_ptr()).unwrap(),
                             Layout::from_size_align_unchecked(32, 4))};

Structs§

FixedSizePoolAllocator
Layout
Layout of a block of memory.
NonNull
*mut T but non-zero and covariant.
PoolAllocator

Enums§

AllocationError
Failures caused by BaseAllocator::allocate() or BaseAllocator::allocate_zeroed().
AllocationGrowError
Failures caused by Allocator::grow() or Allocator::grow_zeroed().
AllocationShrinkError
Failures caused by Allocator::shrink().

Traits§

Allocator
Allocator with grow and shrink features.
BaseAllocator
The most minimalistic requirement for an allocator