1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
//! Memory allocation utilities for pinned buffers.
//!
//! This module provides specialized buffer allocation functions optimized for
//! Direct Memory Access (DMA) operations and io_uring. The allocators ensure
//! proper memory alignment and zero-initialization for safe kernel interactions.
//!
//! # Key Features
//!
//! - Page-aligned allocation for optimal DMA performance
//! - Zero-initialized memory for security
//! - Custom alignment support for specialized use cases
//! - Fallback strategies for allocation failures
//!
//! # Examples
//!
//! ```
//! use safer_ring::buffer::allocation::{allocate_aligned_buffer, allocate_with_alignment};
//!
//! // Allocate a page-aligned buffer for DMA operations
//! let buffer = allocate_aligned_buffer(8192);
//! assert_eq!(buffer.len(), 8192);
//!
//! // Allocate with custom alignment
//! let aligned_buffer = allocate_with_alignment(1024, 64);
//! assert_eq!(aligned_buffer.len(), 1024);
//! ```
use ;
/// 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);
/// ```
/// Allocates a buffer with specific alignment requirements.
///
/// This function allocates zero-initialized memory with a custom alignment
/// requirement. Unlike `allocate_aligned_buffer`, this function allows you to
/// specify the exact alignment needed, which is useful for specialized hardware
/// requirements or performance optimizations.
///
/// # Parameters
///
/// * `size` - The size in bytes of the buffer to allocate. If 0, returns an empty slice.
/// * `align` - The required alignment in bytes. Must be a power of 2.
///
/// # Returns
///
/// Returns a `Box<[u8]>` containing the allocated zero-initialized buffer
/// aligned to the specified boundary.
///
/// # Panics
///
/// Panics if:
/// - The `align` parameter is not a power of 2
/// - The `size` and `align` combination is invalid
/// - Memory allocation fails (out of memory)
/// - The alignment requirement cannot be satisfied
///
/// # Examples
///
/// ```
/// use safer_ring::buffer::allocation::allocate_with_alignment;
///
/// // Allocate 1KB buffer aligned to 64-byte boundary (for cache line alignment)
/// let buffer = allocate_with_alignment(1024, 64);
/// assert_eq!(buffer.len(), 1024);
/// assert!(buffer.iter().all(|&b| b == 0)); // All zeros
///
/// // Allocate with 16-byte alignment
/// let aligned = allocate_with_alignment(256, 16);
/// assert_eq!(aligned.len(), 256);
///
/// // Empty buffer case
/// let empty = allocate_with_alignment(0, 32);
/// assert_eq!(empty.len(), 0);
/// ```