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    pub buffers: usize,
10    pub buffer_size: usize,
11    /// Nonzero power-of-two alignment of each acquired buffer's first byte.
12    pub alignment: usize,
13}
14
15impl BufferPoolOptions {
16    pub const fn new(buffers: usize, buffer_size: usize) -> Self {
17        Self {
18            buffers,
19            buffer_size,
20            alignment: 1,
21        }
22    }
23
24    pub(crate) fn validate(self) -> io::Result<()> {
25        if self.buffers == 0
26            || self.buffer_size == 0
27            || !self.alignment.is_power_of_two()
28            || self
29                .buffer_size
30                .checked_add(self.alignment - 1)
31                .and_then(|size| size.checked_mul(self.buffers))
32                .is_none_or(|size| size > isize::MAX as usize)
33        {
34            return Err(io::Error::new(
35                io::ErrorKind::InvalidInput,
36                "nonzero capacity and size, power-of-two alignment, and a representable allocation are required",
37            ));
38        }
39        Ok(())
40    }
41}