Skip to main content

MemoryPool

Struct MemoryPool 

Source
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

  1. Round the Layout size up to a power of two (at least MIN_SLOT_SIZE).
  2. 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.
  3. 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

Source

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.

Source

pub const fn with_chunk_size(chunk_size: usize) -> MemoryPool

Creates a new MemoryPool that grows in chunks of chunk_size bytes.

Source

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.

Source

pub fn chunk_size(&self) -> usize

Source

pub fn chunk_count(&self) -> usize

Number of chunks currently allocated by the pool.

Source

pub fn allocate_on_available( &mut self, size: usize, align: usize, ) -> Option<*mut u8>

Searches the allocated chunks for one with available space for the requested Layout.

Every visited Chunk is defragmented first so adjacent deallocated sections merge back into continuous memory before its availability is judged.

Source

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.

Source

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.

Trait Implementations§

Source§

impl Debug for MemoryPool

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl Default for MemoryPool

Source§

fn default() -> MemoryPool

Returns the “default value” for a type. Read more
Source§

impl Send for MemoryPool

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> Ungil for T
where T: Send,

Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V