-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathbytes.rs
More file actions
384 lines (324 loc) · 9.05 KB
/
bytes.rs
File metadata and controls
384 lines (324 loc) · 9.05 KB
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
// Copyright 2025 foyer Project Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use std::{
any::Any,
fmt::Debug,
ops::{Deref, DerefMut, RangeBounds},
ptr::NonNull,
slice::{from_raw_parts, from_raw_parts_mut},
sync::Arc,
};
use allocator_api2::alloc::{handle_alloc_error, Allocator, Global, Layout};
use foyer_common::bits;
use super::PAGE;
pub trait IoB: Deref<Target = [u8]> + Send + Sync + 'static + Debug + Any {
fn as_raw_parts(&self) -> (*mut u8, usize);
// TODO(MrCroxx): Remove this after bump MSRV to 1.86.0+
// https://blog.rust-lang.org/2025/04/03/Rust-1.86.0/
fn as_any(&self) -> &dyn Any;
// TODO(MrCroxx): Remove this after bump MSRV to 1.86.0+
// https://blog.rust-lang.org/2025/04/03/Rust-1.86.0/
fn into_any(self: Box<Self>) -> Box<dyn Any>;
}
/// 4K-aligned immutable buf for direct I/O.
pub trait IoBuf: Deref<Target = [u8]> + IoB {
// TODO(MrCroxx): Remove this after bump MSRV to 1.86.0+
// https://blog.rust-lang.org/2025/04/03/Rust-1.86.0/
fn into_iob(self: Box<Self>) -> Box<dyn IoB>;
}
/// 4K-aligned mutable buf for direct I/O.
pub trait IoBufMut: DerefMut<Target = [u8]> + IoB {
// TODO(MrCroxx): Remove this after bump MSRV to 1.86.0+
// https://blog.rust-lang.org/2025/04/03/Rust-1.86.0/
fn into_iob(self: Box<Self>) -> Box<dyn IoB>;
}
/// 4K-aligned raw bytes.
#[derive(Debug)]
pub struct Raw {
ptr: *mut u8,
cap: usize,
}
unsafe impl Send for Raw {}
unsafe impl Sync for Raw {}
impl Raw {
/// Allocate an 4K-aligned [`Raw`] with **AT LEAST** `capacity` bytes.
///
/// # Safety
///
/// The returned buffer contains uninitialized memory. Callers must initialize
/// the memory before reading from it to avoid undefined behavior.
pub fn new(capacity: usize) -> Self {
let capacity = bits::align_up(PAGE, capacity);
let layout = unsafe { Layout::from_size_align_unchecked(capacity, PAGE) };
let mut nonnull = match Global.allocate(layout) {
Ok(nonnull) => nonnull,
Err(_) => handle_alloc_error(layout),
};
let slice = unsafe { nonnull.as_mut() };
let ptr = slice.as_mut_ptr();
let cap = slice.len();
Self { ptr, cap }
}
/// Consume [`Raw`] and get the raw pointer and the capacity.
///
/// # Safety
///
/// [`Raw::from_raw_parts`] must be called later. Otherwise the buffer memory will leak.
pub fn into_raw_parts(self) -> (*mut u8, usize) {
let res = (self.ptr, self.cap);
std::mem::forget(self);
res
}
/// Construct [`Raw`] with the raw pointer and the capacity.
///
/// # Safety
///
/// The `ptr` and `cap` must be returned by [`Raw::into_raw_parts`].
pub unsafe fn from_raw_parts(ptr: *mut u8, cap: usize) -> Self {
Self { ptr, cap }
}
}
impl Clone for Raw {
fn clone(&self) -> Self {
let mut buf = Raw::new(self.cap);
assert_eq!(buf.cap, self.cap);
buf.copy_from_slice(self);
buf
}
}
impl Drop for Raw {
fn drop(&mut self) {
let layout = unsafe { Layout::from_size_align_unchecked(self.cap, PAGE) };
unsafe { Global.deallocate(NonNull::new_unchecked(self.ptr), layout) };
}
}
impl Deref for Raw {
type Target = [u8];
fn deref(&self) -> &Self::Target {
unsafe { from_raw_parts(self.ptr, self.cap) }
}
}
impl DerefMut for Raw {
fn deref_mut(&mut self) -> &mut Self::Target {
unsafe { from_raw_parts_mut(self.ptr, self.cap) }
}
}
impl AsRef<[u8]> for Raw {
fn as_ref(&self) -> &[u8] {
self
}
}
impl AsMut<[u8]> for Raw {
fn as_mut(&mut self) -> &mut [u8] {
&mut *self
}
}
impl PartialEq for Raw {
fn eq(&self, other: &Self) -> bool {
self.as_ref() == other.as_ref()
}
}
impl Eq for Raw {}
impl IoB for Raw {
fn as_raw_parts(&self) -> (*mut u8, usize) {
(self.ptr, self.cap)
}
fn as_any(&self) -> &dyn Any {
self
}
fn into_any(self: Box<Self>) -> Box<dyn Any> {
self
}
}
impl IoBuf for Raw {
fn into_iob(self: Box<Self>) -> Box<dyn IoB> {
self
}
}
impl IoBufMut for Raw {
fn into_iob(self: Box<Self>) -> Box<dyn IoB> {
self
}
}
/// A 4K-aligned slice on the io buffer that can be shared.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct IoSlice {
raw: Arc<Raw>,
start: usize,
end: usize,
}
impl From<Raw> for IoSlice {
fn from(value: Raw) -> Self {
let len = value.len();
Self {
raw: Arc::new(value),
start: 0,
end: len,
}
}
}
impl From<IoSliceMut> for IoSlice {
fn from(value: IoSliceMut) -> Self {
let len = value.len();
Self {
raw: Arc::new(value.raw),
start: 0,
end: len,
}
}
}
impl Deref for IoSlice {
type Target = [u8];
fn deref(&self) -> &Self::Target {
&self.raw[self.start..self.end]
}
}
impl AsRef<[u8]> for IoSlice {
fn as_ref(&self) -> &[u8] {
self
}
}
impl IoB for IoSlice {
fn as_raw_parts(&self) -> (*mut u8, usize) {
let ptr = unsafe { self.raw.ptr.add(self.start) };
let len = self.end - self.start;
(ptr, len)
}
fn as_any(&self) -> &dyn Any {
self
}
fn into_any(self: Box<Self>) -> Box<dyn Any> {
self
}
}
impl IoBuf for IoSlice {
fn into_iob(self: Box<Self>) -> Box<dyn IoB> {
self
}
}
impl IoSlice {
pub fn slice(&self, range: impl RangeBounds<usize>) -> Self {
let s = match range.start_bound() {
std::ops::Bound::Included(i) => self.start + *i,
std::ops::Bound::Excluded(_) => unreachable!(),
std::ops::Bound::Unbounded => self.start,
};
let e = match range.end_bound() {
std::ops::Bound::Included(i) => self.start + *i + 1,
std::ops::Bound::Excluded(i) => self.start + *i,
std::ops::Bound::Unbounded => self.end,
};
if s > e {
panic!("slice index starts at {s} but ends at {e}");
}
bits::assert_aligned(PAGE, s);
bits::assert_aligned(PAGE, e);
Self {
raw: self.raw.clone(),
start: s,
end: e,
}
}
/// Convert into [`IoSliceMut`], if the [`IoSlice`] has exactly one reference.
pub fn try_into_io_slice_mut(self) -> Option<IoSliceMut> {
let raw = Arc::into_inner(self.raw)?;
Some(IoSliceMut { raw })
}
}
/// A 4K-aligned mutable slice.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct IoSliceMut {
raw: Raw,
}
impl IoSliceMut {
pub fn new(capacity: usize) -> Self {
let raw = Raw::new(capacity);
Self { raw }
}
pub fn len(&self) -> usize {
self.raw.cap
}
}
impl Deref for IoSliceMut {
type Target = [u8];
fn deref(&self) -> &Self::Target {
&self.raw
}
}
impl DerefMut for IoSliceMut {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.raw
}
}
impl AsRef<[u8]> for IoSliceMut {
fn as_ref(&self) -> &[u8] {
self
}
}
impl AsMut<[u8]> for IoSliceMut {
fn as_mut(&mut self) -> &mut [u8] {
&mut self.raw
}
}
impl IoB for IoSliceMut {
fn as_raw_parts(&self) -> (*mut u8, usize) {
(self.raw.ptr, self.raw.cap)
}
fn as_any(&self) -> &dyn Any {
self
}
fn into_any(self: Box<Self>) -> Box<dyn Any> {
self
}
}
impl IoBuf for IoSliceMut {
fn into_iob(self: Box<Self>) -> Box<dyn IoB> {
self
}
}
impl IoBufMut for IoSliceMut {
fn into_iob(self: Box<Self>) -> Box<dyn IoB> {
self
}
}
impl IoSliceMut {
pub fn into_io_slice(self) -> IoSlice {
let start = 0;
let end = self.raw.cap;
let raw = Arc::new(self.raw);
IoSlice { raw, start, end }
}
}
impl dyn IoB {
/// Convert into concrete type [`Box<IoSlice>`] if the underlying type fits.
pub fn try_into_io_slice(self: Box<Self>) -> Option<Box<IoSlice>> {
let any: Box<dyn Any> = self.into_any();
any.downcast::<IoSlice>().ok()
}
/// Convert into concrete type [`Box<IoSliceMut>`] if the underlying type fits.
pub fn try_into_io_slice_mut(self: Box<Self>) -> Option<Box<IoSliceMut>> {
let any: Box<dyn Any> = self.into_any();
any.downcast::<IoSliceMut>().ok()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_dyn() {
let raw = Raw::new(4096);
let _: Box<dyn IoBuf> = Box::new(raw.clone());
let _: Box<dyn IoBufMut> = Box::new(raw.clone());
}
}