@@ -3,12 +3,12 @@ use serde_derive::*;
3
3
use std:: cell:: RefCell ;
4
4
use std:: collections:: HashMap ;
5
5
use std:: fmt:: Write ;
6
+ use std:: ops:: Range ;
6
7
use std:: rc:: Rc ;
7
8
use std:: str;
8
9
9
10
use crate :: handles:: Handle ;
10
11
use lldb:: * ;
11
- use superslice:: Ext ;
12
12
13
13
struct Ranges {
14
14
pub by_handle : HashMap < Handle , Rc < DisassembledRange > > ,
@@ -36,17 +36,14 @@ impl DisassembledRanges {
36
36
37
37
fn find_by_address ( & self , load_addr : Address ) -> Option < Rc < DisassembledRange > > {
38
38
let ranges = self . ranges . borrow_mut ( ) ;
39
- let idx = ranges. by_address . upper_bound_by_key ( & load_addr, |dasm| dasm. start_load_addr ) ;
40
- if idx == 0 {
41
- None
42
- } else {
39
+ let idx = ranges. by_address . partition_point ( |dasm| dasm. load_range . start <= load_addr) ;
40
+ if idx > 0 {
43
41
let dasm = & ranges. by_address [ idx - 1 ] ;
44
- if dasm. start_load_addr <= load_addr && load_addr < dasm. end_load_addr {
45
- Some ( dasm. clone ( ) )
46
- } else {
47
- None
42
+ if dasm. load_range . contains ( & load_addr) {
43
+ return Some ( dasm. clone ( ) ) ;
48
44
}
49
45
}
46
+ None
50
47
}
51
48
52
49
pub fn from_address ( & self , load_addr : Address ) -> Result < Rc < DisassembledRange > , Error > {
@@ -108,14 +105,13 @@ impl DisassembledRanges {
108
105
handle : handle,
109
106
target : self . target . clone ( ) ,
110
107
start_addr : start_addr,
111
- start_load_addr : start_load_addr,
112
- end_load_addr : end_load_addr,
108
+ load_range : start_load_addr..end_load_addr,
113
109
source_name : source_name,
114
110
instructions : instructions,
115
111
instruction_addresses : instruction_addrs,
116
112
} ) ;
117
113
ranges. by_handle . insert ( handle, dasm. clone ( ) ) ;
118
- let idx = ranges. by_address . lower_bound_by_key ( & dasm . start_load_addr , |dasm| dasm. start_load_addr ) ;
114
+ let idx = ranges. by_address . partition_point ( |dasm| dasm. load_range . start < start_load_addr) ;
119
115
ranges. by_address . insert ( idx, dasm. clone ( ) ) ;
120
116
dasm
121
117
}
@@ -134,8 +130,7 @@ pub struct DisassembledRange {
134
130
handle : Handle ,
135
131
target : SBTarget ,
136
132
start_addr : SBAddress ,
137
- start_load_addr : Address ,
138
- end_load_addr : Address ,
133
+ load_range : Range < Address > ,
139
134
source_name : String ,
140
135
instructions : SBInstructionList ,
141
136
instruction_addresses : Vec < Address > ,
@@ -151,7 +146,7 @@ impl DisassembledRange {
151
146
}
152
147
153
148
pub fn line_num_by_address ( & self , load_addr : Address ) -> u32 {
154
- self . instruction_addresses . lower_bound ( & load_addr) as u32 + 3
149
+ self . instruction_addresses . partition_point ( |addr| * addr < load_addr) as u32 + 3
155
150
}
156
151
157
152
pub fn address_by_line_num ( & self , line : u32 ) -> Address {
@@ -161,8 +156,8 @@ impl DisassembledRange {
161
156
pub fn adapter_data ( & self ) -> AdapterData {
162
157
let line_offsets = self . instruction_addresses . windows ( 2 ) . map ( |w| ( w[ 1 ] - w[ 0 ] ) as u32 ) . collect ( ) ;
163
158
AdapterData {
164
- start : self . start_load_addr ,
165
- end : self . end_load_addr ,
159
+ start : self . load_range . start ,
160
+ end : self . load_range . end ,
166
161
line_offsets : line_offsets,
167
162
}
168
163
}
@@ -235,6 +230,35 @@ impl DisassembledRange {
235
230
}
236
231
}
237
232
233
+ #[ test]
234
+ fn test_range_lookup ( ) {
235
+ use crate :: TEST_DEBUGGER ;
236
+ use std:: rc:: Rc ;
237
+ let target = TEST_DEBUGGER . dummy_target ( ) ;
238
+ let ranges = DisassembledRanges :: new ( & target) ;
239
+
240
+ assert ! ( ranges. find_by_address( 1234 ) . is_none( ) ) ;
241
+
242
+ let add = |start, end| {
243
+ let start = SBAddress :: from_load_address ( start, & target) ;
244
+ let end = SBAddress :: from_load_address ( end, & target) ;
245
+ ranges. add ( start. clone ( ) , end, target. get_instructions ( & start, & [ ] , None ) )
246
+ } ;
247
+
248
+ let dasm1 = add ( 1000 , 2000 ) ;
249
+ let dasm3 = add ( 4000 , 5000 ) ;
250
+ let dasm2 = add ( 3000 , 4000 ) ;
251
+
252
+ assert ! ( Rc :: ptr_eq( & ranges. find_by_address( 1000 ) . unwrap( ) , & dasm1) ) ;
253
+ assert ! ( Rc :: ptr_eq( & ranges. find_by_address( 1234 ) . unwrap( ) , & dasm1) ) ;
254
+ assert ! ( Rc :: ptr_eq( & ranges. find_by_address( 1999 ) . unwrap( ) , & dasm1) ) ;
255
+ assert ! ( ranges. find_by_address( 2000 ) . is_none( ) ) ;
256
+ assert ! ( Rc :: ptr_eq( & ranges. find_by_address( 3000 ) . unwrap( ) , & dasm2) ) ;
257
+ assert ! ( Rc :: ptr_eq( & ranges. find_by_address( 3999 ) . unwrap( ) , & dasm2) ) ;
258
+ assert ! ( Rc :: ptr_eq( & ranges. find_by_address( 4000 ) . unwrap( ) , & dasm3) ) ;
259
+ assert ! ( ranges. find_by_address( 5000 ) . is_none( ) ) ;
260
+ }
261
+
238
262
#[ test]
239
263
fn test_adapter_data ( ) {
240
264
let addresses = & [ 10 , 20 , 23 , 25 , 30 , 35 , 41 , 42 , 50 ] ;
0 commit comments