Skip to content

Commit 579c323

Browse files
Make MemorySegmentManager's memory field private (#2164)
* Make memory private * Add public getters for memory * Update changelog * Replace std Cow with nostd Cow * Increase code coverage
1 parent 5c48a92 commit 579c323

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
#### Upcoming Changes
44

5+
* fix: make `MemorySegmentManager`'s `memory` field private [#2164](https://github.com/lambdaclass/cairo-vm/pull/2164)
6+
57
* feat: Use BTreeMap in PIE additional data [#2162](https://github.com/lambdaclass/cairo-vm/pull/2162)
68

79
* feat: Remove prover input info struct and add getters instead [#2149](https://github.com/lambdaclass/cairo-vm/pull/2149)

vm/src/vm/vm_memory/memory_segments.rs

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::stdlib::borrow::Cow;
12
use crate::stdlib::collections::HashSet;
23
use core::cmp::max;
34
use core::fmt;
@@ -23,7 +24,7 @@ use super::memory::MemoryCell;
2324
pub struct MemorySegmentManager {
2425
pub segment_sizes: HashMap<usize, usize>,
2526
pub segment_used_sizes: Option<Vec<usize>>,
26-
pub memory: Memory,
27+
pub(crate) memory: Memory,
2728
// A map from segment index to a list of pairs (offset, page_id) that constitute the
2829
// public memory. Note that the offset is absolute (not based on the page_id).
2930
pub public_memory_offsets: HashMap<usize, Vec<(usize, usize)>>,
@@ -328,6 +329,16 @@ impl MemorySegmentManager {
328329
}
329330
Ok(())
330331
}
332+
333+
pub fn get_integer(&self, key: Relocatable) -> Result<Felt252, MemoryError> {
334+
self.memory.get_integer(key).map(Cow::into_owned)
335+
}
336+
pub fn get_relocatable(&self, key: Relocatable) -> Result<Relocatable, MemoryError> {
337+
self.memory.get_relocatable(key)
338+
}
339+
pub fn get_maybe_relocatable(&self, key: Relocatable) -> Result<MaybeRelocatable, MemoryError> {
340+
self.memory.get_maybe_relocatable(key)
341+
}
331342
}
332343

333344
impl Default for MemorySegmentManager {
@@ -1117,4 +1128,47 @@ mod tests {
11171128
])
11181129
);
11191130
}
1131+
1132+
#[test]
1133+
fn test_get_integer() {
1134+
let mut memory_segment_manager = MemorySegmentManager::new();
1135+
memory_segment_manager.memory = memory![((0, 0), 10)];
1136+
assert_eq!(
1137+
memory_segment_manager
1138+
.get_integer(Relocatable::from((0, 0)))
1139+
.unwrap()
1140+
.as_ref(),
1141+
&Felt252::from(10)
1142+
);
1143+
}
1144+
1145+
#[test]
1146+
fn test_get_relocatable() {
1147+
let mut memory_segment_manager = MemorySegmentManager::new();
1148+
memory_segment_manager.memory = memory![((0, 0), (0, 1))];
1149+
assert_eq!(
1150+
memory_segment_manager
1151+
.get_relocatable(Relocatable::from((0, 0)))
1152+
.unwrap(),
1153+
relocatable!(0, 1)
1154+
);
1155+
}
1156+
1157+
#[test]
1158+
fn test_get_maybe_relocatable() {
1159+
let mut memory_segment_manager = MemorySegmentManager::new();
1160+
memory_segment_manager.memory = memory![((0, 0), (0, 1)), ((0, 1), 10)];
1161+
assert_eq!(
1162+
memory_segment_manager
1163+
.get_maybe_relocatable(relocatable!(0, 0))
1164+
.unwrap(),
1165+
mayberelocatable!(0, 1)
1166+
);
1167+
assert_eq!(
1168+
memory_segment_manager
1169+
.get_maybe_relocatable(relocatable!(0, 1))
1170+
.unwrap(),
1171+
mayberelocatable!(10)
1172+
);
1173+
}
11201174
}

0 commit comments

Comments
 (0)