|
1 |
| -use libc::{iovec, UIO_MAXIOV}; |
2 |
| -use std::cmp; |
3 |
| -use std::mem; |
4 |
| -use std::ptr; |
5 |
| -use std::slice; |
| 1 | +use libc::iovec; |
6 | 2 |
|
7 |
| -// Internal state shared by FixedBufRegistry and FixedBuf handles. |
8 |
| -pub(crate) struct FixedBuffers { |
9 |
| - // Pointer to an allocated array of iovec records referencing |
10 |
| - // the allocated buffers. The number of initialized records is the |
11 |
| - // same as the length of the states array. |
12 |
| - raw_bufs: ptr::NonNull<iovec>, |
13 |
| - // State information on the buffers. Indices in this array correspond to |
14 |
| - // the indices in the array at raw_bufs. |
15 |
| - states: Vec<BufState>, |
16 |
| - // Original capacity of raw_bufs as a Vec. |
17 |
| - orig_cap: usize, |
18 |
| -} |
19 |
| - |
20 |
| -// State information of a buffer in the registry, |
21 |
| -enum BufState { |
22 |
| - // The buffer is not in use. |
23 |
| - // The field records the length of the initialized part. |
24 |
| - Free { init_len: usize }, |
25 |
| - // The buffer is checked out. |
26 |
| - // Its data are logically owned by the FixedBuf handle, |
27 |
| - // which also keeps track of the length of the initialized part. |
28 |
| - CheckedOut, |
29 |
| -} |
30 |
| - |
31 |
| -impl FixedBuffers { |
32 |
| - pub(crate) fn new(bufs: impl Iterator<Item = Vec<u8>>) -> Self { |
33 |
| - let bufs = bufs.take(cmp::min(UIO_MAXIOV as usize, 65_536)); |
34 |
| - let (size_hint, _) = bufs.size_hint(); |
35 |
| - let mut iovecs = Vec::with_capacity(size_hint); |
36 |
| - let mut states = Vec::with_capacity(size_hint); |
37 |
| - for mut buf in bufs { |
38 |
| - iovecs.push(iovec { |
39 |
| - iov_base: buf.as_mut_ptr() as *mut _, |
40 |
| - iov_len: buf.capacity(), |
41 |
| - }); |
42 |
| - states.push(BufState::Free { |
43 |
| - init_len: buf.len(), |
44 |
| - }); |
45 |
| - mem::forget(buf); |
46 |
| - } |
47 |
| - debug_assert_eq!(iovecs.len(), states.len()); |
48 |
| - // Safety: Vec::as_mut_ptr never returns null |
49 |
| - let raw_bufs = unsafe { ptr::NonNull::new_unchecked(iovecs.as_mut_ptr()) }; |
50 |
| - let orig_cap = iovecs.capacity(); |
51 |
| - mem::forget(iovecs); |
52 |
| - FixedBuffers { |
53 |
| - raw_bufs, |
54 |
| - states, |
55 |
| - orig_cap, |
56 |
| - } |
57 |
| - } |
58 |
| - |
59 |
| - // If the indexed buffer is free, changes its state to checked out and |
60 |
| - // returns its data. If the buffer is already checked out, returns None. |
61 |
| - pub(crate) fn check_out(&mut self, index: usize) -> Option<(iovec, usize)> { |
62 |
| - let iovecs_ptr = self.raw_bufs; |
63 |
| - self.states.get_mut(index).and_then(|state| match *state { |
64 |
| - BufState::Free { init_len } => { |
65 |
| - *state = BufState::CheckedOut; |
66 |
| - // Safety: the allocated array under the pointer is valid |
67 |
| - // for the lifetime of self, the index is inside the array |
68 |
| - // as checked by Vec::get_mut above, called on the array of |
69 |
| - // states that has the same length. |
70 |
| - let iovec = unsafe { iovecs_ptr.as_ptr().add(index).read() }; |
71 |
| - Some((iovec, init_len)) |
72 |
| - } |
73 |
| - BufState::CheckedOut => None, |
74 |
| - }) |
75 |
| - } |
76 |
| - |
77 |
| - // Sets the indexed buffer's state to free and records the updated length |
78 |
| - // of its initialized part. The buffer addressed must be in the checked out |
79 |
| - // state, otherwise this function may panic. |
80 |
| - pub(crate) fn check_in(&mut self, index: usize, init_len: usize) { |
81 |
| - let state = self.states.get_mut(index).expect("invalid buffer index"); |
82 |
| - debug_assert!( |
83 |
| - matches!(state, BufState::CheckedOut), |
84 |
| - "the buffer must be checked out" |
85 |
| - ); |
86 |
| - *state = BufState::Free { init_len }; |
87 |
| - } |
88 |
| - |
89 |
| - pub(crate) fn iovecs(&self) -> &[iovec] { |
90 |
| - // Safety: the raw_bufs pointer is valid for the lifetime of self, |
91 |
| - // the slice length is valid by construction. |
92 |
| - unsafe { slice::from_raw_parts(self.raw_bufs.as_ptr(), self.states.len()) } |
93 |
| - } |
94 |
| -} |
| 3 | +// Abstracts management of fixed buffers in a buffer registry. |
| 4 | +pub(crate) trait FixedBuffers { |
| 5 | + // Provides access to the raw buffers as a slice of iovec. |
| 6 | + fn iovecs(&self) -> &[iovec]; |
95 | 7 |
|
96 |
| -impl Drop for FixedBuffers { |
97 |
| - fn drop(&mut self) { |
98 |
| - let iovecs = unsafe { |
99 |
| - Vec::from_raw_parts(self.raw_bufs.as_ptr(), self.states.len(), self.orig_cap) |
100 |
| - }; |
101 |
| - for (i, iovec) in iovecs.iter().enumerate() { |
102 |
| - match self.states[i] { |
103 |
| - BufState::Free { init_len } => { |
104 |
| - let ptr = iovec.iov_base as *mut u8; |
105 |
| - let cap = iovec.iov_len; |
106 |
| - let v = unsafe { Vec::from_raw_parts(ptr, init_len, cap) }; |
107 |
| - mem::drop(v); |
108 |
| - } |
109 |
| - BufState::CheckedOut => unreachable!("all buffers must be checked in"), |
110 |
| - } |
111 |
| - } |
112 |
| - } |
| 8 | + /// Sets the indexed buffer's state to free and records the updated length |
| 9 | + /// of its initialized part. |
| 10 | + /// |
| 11 | + /// # Panics |
| 12 | + /// |
| 13 | + /// The buffer addressed must be in the checked out state, |
| 14 | + /// otherwise this function may panic. |
| 15 | + /// |
| 16 | + /// # Safety |
| 17 | + /// |
| 18 | + /// While the implementation of this method typically does not need to |
| 19 | + /// do anything unsafe, the caller must ensure that the bytes in the buffer |
| 20 | + /// are initialized up to the specified length. |
| 21 | + unsafe fn check_in(&mut self, buf_index: u16, init_len: usize); |
113 | 22 | }
|
0 commit comments