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§
Sourcefn reserve(&self, size: usize) -> Box<dyn MemoryReservation>
fn reserve(&self, size: usize) -> Box<dyn MemoryReservation>
Reserves memory from the pool. Infallible.
Returns a reservation of the requested size.