Skip to content

Commit 90ce83c

Browse files
committed
Use slice::partition_points instead of superslice.
1 parent 7122b32 commit 90ce83c

File tree

5 files changed

+60
-43
lines changed

5 files changed

+60
-43
lines changed

Cargo.lock

Lines changed: 2 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

adapter/codelldb/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ regex-lite = "0.1.6"
2727
serde = "1.0.94"
2828
serde_derive = "1.0.94"
2929
serde_json = {version = "1.0.40", features = ["raw_value"]}
30-
superslice = "1.0.0"
3130
tokio = {version = "1.0.0", features = ["full"]}
3231
tokio-util = {version = "0.6.0", features = ["codec"]}
3332

adapter/codelldb/src/disassembly.rs

Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ use serde_derive::*;
33
use std::cell::RefCell;
44
use std::collections::HashMap;
55
use std::fmt::Write;
6+
use std::ops::Range;
67
use std::rc::Rc;
78
use std::str;
89

910
use crate::handles::Handle;
1011
use lldb::*;
11-
use superslice::Ext;
1212

1313
struct Ranges {
1414
pub by_handle: HashMap<Handle, Rc<DisassembledRange>>,
@@ -36,17 +36,14 @@ impl DisassembledRanges {
3636

3737
fn find_by_address(&self, load_addr: Address) -> Option<Rc<DisassembledRange>> {
3838
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 {
4341
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());
4844
}
4945
}
46+
None
5047
}
5148

5249
pub fn from_address(&self, load_addr: Address) -> Result<Rc<DisassembledRange>, Error> {
@@ -108,14 +105,13 @@ impl DisassembledRanges {
108105
handle: handle,
109106
target: self.target.clone(),
110107
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,
113109
source_name: source_name,
114110
instructions: instructions,
115111
instruction_addresses: instruction_addrs,
116112
});
117113
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);
119115
ranges.by_address.insert(idx, dasm.clone());
120116
dasm
121117
}
@@ -134,8 +130,7 @@ pub struct DisassembledRange {
134130
handle: Handle,
135131
target: SBTarget,
136132
start_addr: SBAddress,
137-
start_load_addr: Address,
138-
end_load_addr: Address,
133+
load_range: Range<Address>,
139134
source_name: String,
140135
instructions: SBInstructionList,
141136
instruction_addresses: Vec<Address>,
@@ -151,7 +146,7 @@ impl DisassembledRange {
151146
}
152147

153148
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
155150
}
156151

157152
pub fn address_by_line_num(&self, line: u32) -> Address {
@@ -161,8 +156,8 @@ impl DisassembledRange {
161156
pub fn adapter_data(&self) -> AdapterData {
162157
let line_offsets = self.instruction_addresses.windows(2).map(|w| (w[1] - w[0]) as u32).collect();
163158
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,
166161
line_offsets: line_offsets,
167162
}
168163
}
@@ -235,6 +230,35 @@ impl DisassembledRange {
235230
}
236231
}
237232

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+
238262
#[test]
239263
fn test_adapter_data() {
240264
let addresses = &[10, 20, 23, 25, 30, 35, 41, 42, 50];

adapter/codelldb/src/lib.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,3 +181,14 @@ fn test_init() {
181181
lldb_stub::base.resolve().unwrap().mark_permanent();
182182
lldb_stub::v16.resolve().unwrap().mark_permanent();
183183
}
184+
185+
#[cfg(test)]
186+
lazy_static::lazy_static! {
187+
static ref TEST_DEBUGGER: SBDebugger = {
188+
use lldb::*;
189+
std::env::remove_var("PYTHONHOME");
190+
std::env::remove_var("PYTHONPATH");
191+
SBDebugger::initialize();
192+
SBDebugger::create(false)
193+
};
194+
}

adapter/codelldb/src/python.rs

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -354,21 +354,11 @@ fn test_sizeof() {
354354
assert_eq!(mem::size_of::<PyObject>(), 16);
355355
}
356356

357-
#[cfg(test)]
358-
lazy_static::lazy_static! {
359-
static ref DEBUGGER: SBDebugger = {
360-
use lldb::*;
361-
std::env::remove_var("PYTHONHOME");
362-
std::env::remove_var("PYTHONPATH");
363-
SBDebugger::initialize();
364-
SBDebugger::create(false)
365-
};
366-
}
367-
368357
#[test]
369358
fn pypath() {
370359
use lldb::*;
371-
let interp = DEBUGGER.command_interpreter();
360+
use crate::TEST_DEBUGGER;
361+
let interp = TEST_DEBUGGER.command_interpreter();
372362
let mut result = SBCommandReturnObject::new();
373363
let status = interp.handle_command("script import sys; print(sys.path)", &mut result, false);
374364
println!("result = {:?}", result.output());
@@ -378,13 +368,14 @@ fn pypath() {
378368
#[test]
379369
fn evaluate() {
380370
use lldb::*;
371+
use crate::TEST_DEBUGGER;
381372
let adapter_dir = Path::new(env!("ADAPTER_SOURCE_DIR"));
382-
let interface = initialize(&DEBUGGER, adapter_dir).unwrap();
373+
let interface = initialize(&TEST_DEBUGGER, adapter_dir).unwrap();
383374
let (session, _events) = interface.new_session(
384-
&DEBUGGER,
375+
&TEST_DEBUGGER,
385376
std::fs::File::create(if cfg!(unix) { "/dev/null" } else { "NUL" }).unwrap(),
386377
);
387-
let context = SBExecutionContext::from_target(&DEBUGGER.dummy_target());
378+
let context = SBExecutionContext::from_target(&TEST_DEBUGGER.dummy_target());
388379
let pycode = session.compile_code("2+2", "<string>").unwrap();
389380
let result = session.evaluate(&pycode, &context, EvalContext::PythonExpression);
390381
println!("result = {:?}", result);

0 commit comments

Comments
 (0)