Skip to content

Commit 9cc8dac

Browse files
committed
sim: Put AreaDesc in an Rc
Since there are references to this struct passed to C code, put it into an Rc so that it won't move around when the data is moved. Signed-off-by: David Brown <[email protected]>
1 parent 5d15513 commit 9cc8dac

File tree

1 file changed

+11
-14
lines changed

1 file changed

+11
-14
lines changed

sim/src/image.rs

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,7 @@ use rand::{
1919
rngs::SmallRng,
2020
};
2121
use std::{
22-
collections::{BTreeMap, HashSet},
23-
io::{Cursor, Write},
24-
mem,
25-
slice,
22+
collections::{BTreeMap, HashSet}, io::{Cursor, Write}, mem, rc::Rc, slice
2623
};
2724
use aes::{
2825
Aes128,
@@ -67,7 +64,7 @@ const RAM_LOAD_ADDR: u32 = 1024;
6764
#[derive(Clone)]
6865
pub struct ImagesBuilder {
6966
flash: SimMultiFlash,
70-
areadesc: AreaDesc,
67+
areadesc: Rc<AreaDesc>,
7168
slots: Vec<[SlotInfo; 2]>,
7269
ram: RamData,
7370
}
@@ -77,7 +74,7 @@ pub struct ImagesBuilder {
7774
/// and upgrades hold the expected contents of these images.
7875
pub struct Images {
7976
flash: SimMultiFlash,
80-
areadesc: AreaDesc,
77+
areadesc: Rc<AreaDesc>,
8178
images: Vec<OneImage>,
8279
total_count: Option<i32>,
8380
ram: RamData,
@@ -433,7 +430,7 @@ impl ImagesBuilder {
433430
}
434431

435432
/// Build the Flash and area descriptor for a given device.
436-
pub fn make_device(device: DeviceName, align: usize, erased_val: u8) -> (SimMultiFlash, AreaDesc, &'static [Caps]) {
433+
pub fn make_device(device: DeviceName, align: usize, erased_val: u8) -> (SimMultiFlash, Rc<AreaDesc>, &'static [Caps]) {
437434
match device {
438435
DeviceName::Stm32f4 => {
439436
// STM style flash. Large sectors, with a large scratch area.
@@ -454,7 +451,7 @@ impl ImagesBuilder {
454451

455452
let mut flash = SimMultiFlash::new();
456453
flash.insert(dev_id, dev);
457-
(flash, areadesc, &[Caps::SwapUsingMove])
454+
(flash, Rc::new(areadesc), &[Caps::SwapUsingMove])
458455
}
459456
DeviceName::K64f => {
460457
// NXP style flash. Small sectors, one small sector for scratch.
@@ -469,7 +466,7 @@ impl ImagesBuilder {
469466

470467
let mut flash = SimMultiFlash::new();
471468
flash.insert(dev_id, dev);
472-
(flash, areadesc, &[])
469+
(flash, Rc::new(areadesc), &[])
473470
}
474471
DeviceName::K64fBig => {
475472
// Simulating an STM style flash on top of an NXP style flash. Underlying flash device
@@ -485,7 +482,7 @@ impl ImagesBuilder {
485482

486483
let mut flash = SimMultiFlash::new();
487484
flash.insert(dev_id, dev);
488-
(flash, areadesc, &[Caps::SwapUsingMove])
485+
(flash, Rc::new(areadesc), &[Caps::SwapUsingMove])
489486
}
490487
DeviceName::Nrf52840 => {
491488
// Simulating the flash on the nrf52840 with partitions set up so that the scratch size
@@ -501,7 +498,7 @@ impl ImagesBuilder {
501498

502499
let mut flash = SimMultiFlash::new();
503500
flash.insert(dev_id, dev);
504-
(flash, areadesc, &[])
501+
(flash, Rc::new(areadesc), &[])
505502
}
506503
DeviceName::Nrf52840UnequalSlots => {
507504
let dev = SimFlash::new(vec![4096; 128], align as usize, erased_val);
@@ -514,7 +511,7 @@ impl ImagesBuilder {
514511

515512
let mut flash = SimMultiFlash::new();
516513
flash.insert(dev_id, dev);
517-
(flash, areadesc, &[Caps::SwapUsingScratch, Caps::OverwriteUpgrade])
514+
(flash, Rc::new(areadesc), &[Caps::SwapUsingScratch, Caps::OverwriteUpgrade])
518515
}
519516
DeviceName::Nrf52840SpiFlash => {
520517
// Simulate nrf52840 with external SPI flash. The external SPI flash
@@ -533,7 +530,7 @@ impl ImagesBuilder {
533530
let mut flash = SimMultiFlash::new();
534531
flash.insert(0, dev0);
535532
flash.insert(1, dev1);
536-
(flash, areadesc, &[Caps::SwapUsingMove])
533+
(flash, Rc::new(areadesc), &[Caps::SwapUsingMove])
537534
}
538535
DeviceName::K64fMulti => {
539536
// NXP style flash, but larger, to support multiple images.
@@ -550,7 +547,7 @@ impl ImagesBuilder {
550547

551548
let mut flash = SimMultiFlash::new();
552549
flash.insert(dev_id, dev);
553-
(flash, areadesc, &[])
550+
(flash, Rc::new(areadesc), &[])
554551
}
555552
}
556553
}

0 commit comments

Comments
 (0)