|
2 | 2 |
|
3 | 3 | use std::{io::Write, num::Wrapping};
|
4 | 4 |
|
5 |
| -use vm_memory::{bitmap::BitmapSlice, VolatileSlice}; |
| 5 | +use vm_memory::{bitmap::BitmapSlice, Bytes, VolatileSlice}; |
6 | 6 |
|
7 | 7 | use crate::vhu_vsock::{Error, Result};
|
8 | 8 |
|
@@ -55,7 +55,9 @@ impl LocalTxBuf {
|
55 | 55 | // Check if there is more data to be wrapped around
|
56 | 56 | if len < data_buf.len() {
|
57 | 57 | let remain_txbuf = &mut self.buf[..(data_buf.len() - len)];
|
58 |
| - data_buf.copy_to(remain_txbuf); |
| 58 | + data_buf |
| 59 | + .read_slice(remain_txbuf, len) |
| 60 | + .expect("shouldn't faile because remain_txbuf's len is data_buf.len() - len"); |
59 | 61 | }
|
60 | 62 |
|
61 | 63 | // Increment tail by the amount of data that has been added to the buffer
|
@@ -159,18 +161,20 @@ mod tests {
|
159 | 161 | assert_eq!(loc_tx_buf.tail, Wrapping(CONN_TX_BUF_SIZE * 2));
|
160 | 162 |
|
161 | 163 | // only tail wraps at full
|
162 |
| - let mut buf = vec![1; 4]; |
| 164 | + let mut buf = vec![1, 1, 3, 3]; |
163 | 165 | // SAFETY: Safe as the buffer is guaranteed to be valid here.
|
164 | 166 | let data = unsafe { VolatileSlice::new(buf.as_mut_ptr(), buf.len()) };
|
165 |
| - let mut cmp_data = vec![1; 4]; |
166 |
| - cmp_data.append(&mut vec![0; (CONN_TX_BUF_SIZE - 4) as usize]); |
167 |
| - loc_tx_buf.head = Wrapping(4); |
168 |
| - loc_tx_buf.tail = Wrapping(CONN_TX_BUF_SIZE); |
| 167 | + loc_tx_buf.head = Wrapping(2); |
| 168 | + loc_tx_buf.tail = Wrapping(CONN_TX_BUF_SIZE - 2); |
169 | 169 | let res_push = loc_tx_buf.push(&data);
|
170 | 170 | assert!(res_push.is_ok());
|
171 |
| - assert_eq!(loc_tx_buf.head, Wrapping(4)); |
172 |
| - assert_eq!(loc_tx_buf.tail, Wrapping(CONN_TX_BUF_SIZE + 4)); |
173 |
| - assert_eq!(loc_tx_buf.buf, cmp_data); |
| 171 | + assert_eq!(loc_tx_buf.head, Wrapping(2)); |
| 172 | + assert_eq!(loc_tx_buf.tail, Wrapping(CONN_TX_BUF_SIZE + 2)); |
| 173 | + assert_eq!(loc_tx_buf.buf[0..2], buf[2..4]); |
| 174 | + assert_eq!( |
| 175 | + loc_tx_buf.buf[CONN_TX_BUF_SIZE as usize - 2..CONN_TX_BUF_SIZE as usize], |
| 176 | + buf[0..2] |
| 177 | + ); |
174 | 178 | }
|
175 | 179 |
|
176 | 180 | #[test]
|
|
0 commit comments