Skip to content

Commit

Permalink
[eclipse-iceoryx#532] Extract data segment from publisher
Browse files Browse the repository at this point in the history
  • Loading branch information
elfenpiff committed Nov 28, 2024
1 parent cd1967f commit 8474821
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 32 deletions.
52 changes: 50 additions & 2 deletions iceoryx2/src/port/details/data_segment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,24 @@
//
// SPDX-License-Identifier: Apache-2.0 OR MIT

use iceoryx2_cal::shm_allocator::AllocationStrategy;
use std::alloc::Layout;

use crate::service;
use iceoryx2_bb_log::fail;
use iceoryx2_cal::{
event::NamedConceptBuilder,
shared_memory::{SharedMemory, SharedMemoryBuilder, SharedMemoryCreateError, ShmPointer},
shm_allocator::{
self, pool_allocator::PoolAllocator, AllocationStrategy, PointerOffset, ShmAllocationError,
},
};

use crate::{
config,
service::{
self, config_scheme::data_segment_config,
dynamic_config::publish_subscribe::PublisherDetails, naming_scheme::data_segment_name,
},
};

#[repr(C)]
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
Expand All @@ -30,6 +45,39 @@ impl DataSegmentType {
}
}

#[derive(Debug)]
pub(crate) struct DataSegment<Service: service::Service> {
memory: Service::SharedMemory,
}

impl<Service: service::Service> DataSegment<Service> {
pub(crate) fn create(
details: &PublisherDetails,
global_config: &config::Config,
sample_layout: Layout,
) -> Result<Self, SharedMemoryCreateError> {
let allocator_config = shm_allocator::pool_allocator::Config {
bucket_layout: sample_layout,
};

let memory = fail!(from "Publisher::create_data_segment()",
when <<Service::SharedMemory as SharedMemory<PoolAllocator>>::Builder as NamedConceptBuilder<
Service::SharedMemory,
>>::new(&data_segment_name(&details.publisher_id))
.config(&data_segment_config::<Service>(global_config))
.size(sample_layout.size() * details.number_of_samples + sample_layout.align() - 1)
.create(&allocator_config),
"Unable to create the data segment.");

Ok(Self { memory })
}

pub(crate) fn allocate(&self, layout: Layout) -> Result<ShmPointer, ShmAllocationError> {
Ok(fail!(from self, when self.memory.allocate(layout),
"Unable to allocate memory from the data segment."))
}

pub(crate) unsafe fn deallocate(&self, offset: PointerOffset, layout: Layout) {
self.memory.deallocate(offset, layout)
}
}
36 changes: 6 additions & 30 deletions iceoryx2/src/port/publisher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@
//! # }
//! ```
use super::details::data_segment::DataSegmentType;
use super::details::data_segment::{DataSegment, DataSegmentType};
use super::port_identifiers::UniquePublisherId;
use super::UniqueSubscriberId;
use crate::port::details::subscriber_connections::*;
Expand Down Expand Up @@ -129,14 +129,9 @@ use iceoryx2_bb_log::{debug, error, fail, fatal_panic, warn};
use iceoryx2_bb_system_types::file_name::FileName;
use iceoryx2_cal::dynamic_storage::DynamicStorage;
use iceoryx2_cal::event::NamedConceptMgmt;
use iceoryx2_cal::named_concept::{
NamedConceptBuilder, NamedConceptListError, NamedConceptRemoveError,
};
use iceoryx2_cal::shared_memory::{
SharedMemory, SharedMemoryBuilder, SharedMemoryCreateError, ShmPointer,
};
use iceoryx2_cal::shm_allocator::pool_allocator::PoolAllocator;
use iceoryx2_cal::shm_allocator::{self, AllocationStrategy, PointerOffset, ShmAllocationError};
use iceoryx2_cal::named_concept::{NamedConceptListError, NamedConceptRemoveError};
use iceoryx2_cal::shared_memory::ShmPointer;
use iceoryx2_cal::shm_allocator::{PointerOffset, ShmAllocationError};
use iceoryx2_cal::zero_copy_connection::{
ZeroCopyConnection, ZeroCopyCreationError, ZeroCopySendError, ZeroCopySender,
};
Expand Down Expand Up @@ -241,7 +236,7 @@ pub(crate) enum RemovePubSubPortFromAllConnectionsError {
#[derive(Debug)]
pub(crate) struct PublisherBackend<Service: service::Service> {
sample_reference_counter: Vec<IoxAtomicU64>,
data_segment: Service::SharedMemory,
data_segment: DataSegment<Service>,
payload_size: usize,
payload_type_layout: Layout,
port_id: UniquePublisherId,
Expand Down Expand Up @@ -601,7 +596,7 @@ impl<Service: service::Service, Payload: Debug + ?Sized, UserHeader: Debug>
let global_config = service.__internal_state().shared_node.config();

let data_segment = fail!(from origin,
when Self::create_data_segment(&publisher_details, &global_config, sample_layout),
when DataSegment::create(&publisher_details, &global_config, sample_layout),
with PublisherCreateError::UnableToCreateDataSegment,
"{} since the data segment could not be acquired.", msg);

Expand Down Expand Up @@ -684,25 +679,6 @@ impl<Service: service::Service, Payload: Debug + ?Sized, UserHeader: Debug>
Ok(new_self)
}

fn create_data_segment(
details: &PublisherDetails,
global_config: &config::Config,
sample_layout: Layout,
) -> Result<Service::SharedMemory, SharedMemoryCreateError> {
let allocator_config = shm_allocator::pool_allocator::Config {
bucket_layout: sample_layout,
};

Ok(fail!(from "Publisher::create_data_segment()",
when <<Service::SharedMemory as SharedMemory<PoolAllocator>>::Builder as NamedConceptBuilder<
Service::SharedMemory,
>>::new(&data_segment_name(&details.publisher_id))
.config(&data_segment_config::<Service>(global_config))
.size(sample_layout.size() * details.number_of_samples + sample_layout.align() - 1)
.create(&allocator_config),
"Unable to create the data segment."))
}

/// Returns the [`UniquePublisherId`] of the [`Publisher`]
pub fn id(&self) -> UniquePublisherId {
self.backend.port_id
Expand Down

0 comments on commit 8474821

Please sign in to comment.