Trait MemoryPool

Source
pub trait MemoryPool:
    Debug
    + Send
    + Sync {
    // Required methods
    fn reserve(&self, size: usize) -> Box<dyn MemoryReservation>;
    fn available(&self) -> isize;
    fn used(&self) -> usize;
    fn capacity(&self) -> usize;
}
Expand description

A pool of memory that can be reserved and released.

This is used to accurately track memory usage when buffers are shared between multiple arrays or other data structures.

For example, assume we have two arrays that share underlying buffer. It’s hard to tell how much memory is used by them because we can’t tell if the buffer is shared or not.

      Array A           Array B    
   ┌────────────┐    ┌────────────┐
   │ slices...  │    │ slices...  │
   │────────────│    │────────────│
   │ Arc<Bytes> │    │ Arc<Bytes> │ (shared buffer)
   └─────▲──────┘    └───────▲────┘
         │                   │     
         │       Bytes       │     
         │  ┌─────────────┐  │     
         │  │   data...   │  │     
         │  │─────────────│  │     
         └──│   Memory    │──┘   (tracked with a memory pool)  
            │ Reservation │        
            └─────────────┘        

With a memory pool, we can count the memory usage by the shared buffer directly.

Required Methods§

Source

fn reserve(&self, size: usize) -> Box<dyn MemoryReservation>

Reserves memory from the pool. Infallible.

Returns a reservation of the requested size.

Source

fn available(&self) -> isize

Returns the current available memory in the pool.

The pool may be overfilled, so this method might return a negative value.

Source

fn used(&self) -> usize

Returns the current used memory from the pool.

Source

fn capacity(&self) -> usize

Returns the maximum memory that can be reserved from the pool.

Implementors§