add bark-alloc crate for bark-protocol packet allocation

This commit is contained in:
Hailey Somerville 2023-09-14 17:37:23 +10:00
parent 3e7dbf7726
commit 71bdc833c1
8 changed files with 52 additions and 3 deletions

10
Cargo.lock generated
View file

@ -63,6 +63,7 @@ checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa"
name = "bark"
version = "0.1.0"
dependencies = [
"bark-alloc",
"bark-protocol",
"bitflags 2.4.0",
"bytemuck",
@ -81,10 +82,19 @@ dependencies = [
"xdg",
]
[[package]]
name = "bark-alloc"
version = "0.1.0"
dependencies = [
"bytemuck",
"derive_more",
]
[[package]]
name = "bark-protocol"
version = "0.1.0"
dependencies = [
"bark-alloc",
"bitflags 2.4.0",
"bytemuck",
"derive_more",

View file

@ -2,10 +2,12 @@
resolver = "2"
members = [
"bark",
"bark-alloc",
"bark-protocol",
]
[workspace.dependencies]
bark-alloc = { path = "bark-alloc" }
bark-protocol = { path = "bark-protocol" }
bitflags = { version = "2.4.0", features = ["bytemuck"] }

11
bark-alloc/Cargo.toml Normal file
View file

@ -0,0 +1,11 @@
[package]
name = "bark-alloc"
version = "0.1.0"
edition = "2021"
[features]
alloc = ["bytemuck/extern_crate_alloc"]
[dependencies]
bytemuck = { workspace = true, optional = true }
derive_more = { workspace = true }

24
bark-alloc/src/lib.rs Normal file
View file

@ -0,0 +1,24 @@
#![no_std]
#[cfg(not(feature = "alloc"))]
compile_error!("must enable alloc feature!");
#[cfg(feature = "alloc")]
mod impl_ {
extern crate alloc;
use derive_more::{Deref, DerefMut};
#[repr(transparent)]
#[derive(Deref, DerefMut)]
#[deref(forward)]
pub struct FixedBuffer<const N: usize>(alloc::boxed::Box<[u8]>);
impl<const N: usize> FixedBuffer<N> {
pub fn alloc_zeroed() -> Self {
FixedBuffer(bytemuck::allocation::zeroed_slice_box(N))
}
}
}
pub use impl_::*;

View file

@ -4,6 +4,8 @@ version = "0.1.0"
edition = "2021"
[dependencies]
bark-alloc = { workspace = true }
bitflags = { workspace = true }
bytemuck = { workspace = true }
derive_more = { workspace = true }

View file

@ -1,5 +1,4 @@
#![no_std]
extern crate alloc;
pub mod time;
pub mod types;

View file

@ -18,7 +18,7 @@ pub const MAX_PACKET_SIZE: usize =
size_of::<types::AudioPacketBuffer>();
pub struct PacketBuffer {
raw: alloc::boxed::Box<[u8]>,
raw: bark_alloc::FixedBuffer<MAX_PACKET_SIZE>,
len: usize,
}
@ -31,7 +31,7 @@ impl Debug for PacketBuffer {
impl PacketBuffer {
pub fn allocate() -> Self {
PacketBuffer {
raw: bytemuck::allocation::zeroed_slice_box(MAX_PACKET_SIZE),
raw: bark_alloc::FixedBuffer::alloc_zeroed(),
len: 0,
}
}

View file

@ -6,6 +6,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
bark-alloc = { workspace = true, features = ["alloc"] }
bark-protocol = { workspace = true }
bitflags = { workspace = true }