From 7b7610ed69b624307edabf26eec2846d05c5aa39 Mon Sep 17 00:00:00 2001 From: yellowhatter Date: Mon, 16 Dec 2024 11:46:45 +0300 Subject: [PATCH] wip on SHM channel --- commons/zenoh-shm/src/channel/mod.rs | 57 +++++++++++++++++++++------- 1 file changed, 44 insertions(+), 13 deletions(-) diff --git a/commons/zenoh-shm/src/channel/mod.rs b/commons/zenoh-shm/src/channel/mod.rs index c1812975b..053d36f80 100644 --- a/commons/zenoh-shm/src/channel/mod.rs +++ b/commons/zenoh-shm/src/channel/mod.rs @@ -33,22 +33,21 @@ ________________________________________________________________________ | header |-opt-padding-| elem | elem | ..... | */ - struct InnerLayout> { _phantom: PhantomData, } impl> InnerLayout { - const fn header_with_padding() -> usize { - size_of::() + Self::header_padding() + const fn header_with_padding() -> usize { + size_of::() + Self::header_padding::() } - const fn header_padding() -> usize { - if size_of::() > align_of::() { - return (align_of::() - (size_of::() % align_of::())) + const fn header_padding() -> usize { + if size_of::() > align_of::() { + return (align_of::() - (size_of::() % align_of::())) % align_of::(); - } else if size_of::() < align_of::() { - return align_of::() - size_of::(); + } else if size_of::() < align_of::() { + return align_of::() - size_of::(); } 0 } @@ -64,25 +63,31 @@ impl> InnerLayout { } } + +#[repr(C)] +struct ChannelHeader { + // free data in storage units + pub free: AtomicUsize, +} + pub struct ChannelData<'a, T: IStable + 'a> { // free data in storage units - pub free: &'a mut AtomicUsize, + pub header: pub pop_pos: usize, pub push_pos: usize, /* Shared data structure: - ___________________________________________________ - | 1. self.data | 2. rollover | 3. --- | self.free | + ______________________________________________________ + | 1. self.data | 2. rollover | 3. --- | 4. self.free | 1: - aligned for T - size is a multiply of alignment 2: same layout as 1 3: padding to align self.free - 4: properly aligned - + 4: properly aligned self.free */ pub data: &'a mut [u8], @@ -204,3 +209,29 @@ impl<'a, T: IStable> Rx<'a, T> { self.data.pop() } } + +impl<'a, T: IStable + 'a> TryInto> for &'a mut [T] { + type Error = (); + + fn try_into(self) -> Result, Self::Error> { + // todo: validate alignment for T + + let total_bytes = InnerLayout::byte_len(self); + + let header_with_padding = InnerLayout::header_with_padding::() + + let data_bytes = total_bytes / 2 - size_of::(); + + + + let data = self + + + let data_len = data_bytes / size_of::(); + + let data = &mut self[0..data_len]; + let rollover = &mut self[data_len..data_len*2]; + let free = (&mut self[data_len*2]) as *mut T + + } +} \ No newline at end of file