1247: `PacketQueue::init()` r=davidedellagiustina a=davidedellagiustina

`PacketQueue` is pretty big, so I added a method to initialize it without requiring an allocation on the stack (which could in fact overflow). Before this PR, the only solution would be to declare a `PacketQueue` instance as a `static mut`, while now one could for example have a `Box<MaybeUninit<PacketQueue<...>>>` and then use `init()` on it.

Ideally, the same work would need to be done for all those structures that own big arrays which could overflow the stack.

Co-authored-by: Davide Della Giustina <davide@dellagiustina.com>
This commit is contained in:
bors[bot] 2023-02-28 18:26:37 +00:00 committed by GitHub
commit b16b3b0dbb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -5,6 +5,7 @@
mod _version;
pub mod generic_smi;
use core::mem::MaybeUninit;
use core::task::Context;
use embassy_net_driver::{Capabilities, LinkState};
@ -39,6 +40,13 @@ impl<const TX: usize, const RX: usize> PacketQueue<TX, RX> {
rx_buf: [Packet([0; RX_BUFFER_SIZE]); RX],
}
}
// Allow to initialize a Self without requiring it to go on the stack
pub fn init(this: &mut MaybeUninit<Self>) {
unsafe {
this.as_mut_ptr().write_bytes(0u8, core::mem::size_of::<Self>());
}
}
}
static WAKER: AtomicWaker = AtomicWaker::new();