Skip to content

Commit 867da85

Browse files
committed
refactor(virtqueue): remove MemDescrId newtype
1 parent 7601d5d commit 867da85

File tree

3 files changed

+13
-18
lines changed

3 files changed

+13
-18
lines changed

src/drivers/virtio/virtqueue/mod.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -512,30 +512,26 @@ pub enum BufferType {
512512
Indirect,
513513
}
514514

515-
/// A newtype for descriptor ids, for better readability.
516-
#[derive(Clone, Copy)]
517-
struct MemDescrId(pub u16);
518-
519515
/// MemPool allows to easily control, request and provide memory for Virtqueues.
520516
///
521517
/// The struct is initialized with a limit of free running "tracked"
522518
/// memory descriptor ids. As Virtqueus do only allow a limited amount of descriptors in their queue,
523519
/// the independent queues, can control the number of descriptors by this.
524520
struct MemPool {
525-
pool: Vec<MemDescrId>,
521+
pool: Vec<u16>,
526522
limit: u16,
527523
}
528524

529525
impl MemPool {
530526
/// Returns a given id to the id pool
531-
fn ret_id(&mut self, id: MemDescrId) {
527+
fn ret_id(&mut self, id: u16) {
532528
self.pool.push(id);
533529
}
534530

535531
/// Returns a new instance, with a pool of the specified size.
536532
fn new(size: u16) -> MemPool {
537533
MemPool {
538-
pool: (0..size).map(MemDescrId).collect(),
534+
pool: (0..size).collect(),
539535
limit: size,
540536
}
541537
}

src/drivers/virtio/virtqueue/packed.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ use super::super::transport::mmio::{ComCfg, NotifCfg, NotifCtrl};
2323
use super::super::transport::pci::{ComCfg, NotifCfg, NotifCtrl};
2424
use super::error::VirtqError;
2525
use super::{
26-
AvailBufferToken, BufferType, MemDescrId, MemPool, TransferToken, UsedBufferToken, Virtq,
27-
VirtqPrivate,
26+
AvailBufferToken, BufferType, MemPool, TransferToken, UsedBufferToken, Virtq, VirtqPrivate,
2827
};
2928
use crate::arch::mm::paging::{BasePageSize, PageSize};
3029
use crate::mm::device_alloc::DeviceAlloc;
@@ -214,11 +213,11 @@ impl DescriptorRing {
214213
&mut self,
215214
raw_tkn: TransferToken<pvirtq::Desc>,
216215
start: u16,
217-
buff_id: MemDescrId,
216+
buff_id: u16,
218217
first_flags: DescF,
219218
) {
220219
// provide reference, in order to let TransferToken know upon finish.
221-
self.tkn_ref_ring[usize::from(buff_id.0)] = Some(raw_tkn);
220+
self.tkn_ref_ring[usize::from(buff_id)] = Some(raw_tkn);
222221
// The driver performs a suitable memory barrier to ensure the device sees the updated descriptor table and available ring before the next step.
223222
// See Virtio specification v1.1. - 2.7.21
224223
fence(Ordering::SeqCst);
@@ -304,7 +303,7 @@ impl ReadCtrl<'_> {
304303
for _ in 0..tkn.num_consuming_descr() {
305304
self.incrmt();
306305
}
307-
self.desc_ring.mem_pool.ret_id(MemDescrId(buff_id));
306+
self.desc_ring.mem_pool.ret_id(buff_id);
308307

309308
Some((tkn, write_len))
310309
} else {
@@ -341,7 +340,7 @@ struct WriteCtrl<'a> {
341340
/// The [pvirtq::Desc::flags] value for the first descriptor, the write of which is deferred.
342341
first_flags: DescF,
343342
/// Buff ID of this write
344-
buff_id: MemDescrId,
343+
buff_id: u16,
345344

346345
desc_ring: &'a mut DescriptorRing,
347346
}
@@ -371,7 +370,7 @@ impl WriteCtrl<'_> {
371370

372371
/// Completes the descriptor flags and id, and writes into the queue at the correct position.
373372
fn write_desc(&mut self, mut incomplete_desc: pvirtq::Desc) {
374-
incomplete_desc.id = self.buff_id.0.into();
373+
incomplete_desc.id = self.buff_id.into();
375374
if self.start == self.position {
376375
// We save what the flags value for the first descriptor will be to be able
377376
// to write it later when all the other descriptors are written (so that

src/drivers/virtio/virtqueue/split.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ impl DescrRing {
5858
if let Some(ctrl_desc) = tkn.ctrl_desc.as_ref() {
5959
let descriptor = SplitVq::indirect_desc(ctrl_desc.as_ref());
6060

61-
index = self.mem_pool.pool.pop().ok_or(VirtqError::NoDescrAvail)?.0;
61+
index = self.mem_pool.pool.pop().ok_or(VirtqError::NoDescrAvail)?;
6262
self.descr_table_mut()[usize::from(index)] = MaybeUninit::new(descriptor);
6363
} else {
6464
let mut rev_all_desc_iter = SplitVq::descriptor_iter(&tkn.buff_tkn)?.rev();
@@ -68,14 +68,14 @@ impl DescrRing {
6868
// If the [AvailBufferToken] is empty, we panic
6969
let descriptor = rev_all_desc_iter.next().unwrap();
7070

71-
index = self.mem_pool.pool.pop().ok_or(VirtqError::NoDescrAvail)?.0;
71+
index = self.mem_pool.pool.pop().ok_or(VirtqError::NoDescrAvail)?;
7272
self.descr_table_mut()[usize::from(index)] = MaybeUninit::new(descriptor);
7373
}
7474
for mut descriptor in rev_all_desc_iter {
7575
// We have not updated `index` yet, so it is at this point the index of the previous descriptor that had been written.
7676
descriptor.next = le16::from(index);
7777

78-
index = self.mem_pool.pool.pop().ok_or(VirtqError::NoDescrAvail)?.0;
78+
index = self.mem_pool.pool.pop().ok_or(VirtqError::NoDescrAvail)?;
7979
self.descr_table_mut()[usize::from(index)] = MaybeUninit::new(descriptor);
8080
}
8181
// At this point, `index` is the index of the last element of the reversed iterator,
@@ -111,7 +111,7 @@ impl DescrRing {
111111
// We return the indices of the now freed ring slots back to `mem_pool.`
112112
let mut id_ret_idx = u16::try_from(used_elem.id.to_ne()).unwrap();
113113
loop {
114-
self.mem_pool.ret_id(super::MemDescrId(id_ret_idx));
114+
self.mem_pool.ret_id(id_ret_idx);
115115
let cur_chain_elem =
116116
unsafe { self.descr_table_mut()[usize::from(id_ret_idx)].assume_init() };
117117
if cur_chain_elem.flags.contains(virtq::DescF::NEXT) {

0 commit comments

Comments
 (0)