Skip to main content

base_scheduler/
buffer_pool_options.rs

1// Released under the MIT License.
2// Copyright, 2026, by Samuel Williams.
3
4use std::io;
5
6/// Usable payload capacity and alignment, independent of kernel registration strategy. Backends must satisfy these requirements or return an error; backend metadata and allocation padding may require additional memory.
7#[derive(Clone, Copy, Debug, PartialEq, Eq)]
8pub struct BufferPoolOptions {
9    /// The number of buffers, which is the pool's exact shared capacity. Must be nonzero.
10    pub buffers: usize,
11    /// The usable payload length, in bytes, of each buffer. Must be nonzero.
12    pub buffer_size: usize,
13    /// Nonzero power-of-two alignment of each acquired buffer's first byte.
14    pub alignment: usize,
15}
16
17impl BufferPoolOptions {
18    /// Request `buffers` buffers of `buffer_size` bytes each, with an `alignment` of 1.
19    pub const fn new(buffers: usize, buffer_size: usize) -> Self {
20        Self {
21            buffers,
22            buffer_size,
23            alignment: 1,
24        }
25    }
26
27    pub(crate) fn validate(self) -> io::Result<()> {
28        if self.buffers == 0
29            || self.buffer_size == 0
30            || !self.alignment.is_power_of_two()
31            || self
32                .buffer_size
33                .checked_add(self.alignment - 1)
34                .and_then(|size| size.checked_mul(self.buffers))
35                .is_none_or(|size| size > isize::MAX as usize)
36        {
37            return Err(io::Error::new(
38                io::ErrorKind::InvalidInput,
39                "nonzero capacity and size, power-of-two alignment, and a representable allocation are required",
40            ));
41        }
42        Ok(())
43    }
44}