@@ -17,16 +17,15 @@ use super::super::transport::mmio::{ComCfg, NotifCfg, NotifCtrl};
1717#[ cfg( feature = "pci" ) ]
1818use super :: super :: transport:: pci:: { ComCfg , NotifCfg , NotifCtrl } ;
1919use super :: error:: VirtqError ;
20- use super :: {
21- AvailBufferToken , BufferType , MemPool , TransferToken , UsedBufferToken , Virtq , VirtqPrivate ,
22- } ;
20+ use super :: index_alloc:: IndexAlloc ;
21+ use super :: { AvailBufferToken , BufferType , TransferToken , UsedBufferToken , Virtq , VirtqPrivate } ;
2322use crate :: arch:: memory_barrier;
2423use crate :: mm:: device_alloc:: DeviceAlloc ;
2524
2625struct DescrRing {
2726 read_idx : u16 ,
2827 token_ring : Box < [ Option < Box < TransferToken < virtq:: Desc > > > ] > ,
29- mem_pool : MemPool ,
28+ indexes : IndexAlloc ,
3029
3130 descr_table_cell : Box < UnsafeCell < [ MaybeUninit < virtq:: Desc > ] > , DeviceAlloc > ,
3231 avail_ring_cell : Box < UnsafeCell < virtq:: Avail > , DeviceAlloc > ,
@@ -52,8 +51,8 @@ impl DescrRing {
5251 if let Some ( ctrl_desc) = tkn. ctrl_desc . as_ref ( ) {
5352 let descriptor = SplitVq :: indirect_desc ( ctrl_desc. as_ref ( ) ) ;
5453
55- index = self . mem_pool . pool . pop ( ) . ok_or ( VirtqError :: NoDescrAvail ) ?;
56- self . descr_table_mut ( ) [ usize :: from ( index) ] = MaybeUninit :: new ( descriptor) ;
54+ index = self . indexes . allocate ( ) . ok_or ( VirtqError :: NoDescrAvail ) ?;
55+ self . descr_table_mut ( ) [ index] = MaybeUninit :: new ( descriptor) ;
5756 } else {
5857 let mut rev_all_desc_iter = SplitVq :: descriptor_iter ( & tkn. buff_tkn ) ?. rev ( ) ;
5958
@@ -62,25 +61,26 @@ impl DescrRing {
6261 // If the [AvailBufferToken] is empty, we panic
6362 let descriptor = rev_all_desc_iter. next ( ) . unwrap ( ) ;
6463
65- index = self . mem_pool . pool . pop ( ) . ok_or ( VirtqError :: NoDescrAvail ) ?;
66- self . descr_table_mut ( ) [ usize :: from ( index) ] = MaybeUninit :: new ( descriptor) ;
64+ index = self . indexes . allocate ( ) . ok_or ( VirtqError :: NoDescrAvail ) ?;
65+ self . descr_table_mut ( ) [ index] = MaybeUninit :: new ( descriptor) ;
6766 }
6867 for mut descriptor in rev_all_desc_iter {
6968 // We have not updated `index` yet, so it is at this point the index of the previous descriptor that had been written.
70- descriptor. next = le16:: from ( index) ;
69+ descriptor. next = le16:: from_ne ( index. try_into ( ) . unwrap ( ) ) ;
7170
72- index = self . mem_pool . pool . pop ( ) . ok_or ( VirtqError :: NoDescrAvail ) ?;
73- self . descr_table_mut ( ) [ usize :: from ( index) ] = MaybeUninit :: new ( descriptor) ;
71+ index = self . indexes . allocate ( ) . ok_or ( VirtqError :: NoDescrAvail ) ?;
72+ self . descr_table_mut ( ) [ index] = MaybeUninit :: new ( descriptor) ;
7473 }
7574 // At this point, `index` is the index of the last element of the reversed iterator,
7675 // thus the head of the descriptor chain.
7776 }
7877
79- self . token_ring [ usize :: from ( index) ] = Some ( Box :: new ( tkn) ) ;
78+ self . token_ring [ index] = Some ( Box :: new ( tkn) ) ;
8079
8180 let len = self . token_ring . len ( ) ;
8281 let idx = self . avail_ring_mut ( ) . idx . to_ne ( ) ;
83- self . avail_ring_mut ( ) . ring_mut ( true ) [ idx as usize % len] = index. into ( ) ;
82+ self . avail_ring_mut ( ) . ring_mut ( true ) [ idx as usize % len] =
83+ le16:: from_ne ( index. try_into ( ) . unwrap ( ) ) ;
8484
8585 memory_barrier ( ) ;
8686 let next_idx = idx. wrapping_add ( 1 ) ;
@@ -105,7 +105,9 @@ impl DescrRing {
105105 // We return the indices of the now freed ring slots back to `mem_pool.`
106106 let mut id_ret_idx = u16:: try_from ( used_elem. id . to_ne ( ) ) . unwrap ( ) ;
107107 loop {
108- self . mem_pool . ret_id ( id_ret_idx) ;
108+ unsafe {
109+ self . indexes . deallocate ( id_ret_idx. into ( ) ) ;
110+ }
109111 let cur_chain_elem =
110112 unsafe { self . descr_table_mut ( ) [ usize:: from ( id_ret_idx) ] . assume_init ( ) } ;
111113 if cur_chain_elem. flags . contains ( virtq:: DescF :: NEXT ) {
@@ -289,7 +291,7 @@ impl SplitVq {
289291 . take ( size. into ( ) )
290292 . collect :: < Vec < _ > > ( )
291293 . into_boxed_slice ( ) ,
292- mem_pool : MemPool :: new ( size) ,
294+ indexes : IndexAlloc :: new ( size. into ( ) ) ,
293295
294296 descr_table_cell,
295297 avail_ring_cell,
0 commit comments