Module memory_pool

Source
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 Chunks 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.

Chunks are automatically made available for re-use on drop.

Chunks can be created by:

§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 a MemoryPool.
MemoryPool
Interrupt- and thread-safe memory pool.
MemoryPoolToken
A token reserving an element in a MemoryPool which can be initialized to create a Chunk.