Skip to content

Commit

Permalink
[eclipse-iceoryx#497] Integrate SlotMap
Browse files Browse the repository at this point in the history
  • Loading branch information
elfenpiff committed Nov 19, 2024
1 parent 3eef147 commit 1f84877
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 66 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions iceoryx2-fal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ version = { workspace = true }

[dependencies]
iceoryx2-cal = { workspace = true }
iceoryx2-bb-container = { workspace = true }
iceoryx2-bb-system-types = { workspace = true }
iceoryx2-bb-elementary = { workspace = true }
iceoryx2-bb-log = { workspace = true }
Expand Down
133 changes: 67 additions & 66 deletions iceoryx2-fal/src/resizable_shared_memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@

use std::alloc::Layout;
use std::fmt::Debug;
use std::sync::atomic::Ordering;
use std::time::Duration;

use iceoryx2_bb_container::semantic_string::SemanticString;
use iceoryx2_bb_container::slotmap::{SlotMap, SlotMapKey};
use iceoryx2_bb_elementary::CallbackProgression;
use iceoryx2_bb_log::fail;
use iceoryx2_bb_system_types::file_name::FileName;
Expand All @@ -23,64 +26,30 @@ use iceoryx2_cal::shared_memory::{
};
use iceoryx2_cal::shm_allocator::pool_allocator::PoolAllocator;
use iceoryx2_cal::shm_allocator::{PointerOffset, ShmAllocationError};
use iceoryx2_pal_concurrency_sync::iox_atomic::IoxAtomicBool;

#[derive(Debug)]
pub struct UniqueIndexVector<T: Debug> {
data: Vec<T>,
idx_to_data: Vec<usize>,
current_idx: usize,
pub struct DynamicPointerOffset {
value: u64,
}

impl<T: Debug> UniqueIndexVector<T> {
pub fn new() -> Self {
impl DynamicPointerOffset {
pub fn new(offset: usize, segment_id: u8) -> Self {
Self {
data: vec![],
idx_to_data: vec![],
current_idx: 0,
value: (offset as u64) << 8 | segment_id as u64,
}
}

pub fn push(&mut self, value: T) -> usize {
todo!()
}

pub fn write_at(&mut self, value: T, index: usize) {}

pub fn remove(&mut self, index: usize) {}

pub fn get(&self, index: usize) -> &T {
todo!()
}

pub fn get_mut(&mut self, index: usize) -> &mut T {
todo!()
}
}

impl<T: Debug, const N: usize> From<[T; N]> for UniqueIndexVector<T> {
fn from(value: [T; N]) -> Self {
todo!()
}
}

pub struct DynamicPointerOffset {
value: u64,
}

impl DynamicPointerOffset {
pub fn offset(&self) -> PointerOffset {
todo!()
PointerOffset::new((self.value >> 8) as usize)
}

pub fn segment_id(&self) -> u8 {
todo!()
}

pub fn has_segment_update(&self) -> u8 {
todo!()
(self.value & 0x00000000000000ff) as u8
}
}

const MAX_DATASEGMENTS: usize = 256;

#[derive(Default)]
pub enum AllocationStrategy {
#[default]
Expand All @@ -93,6 +62,7 @@ pub struct ResizableSharedMemoryBuilder<T: SharedMemory<PoolAllocator>>
where
T::Builder: Debug,
{
base_name: FileName,
builder: T::Builder,
bucket_layout_hint: Layout,
number_of_buckets_hint: usize,
Expand All @@ -103,8 +73,13 @@ where
T::Builder: Debug,
{
pub fn new(name: &FileName) -> Self {
let mut first_shm_segment = *name;
first_shm_segment
.push_bytes(b"__0")
.expect("Adding __0 results in a valid file name");
Self {
builder: T::Builder::new(name),
base_name: *name,
bucket_layout_hint: Layout::new::<u8>(),
number_of_buckets_hint: 1,
}
Expand Down Expand Up @@ -155,11 +130,18 @@ where
.create(&initial_allocator_config),
"Unable to create ResizableSharedMemory since the underlying shared memory could not be created.");

let mut shared_memory_map = SlotMap::new(MAX_DATASEGMENTS);
let current_idx = shared_memory_map
.insert(shm)
.expect("MAX_DATASEGMENTS is greater or equal 1");

Ok(ResizableSharedMemory {
shared_memory_vec: UniqueIndexVector::from([shm]),
current_idx: 0,
base_name: self.base_name,
shared_memory_map,
current_idx,
number_of_buckets: self.number_of_buckets_hint,
bucket_layout: self.bucket_layout_hint,
has_ownership: IoxAtomicBool::new(true),
})
}

Expand All @@ -172,36 +154,55 @@ where
.open(),
"Unable to open ResizableSharedMemoryView since the underlying shared memory could not be opened.");

let mut shared_memory_map = SlotMap::new(MAX_DATASEGMENTS);
let current_idx = shared_memory_map
.insert(shm)
.expect("MAX_DATASEGMENTS is greater or equal 1");

Ok(ResizableSharedMemoryView {
shared_memory_vec: UniqueIndexVector::from([shm]),
base_name: self.base_name,
shared_memory_map,
current_idx,
})
}
}

pub struct ResizableSharedMemoryView<T: SharedMemory<PoolAllocator>> {
shared_memory_vec: UniqueIndexVector<T>,
base_name: FileName,
shared_memory_map: SlotMap<T>,
current_idx: SlotMapKey,
}

impl<T: SharedMemory<PoolAllocator>> ResizableSharedMemoryView<T> {
fn translate_offset(&self, offset: DynamicPointerOffset) -> usize {
// updates and remaps shared memories
//
todo!()
fn update_map_view(&mut self) {}

pub fn translate_offset(&mut self, offset: DynamicPointerOffset) -> usize {
let segment_id = offset.segment_id();
let offset = offset.offset().value();

match self
.shared_memory_map
.get(SlotMapKey::new(segment_id as usize))
{
None => {
self.update_map_view();
todo!()
}
Some(shm) => offset + shm.payload_start_address(),
}
}
}

pub struct ResizableSharedMemory<T: SharedMemory<PoolAllocator>> {
shared_memory_vec: UniqueIndexVector<T>,
current_idx: u8,
base_name: FileName,
shared_memory_map: SlotMap<T>,
current_idx: SlotMapKey,
number_of_buckets: usize,
bucket_layout: Layout,
has_ownership: IoxAtomicBool,
}

impl<T: SharedMemory<PoolAllocator>> ResizableSharedMemory<T> {
fn name(&self) -> &FileName {
todo!()
}

unsafe fn remove(
name: &FileName,
config: &T::Configuration,
Expand Down Expand Up @@ -240,24 +241,24 @@ impl<T: SharedMemory<PoolAllocator>> ResizableSharedMemory<T> {

/// Returns if the [`SharedMemory`] supports persistency, meaning that the underlying OS
/// resource remain even when every [`SharedMemory`] instance in every process was removed.
fn does_support_persistency() -> bool {
todo!()
pub fn does_support_persistency() -> bool {
T::does_support_persistency()
}

/// Returns true if the [`SharedMemory`] holds the ownership, otherwise false
fn has_ownership(&self) -> bool {
todo!()
pub fn has_ownership(&self) -> bool {
self.has_ownership.load(Ordering::Relaxed)
}

/// Acquires the ownership of the [`SharedMemory`]. When the object goes out of scope the
/// underlying resources will be removed.
fn acquire_ownership(&self) {
todo!()
pub fn acquire_ownership(&self) {
self.has_ownership.store(true, Ordering::Relaxed);
}

/// Releases the ownership of the [`SharedMemory`] meaning when it goes out of scope the
/// underlying resource will not be removed.
fn release_ownership(&self) {
todo!()
pub fn release_ownership(&self) {
self.has_ownership.store(false, Ordering::Relaxed);
}
}

0 comments on commit 1f84877

Please sign in to comment.