Skip to content

Commit

Permalink
refactor: use stabilized range 'contains'
Browse files Browse the repository at this point in the history
  • Loading branch information
darfink committed Jun 9, 2019
1 parent a4a4172 commit 073e0e7
Show file tree
Hide file tree
Showing 6 changed files with 8 additions and 25 deletions.
5 changes: 2 additions & 3 deletions src/alloc/proximity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use slice_pool::{PoolVal, SlicePool};

use super::search as region_search;
use crate::error::{Error, Result};
use crate::util::RangeContains;

/// Defines the allocation type.
pub type Allocation = PoolVal<u8>;
Expand Down Expand Up @@ -45,7 +44,7 @@ impl ProximityAllocator {
let upper = lower + pool.len();

// Determine if this is the associated memory pool
(lower..upper).contains_(value.as_ptr() as usize)
(lower..upper).contains(&(value.as_ptr() as usize))
})
.expect("retrieving associated memory pool");

Expand All @@ -61,7 +60,7 @@ impl ProximityAllocator {
let is_pool_in_range = |pool: &SlicePool<u8>| {
let lower = pool.as_ptr() as usize;
let upper = lower + pool.len();
range.contains_(lower) && range.contains_(upper - 1)
range.contains(&lower) && range.contains(&(upper - 1))
};

// Tries to allocate a slice within any eligible pool
Expand Down
3 changes: 1 addition & 2 deletions src/alloc/search.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::error::{Error, Result};
use crate::util::RangeContains;
use std::ops::Range;

/// Returns an iterator for free after the specified address.
Expand Down Expand Up @@ -43,7 +42,7 @@ impl Iterator for FreeRegionIter {
fn next(&mut self) -> Option<Self::Item> {
let page_size = region::page::size();

while self.current > 0 && self.range.contains_(self.current) {
while self.current > 0 && self.range.contains(&self.current) {
match region::query(self.current as *const _) {
Ok(region) => {
self.current = match self.search {
Expand Down
4 changes: 2 additions & 2 deletions src/arch/arm/thunk/thumb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ struct Relay {
let is_both_thumb = ;
let is_both_arm = ;

if is_both_thumb && (-252..258).contains(offset) {
} else if is_both_arm && (-0x2000000..0x2000000).contains(offset) {
if is_both_thumb && (-252..258).contains(&offset) {
} else if is_both_arm && (-0x2000000..0x2000000).contains(&offset) {
} else {
}

Expand Down
4 changes: 1 addition & 3 deletions src/arch/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ mod memory;

/// Returns true if the displacement is within a certain range.
pub fn is_within_range(displacement: isize) -> bool {
use crate::util::RangeContains;

let range = meta::DETOUR_RANGE as isize;
(-range..range).contains_(displacement)
(-range..range).contains(&displacement)
}
5 changes: 2 additions & 3 deletions src/arch/x86/trampoline/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::mem;
use crate::arch::x86::thunk;
use crate::error::{Result, Error};
use crate::util::RangeContains;
use crate::pic;
use self::disasm::*;

Expand Down Expand Up @@ -144,7 +143,7 @@ impl Builder {
self.finished = instruction.is_unconditional_jump();

// Nothing should be done if `displacement` is within the prolog.
if (-(self.total_bytes_disassembled as isize)..0).contains_(displacement) {
if (-(self.total_bytes_disassembled as isize)..0).contains(&displacement) {
return Ok(Box::new(instruction.as_slice().to_vec()));
}

Expand Down Expand Up @@ -197,7 +196,7 @@ impl Builder {
// If the relative jump is internal, and short enough to
// fit within the copied function prolog (i.e `margin`),
// the jump instruction can be copied indiscriminately.
if prolog_range.contains_(destination_address_abs) {
if prolog_range.contains(&destination_address_abs) {
// Keep track of the jump's destination address
self.branch_address = Some(destination_address_abs);
Ok(Box::new(instruction.as_slice().to_vec()))
Expand Down
12 changes: 0 additions & 12 deletions src/util.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::error::Result;
use std::ops::Range;

/// Returns true if an address is executable.
pub fn is_executable_address(address: *const ()) -> Result<bool> {
Expand All @@ -9,14 +8,3 @@ pub fn is_executable_address(address: *const ()) -> Result<bool> {
.contains(region::Protection::Execute),
)
}

/// Trait for ranges containing values.
pub trait RangeContains<Idx: PartialOrd<Idx>> {
fn contains_(&self, item: Idx) -> bool;
}

impl<Idx: PartialOrd<Idx>> RangeContains<Idx> for Range<Idx> {
fn contains_(&self, item: Idx) -> bool {
self.start <= item && self.end > item
}
}

0 comments on commit 073e0e7

Please sign in to comment.