Skip to content

Commit

Permalink
Merge #22
Browse files Browse the repository at this point in the history
22: Make the creation of TxRingEntry and RxRingEntry const r=adamgreig a=thalesfragoso

This allow for people to create `RingEntry` on a const context, which saves a considerable amount of stack during initialization if having them in a `static`.

I also left the non const `Default` implementations for compatibility and also because of rust-lang/rust#49147, which causes the creation of entries a bit cumbersome since `Aligned` isn't `Copy`, so users can use `default` on non const contexts if they want to.

CC @adamgreig 

Co-authored-by: Thales Fragoso <[email protected]>
  • Loading branch information
bors[bot] and thalesfragoso authored Aug 29, 2020
2 parents f8faf0b + 2744d83 commit 4d6b29b
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 22 deletions.
10 changes: 7 additions & 3 deletions src/desc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,17 @@ impl Clone for Descriptor {

impl Default for Descriptor {
fn default() -> Self {
Descriptor {
desc: Aligned([0; 4]),
}
Self::new()
}
}

impl Descriptor {
pub const fn new() -> Self {
Self {
desc: Aligned([0; 4]),
}
}

fn r(&self, n: usize) -> &RO<u32> {
let ro = &self.desc.deref()[n] as *const _ as *const RO<u32>;
unsafe { &*ro }
Expand Down
8 changes: 4 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ mod smi;
pub use ring::RingEntry;
mod desc;
mod rx;
pub use rx::{RxDescriptor, RxError};
use rx::{RxPacket, RxRing, RxRingEntry};
pub use rx::{RxDescriptor, RxError, RxRingEntry};
use rx::{RxPacket, RxRing};
mod tx;
pub use tx::{TxDescriptor, TxError};
use tx::{TxRing, TxRingEntry};
use tx::TxRing;
pub use tx::{TxDescriptor, TxError, TxRingEntry};
pub mod setup;
pub use setup::EthPins;
use setup::{
Expand Down
26 changes: 21 additions & 5 deletions src/ring.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use aligned::{Aligned, A8};
use core::ops::{Deref, DerefMut};

use crate::MTU;
use crate::{RxDescriptor, TxDescriptor, MTU};

pub trait RingDescriptor {
fn setup(&mut self, buffer: *const u8, len: usize, next: Option<&Self>);
Expand All @@ -23,18 +23,34 @@ impl<T: Clone + RingDescriptor> Clone for RingEntry<T> {

impl<T: Clone + RingDescriptor + Default> Default for RingEntry<T> {
fn default() -> Self {
Self::new()
RingEntry {
desc: Aligned([T::default()]),
buffer: Aligned([0; MTU]),
}
}
}

impl<T: Clone + RingDescriptor + Default> RingEntry<T> {
pub fn new() -> Self {
impl RingEntry<TxDescriptor> {
/// Creates a RingEntry with a TxDescriptor.
pub const fn new() -> Self {
RingEntry {
desc: Aligned([T::default()]),
desc: Aligned([TxDescriptor::new()]),
buffer: Aligned([0; MTU]),
}
}
}

impl RingEntry<RxDescriptor> {
/// Creates a RingEntry with a RxDescriptor.
pub const fn new() -> Self {
RingEntry {
desc: Aligned([RxDescriptor::new()]),
buffer: Aligned([0; MTU]),
}
}
}

impl<T: Clone + RingDescriptor> RingEntry<T> {
pub(crate) fn setup(&mut self, next: Option<&Self>) {
let buffer = self.buffer.as_ptr();
let len = self.buffer.len();
Expand Down
17 changes: 12 additions & 5 deletions src/rx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,18 @@ pub struct RxDescriptor {

impl Default for RxDescriptor {
fn default() -> Self {
let mut desc = Descriptor::default();
unsafe {
desc.write(1, RXDESC_1_RCH);
}
RxDescriptor { desc }
Self::new()
}
}

impl RxDescriptor {
/// Creates an zeroed RxDescriptor.
pub const fn new() -> Self {
Self {
desc: Descriptor::new(),
}
}

/// Is owned by the DMA engine?
fn is_owned(&self) -> bool {
(self.desc.read(0) & RXDESC_0_OWN) == RXDESC_0_OWN
Expand Down Expand Up @@ -126,6 +129,10 @@ pub type RxRingEntry = RingEntry<RxDescriptor>;

impl RingDescriptor for RxDescriptor {
fn setup(&mut self, buffer: *const u8, len: usize, next: Option<&Self>) {
// Defer this initialization to this function, so we can have `RingEntry` on bss.
unsafe {
self.desc.write(1, RXDESC_1_RCH);
}
self.set_buffer1(buffer, len);
match next {
Some(next) => self.set_buffer2(&next.desc as *const Descriptor as *const u8),
Expand Down
18 changes: 13 additions & 5 deletions src/tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,18 @@ pub struct TxDescriptor {

impl Default for TxDescriptor {
fn default() -> Self {
let mut desc = Descriptor::default();
unsafe {
desc.write(0, TXDESC_0_TCH | TXDESC_0_IC | TXDESC_0_FS | TXDESC_0_LS);
}
TxDescriptor { desc }
Self::new()
}
}

impl TxDescriptor {
/// Creates an zeroed TxDescriptor.
pub const fn new() -> Self {
Self {
desc: Descriptor::new(),
}
}

/// Is owned by the DMA engine?
fn is_owned(&self) -> bool {
(self.desc.read(0) & TXDESC_0_OWN) == TXDESC_0_OWN
Expand Down Expand Up @@ -115,6 +118,11 @@ pub type TxRingEntry = RingEntry<TxDescriptor>;

impl RingDescriptor for TxDescriptor {
fn setup(&mut self, buffer: *const u8, _len: usize, next: Option<&Self>) {
// Defer this initialization to this function, so we can have `RingEntry` on bss.
unsafe {
self.desc
.write(0, TXDESC_0_TCH | TXDESC_0_IC | TXDESC_0_FS | TXDESC_0_LS);
}
self.set_buffer1(buffer);
match next {
Some(next) => self.set_buffer2(&next.desc as *const Descriptor as *const u8),
Expand Down

0 comments on commit 4d6b29b

Please sign in to comment.