Start adding stack-allocation helper functions. (ext.rs)

Fortune for rsh's current commit: Half curse − 半凶
specialisation
Avril 3 years ago
parent 4877a843bd
commit 226901861a
Signed by: flanchan
GPG Key ID: 284488987C31F630

11
Cargo.lock generated

@ -780,6 +780,7 @@ dependencies = [
"pin-project", "pin-project",
"serde", "serde",
"serde_cbor", "serde_cbor",
"stackalloc",
"tokio 0.2.25", "tokio 0.2.25",
"tokio-uring", "tokio-uring",
"uuid", "uuid",
@ -894,6 +895,16 @@ version = "1.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fe0f37c9e8f3c5a4a66ad655a93c74daac4ad00c441533bf5c6e7990bb42604e" checksum = "fe0f37c9e8f3c5a4a66ad655a93c74daac4ad00c441533bf5c6e7990bb42604e"
[[package]]
name = "stackalloc"
version = "1.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a4f5c9dd3feb8a4adc8eae861e5f48862a92f9a9f38cf8fc99b92fc6ec016121"
dependencies = [
"cc",
"rustc_version",
]
[[package]] [[package]]
name = "subtle" name = "subtle"
version = "2.4.1" version = "2.4.1"

@ -12,6 +12,7 @@ cryptohelpers = { version = "1.8", features = ["serialise", "full"] }
pin-project = "1.0.8" pin-project = "1.0.8"
serde = { version = "1.0.126", features = ["derive"] } serde = { version = "1.0.126", features = ["derive"] }
serde_cbor = "0.11.1" serde_cbor = "0.11.1"
stackalloc = "1.1.1"
tokio = { version = "0.2", features = ["full"] } tokio = { version = "0.2", features = ["full"] }
tokio-uring = "0.1.0" tokio-uring = "0.1.0"
uuid = { version = "0.8.2", features = ["v4", "serde"] } uuid = { version = "0.8.2", features = ["v4", "serde"] }

@ -0,0 +1,38 @@
//! Extensions
use super::*;
use std::mem::{self, MaybeUninit};
use std::iter;
/// Max size of memory allowed to be allocated on the stack.
pub const STACK_MEM_ALLOC_MAX: usize = 4096;
pub fn vec_uninit<T>(sz: usize) -> Vec<MaybeUninit<T>>
{
let mut mem: Vec<T> = Vec::with_capacity(sz);
unsafe {
mem.set_len(sz);
mem::transmute(mem)
}
}
/// Allocate a local buffer initialised with `init`.
pub fn alloc_local_with<T, U>(sz: usize, init: impl FnMut() -> T, within: impl FnOnce(&mut [T]) -> U) -> U
{
if sz > STACK_MEM_ALLOC_MAX {
let mut memory: Vec<T> = iter::repeat_with(init).take(sz).collect();
within(&mut memory[..])
} else {
stackalloc::stackalloc_with(sz, init, within)
}
}
/// Allocate a local buffer initialised with `init`.
pub fn alloc_local<T: Clone, U>(sz: usize, init: T, within: impl FnOnce(&mut [T]) -> U) -> U
{
if sz > STACK_MEM_ALLOC_MAX {
let mut memory: Vec<T> = iter::repeat(init).take(sz).collect();
within(&mut memory[..])
} else {
stackalloc::stackalloc(sz, init, within)
}
}

@ -19,6 +19,7 @@ use std::convert::{
TryInto, TryInto,
}; };
mod ext; use ext::*;
mod message; mod message;
#[tokio::main] #[tokio::main]

Loading…
Cancel
Save