zenoh_shm/api/provider/
types.rs1use std::{convert::Infallible, fmt::Display, num::NonZeroUsize};
16
17use super::chunk::AllocatedChunk;
18
19#[zenoh_macros::unstable_doc]
21#[derive(Debug)]
22pub enum ZAllocError {
23 NeedDefragment,
25 OutOfMemory,
27 Other,
29}
30
31impl Display for ZAllocError {
32 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
33 match self {
34 ZAllocError::NeedDefragment => write!(f, "need defragmentation"),
35 ZAllocError::OutOfMemory => write!(f, "out of memory"),
36 ZAllocError::Other => write!(f, "other"),
37 }
38 }
39}
40
41impl std::error::Error for ZAllocError {}
42
43impl From<zenoh_result::Error> for ZAllocError {
44 fn from(_value: zenoh_result::Error) -> Self {
45 Self::Other
46 }
47}
48
49#[zenoh_macros::unstable_doc]
51#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
52pub struct AllocAlignment {
53 pow: u8,
54}
55
56impl Display for AllocAlignment {
57 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
58 f.write_fmt(format_args!("[{}]", self.get_alignment_value()))
59 }
60}
61
62impl Default for AllocAlignment {
63 fn default() -> Self {
64 Self::ALIGN_1_BYTE
65 }
66}
67
68impl AllocAlignment {
69 pub const ALIGN_1_BYTE: AllocAlignment = AllocAlignment { pow: 0 };
70 pub const ALIGN_2_BYTES: AllocAlignment = AllocAlignment { pow: 1 };
71 pub const ALIGN_4_BYTES: AllocAlignment = AllocAlignment { pow: 2 };
72 pub const ALIGN_8_BYTES: AllocAlignment = AllocAlignment { pow: 3 };
73
74 #[zenoh_macros::unstable_doc]
80 pub const fn new(pow: u8) -> Result<Self, ZLayoutError> {
81 if pow < usize::BITS as u8 {
82 Ok(Self { pow })
83 } else {
84 Err(ZLayoutError::IncorrectLayoutArgs)
85 }
86 }
87
88 #[zenoh_macros::unstable_doc]
90 pub const fn for_type<T: Sized>() -> Self {
91 let align = std::mem::align_of::<T>();
92 let pow = align.trailing_zeros() as u8;
93 Self { pow }
94 }
95
96 #[zenoh_macros::unstable_doc]
98 pub const fn for_value<T: Sized>(_: &T) -> Self {
99 Self::for_type::<T>()
100 }
101
102 #[zenoh_macros::unstable_doc]
104 pub fn get_alignment_value(&self) -> NonZeroUsize {
105 unsafe { NonZeroUsize::new_unchecked(1usize << self.pow) }
107 }
108
109 #[zenoh_macros::unstable_doc]
122 pub fn align_size(&self, size: NonZeroUsize) -> NonZeroUsize {
123 let a_minus_1 = self.get_alignment_value().get() - 1;
146
147 let bound = usize::MAX - a_minus_1;
150 assert!(
151 size.get() <= bound,
152 "The given size {} exceeded the maximum {}",
153 size.get(),
154 bound
155 );
156
157 let r = (size.get() + a_minus_1) & !a_minus_1;
159
160 unsafe { NonZeroUsize::new_unchecked(r) }
162 }
163}
164
165#[zenoh_macros::unstable_doc]
167#[derive(Debug)]
168pub enum ZLayoutError {
169 IncorrectLayoutArgs,
171 ProviderIncompatibleLayout,
173}
174
175impl Display for ZLayoutError {
176 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
177 match self {
178 Self::IncorrectLayoutArgs => write!(f, "incorrect layout arguments"),
179 Self::ProviderIncompatibleLayout => {
180 write!(f, "layout is incompatible with the provider")
181 }
182 }
183 }
184}
185
186impl std::error::Error for ZLayoutError {}
187
188impl From<Infallible> for ZLayoutError {
189 fn from(_: Infallible) -> Self {
190 unreachable!()
191 }
192}
193
194#[zenoh_macros::unstable_doc]
196pub type ChunkAllocResult = Result<AllocatedChunk, ZAllocError>;
197
198#[zenoh_macros::unstable_doc]
200#[derive(Debug)]
201pub enum ZLayoutAllocError {
202 Alloc(ZAllocError),
204 Layout(ZLayoutError),
206}
207
208impl Display for ZLayoutAllocError {
209 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
210 match self {
211 ZLayoutAllocError::Alloc(err) => write!(f, "{}", err),
212 ZLayoutAllocError::Layout(err) => write!(f, "{}", err),
213 }
214 }
215}
216
217impl std::error::Error for ZLayoutAllocError {}
218
219impl From<ZLayoutError> for ZLayoutAllocError {
220 fn from(value: ZLayoutError) -> Self {
221 Self::Layout(value)
222 }
223}
224
225impl From<ZAllocError> for ZLayoutAllocError {
226 fn from(value: ZAllocError) -> Self {
227 Self::Alloc(value)
228 }
229}