Skip to content

Commit

Permalink
modulus: Missed a few places
Browse files Browse the repository at this point in the history
I missed a few places where the hard-coded `if` sequence was
written out.  Also, because it's so redundant, wrap it up into
a little function.
  • Loading branch information
Dan Cross authored and dancrossnyc committed Aug 17, 2023
1 parent 2889fb6 commit 42df8e9
Showing 1 changed file with 19 additions and 22 deletions.
41 changes: 19 additions & 22 deletions drv/stm32h7-eth/src/ring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,12 @@ impl TxRing {
pub fn tail_ptr(&self) -> *const TxDesc {
self.storage.as_ptr_range().end
}

/// Increments the `next` descriptor index, modulo the length of the ring.
fn incr_next(&self) {
let next = self.next.get() + 1;
self.next.set(next % self.storage.len());
}
}

#[cfg(not(feature = "vlan"))]
Expand Down Expand Up @@ -249,11 +255,7 @@ impl TxRing {
| len as u32;
d.tdes[3].store(tdes3, Ordering::Release); // <-- release

self.next.set(if self.next.get() + 1 == self.storage.len() {
0
} else {
self.next.get() + 1
});
self.incr_next();

Some(result)
}
Expand Down Expand Up @@ -337,11 +339,7 @@ impl TxRing {
| len as u32;
d.tdes[1][3].store(tdes3, Ordering::Release); // <-- release

self.next.set(if self.next.get() + 1 == self.storage.len() {
0
} else {
self.next.get() + 1
});
self.incr_next();

Some(result)
}
Expand Down Expand Up @@ -468,6 +466,12 @@ impl RxRing {
self.storage.len()
}

/// Increments the `next` descriptor index, modulo the length of the ring.
fn incr_next(&self) {
let next = self.next.get() + 1;
self.next.set(next % self.storage.len());
}

/// Programs the words in `d` to prepare to receive into `buffer` and sets
/// `d` accessible to hardware. The final write to make it accessible is
/// performed with Release ordering to get a barrier.
Expand Down Expand Up @@ -513,11 +517,8 @@ impl RxRing {
}

// Otherwise, drop the packet by bumping our index
self.next.set(if self.next.get() + 1 == self.storage.len() {
0
} else {
self.next.get() + 1
});
self.incr_next();

any_dropped = true;
}
}
Expand Down Expand Up @@ -588,11 +589,7 @@ impl RxRing {
// potentially in use, and we must not access either.

// Bump index forward.
self.next.set(if self.next.get() + 1 == self.storage.len() {
0
} else {
self.next.get() + 1
});
self.incr_next();

result
}
Expand Down Expand Up @@ -656,7 +653,7 @@ impl RxRing {
Self::set_descriptor(d, buffer);

// Bump index forward.
self.next.set((self.next.get() + 1) % self.storage.len());
self.incr_next();

any_dropped = true;
}
Expand Down Expand Up @@ -723,7 +720,7 @@ impl RxRing {
// potentially in use, and we must not access either.

// Bump index forward.
self.next.set((self.next.get() + 1) % self.storage.len());
self.incr_next();

retval
}
Expand Down

0 comments on commit 42df8e9

Please sign in to comment.