Skip to content

Commit

Permalink
[eclipse-iceoryx#504] FixedSizeSlot initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
elfenpiff committed Nov 12, 2024
1 parent d0f87fe commit df03da6
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 21 deletions.
54 changes: 33 additions & 21 deletions iceoryx2-bb/container/src/slotmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use crate::queue::details::MetaQueue;
use crate::vec::details::MetaVec;
use crate::{queue::RelocatableQueue, vec::RelocatableVec};
use iceoryx2_bb_elementary::bump_allocator::BumpAllocator;
use iceoryx2_bb_elementary::math::align_to;
use iceoryx2_bb_elementary::owning_pointer::OwningPointer;
use iceoryx2_bb_elementary::pointer_trait::PointerTrait;
Expand Down Expand Up @@ -107,6 +108,15 @@ pub mod details {
None
}

pub(crate) unsafe fn initialize_data_structures(&mut self) {
self.idx_to_data.fill(INVALID_KEY);
self.data.fill_with(|| None);
for n in 0..self.capacity_impl() {
self.idx_to_data_next_free_index.push_impl(n);
self.data_next_free_index.push_impl(n);
}
}

pub(crate) unsafe fn iter_impl(&self) -> Iter<T, DataPtrType, IdxPtrType> {
Iter {
slotmap: self,
Expand Down Expand Up @@ -178,7 +188,7 @@ pub mod details {
}

pub(crate) unsafe fn len_impl(&self) -> usize {
self.capacity_impl() - self.data_next_free_index.len()
self.capacity_impl() - self.idx_to_data_next_free_index.len()
}

pub(crate) unsafe fn capacity_impl(&self) -> usize {
Expand All @@ -202,7 +212,7 @@ pub mod details {
>
{
unsafe fn new(capacity: usize, distance_to_data: isize) -> Self {
Self {
let mut new_self = Self {
idx_to_data: RelocatableVec::new(capacity, distance_to_data),
idx_to_data_next_free_index: RelocatableQueue::new(
capacity,
Expand All @@ -222,7 +232,9 @@ pub mod details {
+ RelocatableQueue::<usize>::const_memory_size(capacity) as isize
+ RelocatableVec::<Option<T>>::const_memory_size(capacity) as isize,
),
}
};
new_self.initialize_data_structures();
new_self
}

unsafe fn new_uninit(capacity: usize) -> Self {
Expand Down Expand Up @@ -283,13 +295,7 @@ pub mod details {
data: MetaVec::new(capacity),
data_next_free_index: MetaQueue::new(capacity),
};

new_self.idx_to_data.fill(INVALID_KEY);
new_self.data.fill_with(|| None);
for n in 0..capacity {
new_self.idx_to_data_next_free_index.push(n);
new_self.data_next_free_index.push(n);
}
unsafe { new_self.initialize_data_structures() };
new_self
}

Expand Down Expand Up @@ -404,24 +410,30 @@ pub struct FixedSizeSlotMap<T, const CAPACITY: usize> {

impl<T, const CAPACITY: usize> Default for FixedSizeSlotMap<T, CAPACITY> {
fn default() -> Self {
Self {
state: Self::initialize_state(),
let mut new_self = Self {
_idx_to_data: core::array::from_fn(|_| INVALID_KEY),
_idx_to_data_next_free_index: core::array::from_fn(|i| i),
_idx_to_data_next_free_index: core::array::from_fn(|_| 0),
_data: core::array::from_fn(|_| None),
_data_next_free_index: core::array::from_fn(|_| INVALID_KEY),
}
_data_next_free_index: core::array::from_fn(|_| 0),
state: Self::initialize_state(),
};

let allocator = BumpAllocator::new(core::ptr::addr_of!(new_self._idx_to_data) as usize);
unsafe {
new_self
.state
.init(&allocator)
.expect("All required memory is preallocated.")
};
unsafe { new_self.state.initialize_data_structures() };

new_self
}
}

impl<T, const CAPACITY: usize> FixedSizeSlotMap<T, CAPACITY> {
fn initialize_state() -> RelocatableSlotMap<T> {
unsafe {
RelocatableSlotMap::new(
CAPACITY,
align_to::<MaybeUninit<T>>(std::mem::size_of::<RelocatableSlotMap<T>>()) as isize,
)
}
unsafe { RelocatableSlotMap::new_uninit(CAPACITY) }
}

pub fn new() -> Self {
Expand Down
12 changes: 12 additions & 0 deletions iceoryx2-bb/container/tests/slotmap_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,29 @@ use iceoryx2_bb_testing::assert_that;

mod slot_map {

use iceoryx2_bb_container::slotmap::FixedSizeSlotMap;

use super::*;

const SUT_CAPACITY: usize = 128;
type Sut = SlotMap<usize>;
type FixedSizeSut = FixedSizeSlotMap<usize, SUT_CAPACITY>;

#[test]
fn new_slotmap_is_empty() {
let sut = Sut::new(SUT_CAPACITY);

assert_that!(sut, len 0);
assert_that!(sut, is_empty);
assert_that!(sut.is_full(), eq false);
}

#[test]
fn new_fixed_size_slotmap_is_empty() {
let sut = FixedSizeSut::new();

assert_that!(sut, len 0);
assert_that!(sut, is_empty);
assert_that!(sut.is_full(), eq false);
}
}

0 comments on commit df03da6

Please sign in to comment.