Browse Source

Return Vec rather than taking mutable slice.

Andrew Walbran 2 years ago
parent
commit
6dc6eeeef3
3 changed files with 14 additions and 28 deletions
  1. 4 7
      src/device/console.rs
  2. 9 15
      src/queue.rs
  3. 1 6
      src/transport/fake.rs

+ 4 - 7
src/device/console.rs

@@ -314,15 +314,12 @@ mod tests {
                 thread::sleep(Duration::from_millis(10));
             }
             println!("Transmit queue was notified.");
-            // Allocate a bigger buffer than we expect to use, so we can check how much was actually
-            // used.
-            let mut data = [0; 2];
-            let length_read = state
+
+            let data = state
                 .lock()
                 .unwrap()
-                .read_from_queue::<QUEUE_SIZE>(QUEUE_TRANSMITQ_PORT_0, &mut data);
-            assert_eq!(length_read, 1);
-            assert_eq!(data, [b'Q', 0]);
+                .read_from_queue::<QUEUE_SIZE>(QUEUE_TRANSMITQ_PORT_0);
+            assert_eq!(data, b"Q");
         });
 
         assert_eq!(console.send(b'Q'), Ok(()));

+ 9 - 15
src/queue.rs

@@ -579,16 +579,15 @@ pub(crate) fn fake_write_to_queue<const QUEUE_SIZE: usize>(
 
 /// Simulates the device reading from a VirtIO queue, for use in tests.
 ///
-/// Data is read into the `data` buffer passed in. Returns the number of bytes actually read.
-///
 /// The fake device always uses descriptors in order.
 #[cfg(test)]
 pub(crate) fn fake_read_from_queue<const QUEUE_SIZE: usize>(
     queue_descriptors: *const Descriptor,
     queue_driver_area: VirtAddr,
     queue_device_area: VirtAddr,
-    data: &mut [u8],
-) -> usize {
+) -> Vec<u8> {
+    use core::slice;
+
     let descriptors = ptr::slice_from_raw_parts(queue_descriptors, QUEUE_SIZE);
     let available_ring = queue_driver_area as *const AvailRing<QUEUE_SIZE>;
     let used_ring = queue_device_area as *mut UsedRing<QUEUE_SIZE>;
@@ -605,21 +604,16 @@ pub(crate) fn fake_read_from_queue<const QUEUE_SIZE: usize>(
         let mut descriptor = &(*descriptors)[head_descriptor_index as usize];
 
         // Loop through all descriptors in the chain, reading data from them.
-        let mut remaining_data = data;
-        let mut total_length_read = 0;
+        let mut input = Vec::new();
         loop {
             // Check the buffer and read from it.
             let flags = descriptor.flags;
             assert!(!flags.contains(DescFlags::WRITE));
             let buffer_length = descriptor.len as usize;
-            let length_to_read = min(remaining_data.len(), buffer_length);
-            ptr::copy(
+            input.extend_from_slice(slice::from_raw_parts(
                 descriptor.addr as *const u8,
-                remaining_data.as_mut_ptr(),
-                length_to_read,
-            );
-            remaining_data = &mut remaining_data[length_to_read..];
-            total_length_read += length_to_read;
+                buffer_length,
+            ));
 
             if let Some(next) = descriptor.next() {
                 descriptor = &(*descriptors)[next as usize];
@@ -630,10 +624,10 @@ pub(crate) fn fake_read_from_queue<const QUEUE_SIZE: usize>(
 
         // Mark the buffer as used.
         (*used_ring).ring[next_slot as usize].id = head_descriptor_index as u32;
-        (*used_ring).ring[next_slot as usize].len = total_length_read as u32;
+        (*used_ring).ring[next_slot as usize].len = input.len() as u32;
         (*used_ring).idx += 1;
 
-        total_length_read
+        input
     }
 }
 

+ 1 - 6
src/transport/fake.rs

@@ -124,18 +124,13 @@ impl State {
     /// Data is read into the `data` buffer passed in. Returns the number of bytes actually read.
     ///
     /// The fake device always uses descriptors in order.
-    pub fn read_from_queue<const QUEUE_SIZE: usize>(
-        &mut self,
-        queue_index: u16,
-        data: &mut [u8],
-    ) -> usize {
+    pub fn read_from_queue<const QUEUE_SIZE: usize>(&mut self, queue_index: u16) -> Vec<u8> {
         let queue = &self.queues[queue_index as usize];
         assert_ne!(queue.descriptors, 0);
         fake_read_from_queue::<QUEUE_SIZE>(
             queue.descriptors as *const Descriptor,
             queue.driver_area,
             queue.device_area,
-            data,
         )
     }
 }