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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
use std::ptr;
use crate::sys::uring::UringBuffer;
use alloc::alloc::Layout;
#[derive(Debug)]
pub(crate) struct SysAlloc {
data: ptr::NonNull<u8>,
layout: Layout,
}
impl SysAlloc {
fn new(size: usize) -> Option<Self> {
if size == 0 {
None
} else {
let layout = Layout::from_size_align(size, 4096).unwrap();
let data = unsafe { alloc::alloc::alloc(layout) as *mut u8 };
let data = ptr::NonNull::new(data)?;
Some(SysAlloc { data, layout })
}
}
fn as_ptr(&self) -> *const u8 {
self.data.as_ptr()
}
fn as_mut_ptr(&mut self) -> *mut u8 {
self.data.as_ptr()
}
}
impl Drop for SysAlloc {
fn drop(&mut self) {
unsafe {
alloc::alloc::dealloc(self.data.as_ptr(), self.layout);
}
}
}
#[derive(Debug)]
pub(crate) enum BufferStorage {
Sys(SysAlloc),
Uring(UringBuffer),
}
impl BufferStorage {
fn as_ptr(&self) -> *const u8 {
match self {
BufferStorage::Sys(x) => x.as_ptr(),
BufferStorage::Uring(x) => x.as_ptr(),
}
}
fn as_mut_ptr(&mut self) -> *mut u8 {
match self {
BufferStorage::Sys(x) => x.as_mut_ptr(),
BufferStorage::Uring(x) => x.as_mut_ptr(),
}
}
}
#[derive(Debug)]
pub struct DmaBuffer {
storage: BufferStorage,
trim: usize,
size: usize,
}
impl DmaBuffer {
pub(crate) fn new(size: usize) -> Option<DmaBuffer> {
Some(DmaBuffer {
storage: BufferStorage::Sys(SysAlloc::new(size)?),
size,
trim: 0,
})
}
pub(crate) fn with_storage(size: usize, storage: BufferStorage) -> DmaBuffer {
DmaBuffer {
storage,
size,
trim: 0,
}
}
pub(crate) fn uring_buffer_id(&self) -> Option<u32> {
match &self.storage {
BufferStorage::Uring(x) => x.uring_buffer_id(),
_ => None,
}
}
pub(crate) fn trim_to_size(&mut self, newsize: usize) {
assert!(newsize <= self.size);
self.size = newsize;
}
pub(crate) fn trim_front(&mut self, trim: usize) {
assert!(trim <= self.size);
self.trim += trim;
self.size -= trim;
}
pub fn as_bytes(&self) -> &[u8] {
unsafe { std::slice::from_raw_parts(self.as_ptr(), self.size) }
}
pub fn as_bytes_mut(&mut self) -> &mut [u8] {
unsafe { std::slice::from_raw_parts_mut(self.as_mut_ptr(), self.size) }
}
pub fn len(&self) -> usize {
self.size
}
pub fn is_empty(&self) -> bool {
self.size == 0
}
pub fn as_mut_ptr(&mut self) -> *mut u8 {
unsafe { self.storage.as_mut_ptr().add(self.trim) }
}
pub fn as_ptr(&self) -> *const u8 {
unsafe { self.storage.as_ptr().add(self.trim) }
}
}
impl AsRef<[u8]> for DmaBuffer {
#[inline(always)]
fn as_ref(&self) -> &[u8] {
self.as_bytes()
}
}
impl AsMut<[u8]> for DmaBuffer {
#[inline(always)]
fn as_mut(&mut self) -> &mut [u8] {
self.as_bytes_mut()
}
}