@@ -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 /// Descriptor Tables
3231 ///
@@ -58,8 +57,8 @@ impl DescrRing {
5857 if let Some ( ctrl_desc) = tkn. ctrl_desc . as_ref ( ) {
5958 let descriptor = SplitVq :: indirect_desc ( ctrl_desc. as_ref ( ) ) ;
6059
61- index = self . mem_pool . pool . pop ( ) . ok_or ( VirtqError :: NoDescrAvail ) ?;
62- self . descr_table_mut ( ) [ usize :: from ( index) ] = MaybeUninit :: new ( descriptor) ;
60+ index = self . indexes . allocate ( ) . ok_or ( VirtqError :: NoDescrAvail ) ?;
61+ self . descr_table_mut ( ) [ index] = MaybeUninit :: new ( descriptor) ;
6362 } else {
6463 let mut rev_all_desc_iter = SplitVq :: descriptor_iter ( & tkn. buff_tkn ) ?. rev ( ) ;
6564
@@ -68,25 +67,26 @@ impl DescrRing {
6867 // If the [AvailBufferToken] is empty, we panic
6968 let descriptor = rev_all_desc_iter. next ( ) . unwrap ( ) ;
7069
71- index = self . mem_pool . pool . pop ( ) . ok_or ( VirtqError :: NoDescrAvail ) ?;
72- self . descr_table_mut ( ) [ usize :: from ( index) ] = MaybeUninit :: new ( descriptor) ;
70+ index = self . indexes . allocate ( ) . ok_or ( VirtqError :: NoDescrAvail ) ?;
71+ self . descr_table_mut ( ) [ index] = MaybeUninit :: new ( descriptor) ;
7372 }
7473 for mut descriptor in rev_all_desc_iter {
7574 // We have not updated `index` yet, so it is at this point the index of the previous descriptor that had been written.
76- descriptor. next = le16:: from ( index) ;
75+ descriptor. next = le16:: from_ne ( index. try_into ( ) . unwrap ( ) ) ;
7776
78- index = self . mem_pool . pool . pop ( ) . ok_or ( VirtqError :: NoDescrAvail ) ?;
79- self . descr_table_mut ( ) [ usize :: from ( index) ] = MaybeUninit :: new ( descriptor) ;
77+ index = self . indexes . allocate ( ) . ok_or ( VirtqError :: NoDescrAvail ) ?;
78+ self . descr_table_mut ( ) [ index] = MaybeUninit :: new ( descriptor) ;
8079 }
8180 // At this point, `index` is the index of the last element of the reversed iterator,
8281 // thus the head of the descriptor chain.
8382 }
8483
85- self . token_ring [ usize :: from ( index) ] = Some ( Box :: new ( tkn) ) ;
84+ self . token_ring [ index] = Some ( Box :: new ( tkn) ) ;
8685
8786 let len = self . token_ring . len ( ) ;
8887 let idx = self . avail_ring_mut ( ) . idx . to_ne ( ) ;
89- self . avail_ring_mut ( ) . ring_mut ( true ) [ idx as usize % len] = index. into ( ) ;
88+ self . avail_ring_mut ( ) . ring_mut ( true ) [ idx as usize % len] =
89+ le16:: from_ne ( index. try_into ( ) . unwrap ( ) ) ;
9090
9191 memory_barrier ( ) ;
9292 let next_idx = idx. wrapping_add ( 1 ) ;
@@ -111,7 +111,9 @@ 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 ( id_ret_idx) ;
114+ unsafe {
115+ self . indexes . deallocate ( id_ret_idx. into ( ) ) ;
116+ }
115117 let cur_chain_elem =
116118 unsafe { self . descr_table_mut ( ) [ usize:: from ( id_ret_idx) ] . assume_init ( ) } ;
117119 if cur_chain_elem. flags . contains ( virtq:: DescF :: NEXT ) {
@@ -295,7 +297,7 @@ impl SplitVq {
295297 . take ( size. into ( ) )
296298 . collect :: < Vec < _ > > ( )
297299 . into_boxed_slice ( ) ,
298- mem_pool : MemPool :: new ( size) ,
300+ indexes : IndexAlloc :: new ( size. into ( ) ) ,
299301
300302 descr_table_cell,
301303 avail_ring_cell,
0 commit comments