Remove implicit bounds checking from rcc module

This commit is contained in:
David Flemström 2024-06-28 21:10:41 +02:00
parent cbc67469d3
commit 73d937dc33

View File

@ -138,12 +138,18 @@ impl RccInfo {
pub(crate) fn enable_and_reset_with_cs(&self, _cs: CriticalSection) {
if self.refcount_idx_or_0xff != 0xff {
let refcount_idx = self.refcount_idx_or_0xff as usize;
unsafe {
crate::_generated::REFCOUNTS[refcount_idx] += 1;
}
if unsafe { crate::_generated::REFCOUNTS[refcount_idx] } > 1 {
// Use .get_mut instead of []-operator so that we control how bounds checks happen.
// Otherwise, core::fmt will be pulled in here in order to format the integer in the
// out-of-bounds error.
if let Some(refcount) = unsafe { crate::_generated::REFCOUNTS }.get_mut(refcount_idx) {
*refcount += 1;
if *refcount > 1 {
return;
}
} else {
panic!("refcount_idx out of bounds: {}", refcount_idx)
}
}
#[cfg(feature = "low-power")]
@ -196,13 +202,17 @@ impl RccInfo {
pub(crate) fn disable_with_cs(&self, _cs: CriticalSection) {
if self.refcount_idx_or_0xff != 0xff {
let refcount_idx = self.refcount_idx_or_0xff as usize;
unsafe {
crate::_generated::REFCOUNTS[refcount_idx] -= 1;
}
if unsafe { crate::_generated::REFCOUNTS[refcount_idx] } > 0 {
// Use .get_mut instead of []-operator so that we control how bounds checks happen.
// Otherwise, core::fmt will be pulled in here in order to format the integer in the
// out-of-bounds error.
if let Some(refcount) = unsafe { crate::_generated::REFCOUNTS }.get_mut(refcount_idx) {
*refcount -= 1;
if *refcount > 0 {
return;
}
}
}
#[cfg(feature = "low-power")]
match self.stop_mode {