zenoh_shm/api/provider/shm_provider_backend.rs
1//
2// Copyright (c) 2023 ZettaScale Technology
3//
4// This program and the accompanying materials are made available under the
5// terms of the Eclipse Public License 2.0 which is available at
6// http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
7// which is available at https://www.apache.org/licenses/LICENSE-2.0.
8//
9// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
10//
11// Contributors:
12// ZettaScale Zenoh Team, <zenoh@zettascale.tech>
13//
14use super::{
15 chunk::ChunkDescriptor,
16 types::{ChunkAllocResult, ZLayoutError},
17};
18use crate::api::{common::with_id::WithProtocolID, provider::memory_layout::MemoryLayout};
19
20/// The provider backend trait
21/// Implement this interface to create a Zenoh-compatible shared memory provider
22#[zenoh_macros::unstable_doc]
23pub trait ShmProviderBackend: WithProtocolID {
24 /// Allocate the chunk of desired size.
25 /// If successful, the result's chunk size will be >= len
26 #[zenoh_macros::unstable_doc]
27 fn alloc(&self, layout: &MemoryLayout) -> ChunkAllocResult;
28
29 /// Deallocate the chunk.
30 /// It is guaranteed that chunk's descriptor will correspond to the one returned from alloc(...)
31 #[zenoh_macros::unstable_doc]
32 fn free(&self, chunk: &ChunkDescriptor);
33
34 /// Defragment the memory.
35 /// Should return the size of largest defragmented chunk
36 #[zenoh_macros::unstable_doc]
37 fn defragment(&self) -> usize;
38
39 /// Bytes available for use
40 #[zenoh_macros::unstable_doc]
41 fn available(&self) -> usize;
42
43 /// Check and calculate suitable layout for layout.
44 /// Depending on the implementation, backend may relayout allocations for bigger layouts.
45 /// This method is used to:
46 /// - validate, if the provided layout can be used with this backend
47 /// - adopt the layout for backend capabilities
48 #[zenoh_macros::unstable_doc]
49 fn layout_for(&self, layout: MemoryLayout) -> Result<MemoryLayout, ZLayoutError>;
50}