Expand description
An interrupt/thread-safe memory pool.
The memory pool allows using static, stack or heap memory to store SIZE
instances of T
.
MemoryPool::chunk
provides Chunk
s to interact with instances of T
.
Chunk
is a pointer type, which means it is cheap to move.
This makes the memory pool well suited for moving data between actors without copying.
The memory pool is especially useful for large chunks of data or data that is expensive to move.
Chunk
s are automatically made available for re-use on drop.
Chunk
s can be created by:
MemoryPool::reserve
andMemoryPoolToken::init
, which uses the provided value ofT
to initialize the chunk.MemoryPool::chunk
combines both into a single method call.MemoryPool::reserve
andMemoryPoolToken::init_in_place
to initializeT
in place.
§Example
use veecle_os_runtime::{ExclusiveReader, Writer};
use veecle_os_runtime::memory_pool::{Chunk, MemoryPool};
use core::convert::Infallible;
use veecle_os_runtime::Storable;
#[derive(Debug, Storable)]
#[storable(data_type = "Chunk<'static, u8>")]
pub struct Data;
#[veecle_os_runtime::actor]
async fn exclusive_read_actor(mut reader: ExclusiveReader<'_, Data>) -> Infallible {
loop {
if let Some(chunk) = reader.take() {
println!("Chunk received: {:?}", chunk);
println!("Chunk content: {:?}", *chunk);
} else {
reader.wait_for_update().await;
}
}
}
#[veecle_os_runtime::actor]
async fn write_actor(
mut writer: Writer<'_, Data>,
#[init_context] pool: &'static MemoryPool<u8, 5>,
) -> Infallible {
for index in 0..10 {
writer.write(pool.chunk(index).unwrap()).await;
}
}
static POOL: MemoryPool<u8, 5> = MemoryPool::new();
veecle_os_runtime::execute! {
store: [Data],
actors: [
ExclusiveReadActor,
WriteActor: &POOL,
]
}
Structs§
- Chunk
- A pointer type pointing to an instance of
T
in aMemoryPool
. - Memory
Pool - Interrupt- and thread-safe memory pool.
- Memory
Pool Token - A token reserving an element in a
MemoryPool
which can be initialized to create aChunk
.