Allocate a new aligned buffer with at least `min_capacity` bytes.
(min_capacity: usize)
| 23 | impl AlignedBuf { |
| 24 | /// Allocate a new aligned buffer with at least `min_capacity` bytes. |
| 25 | pub fn new(min_capacity: usize) -> std::io::Result<Self> { |
| 26 | let capacity = round_up(min_capacity.max(ALIGNMENT), ALIGNMENT); |
| 27 | let layout = std::alloc::Layout::from_size_align(capacity, ALIGNMENT) |
| 28 | .map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidInput, e))?; |
| 29 | |
| 30 | let ptr = unsafe { std::alloc::alloc_zeroed(layout) }; |
| 31 | if ptr.is_null() { |
| 32 | return Err(std::io::Error::new( |
| 33 | std::io::ErrorKind::OutOfMemory, |
| 34 | "aligned buffer allocation failed", |
| 35 | )); |
| 36 | } |
| 37 | |
| 38 | Ok(Self { |
| 39 | ptr, |
| 40 | capacity, |
| 41 | layout, |
| 42 | }) |
| 43 | } |
| 44 | |
| 45 | /// Mutable raw pointer to the buffer start (for io_uring SQE submission). |
| 46 | #[inline] |