22//! This implementation performs guest memory tracking by use of a simple sorted
33//! list residing in the guest's user space. Hence no interaction with the host
44//! is required.
5- use alloc:: vec :: Vec ;
5+ use alloc:: collections :: BTreeSet ;
66use core:: cmp:: Ordering ;
77
88use log:: debug;
@@ -44,7 +44,7 @@ impl Ord for Range {
4444 }
4545}
4646
47- type Ranges = Vec < Range > ;
47+ type Ranges = BTreeSet < Range > ;
4848
4949#[ derive( Debug ) ]
5050pub struct GuestTracking {
@@ -66,41 +66,29 @@ impl Tracking for GuestTracking {
6666
6767 let item = Range { start, len } ;
6868
69- let pos = self . ranges . binary_search ( & item) ;
70- match pos {
71- Ok ( pos) => {
72- let conflict = & self . ranges [ pos] ;
73- Err ( GuestTrackingError :: TrackingConflict (
74- conflict. start ,
75- conflict. len ,
76- item. start ,
77- item. len ,
78- ) ) ?;
79- }
80- Err ( pos) => {
81- self . ranges . insert ( pos, item) ;
82- }
69+ if !self . ranges . insert ( item) {
70+ Err ( GuestTrackingError :: TrackingConflict ( start, len) ) ?;
8371 }
8472
8573 Ok ( ( ) )
8674 }
8775
8876 fn untrack ( & mut self , start : GuestAddr ) -> Result < ( ) , Self :: Error > {
8977 debug ! ( "dealloc - start: 0x{:x}" , start) ;
90- let pos = self . ranges . binary_search_by ( |item| item. start . cmp ( & start) ) ;
91- match pos {
92- Ok ( pos) => {
93- self . ranges . remove ( pos) ;
94- Ok ( ( ) )
95- }
96- Err ( _pos) => Err ( GuestTrackingError :: AllocationNotFound ( start) ) ,
78+ let item = Range { start, len : 1 } ;
79+
80+ if !self . ranges . remove ( & item) {
81+ Err ( GuestTrackingError :: AllocationNotFound ( start) ) ?;
9782 }
83+ Ok ( ( ) )
9884 }
9985}
10086
10187impl GuestTracking {
10288 pub fn new ( ) -> Result < Self , GuestTrackingError > {
103- Ok ( GuestTracking { ranges : Vec :: new ( ) } )
89+ Ok ( GuestTracking {
90+ ranges : BTreeSet :: new ( ) ,
91+ } )
10492 }
10593
10694 pub fn is_out_of_bounds ( addr : GuestAddr , len : usize ) -> bool {
@@ -118,8 +106,8 @@ pub enum GuestTrackingError {
118106 AddressRangeOverflow ( GuestAddr , usize ) ,
119107 #[ error( "Allocation not found: {0:x}" ) ]
120108 AllocationNotFound ( GuestAddr ) ,
121- #[ error( "Tracking conflict" ) ]
122- TrackingConflict ( GuestAddr , usize , GuestAddr , usize ) ,
109+ #[ error( "Tracking conflict: {0:x}, len: {1:x} " ) ]
110+ TrackingConflict ( GuestAddr , usize ) ,
123111 #[ error( "Zero Length" ) ]
124112 ZeroLength ( GuestAddr ) ,
125113}
0 commit comments