1
- use std:: collections:: { btree_map , VecDeque } ;
1
+ use std:: collections:: VecDeque ;
2
2
use std:: ptr;
3
3
4
4
use rustc:: hir:: def_id:: DefId ;
@@ -519,7 +519,7 @@ impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'a, 'mir, 'tcx, M> {
519
519
520
520
fn get_bytes ( & self , ptr : MemoryPointer , size : Size , align : Align ) -> EvalResult < ' tcx , & [ u8 ] > {
521
521
assert_ne ! ( size. bytes( ) , 0 ) ;
522
- if self . relocations ( ptr, size) ?. count ( ) != 0 {
522
+ if self . relocations ( ptr, size) ?. len ( ) != 0 {
523
523
return err ! ( ReadPointerAsBytes ) ;
524
524
}
525
525
self . check_defined ( ptr, size) ?;
@@ -614,9 +614,9 @@ impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'a, 'mir, 'tcx, M> {
614
614
// first copy the relocations to a temporary buffer, because
615
615
// `get_bytes_mut` will clear the relocations, which is correct,
616
616
// since we don't want to keep any relocations at the target.
617
-
618
617
let relocations: Vec < _ > = self . relocations ( src, size) ?
619
- . map ( |( & offset, & alloc_id) | {
618
+ . iter ( )
619
+ . map ( |& ( offset, alloc_id) | {
620
620
// Update relocation offsets for the new positions in the destination allocation.
621
621
( offset + dest. offset - src. offset , alloc_id)
622
622
} )
@@ -648,7 +648,7 @@ impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'a, 'mir, 'tcx, M> {
648
648
649
649
self . copy_undef_mask ( src, dest, size) ?;
650
650
// copy back the relocations
651
- self . get_mut ( dest. alloc_id ) ?. relocations . extend ( relocations) ;
651
+ self . get_mut ( dest. alloc_id ) ?. relocations . insert_presorted ( relocations) ;
652
652
653
653
Ok ( ( ) )
654
654
}
@@ -660,7 +660,7 @@ impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'a, 'mir, 'tcx, M> {
660
660
match alloc. bytes [ offset..] . iter ( ) . position ( |& c| c == 0 ) {
661
661
Some ( size) => {
662
662
let p1 = Size :: from_bytes ( ( size + 1 ) as u64 ) ;
663
- if self . relocations ( ptr, p1) ?. count ( ) != 0 {
663
+ if self . relocations ( ptr, p1) ?. len ( ) != 0 {
664
664
return err ! ( ReadPointerAsBytes ) ;
665
665
}
666
666
self . check_defined ( ptr, p1) ?;
@@ -720,7 +720,7 @@ impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'a, 'mir, 'tcx, M> {
720
720
let bytes = read_target_uint ( endianness, bytes) . unwrap ( ) ;
721
721
// See if we got a pointer
722
722
if size != self . pointer_size ( ) {
723
- if self . relocations ( ptr, size) ?. count ( ) != 0 {
723
+ if self . relocations ( ptr, size) ?. len ( ) != 0 {
724
724
return err ! ( ReadPointerAsBytes ) ;
725
725
}
726
726
} else {
@@ -808,24 +808,26 @@ impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'a, 'mir, 'tcx, M> {
808
808
& self ,
809
809
ptr : MemoryPointer ,
810
810
size : Size ,
811
- ) -> EvalResult < ' tcx , btree_map :: Range < Size , AllocId > > {
811
+ ) -> EvalResult < ' tcx , & [ ( Size , AllocId ) ] > {
812
812
let start = ptr. offset . bytes ( ) . saturating_sub ( self . pointer_size ( ) . bytes ( ) - 1 ) ;
813
813
let end = ptr. offset + size;
814
814
Ok ( self . get ( ptr. alloc_id ) ?. relocations . range ( Size :: from_bytes ( start) ..end) )
815
815
}
816
816
817
817
fn clear_relocations ( & mut self , ptr : MemoryPointer , size : Size ) -> EvalResult < ' tcx > {
818
- // Find all relocations overlapping the given range.
819
- let keys: Vec < _ > = self . relocations ( ptr, size) ?. map ( |( & k, _) | k) . collect ( ) ;
820
- if keys. is_empty ( ) {
821
- return Ok ( ( ) ) ;
822
- }
823
-
824
818
// Find the start and end of the given range and its outermost relocations.
819
+ let ( first, last) = {
820
+ // Find all relocations overlapping the given range.
821
+ let relocations = self . relocations ( ptr, size) ?;
822
+ if relocations. is_empty ( ) {
823
+ return Ok ( ( ) ) ;
824
+ }
825
+
826
+ ( relocations. first ( ) . unwrap ( ) . 0 ,
827
+ relocations. last ( ) . unwrap ( ) . 0 + self . pointer_size ( ) )
828
+ } ;
825
829
let start = ptr. offset ;
826
830
let end = start + size;
827
- let first = * keys. first ( ) . unwrap ( ) ;
828
- let last = * keys. last ( ) . unwrap ( ) + self . pointer_size ( ) ;
829
831
830
832
let alloc = self . get_mut ( ptr. alloc_id ) ?;
831
833
@@ -839,16 +841,14 @@ impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'a, 'mir, 'tcx, M> {
839
841
}
840
842
841
843
// Forget all the relocations.
842
- for k in keys {
843
- alloc. relocations . remove ( & k) ;
844
- }
844
+ alloc. relocations . remove_range ( first ..= last) ;
845
845
846
846
Ok ( ( ) )
847
847
}
848
848
849
849
fn check_relocation_edges ( & self , ptr : MemoryPointer , size : Size ) -> EvalResult < ' tcx > {
850
- let overlapping_start = self . relocations ( ptr, Size :: from_bytes ( 0 ) ) ?. count ( ) ;
851
- let overlapping_end = self . relocations ( ptr. offset ( size, self ) ?, Size :: from_bytes ( 0 ) ) ?. count ( ) ;
850
+ let overlapping_start = self . relocations ( ptr, Size :: from_bytes ( 0 ) ) ?. len ( ) ;
851
+ let overlapping_end = self . relocations ( ptr. offset ( size, self ) ?, Size :: from_bytes ( 0 ) ) ?. len ( ) ;
852
852
if overlapping_start + overlapping_end != 0 {
853
853
return err ! ( ReadPointerAsBytes ) ;
854
854
}
0 commit comments