allocate_aligned_buffer

Function allocate_aligned_buffer 

Source
pub fn allocate_aligned_buffer(size: usize) -> Box<[u8]>
Expand description

Allocates a zero-initialized buffer with optimal alignment for DMA operations.

This function attempts to allocate memory with page-aligned (4096 byte) alignment for optimal performance with Direct Memory Access operations. If page alignment fails, it falls back to natural byte alignment. The allocated memory is zero-initialized for security.

§Parameters

  • size - The size in bytes of the buffer to allocate. If 0, returns an empty slice.

§Returns

Returns a Box<[u8]> containing the allocated zero-initialized buffer. The buffer will be page-aligned (4096 bytes) when possible, or naturally aligned as a fallback.

§Panics

Panics if:

  • Memory allocation fails (out of memory)
  • The size parameter is too large for the system to handle

§Examples

use safer_ring::buffer::allocation::allocate_aligned_buffer;

// Allocate an 8KB buffer
let buffer = allocate_aligned_buffer(8192);
assert_eq!(buffer.len(), 8192);
assert!(buffer.iter().all(|&b| b == 0)); // All zeros

// Empty buffer case
let empty = allocate_aligned_buffer(0);
assert_eq!(empty.len(), 0);