Skip to main content

Allocator

Trait Allocator 

Source
pub trait Allocator:
    Send
    + Sync
    + 'static {
    // Required method
    fn allocate(&self, capacity: usize) -> Vec<u8> ;
}
Expand description

Controls how the pool creates raw byte buffers.

§Contract

  • allocate(capacity) must return a Vec<u8> with capacity() >= capacity.
  • The returned Vec must have len() == 0.
  • The Vec must be deallocatable by the standard global allocator.

§Example

use zeropool::{Allocator, ZeroPool};

struct PrefaultAllocator;

impl Allocator for PrefaultAllocator {
    fn allocate(&self, capacity: usize) -> Vec<u8> {
        let mut buf = Vec::with_capacity(capacity);
        buf.resize(capacity, 0); // pre-fault pages
        buf.clear();
        buf
    }
}

let pool = ZeroPool::new().allocator(PrefaultAllocator);

Required Methods§

Source

fn allocate(&self, capacity: usize) -> Vec<u8>

Allocate a buffer with at least capacity bytes.

Trait Implementations§

Source§

impl Debug for dyn Allocator

Source§

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

Formats the value using the given formatter. Read more

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§