Skip to content

Commit

Permalink
[eclipse-iceoryx#498] Introduce CustomPayloadMarker
Browse files Browse the repository at this point in the history
  • Loading branch information
elfenpiff committed Nov 1, 2024
1 parent 29c71af commit 88e86e6
Show file tree
Hide file tree
Showing 10 changed files with 84 additions and 78 deletions.
1 change: 0 additions & 1 deletion iceoryx2-bb/posix/tests/unix_datagram_socket_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ use iceoryx2_bb_container::semantic_string::SemanticString;
use iceoryx2_bb_posix::barrier::*;
use iceoryx2_bb_posix::config::*;
use iceoryx2_bb_posix::creation_mode::*;
use iceoryx2_bb_posix::directory::Directory;
use iceoryx2_bb_posix::file::*;
use iceoryx2_bb_posix::file_descriptor::*;
use iceoryx2_bb_posix::socket_ancillary::*;
Expand Down
33 changes: 7 additions & 26 deletions iceoryx2-ffi/ffi/src/api/publish_subscribe_header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ pub unsafe extern "C" fn iox2_publish_subscribe_header_publisher_id(
*id_handle_ptr = (*storage_ptr).as_handle();
}

/// Returns the payloads type size.
/// Returns the number of bytes of the payload
///
/// # Arguments
///
Expand All @@ -165,35 +165,16 @@ pub unsafe extern "C" fn iox2_publish_subscribe_header_publisher_id(
///
/// * `header_handle` is valid and non-null
#[no_mangle]
pub unsafe extern "C" fn iox2_publish_subscribe_header_payload_type_size(
pub unsafe extern "C" fn iox2_publish_subscribe_header_number_of_bytes(
header_handle: iox2_publish_subscribe_header_h_ref,
) -> usize {
) -> u64 {
header_handle.assert_non_null();

let header = &mut *header_handle.as_type();

header.value.as_ref().payload_type_layout().size()
// In the typed Rust API it is the number of elements and the element is a
// CustomPayloadMarker. But this translates to the number of bytes whenever CustomPayloadMarker
// is used.
header.value.as_ref().number_of_elements()
}

/// Returns the payloads type alignment.
///
/// # Arguments
///
/// * `handle` is valid, non-null and was initialized with
/// [`iox2_sample_header()`](crate::iox2_sample_header)
///
/// # Safety
///
/// * `header_handle` is valid and non-null
#[no_mangle]
pub unsafe extern "C" fn iox2_publish_subscribe_header_payload_type_alignment(
header_handle: iox2_publish_subscribe_header_h_ref,
) -> usize {
header_handle.assert_non_null();

let header = &mut *header_handle.as_type();

header.value.as_ref().payload_type_layout().align()
}

// END C API
6 changes: 3 additions & 3 deletions iceoryx2-ffi/ffi/src/api/publisher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ unsafe fn send_copy<S: Service>(
// loan_slice_uninit(1) <= 1 is correct here since it defines the number of
// slice elements not bytes. The element was set via TypeDetails and has a
// defined size and alignment.
let mut sample = match publisher.loan_slice_uninit(1) {
let mut sample = match publisher.loan_custom_payload(1) {
Ok(sample) => sample,
Err(e) => return e.into_c_int(),
};
Expand Down Expand Up @@ -383,7 +383,7 @@ pub unsafe extern "C" fn iox2_publisher_loan(
let publisher = &mut *publisher_handle.as_type();

match publisher.service_type {
iox2_service_type_e::IPC => match publisher.value.as_ref().ipc.loan_slice_uninit(1) {
iox2_service_type_e::IPC => match publisher.value.as_ref().ipc.loan_custom_payload(1) {
Ok(sample) => {
let (sample_struct_ptr, deleter) = init_sample_struct_ptr(sample_struct_ptr);
(*sample_struct_ptr).init(
Expand All @@ -396,7 +396,7 @@ pub unsafe extern "C" fn iox2_publisher_loan(
}
Err(error) => error.into_c_int(),
},
iox2_service_type_e::LOCAL => match publisher.value.as_ref().local.loan_slice_uninit(1) {
iox2_service_type_e::LOCAL => match publisher.value.as_ref().local.loan_custom_payload(1) {
Ok(sample) => {
let (sample_struct_ptr, deleter) = init_sample_struct_ptr(sample_struct_ptr);
(*sample_struct_ptr).init(
Expand Down
6 changes: 3 additions & 3 deletions iceoryx2-ffi/ffi/src/api/service_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use crate::api::{iox2_service_type_e, AssertNonNullHandle, HandleToType};

use iceoryx2::prelude::*;
use iceoryx2::service::builder::publish_subscribe::CustomHeaderMarker;
use iceoryx2::service::builder::publish_subscribe::{CustomHeaderMarker, CustomPayloadMarker};
use iceoryx2::service::builder::{
event::Builder as ServiceBuilderEvent, publish_subscribe::Builder as ServiceBuilderPubSub,
Builder as ServiceBuilderBase,
Expand All @@ -29,8 +29,8 @@ use core::mem::MaybeUninit;
// BEGIN types definition

pub(super) type UserHeaderFfi = CustomHeaderMarker;
pub(super) type PayloadFfi = [u8];
pub(super) type UninitPayloadFfi = [MaybeUninit<u8>];
pub(super) type PayloadFfi = [CustomPayloadMarker];
pub(super) type UninitPayloadFfi = [MaybeUninit<CustomPayloadMarker>];

pub(super) union ServiceBuilderUnionNested<S: Service> {
pub(super) base: ManuallyDrop<ServiceBuilderBase<S>>,
Expand Down
75 changes: 48 additions & 27 deletions iceoryx2/src/port/publisher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ use crate::port::update_connections::{ConnectionFailure, UpdateConnections};
use crate::port::DegrationAction;
use crate::raw_sample::RawSampleMut;
use crate::sample_mut_uninit::SampleMutUninit;
use crate::service::builder::publish_subscribe::CustomPayloadMarker;
use crate::service::config_scheme::{connection_config, data_segment_config};
use crate::service::dynamic_config::publish_subscribe::{PublisherDetails, SubscriberDetails};
use crate::service::header::publish_subscribe::Header;
Expand Down Expand Up @@ -738,14 +739,6 @@ impl<Service: service::Service, Payload: Debug + ?Sized, UserHeader: Debug>
.sample_layout(number_of_elements)
}

fn payload_layout(&self, number_of_elements: usize) -> Layout {
self.data_segment
.subscriber_connections
.static_config
.message_type_details
.payload_layout(number_of_elements)
}

fn user_header_ptr(&self, header: *const Header) -> *const u8 {
self.data_segment
.subscriber_connections
Expand Down Expand Up @@ -837,12 +830,7 @@ impl<Service: service::Service, Payload: Debug + Sized, UserHeader: Debug>
let user_header_ptr = self.user_header_ptr(header_ptr) as *mut UserHeader;
let payload_ptr = self.payload_ptr(header_ptr) as *mut MaybeUninit<Payload>;

unsafe {
header_ptr.write(Header::new(
self.data_segment.port_id,
Layout::new::<Payload>(),
))
};
unsafe { header_ptr.write(Header::new(self.data_segment.port_id, 1)) };

let sample =
unsafe { RawSampleMut::new_unchecked(header_ptr, user_header_ptr, payload_ptr) };
Expand Down Expand Up @@ -988,24 +976,13 @@ impl<Service: service::Service, Payload: Debug, UserHeader: Debug>
let user_header_ptr = self.user_header_ptr(header_ptr) as *mut UserHeader;
let payload_ptr = self.payload_ptr(header_ptr) as *mut MaybeUninit<Payload>;

unsafe {
header_ptr.write(Header::new(
self.data_segment.port_id,
self.payload_layout(slice_len),
))
};

let slice_len_adjusted_to_payload_type_details =
self.payload_size * slice_len / core::mem::size_of::<Payload>();
unsafe { header_ptr.write(Header::new(self.data_segment.port_id, slice_len as _)) };

let sample = unsafe {
RawSampleMut::new_unchecked(
header_ptr,
user_header_ptr,
core::slice::from_raw_parts_mut(
payload_ptr,
slice_len_adjusted_to_payload_type_details,
),
core::slice::from_raw_parts_mut(payload_ptr, slice_len),
)
};

Expand All @@ -1018,6 +995,50 @@ impl<Service: service::Service, Payload: Debug, UserHeader: Debug>
)
}
}

impl<Service: service::Service, UserHeader: Debug>
Publisher<Service, [CustomPayloadMarker], UserHeader>
{
#[doc(hidden)]
pub unsafe fn loan_custom_payload(
&self,
slice_len: usize,
) -> Result<
SampleMutUninit<Service, [MaybeUninit<CustomPayloadMarker>], UserHeader>,
PublisherLoanError,
> {
let max_slice_len = self.data_segment.config.max_slice_len;
if max_slice_len < slice_len {
fail!(from self, with PublisherLoanError::ExceedsMaxLoanSize,
"Unable to loan slice with {} elements since it would exceed the max supported slice length of {}.",
slice_len, max_slice_len);
}

let sample_layout = self.sample_layout(slice_len);
let chunk = self.allocate(sample_layout)?;
let header_ptr = chunk.data_ptr as *mut Header;
let user_header_ptr = self.user_header_ptr(header_ptr) as *mut UserHeader;
let payload_ptr = self.payload_ptr(header_ptr) as *mut MaybeUninit<CustomPayloadMarker>;

let slice_len = self.payload_size * slice_len;

unsafe { header_ptr.write(Header::new(self.data_segment.port_id, slice_len as _)) };

let sample = unsafe {
RawSampleMut::new_unchecked(
header_ptr,
user_header_ptr,
core::slice::from_raw_parts_mut(payload_ptr, slice_len),
)
};

Ok(SampleMutUninit::<
Service,
[MaybeUninit<CustomPayloadMarker>],
UserHeader,
>::new(&self.data_segment, sample, chunk.offset))
}
}
////////////////////////
// END: sliced API
////////////////////////
Expand Down
6 changes: 2 additions & 4 deletions iceoryx2/src/port/subscriber.rs
Original file line number Diff line number Diff line change
Expand Up @@ -447,17 +447,15 @@ impl<Service: service::Service, Payload: Debug, UserHeader: Debug>
let header_ptr = absolute_address as *const Header;
let user_header_ptr = self.user_header_ptr(header_ptr).cast();
let payload_ptr = self.payload_ptr(header_ptr).cast();

let payload_layout = unsafe { (*header_ptr).payload_type_layout() };
let number_of_elements = payload_layout.size() / core::mem::size_of::<Payload>();
let number_of_elements = unsafe { (*header_ptr).number_of_elements() };

Sample {
details,
ptr: unsafe {
RawSample::<Header, UserHeader, [Payload]>::new_slice_unchecked(
header_ptr,
user_header_ptr,
core::slice::from_raw_parts(payload_ptr, number_of_elements),
core::slice::from_raw_parts(payload_ptr, number_of_elements as _),
)
},
}
Expand Down
9 changes: 8 additions & 1 deletion iceoryx2/src/service/builder/publish_subscribe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ use super::{OpenDynamicStorageFailure, ServiceState};
#[doc(hidden)]
pub struct CustomHeaderMarker {}

#[repr(C)]
#[derive(Debug)]
#[doc(hidden)]
pub struct CustomPayloadMarker {}

/// Errors that can occur when an existing [`MessagingPattern::PublishSubscribe`] [`Service`] shall be opened.
#[derive(Debug, Clone, Copy, Eq, Hash, PartialEq)]
pub enum PublishSubscribeOpenError {
Expand Down Expand Up @@ -716,7 +721,9 @@ impl<Payload: Debug + ?Sized, UserHeader: Debug, ServiceType: service::Service>
}
}

impl<UserHeader: Debug, ServiceType: service::Service> Builder<[u8], UserHeader, ServiceType> {
impl<UserHeader: Debug, ServiceType: service::Service>
Builder<[CustomPayloadMarker], UserHeader, ServiceType>
{
#[doc(hidden)]
pub unsafe fn __internal_set_payload_type_details(mut self, value: &TypeDetail) -> Self {
self.override_payload_type = Some(value.clone());
Expand Down
13 changes: 6 additions & 7 deletions iceoryx2/src/service/header/publish_subscribe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
//! # Ok(())
//! # }
//! ```
use std::alloc::Layout;
use crate::port::port_identifiers::UniquePublisherId;

Expand All @@ -40,14 +39,14 @@ use crate::port::port_identifiers::UniquePublisherId;
#[repr(C)]
pub struct Header {
publisher_port_id: UniquePublisherId,
payload_type_layout: Layout,
number_of_elements: u64,
}

impl Header {
pub(crate) fn new(publisher_port_id: UniquePublisherId, payload_type_layout: Layout) -> Self {
pub(crate) fn new(publisher_port_id: UniquePublisherId, number_of_elements: u64) -> Self {
Self {
publisher_port_id,
payload_type_layout,
number_of_elements,
}
}

Expand All @@ -56,8 +55,8 @@ impl Header {
self.publisher_port_id
}

/// Returns the [`Layout`] of the corresponding payload.
pub fn payload_type_layout(&self) -> Layout {
self.payload_type_layout
/// Returns the amount of elements of the payload.
pub fn number_of_elements(&self) -> u64 {
self.number_of_elements
}
}
5 changes: 3 additions & 2 deletions iceoryx2/tests/publisher_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ mod publisher {

use iceoryx2::port::publisher::{PublisherCreateError, PublisherLoanError};
use iceoryx2::prelude::*;
use iceoryx2::service::builder::publish_subscribe::CustomPayloadMarker;
use iceoryx2::service::port_factory::publisher::UnableToDeliverStrategy;
use iceoryx2::service::static_config::message_type_details::{TypeDetail, TypeVariant};
use iceoryx2::service::{service_name::ServiceName, Service};
Expand Down Expand Up @@ -404,14 +405,14 @@ mod publisher {

let service = unsafe {
node.service_builder(&service_name)
.publish_subscribe::<[u8]>()
.publish_subscribe::<[CustomPayloadMarker]>()
.__internal_set_payload_type_details(&type_detail)
.create()?
};

let sut = service.publisher_builder().create()?;

let sample = sut.loan_slice(1)?;
let sample = unsafe { sut.loan_custom_payload(1)? };

assert_that!(sample.payload(), len TYPE_SIZE_OVERRIDE);

Expand Down
8 changes: 4 additions & 4 deletions iceoryx2/tests/service_publish_subscribe_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ mod service_publish_subscribe {
use iceoryx2::port::subscriber::SubscriberCreateError;
use iceoryx2::port::update_connections::UpdateConnections;
use iceoryx2::prelude::*;
use iceoryx2::service::builder::publish_subscribe::CustomHeaderMarker;
use iceoryx2::service::builder::publish_subscribe::PublishSubscribeCreateError;
use iceoryx2::service::builder::publish_subscribe::PublishSubscribeOpenError;
use iceoryx2::service::builder::publish_subscribe::{CustomHeaderMarker, CustomPayloadMarker};
use iceoryx2::service::messaging_pattern::MessagingPattern;
use iceoryx2::service::port_factory::publisher::UnableToDeliverStrategy;
use iceoryx2::service::static_config::message_type_details::{TypeDetail, TypeVariant};
Expand Down Expand Up @@ -2611,7 +2611,7 @@ mod service_publish_subscribe {

let _sut = unsafe {
node.service_builder(&service_name)
.publish_subscribe::<[u8]>()
.publish_subscribe::<[CustomPayloadMarker]>()
.__internal_set_payload_type_details(&TypeDetail::__internal_new::<u64>(
TypeVariant::FixedSize,
))
Expand Down Expand Up @@ -2704,7 +2704,7 @@ mod service_publish_subscribe {

let sut2 = unsafe {
node.service_builder(&service_name)
.publish_subscribe::<[u8]>()
.publish_subscribe::<[CustomPayloadMarker]>()
.__internal_set_payload_type_details(&TypeDetail::__internal_new::<u128>(
TypeVariant::FixedSize,
))
Expand All @@ -2715,7 +2715,7 @@ mod service_publish_subscribe {

let sut3 = unsafe {
node.service_builder(&service_name)
.publish_subscribe::<[u8]>()
.publish_subscribe::<[CustomPayloadMarker]>()
.__internal_set_payload_type_details(&TypeDetail::__internal_new::<u64>(
TypeVariant::FixedSize,
))
Expand Down

0 comments on commit 88e86e6

Please sign in to comment.