Skip to content

Commit

Permalink
Merge pull request #387 from ecstatic-morse/eq-hash
Browse files Browse the repository at this point in the history
Implement `Eq`, `Clone` and `Hash` for MemoryData and Evaluator
  • Loading branch information
RalfJung authored Jul 13, 2018
2 parents 19e214e + a2f4d84 commit ff64b42
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 5 deletions.
41 changes: 38 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,13 @@ use rustc::ty::subst::Subst;
use rustc::hir::def_id::DefId;
use rustc::mir;

use rustc_data_structures::fx::FxHasher;

use syntax::ast::Mutability;
use syntax::codemap::Span;

use std::collections::{HashMap, BTreeMap};
use std::hash::{Hash, Hasher};

pub use rustc::mir::interpret::*;
pub use rustc_mir::interpret::*;
Expand Down Expand Up @@ -296,7 +299,7 @@ pub fn eval_main<'a, 'tcx: 'a>(
}
}

#[derive(Default)]
#[derive(Clone, Default, PartialEq, Eq)]
pub struct Evaluator<'tcx> {
/// Environment variables set by `setenv`
/// Miri does not expose env vars from the host to the emulated program
Expand All @@ -306,15 +309,34 @@ pub struct Evaluator<'tcx> {
pub(crate) suspended: HashMap<DynamicLifetime, Vec<ValidationQuery<'tcx>>>,
}

impl<'tcx> Hash for Evaluator<'tcx> {
fn hash<H: Hasher>(&self, state: &mut H) {
let Evaluator {
env_vars,
suspended: _,
} = self;

env_vars.iter()
.map(|(env, ptr)| {
let mut h = FxHasher::default();
env.hash(&mut h);
ptr.hash(&mut h);
h.finish()
})
.fold(0u64, |acc, hash| acc.wrapping_add(hash))
.hash(state);
}
}

pub type TlsKey = u128;

#[derive(Copy, Clone, Debug)]
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]
pub struct TlsEntry<'tcx> {
data: Scalar, // Will eventually become a map from thread IDs to `Scalar`s, if we ever support more than one thread.
dtor: Option<ty::Instance<'tcx>>,
}

#[derive(Default)]
#[derive(Clone, Default, PartialEq, Eq)]
pub struct MemoryData<'tcx> {
/// The Key to use for the next thread-local allocation.
next_thread_local: TlsKey,
Expand All @@ -331,6 +353,19 @@ pub struct MemoryData<'tcx> {
statics: HashMap<GlobalId<'tcx>, AllocId>,
}

impl<'tcx> Hash for MemoryData<'tcx> {
fn hash<H: Hasher>(&self, state: &mut H) {
let MemoryData {
next_thread_local: _,
thread_local,
locks: _,
statics: _,
} = self;

thread_local.hash(state);
}
}

impl<'mir, 'tcx: 'mir> Machine<'mir, 'tcx> for Evaluator<'tcx> {
type MemoryData = MemoryData<'tcx>;
type MemoryKinds = memory::MemoryKind;
Expand Down
2 changes: 1 addition & 1 deletion src/locks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use rustc::ty::layout::Size;
////////////////////////////////////////////////////////////////////////////////

/// Information about a lock that is currently held.
#[derive(Clone, Debug)]
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct LockInfo<'tcx> {
/// Stores for which lifetimes (of the original write lock) we got
/// which suspensions.
Expand Down
2 changes: 1 addition & 1 deletion src/range_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use std::collections::BTreeMap;
use std::ops;

#[derive(Clone, Debug)]
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct RangeMap<T> {
map: BTreeMap<Range, T>,
}
Expand Down

0 comments on commit ff64b42

Please sign in to comment.