Skip to content

Commit d7ee51c

Browse files
committed
[flash] Eliminate the mostly useless Ptr type
Signed-off-by: Miguel Young de la Sota <[email protected]>
1 parent 3e4663e commit d7ee51c

File tree

2 files changed

+41
-71
lines changed

2 files changed

+41
-71
lines changed

src/hardware/flash.rs

Lines changed: 38 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ pub unsafe trait Flash {
7777
fn size(&self) -> Result<u32, Error>;
7878

7979
/// Attempts to read `out.len()` bytes starting at `offset`.
80-
fn read(&self, offset: Ptr, out: &mut [u8]) -> Result<(), Error>;
80+
fn read(&self, offset: u32, out: &mut [u8]) -> Result<(), Error>;
8181

8282
/// Attempts to perform a "direct read" of the given `Region`.
8383
///
@@ -100,7 +100,7 @@ pub unsafe trait Flash {
100100
/// # use manticore::hardware::flash::*;
101101
/// # struct Foo;
102102
/// # impl Foo {
103-
/// # fn read(&self, offset: Ptr, out: &mut [u8]) -> Result<(), Error> {
103+
/// # fn read(&self, offset: u32, out: &mut [u8]) -> Result<(), Error> {
104104
/// # Ok(())
105105
/// # }
106106
/// fn read_direct<'a: 'c, 'b: 'c, 'c>(
@@ -110,7 +110,7 @@ pub unsafe trait Flash {
110110
/// align: usize,
111111
/// ) -> Result<&'c [u8], Error> {
112112
/// let mut buf = arena.alloc_aligned(region.len as usize, align)?;
113-
/// self.read(region.ptr, &mut buf)?;
113+
/// self.read(region.offset, &mut buf)?;
114114
/// Ok(buf)
115115
/// }
116116
/// # }
@@ -135,7 +135,7 @@ pub unsafe trait Flash {
135135
/// Implementations are, as an optimization, permitted to assume that
136136
/// writes will be serial and localized, so as to minimize clearing
137137
/// operations on flash hardware.
138-
fn program(&mut self, offset: Ptr, buf: &[u8]) -> Result<(), Error>;
138+
fn program(&mut self, offset: u32, buf: &[u8]) -> Result<(), Error>;
139139

140140
/// Flushes any pending `program()` operations.
141141
fn flush(&mut self) -> Result<(), Error> {
@@ -151,7 +151,7 @@ unsafe impl<F: Flash> Flash for &F {
151151
}
152152

153153
#[inline]
154-
fn read(&self, offset: Ptr, out: &mut [u8]) -> Result<(), Error> {
154+
fn read(&self, offset: u32, out: &mut [u8]) -> Result<(), Error> {
155155
F::read(self, offset, out)
156156
}
157157

@@ -166,7 +166,7 @@ unsafe impl<F: Flash> Flash for &F {
166166
}
167167

168168
#[inline]
169-
fn program(&mut self, _: Ptr, _: &[u8]) -> Result<(), Error> {
169+
fn program(&mut self, _: u32, _: &[u8]) -> Result<(), Error> {
170170
Err(Error::Locked)
171171
}
172172

@@ -183,7 +183,7 @@ unsafe impl<F: Flash> Flash for &mut F {
183183
}
184184

185185
#[inline]
186-
fn read(&self, offset: Ptr, out: &mut [u8]) -> Result<(), Error> {
186+
fn read(&self, offset: u32, out: &mut [u8]) -> Result<(), Error> {
187187
F::read(self, offset, out)
188188
}
189189

@@ -198,7 +198,7 @@ unsafe impl<F: Flash> Flash for &mut F {
198198
}
199199

200200
#[inline]
201-
fn program(&mut self, offset: Ptr, buf: &[u8]) -> Result<(), Error> {
201+
fn program(&mut self, offset: u32, buf: &[u8]) -> Result<(), Error> {
202202
F::program(self, offset, buf)
203203
}
204204

@@ -218,7 +218,7 @@ pub trait FlashExt<'flash> {
218218
/// See [`ArenaExt::alloc()`](../../mem/arena/trait.ArenaExt.html#tymethod.alloc).
219219
fn read_object<'b: 'c, 'c, T>(
220220
self,
221-
offset: Ptr,
221+
offset: u32,
222222
arena: &'b dyn Arena,
223223
) -> Result<&'c T, Error>
224224
where
@@ -230,7 +230,7 @@ pub trait FlashExt<'flash> {
230230
/// See [`ArenaExt::alloc_slice()`](../../mem/arena/trait.ArenaExt.html#tymethod.alloc_slice).
231231
fn read_slice<'b: 'c, 'c, T>(
232232
self,
233-
offset: Ptr,
233+
offset: u32,
234234
n: usize,
235235
arena: &'b dyn Arena,
236236
) -> Result<&'c [T], Error>
@@ -242,15 +242,15 @@ pub trait FlashExt<'flash> {
242242
impl<'flash, F: Flash> FlashExt<'flash> for &'flash F {
243243
fn read_object<'b: 'c, 'c, T>(
244244
self,
245-
offset: Ptr,
245+
offset: u32,
246246
arena: &'b dyn Arena,
247247
) -> Result<&'c T, Error>
248248
where
249249
'flash: 'c,
250250
T: AsBytes + FromBytes + Copy,
251251
{
252252
let bytes = self.read_direct(
253-
Region::new(offset.address, mem::size_of::<T>() as u32),
253+
Region::new(offset, mem::size_of::<T>() as u32),
254254
arena,
255255
mem::align_of::<T>(),
256256
)?;
@@ -262,7 +262,7 @@ impl<'flash, F: Flash> FlashExt<'flash> for &'flash F {
262262

263263
fn read_slice<'b: 'c, 'c, T>(
264264
self,
265-
offset: Ptr,
265+
offset: u32,
266266
n: usize,
267267
arena: &'b dyn Arena,
268268
) -> Result<&'c [T], Error>
@@ -273,7 +273,7 @@ impl<'flash, F: Flash> FlashExt<'flash> for &'flash F {
273273
let bytes_requested =
274274
stride_of::<T>().checked_mul(n).ok_or(OutOfMemory)?;
275275
let bytes = self.read_direct(
276-
Region::new(offset.address, bytes_requested as u32),
276+
Region::new(offset, bytes_requested as u32),
277277
arena,
278278
mem::align_of::<T>(),
279279
)?;
@@ -303,9 +303,9 @@ unsafe impl<Bytes: AsRef<[u8]>> Flash for Ram<Bytes> {
303303
}
304304

305305
#[inline]
306-
fn read(&self, offset: Ptr, out: &mut [u8]) -> Result<(), Error> {
306+
fn read(&self, offset: u32, out: &mut [u8]) -> Result<(), Error> {
307307
out.copy_from_slice(self.read_direct(
308-
Region::new(offset.address, out.len() as u32),
308+
Region::new(offset, out.len() as u32),
309309
&OutOfMemory,
310310
1,
311311
)?);
@@ -318,7 +318,7 @@ unsafe impl<Bytes: AsRef<[u8]>> Flash for Ram<Bytes> {
318318
arena: &'b dyn Arena,
319319
align: usize,
320320
) -> Result<&'c [u8], Error> {
321-
let start = region.ptr.address as usize;
321+
let start = region.offset as usize;
322322
let end = start
323323
.checked_add(region.len as usize)
324324
.ok_or(Error::OutOfRange)?;
@@ -337,7 +337,7 @@ unsafe impl<Bytes: AsRef<[u8]>> Flash for Ram<Bytes> {
337337
Ok(buf)
338338
}
339339

340-
fn program(&mut self, _: Ptr, _: &[u8]) -> Result<(), Error> {
340+
fn program(&mut self, _: u32, _: &[u8]) -> Result<(), Error> {
341341
return Err(Error::Locked);
342342
}
343343
}
@@ -361,9 +361,9 @@ unsafe impl<Bytes: AsRef<[u8]> + AsMut<[u8]>> Flash for RamMut<Bytes> {
361361
}
362362

363363
#[inline]
364-
fn read(&self, offset: Ptr, out: &mut [u8]) -> Result<(), Error> {
364+
fn read(&self, offset: u32, out: &mut [u8]) -> Result<(), Error> {
365365
out.copy_from_slice(self.read_direct(
366-
Region::new(offset.address, out.len() as u32),
366+
Region::new(offset, out.len() as u32),
367367
&OutOfMemory,
368368
1,
369369
)?);
@@ -376,7 +376,7 @@ unsafe impl<Bytes: AsRef<[u8]> + AsMut<[u8]>> Flash for RamMut<Bytes> {
376376
arena: &'b dyn Arena,
377377
align: usize,
378378
) -> Result<&'c [u8], Error> {
379-
let start = region.ptr.address as usize;
379+
let start = region.offset as usize;
380380
let end = start
381381
.checked_add(region.len as usize)
382382
.ok_or(Error::OutOfRange)?;
@@ -395,8 +395,8 @@ unsafe impl<Bytes: AsRef<[u8]> + AsMut<[u8]>> Flash for RamMut<Bytes> {
395395
Ok(buf)
396396
}
397397

398-
fn program(&mut self, offset: Ptr, buf: &[u8]) -> Result<(), Error> {
399-
let start = offset.address as usize;
398+
fn program(&mut self, offset: u32, buf: &[u8]) -> Result<(), Error> {
399+
let start = offset as usize;
400400
let end = start.checked_add(buf.len()).ok_or(Error::OutOfRange)?;
401401
if end > self.0.as_ref().len() {
402402
return Err(Error::OutOfRange);
@@ -451,7 +451,7 @@ impl<F: Flash> FlashIo<F> {
451451
/// Adapts this `FlashIo` to only read bytes out from the selected
452452
/// `Region`.
453453
pub fn reslice(&mut self, region: Region) {
454-
self.cursor = region.ptr.address;
454+
self.cursor = region.offset;
455455
self.len = region.end();
456456
}
457457
}
@@ -463,7 +463,7 @@ impl<F: Flash> io::Read for FlashIo<F> {
463463
}
464464

465465
self.flash
466-
.read(Ptr::new(self.cursor), out)
466+
.read(self.cursor, out)
467467
.map_err(|_| io::Error::Internal)?;
468468
self.cursor += out.len() as u32;
469469
Ok(())
@@ -482,7 +482,7 @@ impl<F: Flash> Iterator for FlashIo<F> {
482482
}
483483

484484
let mut byte = [0];
485-
if let Err(e) = self.flash.read(Ptr::new(self.cursor), &mut byte) {
485+
if let Err(e) = self.flash.read(self.cursor, &mut byte) {
486486
return Some(Err(e));
487487
}
488488
self.cursor += 1;
@@ -497,61 +497,34 @@ impl<F: Flash> io::Write for FlashIo<F> {
497497
}
498498

499499
self.flash
500-
.program(Ptr::new(self.cursor), buf)
500+
.program(self.cursor, buf)
501501
.map_err(|_| io::Error::Internal)?;
502502
self.cursor += buf.len() as u32;
503503
Ok(())
504504
}
505505
}
506506

507-
/// An abstract pointer into a [`Flash`] type.
507+
/// A region within a [`Flash`] type.
508508
///
509-
/// A `Ptr` needs to be used in conjunction with a [`Flash`]
510-
/// implementation to be read from or written to.
509+
/// A `Region` needs to be interpreted with respect to a [`Flash`]
510+
/// implementation; it is otherwise a dumb pointer-length pair.
511511
///
512512
/// [`Flash`]: trait.Flash.html
513513
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, AsBytes, FromBytes)]
514-
#[repr(transparent)]
515-
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
516-
pub struct Ptr {
517-
/// The abstract address of this pointer.
518-
pub address: u32,
519-
}
520-
521-
impl Ptr {
522-
/// Convenience method for creating a `Ptr` without having to use
523-
/// a struct literal.
524-
pub const fn new(address: u32) -> Self {
525-
Self { address }
526-
}
527-
}
528-
529-
/// A region within a [`Flash`] type.
530-
///
531-
/// Much like a [`Ptr`], a `Region` needs to be interpreted with
532-
/// respect to a [`Flash`] implementation.
533-
///
534-
/// [`Flash`]: trait.Flash.html
535-
/// [`Ptr`]: struct.Ptr.html
536-
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, AsBytes, FromBytes)]
537514
#[repr(C)]
538515
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
539516
pub struct Region {
540517
/// The base pointer for this slice.
541-
#[cfg_attr(feature = "serde", serde(flatten))]
542-
pub ptr: Ptr,
518+
pub offset: u32,
543519
/// The length of the slice, in bytes.
544520
pub len: u32,
545521
}
546522

547523
impl Region {
548524
/// Convenience method for creating a `Region` without having to use
549525
/// a struct literal.
550-
pub const fn new(ptr: u32, len: u32) -> Self {
551-
Self {
552-
ptr: Ptr::new(ptr),
553-
len,
554-
}
526+
pub const fn new(offset: u32, len: u32) -> Self {
527+
Self { offset, len }
555528
}
556529

557530
/// Returns a `Region` big enough to hold a `T`.
@@ -569,7 +542,7 @@ impl Region {
569542

570543
/// Returns the end address of `self`, pointing one past the end of it.
571544
pub fn end(self) -> u32 {
572-
self.ptr.address.saturating_add(self.len)
545+
self.offset.saturating_add(self.len)
573546
}
574547

575548
/// Returns a new `Region` that comes immediately after `self`, with the
@@ -583,14 +556,11 @@ impl Region {
583556
///
584557
/// Returns `None` if `sub` is not a subregion of `self`.
585558
pub fn subregion(self, sub: Region) -> Option<Self> {
586-
if sub.len.saturating_add(sub.ptr.address) > self.len {
559+
if sub.len.saturating_add(sub.offset) > self.len {
587560
return None;
588561
}
589562

590-
Some(Region::new(
591-
self.ptr.address.checked_add(sub.ptr.address)?,
592-
sub.len,
593-
))
563+
Some(Region::new(self.offset.checked_add(sub.offset)?, sub.len))
594564
}
595565

596566
/// Contracts `self` by dropping the first `n` bytes.
@@ -599,7 +569,7 @@ impl Region {
599569
/// occurs.
600570
pub fn skip(self, n: u32) -> Option<Self> {
601571
Some(Region::new(
602-
self.ptr.address.checked_add(n)?,
572+
self.offset.checked_add(n)?,
603573
self.len.checked_sub(n)?,
604574
))
605575
}
@@ -608,6 +578,6 @@ impl Region {
608578
///
609579
/// Returns `None` if `n` is greater than `self.len`.
610580
pub fn take(self, n: u32) -> Option<Self> {
611-
Some(Region::new(self.ptr.address, self.len.checked_sub(n)?))
581+
Some(Region::new(self.offset, self.len.checked_sub(n)?))
612582
}
613583
}

src/manifest/fpm.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ impl<'m, Provenance> Fpm<'m, Provenance> {
255255
pub fn unparse(&self, mut out: impl io::Write) -> Result<(), Error> {
256256
out.write_le(self.versions.len() as u32)?;
257257
for version in self.versions() {
258-
out.write_le(version.version_region.ptr.address)?;
258+
out.write_le(version.version_region.offset)?;
259259

260260
let signed_len: u16 = version
261261
.signed_region
@@ -288,11 +288,11 @@ impl<'m, Provenance> Fpm<'m, Provenance> {
288288
}
289289

290290
for slice in version.signed_region.iter() {
291-
out.write_le(slice.ptr.address)?;
291+
out.write_le(slice.offset)?;
292292
out.write_le(slice.len)?;
293293
}
294294
for slice in version.write_region.iter() {
295-
out.write_le(slice.ptr.address)?;
295+
out.write_le(slice.offset)?;
296296
out.write_le(slice.len)?;
297297
}
298298
out.write_bytes(&*version.signed_region_hash)?;

0 commit comments

Comments
 (0)