Crate memory_pool_allocator

Crate memory_pool_allocator 

Source
Expand description

§memory-pool-allocator

A no_std, thread-safe, array chunk tracking-based fixed-size memory pool allocator.

§Features

  • User-provided memory pool (via *mut u8)
  • Chunked allocation with array-based chunk tracking
  • Optional statistics and zero-on-free features
  • Thread-safe via parking_lot::Mutex

§Default Features

  • zero-on-free: Zeroes memory of each allocation when it is deallocated.
  • zero-on-drop: Zeroes the entire memory pool when the allocator is dropped.
  • statistics: Tracks allocation and deallocation statistics (number of allocated chunks, allocation/deallocation errors).

§Optional Feature

  • debug: Adds assertions of pool consistency for debug builds.

§Type Parameters

  • N: Total pool size in bytes
  • M: Number of chunks to divide the pool into

§Safety

  • The user must provide a pointer to a memory region of at least N bytes, aligned to the maximum alignment required by allocations.
  • The allocator does not manage the lifetime of the memory pool; the user is responsible for ensuring it is valid for the allocator’s lifetime.
  • If the pool is not sufficiently aligned, allocations with higher alignment requirements may fail or result in undefined behavior.

§Example

#[repr(align(64))]
struct Aligned {
    mem: [u8; 1024]
}
let mut aligned = Aligned { mem: [0; 1024] };
let allocator = unsafe { MemoryPoolAllocator::<1024, 64>::new(aligned.mem.as_mut_ptr()) };
let layout = Layout::from_size_align(128, 64).unwrap();
let ptr = allocator.try_allocate(layout).unwrap();
assert_eq!(ptr as usize % 64, 0);
allocator.try_deallocate(ptr).unwrap();

§License

Licensed under either of Apache License, Version 2.0 or MIT license at your option.

Structs§

MemoryPoolAllocator
Array chunk tracking-based fixed-size memory pool allocator
PoolStats
Pool statistics

Enums§

AllocError
Allocation errors