pub struct MemoryPool { /* private fields */ }Expand description
Memory pool manager that preallocates memory in chunks of DEFAULT_GLOBAL_MB_ALLOCATION bytes
and hands out *mut u8 pointers whose sizes are rounded up to the next power of two.
§Order of Operations
- Round the Layout size up to a power of two (at least MIN_SLOT_SIZE).
- Search the allocated Chunk list for one with an available slot via [Self::find_available_chunk]. Each candidate first defragments its deallocated sections so adjacent freed slots merge back into continuous memory that can be recycled.
- If no Chunk has a slot available, allocate a new Chunk of at least DEFAULT_GLOBAL_MB_ALLOCATION bytes and serve the request from it.
Deallocated pointers are recorded per Chunk as sections of continuous free memory and get recycled by later allocations. Chunk memory is only returned to the system when the MemoryPool itself is dropped.
§Safety
- No calls to drop are invoked on the objects living inside the slots! This pool deals in raw bytes; RAII resources must be managed by the caller.
- Slots are guaranteed to satisfy the alignment requested through the Layout.
§Example
use std::alloc::Layout;
use crate::rumtk_arena::mem::MemoryPool;
let mut pool = MemoryPool::new();
let layout = Layout::from_size_align(100, 16).unwrap();
let ptr = pool.allocate(layout);
assert!(!ptr.is_null());
unsafe { pool.deallocate(ptr, layout) };Implementations§
Source§impl MemoryPool
impl MemoryPool
Sourcepub const fn new() -> MemoryPool
pub const fn new() -> MemoryPool
Creates a new MemoryPool that grows in chunks of DEFAULT_GLOBAL_MB_ALLOCATION bytes.
No memory is requested from the system until the first allocation arrives.
Sourcepub const fn with_chunk_size(chunk_size: usize) -> MemoryPool
pub const fn with_chunk_size(chunk_size: usize) -> MemoryPool
Creates a new MemoryPool that grows in chunks of chunk_size bytes.
Sourcepub fn slot_size(layout: &Layout) -> Option<usize>
pub fn slot_size(layout: &Layout) -> Option<usize>
Rounds the Layout size up to the next power of two, never below MIN_SLOT_SIZE.
Returns None if the requested size cannot be rounded without overflowing.
pub fn chunk_size(&self) -> usize
Sourcepub fn chunk_count(&self) -> usize
pub fn chunk_count(&self) -> usize
Number of chunks currently allocated by the pool.
Sourcepub fn allocate(&mut self, layout: Layout) -> *mut u8
pub fn allocate(&mut self, layout: Layout) -> *mut u8
Obtains a *mut u8 pointer to a slot of at least Layout size rounded up to the next
power of two and aligned to the Layout alignment.
Deallocated sections are defragmented and recycled first; a new Chunk is allocated only when no existing slot can serve the request. Returns a null pointer if the system is out of memory or the rounded size overflows.
Sourcepub unsafe fn deallocate(&mut self, ptr: *mut u8, layout: Layout)
pub unsafe fn deallocate(&mut self, ptr: *mut u8, layout: Layout)
Returns a slot to the pool. The owning Chunk records the slot as a section of continuous free memory that later allocations recycle. No memory is returned to the system.
§Safety
ptr must have been obtained from Self::allocate on this pool with the same Layout,
and must not be used after this call. Unknown pointers are ignored, but double frees
corrupt the bookkeeping and lead to overlapping allocations.