diff options
author | Dario Nieuwenhuis <dirbaio@dirbaio.net> | 2021-06-07 15:33:17 +0200 |
---|---|---|
committer | alexmoon <alex.r.moon@gmail.com> | 2022-07-14 12:01:41 -0400 |
commit | 4713a76e37e6c0114d47941a31c069ed1813530e (patch) | |
tree | bae48f74865972acb8fb96066212930ffddcf427 /examples/src/bin/ble_l2cap_peripheral.rs | |
parent | d54aceb4fce464f23dcedbff94f659b558124b9c (diff) | |
download | nrf-softdevice-4713a76e37e6c0114d47941a31c069ed1813530e.zip |
No alloc
Diffstat (limited to 'examples/src/bin/ble_l2cap_peripheral.rs')
-rw-r--r-- | examples/src/bin/ble_l2cap_peripheral.rs | 48 |
1 files changed, 31 insertions, 17 deletions
diff --git a/examples/src/bin/ble_l2cap_peripheral.rs b/examples/src/bin/ble_l2cap_peripheral.rs index 23ef03e..baf2389 100644 --- a/examples/src/bin/ble_l2cap_peripheral.rs +++ b/examples/src/bin/ble_l2cap_peripheral.rs @@ -1,9 +1,7 @@ #![no_std] #![no_main] #![feature(type_alias_impl_trait)] -#![feature(alloc_error_handler)] #![allow(incomplete_features)] -extern crate alloc; #[path = "../example_common.rs"] mod example_common; @@ -59,35 +57,51 @@ async fn bluetooth_task(sd: &'static Softdevice) { loop { let pkt = unwrap!(ch.rx().await); - info!("rx: {:x}", pkt.0); + info!("rx: {:x}", pkt.as_bytes()); } } } -use alloc::vec::Vec; +use atomic_pool::{pool, Box}; + +pool!(PacketPool: [[u8; 512]; 10]); + +struct Packet { + len: usize, + buf: Box<PacketPool>, +} + +impl Packet { + fn as_bytes(&self) -> &[u8] { + &self.buf[..self.len] + } +} -struct Packet(Vec<u8>); impl l2cap::Packet for Packet { const MTU: usize = 512; fn allocate() -> Option<NonNull<u8>> { - let mut v = Vec::with_capacity(Self::MTU); - let ptr = v.as_mut_ptr(); - mem::forget(v); - info!("allocate {}", ptr as u32); - NonNull::new(ptr) + if let Some(buf) = Box::<PacketPool>::new([0; 512]) { + let ptr = Box::into_raw(buf).cast::<u8>(); + info!("allocate {}", ptr.as_ptr() as u32); + Some(ptr) + } else { + None + } } - fn into_raw_parts(mut self) -> (NonNull<u8>, usize) { - let ptr = self.0.as_mut_ptr(); - let len = self.0.len(); - mem::forget(self); - info!("into_raw_parts {}", ptr as u32); - (unwrap!(NonNull::new(ptr)), len) + fn into_raw_parts(self) -> (NonNull<u8>, usize) { + let ptr = Box::into_raw(self.buf).cast::<u8>(); + let len = self.len; + info!("into_raw_parts {}", ptr.as_ptr() as u32); + (ptr, len) } unsafe fn from_raw_parts(ptr: NonNull<u8>, len: usize) -> Self { info!("from_raw_parts {}", ptr.as_ptr() as u32); - Self(Vec::from_raw_parts(ptr.as_ptr(), len, Self::MTU)) + Self { + len, + buf: Box::from_raw(ptr.cast::<[u8; 512]>()), + } } } |