From faa867e63ded8e034e65aaf6179283a7bc2cba4b Mon Sep 17 00:00:00 2001 From: bunnie Date: Sun, 28 Jan 2024 17:08:04 +0800 Subject: [PATCH] fix bug in page alignment logic size, you can do this one liner: let aligned_size = ((unasigned_size + alignment_quanta - 1) / (alignment_quanta)) * alignment_quanta; before i was doing this with a `let mut` + `if` statement. this is not as obvious but more compact, probably you'll start to see this idiom more and more in the code. I think it's also computationally efficient, because as long as the alignment_quanta is a power of 2, the compiler does something like a shift right with the `/` operator (and it may also do something like a shift left with the `*` operator as well). --- libs/cramium-hal/src/ifram.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/libs/cramium-hal/src/ifram.rs b/libs/cramium-hal/src/ifram.rs index 54407d8da..9f32d907f 100644 --- a/libs/cramium-hal/src/ifram.rs +++ b/libs/cramium-hal/src/ifram.rs @@ -63,10 +63,8 @@ impl IframRange { match send_message(conn, Message::new_blocking_scalar(0 /* MapIram */, length, bank_code, 0, 0)) { Ok(Result::Scalar5(_, maybe_size, maybe_phys_address, _, _)) => { if maybe_size != 0 && maybe_phys_address != 0 { - let mut page_aligned_size = maybe_size / 4096; - if maybe_size % 4096 != 0 { - page_aligned_size += 1; - } + // round up to the nearest whole page + let page_aligned_size = ((maybe_size + 4095) / 4096) * 4096; let virtual_pages = xous::map_memory( core::num::NonZeroUsize::new(maybe_phys_address), None,