From ecaab58741443d1e36fbd372e71cedeaa2a45e06 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Mon, 7 Jul 2025 17:53:23 +0200 Subject: [PATCH] move our data structures into a central location --- src/borrow_tracker/stacked_borrows/mod.rs | 4 ++-- src/borrow_tracker/tree_borrows/mod.rs | 2 +- src/borrow_tracker/tree_borrows/tree.rs | 6 ++--- src/concurrency/data_race.rs | 4 ++-- src/concurrency/mod.rs | 1 - src/concurrency/weak_memory.rs | 2 +- .../dedup_range_map.rs} | 22 +++++++++---------- src/data_structures/mod.rs | 3 +++ src/{ => data_structures}/mono_hash_map.rs | 0 .../range_object_map.rs | 0 src/lib.rs | 7 +++--- 11 files changed, 26 insertions(+), 25 deletions(-) rename src/{range_map.rs => data_structures/dedup_range_map.rs} (95%) create mode 100644 src/data_structures/mod.rs rename src/{ => data_structures}/mono_hash_map.rs (100%) rename src/{concurrency => data_structures}/range_object_map.rs (100%) diff --git a/src/borrow_tracker/stacked_borrows/mod.rs b/src/borrow_tracker/stacked_borrows/mod.rs index b8bcacf7c9..834a4b41f2 100644 --- a/src/borrow_tracker/stacked_borrows/mod.rs +++ b/src/borrow_tracker/stacked_borrows/mod.rs @@ -30,7 +30,7 @@ pub type AllocState = Stacks; #[derive(Clone, Debug)] pub struct Stacks { // Even reading memory can have effects on the stack, so we need a `RefCell` here. - stacks: RangeMap, + stacks: DedupRangeMap, /// Stores past operations on this allocation history: AllocHistory, /// The set of tags that have been exposed inside this allocation. @@ -468,7 +468,7 @@ impl<'tcx> Stacks { let stack = Stack::new(item); Stacks { - stacks: RangeMap::new(size, stack), + stacks: DedupRangeMap::new(size, stack), history: AllocHistory::new(id, item, machine), exposed_tags: FxHashSet::default(), } diff --git a/src/borrow_tracker/tree_borrows/mod.rs b/src/borrow_tracker/tree_borrows/mod.rs index a0761cb07a..c157c69d7c 100644 --- a/src/borrow_tracker/tree_borrows/mod.rs +++ b/src/borrow_tracker/tree_borrows/mod.rs @@ -314,7 +314,7 @@ trait EvalContextPrivExt<'tcx>: crate::MiriInterpCxExt<'tcx> { let span = this.machine.current_span(); // Store initial permissions and their corresponding range. - let mut perms_map: RangeMap = RangeMap::new( + let mut perms_map: DedupRangeMap = DedupRangeMap::new( ptr_size, LocationState::new_accessed(Permission::new_disabled(), IdempotentForeignAccess::None), // this will be overwritten ); diff --git a/src/borrow_tracker/tree_borrows/tree.rs b/src/borrow_tracker/tree_borrows/tree.rs index 48e4a19e26..1f29bcfc2b 100644 --- a/src/borrow_tracker/tree_borrows/tree.rs +++ b/src/borrow_tracker/tree_borrows/tree.rs @@ -247,7 +247,7 @@ pub struct Tree { /// `unwrap` any `perm.get(key)`. /// /// We do uphold the fact that `keys(perms)` is a subset of `keys(nodes)` - pub(super) rperms: RangeMap>, + pub(super) rperms: DedupRangeMap>, /// The index of the root node. pub(super) root: UniIndex, } @@ -609,7 +609,7 @@ impl Tree { IdempotentForeignAccess::None, ), ); - RangeMap::new(size, perms) + DedupRangeMap::new(size, perms) }; Self { root: root_idx, nodes, rperms, tag_mapping } } @@ -631,7 +631,7 @@ impl<'tcx> Tree { base_offset: Size, parent_tag: BorTag, new_tag: BorTag, - initial_perms: RangeMap, + initial_perms: DedupRangeMap, default_perm: Permission, protected: bool, span: Span, diff --git a/src/concurrency/data_race.rs b/src/concurrency/data_race.rs index b5e7e9d0ac..38d76f5cf7 100644 --- a/src/concurrency/data_race.rs +++ b/src/concurrency/data_race.rs @@ -997,7 +997,7 @@ pub trait EvalContextExt<'tcx>: MiriInterpCxExt<'tcx> { #[derive(Debug, Clone)] pub struct VClockAlloc { /// Assigning each byte a MemoryCellClocks. - alloc_ranges: RefCell>, + alloc_ranges: RefCell>, } impl VisitProvenance for VClockAlloc { @@ -1045,7 +1045,7 @@ impl VClockAlloc { (VTimestamp::ZERO, global.thread_index(ThreadId::MAIN_THREAD)), }; VClockAlloc { - alloc_ranges: RefCell::new(RangeMap::new( + alloc_ranges: RefCell::new(DedupRangeMap::new( len, MemoryCellClocks::new(alloc_timestamp, alloc_index), )), diff --git a/src/concurrency/mod.rs b/src/concurrency/mod.rs index 49bcc0d30b..c2ea8a00de 100644 --- a/src/concurrency/mod.rs +++ b/src/concurrency/mod.rs @@ -2,7 +2,6 @@ pub mod cpu_affinity; pub mod data_race; mod data_race_handler; pub mod init_once; -mod range_object_map; pub mod sync; pub mod thread; mod vector_clock; diff --git a/src/concurrency/weak_memory.rs b/src/concurrency/weak_memory.rs index 95c010be2f..a752ef7e45 100644 --- a/src/concurrency/weak_memory.rs +++ b/src/concurrency/weak_memory.rs @@ -90,9 +90,9 @@ use rustc_data_structures::fx::FxHashMap; use super::AllocDataRaceHandler; use super::data_race::{GlobalState as DataRaceState, ThreadClockSet}; -use super::range_object_map::{AccessType, RangeObjectMap}; use super::vector_clock::{VClock, VTimestamp, VectorIdx}; use crate::concurrency::GlobalDataRaceHandler; +use crate::data_structures::range_object_map::{AccessType, RangeObjectMap}; use crate::*; pub type AllocState = StoreBufferAlloc; diff --git a/src/range_map.rs b/src/data_structures/dedup_range_map.rs similarity index 95% rename from src/range_map.rs rename to src/data_structures/dedup_range_map.rs index 29a5a8537a..56e9883b1c 100644 --- a/src/range_map.rs +++ b/src/data_structures/dedup_range_map.rs @@ -17,18 +17,18 @@ struct Elem { data: T, } #[derive(Clone, Debug)] -pub struct RangeMap { +pub struct DedupRangeMap { v: Vec>, } -impl RangeMap { +impl DedupRangeMap { /// Creates a new `RangeMap` for the given size, and with the given initial value used for /// the entire range. #[inline(always)] - pub fn new(size: Size, init: T) -> RangeMap { + pub fn new(size: Size, init: T) -> DedupRangeMap { let size = size.bytes(); let v = if size > 0 { vec![Elem { range: 0..size, data: init }] } else { Vec::new() }; - RangeMap { v } + DedupRangeMap { v } } pub fn size(&self) -> Size { @@ -246,7 +246,7 @@ mod tests { use super::*; /// Query the map at every offset in the range and collect the results. - fn to_vec(map: &RangeMap, offset: u64, len: u64) -> Vec { + fn to_vec(map: &DedupRangeMap, offset: u64, len: u64) -> Vec { (offset..offset + len) .map(|i| { map.iter(Size::from_bytes(i), Size::from_bytes(1)).next().map(|(_, &t)| t).unwrap() @@ -256,7 +256,7 @@ mod tests { #[test] fn basic_insert() { - let mut map = RangeMap::::new(Size::from_bytes(20), -1); + let mut map = DedupRangeMap::::new(Size::from_bytes(20), -1); // Insert. for (_, x) in map.iter_mut(Size::from_bytes(10), Size::from_bytes(1)) { *x = 42; @@ -278,7 +278,7 @@ mod tests { #[test] fn gaps() { - let mut map = RangeMap::::new(Size::from_bytes(20), -1); + let mut map = DedupRangeMap::::new(Size::from_bytes(20), -1); for (_, x) in map.iter_mut(Size::from_bytes(11), Size::from_bytes(1)) { *x = 42; } @@ -319,26 +319,26 @@ mod tests { #[test] #[should_panic] fn out_of_range_iter_mut() { - let mut map = RangeMap::::new(Size::from_bytes(20), -1); + let mut map = DedupRangeMap::::new(Size::from_bytes(20), -1); let _ = map.iter_mut(Size::from_bytes(11), Size::from_bytes(11)); } #[test] #[should_panic] fn out_of_range_iter() { - let map = RangeMap::::new(Size::from_bytes(20), -1); + let map = DedupRangeMap::::new(Size::from_bytes(20), -1); let _ = map.iter(Size::from_bytes(11), Size::from_bytes(11)); } #[test] fn empty_map_iter() { - let map = RangeMap::::new(Size::from_bytes(0), -1); + let map = DedupRangeMap::::new(Size::from_bytes(0), -1); let _ = map.iter(Size::from_bytes(0), Size::from_bytes(0)); } #[test] fn empty_map_iter_mut() { - let mut map = RangeMap::::new(Size::from_bytes(0), -1); + let mut map = DedupRangeMap::::new(Size::from_bytes(0), -1); let _ = map.iter_mut(Size::from_bytes(0), Size::from_bytes(0)); } } diff --git a/src/data_structures/mod.rs b/src/data_structures/mod.rs new file mode 100644 index 0000000000..d4468bc57e --- /dev/null +++ b/src/data_structures/mod.rs @@ -0,0 +1,3 @@ +pub mod dedup_range_map; +pub mod mono_hash_map; +pub mod range_object_map; diff --git a/src/mono_hash_map.rs b/src/data_structures/mono_hash_map.rs similarity index 100% rename from src/mono_hash_map.rs rename to src/data_structures/mono_hash_map.rs diff --git a/src/concurrency/range_object_map.rs b/src/data_structures/range_object_map.rs similarity index 100% rename from src/concurrency/range_object_map.rs rename to src/data_structures/range_object_map.rs diff --git a/src/lib.rs b/src/lib.rs index ca99d69b32..642b7ee984 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -75,16 +75,15 @@ mod alloc_addresses; mod borrow_tracker; mod clock; mod concurrency; +mod data_structures; mod diagnostics; mod eval; mod helpers; mod intrinsics; mod machine; mod math; -mod mono_hash_map; mod operator; mod provenance_gc; -mod range_map; mod shims; // Establish a "crate-wide prelude": we often import `crate::*`. @@ -132,6 +131,8 @@ pub use crate::concurrency::thread::{ ThreadManager, TimeoutAnchor, TimeoutClock, UnblockKind, }; pub use crate::concurrency::{GenmcConfig, GenmcCtx}; +pub use crate::data_structures::dedup_range_map::DedupRangeMap; +pub use crate::data_structures::mono_hash_map::MonoHashMap; pub use crate::diagnostics::{ EvalContextExt as _, NonHaltingDiagnostic, TerminationInfo, report_error, }; @@ -145,10 +146,8 @@ pub use crate::machine::{ AllocExtra, DynMachineCallback, FrameExtra, MachineCallback, MemoryKind, MiriInterpCx, MiriInterpCxExt, MiriMachine, MiriMemoryKind, PrimitiveLayouts, Provenance, ProvenanceExtra, }; -pub use crate::mono_hash_map::MonoHashMap; pub use crate::operator::EvalContextExt as _; pub use crate::provenance_gc::{EvalContextExt as _, LiveAllocs, VisitProvenance, VisitWith}; -pub use crate::range_map::RangeMap; pub use crate::shims::EmulateItemResult; pub use crate::shims::env::{EnvVars, EvalContextExt as _}; pub use crate::shims::foreign_items::{DynSym, EvalContextExt as _};