pub struct PagedPool { /* private fields */ }Expand description
A pool of reusable fixed-size buffers allocated in large pages.
This type implements MemoryPool and can be configured as a custom memory pool for a S3CrtClient. It can also return mutable buffers (PoolBufferMut) to use in other scenarios, such as reading cache blocks from disk or buffering incremental uploads.
The pool is configured by providing a set of sizes for the buffers, e.g. read and write part sizes, cache block size. For each of these sizes, the pool will maintain a [SizePool] instance, which will allocate “pages” of BUFFERS_PER_PAGE (16) buffers on demand.
When a buffer of a given size is requested, the pool will return a buffer from one of 2 logical areas: primary and secondary:
- primary: the [SizePool] with the smallest buffer size large enough to contain the requested size is selected. If any of its existing pages has a free buffer, it is marked as reserved and returned, otherwise a new page is allocated.
- secondary: if the requested size is larger than the maximum buffer size in [SizePool]s, a buffer of the exact size is allocated and returned.
When a primary buffer is dropped, it will automatically be released back to the pool, so it can be reused. Secondary buffers will also notify the pool on drop, but only for tracking.
The trim method can be invoked to free all empty pages, i.e. with no
currently reserved buffers, across [SizePool]s.
When reserving a buffer, a BufferKind parameter is required to keep track of the usage. Requests through the MemoryPool interface will map the originating MetaRequestType to BufferKind.
Implementations§
Source§impl PagedPool
impl PagedPool
Sourcepub const MAX_BUFFER_SIZE: usize
pub const MAX_BUFFER_SIZE: usize
Maximum size for a primary buffer.
Buffers larger than this size will be allocated from secondary memory.
Sourcepub const DEFAULT_BUFFER_SIZE: usize
pub const DEFAULT_BUFFER_SIZE: usize
Default size for a primary buffer.
Used when no other valid buffer size is provided when creating the pool.
Sourcepub fn new_with_candidate_sizes(buffer_sizes: impl Into<Vec<usize>>) -> Self
pub fn new_with_candidate_sizes(buffer_sizes: impl Into<Vec<usize>>) -> Self
Create a new pool, configuring primary memory with the given set of buffer sizes, if valid.
Ignores invalid (0 or greater than MAX_BUFFER_SIZE) or duplicate values for buffer sizes. If no valid value is provided, uses DEFAULT_BUFFER_SIZE.
Sourcepub fn schedule_trim(&self, recurring_time: Duration)
pub fn schedule_trim(&self, recurring_time: Duration)
Schedule recurring calls to trim.
Sourcepub fn reserved_bytes(&self, kind: BufferKind) -> usize
pub fn reserved_bytes(&self, kind: BufferKind) -> usize
Return the reserved memory in bytes for the given kind of buffer.
Sourcepub fn get_buffer_mut(&self, capacity: usize, kind: BufferKind) -> PoolBufferMut
pub fn get_buffer_mut(&self, capacity: usize, kind: BufferKind) -> PoolBufferMut
Get a new empty mutable buffer from the pool with the requested capacity.