Skip to content

Commit

Permalink
Merge branch 'dev/1.0.0' into selector_rework3
Browse files Browse the repository at this point in the history
  • Loading branch information
milyin committed Jun 11, 2024
2 parents b84bd90 + bca5c4d commit 357b90e
Show file tree
Hide file tree
Showing 60 changed files with 625 additions and 587 deletions.
14 changes: 7 additions & 7 deletions commons/zenoh-codec/src/core/shm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use zenoh_buffers::{
};
use zenoh_shm::{
api::provider::chunk::ChunkDescriptor, header::descriptor::HeaderDescriptor,
watchdog::descriptor::Descriptor, SharedMemoryBufInfo,
watchdog::descriptor::Descriptor, ShmBufInfo,
};

use crate::{RCodec, WCodec, Zenoh080};
Expand Down Expand Up @@ -62,14 +62,14 @@ where
}
}

impl<W> WCodec<&SharedMemoryBufInfo, &mut W> for Zenoh080
impl<W> WCodec<&ShmBufInfo, &mut W> for Zenoh080
where
W: Writer,
{
type Output = Result<(), DidntWrite>;

fn write(self, writer: &mut W, x: &SharedMemoryBufInfo) -> Self::Output {
let SharedMemoryBufInfo {
fn write(self, writer: &mut W, x: &ShmBufInfo) -> Self::Output {
let ShmBufInfo {
data_descriptor,
shm_protocol,
data_len,
Expand Down Expand Up @@ -138,21 +138,21 @@ where
}
}

impl<R> RCodec<SharedMemoryBufInfo, &mut R> for Zenoh080
impl<R> RCodec<ShmBufInfo, &mut R> for Zenoh080
where
R: Reader,
{
type Error = DidntRead;

fn read(self, reader: &mut R) -> Result<SharedMemoryBufInfo, Self::Error> {
fn read(self, reader: &mut R) -> Result<ShmBufInfo, Self::Error> {
let data_descriptor = self.read(&mut *reader)?;
let shm_protocol = self.read(&mut *reader)?;
let data_len = self.read(&mut *reader)?;
let watchdog_descriptor = self.read(&mut *reader)?;
let header_descriptor = self.read(&mut *reader)?;
let generation = self.read(&mut *reader)?;

let shm_info = SharedMemoryBufInfo::new(
let shm_info = ShmBufInfo::new(
data_descriptor,
shm_protocol,
data_len,
Expand Down
6 changes: 3 additions & 3 deletions commons/zenoh-codec/tests/codec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -363,12 +363,12 @@ fn codec_encoding() {
fn codec_shm_info() {
use zenoh_shm::{
api::provider::chunk::ChunkDescriptor, header::descriptor::HeaderDescriptor,
watchdog::descriptor::Descriptor, SharedMemoryBufInfo,
watchdog::descriptor::Descriptor, ShmBufInfo,
};

run!(SharedMemoryBufInfo, {
run!(ShmBufInfo, {
let mut rng = rand::thread_rng();
SharedMemoryBufInfo::new(
ShmBufInfo::new(
ChunkDescriptor::new(rng.gen(), rng.gen(), rng.gen()),
rng.gen(),
rng.gen(),
Expand Down
2 changes: 1 addition & 1 deletion commons/zenoh-config/src/defaults.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ impl Default for LinkRxConf {

// Make explicit the value and ignore clippy warning
#[allow(clippy::derivable_impls)]
impl Default for SharedMemoryConf {
impl Default for ShmConf {
fn default() -> Self {
Self { enabled: false }
}
Expand Down
2 changes: 1 addition & 1 deletion commons/zenoh-config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,7 @@ validated_struct::validator! {
},
},
pub shared_memory:
SharedMemoryConf {
ShmConf {
/// Whether shared memory is enabled or not.
/// If set to `true`, the SHM buffer optimization support will be announced to other parties. (default `false`).
/// This option doesn't make SHM buffer optimization mandatory, the real support depends on other party setting
Expand Down
6 changes: 6 additions & 0 deletions commons/zenoh-protocol/src/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ pub mod imsg {
byte
}

pub const fn set_bitfield(mut byte: u8, value: u8, mask: u8) -> u8 {
byte = unset_flag(byte, mask);
byte |= value;
byte
}

pub const fn has_option(options: u64, flag: u64) -> bool {
options & flag != 0
}
Expand Down
2 changes: 1 addition & 1 deletion commons/zenoh-protocol/src/network/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ pub mod ext {
}

pub fn set_priority(&mut self, priority: Priority) {
self.inner = imsg::set_flag(self.inner, priority as u8);
self.inner = imsg::set_bitfield(self.inner, priority as u8, Self::P_MASK);
}

pub const fn get_priority(&self) -> Priority {
Expand Down
11 changes: 0 additions & 11 deletions commons/zenoh-protocol/src/scouting/hello.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
// ZettaScale Zenoh Team, <[email protected]>
//
use alloc::vec::Vec;
use core::fmt;

use crate::core::{Locator, WhatAmI, ZenohId};

Expand Down Expand Up @@ -107,16 +106,6 @@ pub struct Hello {
pub locators: Vec<Locator>,
}

impl fmt::Display for Hello {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("Hello")
.field("zid", &self.zid)
.field("whatami", &self.whatami)
.field("locators", &self.locators)
.finish()
}
}

impl Hello {
#[cfg(feature = "test")]
pub fn rand() -> Self {
Expand Down
4 changes: 2 additions & 2 deletions commons/zenoh-shm/src/api/buffer/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
use std::ops::{Deref, DerefMut};

#[zenoh_macros::unstable_doc]
pub trait SHMBuf: Deref<Target = [u8]> + AsRef<[u8]> {
pub trait ShmBuf: Deref<Target = [u8]> + AsRef<[u8]> {
#[zenoh_macros::unstable_doc]
fn is_valid(&self) -> bool;
}

#[zenoh_macros::unstable_doc]
pub trait SHMBufMut: SHMBuf + DerefMut + AsMut<[u8]> {}
pub trait ShmBufMut: ShmBuf + DerefMut + AsMut<[u8]> {}
32 changes: 16 additions & 16 deletions commons/zenoh-shm/src/api/buffer/zshm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@ use std::{

use zenoh_buffers::{ZBuf, ZSlice};

use super::{traits::SHMBuf, zshmmut::zshmmut};
use crate::SharedMemoryBuf;
use super::{traits::ShmBuf, zshmmut::zshmmut};
use crate::ShmBufInner;

/// An immutable SHM buffer
#[zenoh_macros::unstable_doc]
#[repr(transparent)]
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ZShm(pub(crate) SharedMemoryBuf);
pub struct ZShm(pub(crate) ShmBufInner);

impl SHMBuf for ZShm {
impl ShmBuf for ZShm {
fn is_valid(&self) -> bool {
self.0.is_valid()
}
Expand All @@ -44,15 +44,15 @@ impl PartialEq<&zshm> for ZShm {
impl Borrow<zshm> for ZShm {
fn borrow(&self) -> &zshm {
// SAFETY: ZShm, ZShmMut, zshm and zshmmut are #[repr(transparent)]
// to SharedMemoryBuf type, so it is safe to transmute them in any direction
// to ShmBufInner type, so it is safe to transmute them in any direction
unsafe { core::mem::transmute(self) }
}
}

impl BorrowMut<zshm> for ZShm {
fn borrow_mut(&mut self) -> &mut zshm {
// SAFETY: ZShm, ZShmMut, zshm and zshmmut are #[repr(transparent)]
// to SharedMemoryBuf type, so it is safe to transmute them in any direction
// to ShmBufInner type, so it is safe to transmute them in any direction
unsafe { core::mem::transmute(self) }
}
}
Expand All @@ -71,8 +71,8 @@ impl AsRef<[u8]> for ZShm {
}
}

impl From<SharedMemoryBuf> for ZShm {
fn from(value: SharedMemoryBuf) -> Self {
impl From<ShmBufInner> for ZShm {
fn from(value: ShmBufInner) -> Self {
Self(value)
}
}
Expand All @@ -96,7 +96,7 @@ impl TryFrom<&mut ZShm> for &mut zshmmut {
match value.0.is_unique() && value.0.is_valid() {
true => {
// SAFETY: ZShm, ZShmMut, zshm and zshmmut are #[repr(transparent)]
// to SharedMemoryBuf type, so it is safe to transmute them in any direction
// to ShmBufInner type, so it is safe to transmute them in any direction
Ok(unsafe { core::mem::transmute(value) })
}
false => Err(()),
Expand Down Expand Up @@ -139,18 +139,18 @@ impl DerefMut for zshm {
}
}

impl From<&SharedMemoryBuf> for &zshm {
fn from(value: &SharedMemoryBuf) -> Self {
impl From<&ShmBufInner> for &zshm {
fn from(value: &ShmBufInner) -> Self {
// SAFETY: ZShm, ZShmMut, zshm and zshmmut are #[repr(transparent)]
// to SharedMemoryBuf type, so it is safe to transmute them in any direction
// to ShmBufInner type, so it is safe to transmute them in any direction
unsafe { core::mem::transmute(value) }
}
}

impl From<&mut SharedMemoryBuf> for &mut zshm {
fn from(value: &mut SharedMemoryBuf) -> Self {
impl From<&mut ShmBufInner> for &mut zshm {
fn from(value: &mut ShmBufInner) -> Self {
// SAFETY: ZShm, ZShmMut, zshm and zshmmut are #[repr(transparent)]
// to SharedMemoryBuf type, so it is safe to transmute them in any direction
// to ShmBufInner type, so it is safe to transmute them in any direction
unsafe { core::mem::transmute(value) }
}
}
Expand All @@ -162,7 +162,7 @@ impl TryFrom<&mut zshm> for &mut zshmmut {
match value.0 .0.is_unique() && value.0 .0.is_valid() {
true => {
// SAFETY: ZShm, ZShmMut, zshm and zshmmut are #[repr(transparent)]
// to SharedMemoryBuf type, so it is safe to transmute them in any direction
// to ShmBufInner type, so it is safe to transmute them in any direction
Ok(unsafe { core::mem::transmute(value) })
}
false => Err(()),
Expand Down
32 changes: 16 additions & 16 deletions commons/zenoh-shm/src/api/buffer/zshmmut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,27 @@ use std::borrow::{Borrow, BorrowMut};
use zenoh_buffers::{ZBuf, ZSlice};

use super::{
traits::{SHMBuf, SHMBufMut},
traits::{ShmBuf, ShmBufMut},
zshm::{zshm, ZShm},
};
use crate::SharedMemoryBuf;
use crate::ShmBufInner;

/// A mutable SHM buffer
#[zenoh_macros::unstable_doc]
#[derive(Debug, PartialEq, Eq)]
#[repr(transparent)]
pub struct ZShmMut(SharedMemoryBuf);
pub struct ZShmMut(ShmBufInner);

impl SHMBuf for ZShmMut {
impl ShmBuf for ZShmMut {
fn is_valid(&self) -> bool {
self.0.is_valid()
}
}

impl SHMBufMut for ZShmMut {}
impl ShmBufMut for ZShmMut {}

impl ZShmMut {
pub(crate) unsafe fn new_unchecked(data: SharedMemoryBuf) -> Self {
pub(crate) unsafe fn new_unchecked(data: ShmBufInner) -> Self {
Self(data)
}
}
Expand All @@ -49,10 +49,10 @@ impl PartialEq<zshmmut> for &ZShmMut {
}
}

impl TryFrom<SharedMemoryBuf> for ZShmMut {
type Error = SharedMemoryBuf;
impl TryFrom<ShmBufInner> for ZShmMut {
type Error = ShmBufInner;

fn try_from(value: SharedMemoryBuf) -> Result<Self, Self::Error> {
fn try_from(value: ShmBufInner) -> Result<Self, Self::Error> {
match value.is_unique() && value.is_valid() {
true => Ok(Self(value)),
false => Err(value),
Expand All @@ -74,31 +74,31 @@ impl TryFrom<ZShm> for ZShmMut {
impl Borrow<zshm> for ZShmMut {
fn borrow(&self) -> &zshm {
// SAFETY: ZShm, ZShmMut, zshm and zshmmut are #[repr(transparent)]
// to SharedMemoryBuf type, so it is safe to transmute them in any direction
// to ShmBufInner type, so it is safe to transmute them in any direction
unsafe { core::mem::transmute(self) }
}
}

impl BorrowMut<zshm> for ZShmMut {
fn borrow_mut(&mut self) -> &mut zshm {
// SAFETY: ZShm, ZShmMut, zshm and zshmmut are #[repr(transparent)]
// to SharedMemoryBuf type, so it is safe to transmute them in any direction
// to ShmBufInner type, so it is safe to transmute them in any direction
unsafe { core::mem::transmute(self) }
}
}

impl Borrow<zshmmut> for ZShmMut {
fn borrow(&self) -> &zshmmut {
// SAFETY: ZShm, ZShmMut, zshm and zshmmut are #[repr(transparent)]
// to SharedMemoryBuf type, so it is safe to transmute them in any direction
// to ShmBufInner type, so it is safe to transmute them in any direction
unsafe { core::mem::transmute(self) }
}
}

impl BorrowMut<zshmmut> for ZShmMut {
fn borrow_mut(&mut self) -> &mut zshmmut {
// SAFETY: ZShm, ZShmMut, zshm and zshmmut are #[repr(transparent)]
// to SharedMemoryBuf type, so it is safe to transmute them in any direction
// to ShmBufInner type, so it is safe to transmute them in any direction
unsafe { core::mem::transmute(self) }
}
}
Expand Down Expand Up @@ -174,13 +174,13 @@ impl DerefMut for zshmmut {
}
}

impl TryFrom<&mut SharedMemoryBuf> for &mut zshmmut {
impl TryFrom<&mut ShmBufInner> for &mut zshmmut {
type Error = ();

fn try_from(value: &mut SharedMemoryBuf) -> Result<Self, Self::Error> {
fn try_from(value: &mut ShmBufInner) -> Result<Self, Self::Error> {
match value.is_unique() && value.is_valid() {
// SAFETY: ZShm, ZShmMut, zshm and zshmmut are #[repr(transparent)]
// to SharedMemoryBuf type, so it is safe to transmute them in any direction
// to ShmBufInner type, so it is safe to transmute them in any direction
true => Ok(unsafe { core::mem::transmute(value) }),
false => Err(()),
}
Expand Down
4 changes: 2 additions & 2 deletions commons/zenoh-shm/src/api/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@
// ZettaScale Zenoh Team, <[email protected]>
//

pub mod shared_memory_client;
pub mod shared_memory_segment;
pub mod shm_client;
pub mod shm_segment;
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ use std::{fmt::Debug, sync::Arc};

use zenoh_result::ZResult;

use super::shared_memory_segment::SharedMemorySegment;
use super::shm_segment::ShmSegment;
use crate::api::common::types::SegmentID;

/// SharedMemoryClient - client factory implementation for particular shared memory protocol
/// ShmClient - client factory implementation for particular shared memory protocol
#[zenoh_macros::unstable_doc]
pub trait SharedMemoryClient: Debug + Send + Sync {
pub trait ShmClient: Debug + Send + Sync {
/// Attach to particular shared memory segment
#[zenoh_macros::unstable_doc]
fn attach(&self, segment: SegmentID) -> ZResult<Arc<dyn SharedMemorySegment>>;
fn attach(&self, segment: SegmentID) -> ZResult<Arc<dyn ShmSegment>>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ use zenoh_result::ZResult;

use crate::api::common::types::ChunkID;

/// SharedMemorySegment - RAII interface to interact with particular shared memory segment
/// ShmSegment - RAII interface to interact with particular shared memory segment
#[zenoh_macros::unstable_doc]
pub trait SharedMemorySegment: Debug + Send + Sync {
pub trait ShmSegment: Debug + Send + Sync {
/// Obtain the actual region of memory identified by it's id
#[zenoh_macros::unstable_doc]
fn map(&self, chunk: ChunkID) -> ZResult<AtomicPtr<u8>>;
Expand Down
Loading

0 comments on commit 357b90e

Please sign in to comment.