Skip to content

Commit

Permalink
[#504] Integrate GenericPointer
Browse files Browse the repository at this point in the history
  • Loading branch information
elfenpiff committed Nov 12, 2024
1 parent 2b3c766 commit d4321bc
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 119 deletions.
28 changes: 15 additions & 13 deletions iceoryx2-bb/container/src/queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,40 +104,42 @@
use iceoryx2_bb_elementary::allocator::{AllocationError, BaseAllocator};
use iceoryx2_bb_elementary::bump_allocator::BumpAllocator;
use iceoryx2_bb_elementary::math::unaligned_mem_size;
use iceoryx2_bb_elementary::owning_pointer::OwningPointer;
use iceoryx2_bb_elementary::owning_pointer::{GenericOwningPointer, OwningPointer};
use iceoryx2_bb_elementary::placement_default::PlacementDefault;
use iceoryx2_bb_elementary::pointer_trait::PointerTrait;
pub use iceoryx2_bb_elementary::relocatable_container::RelocatableContainer;
use iceoryx2_bb_elementary::relocatable_ptr::RelocatablePointer;
use iceoryx2_bb_elementary::relocatable_ptr::{GenericRelocatablePointer, RelocatablePointer};
use iceoryx2_bb_log::{fail, fatal_panic};
use iceoryx2_pal_concurrency_sync::iox_atomic::IoxAtomicBool;
use std::marker::PhantomData;
use std::{alloc::Layout, fmt::Debug, mem::MaybeUninit};

/// Queue with run-time fixed size capacity. In contrast to its counterpart the
/// [`RelocatableQueue`] it is movable but is not shared memory compatible.
pub type Queue<T> = details::MetaQueue<T, OwningPointer<MaybeUninit<T>>>;
pub type Queue<T> = details::MetaQueue<T, GenericOwningPointer>;
/// **Non-movable** relocatable queue with runtime fixed size capacity.
pub type RelocatableQueue<T> = details::MetaQueue<T, RelocatablePointer<MaybeUninit<T>>>;
pub type RelocatableQueue<T> = details::MetaQueue<T, GenericRelocatablePointer>;

#[doc(hidden)]
pub mod details {
use iceoryx2_bb_elementary::generic_pointer::GenericPointer;

use super::*;
/// **Non-movable** relocatable queue with runtime fixed size capacity.
#[repr(C)]
#[derive(Debug)]
pub struct MetaQueue<T, PointerType: PointerTrait<MaybeUninit<T>>> {
data_ptr: PointerType,
pub struct MetaQueue<T, Ptr: GenericPointer> {
data_ptr: Ptr::Type<MaybeUninit<T>>,
start: usize,
len: usize,
capacity: usize,
is_initialized: IoxAtomicBool,
_phantom_data: PhantomData<T>,
}

unsafe impl<T: Send, PointerType: PointerTrait<MaybeUninit<T>>> Send for MetaQueue<T, PointerType> {}
unsafe impl<T: Send, Ptr: GenericPointer> Send for MetaQueue<T, Ptr> {}

impl<T> MetaQueue<T, OwningPointer<MaybeUninit<T>>> {
impl<T> MetaQueue<T, GenericOwningPointer> {
/// Creates a new [`Queue`] with the provided capacity
pub fn new(capacity: usize) -> Self {
Self {
Expand Down Expand Up @@ -184,7 +186,7 @@ pub mod details {
}
}

impl<T: Copy + Debug, PointerType: PointerTrait<MaybeUninit<T>> + Debug> MetaQueue<T, PointerType> {
impl<T: Copy + Debug, Ptr: GenericPointer + Debug> MetaQueue<T, Ptr> {
/// Returns a copy of the element stored at index. The index is starting by 0 for the first
/// element until [`Queue::len()`].
///
Expand Down Expand Up @@ -212,7 +214,7 @@ pub mod details {
}
}

impl<T> RelocatableContainer for MetaQueue<T, RelocatablePointer<MaybeUninit<T>>> {
impl<T> RelocatableContainer for MetaQueue<T, GenericRelocatablePointer> {
unsafe fn new_uninit(capacity: usize) -> Self {
Self {
data_ptr: RelocatablePointer::new_uninit(),
Expand Down Expand Up @@ -255,7 +257,7 @@ pub mod details {
}
}

impl<T> MetaQueue<T, RelocatablePointer<MaybeUninit<T>>> {
impl<T> MetaQueue<T, GenericRelocatablePointer> {
/// Returns the required memory size for a queue with a specified capacity
pub const fn const_memory_size(capacity: usize) -> usize {
unaligned_mem_size::<T>(capacity)
Expand Down Expand Up @@ -325,7 +327,7 @@ pub mod details {
}
}

impl<T, PointerType: PointerTrait<MaybeUninit<T>>> MetaQueue<T, PointerType> {
impl<T, Ptr: GenericPointer> MetaQueue<T, Ptr> {
#[inline(always)]
fn verify_init(&self, source: &str) {
debug_assert!(
Expand Down Expand Up @@ -438,7 +440,7 @@ pub mod details {
}
}

impl<T, PointerType: PointerTrait<MaybeUninit<T>>> Drop for MetaQueue<T, PointerType> {
impl<T, Ptr: GenericPointer> Drop for MetaQueue<T, Ptr> {
fn drop(&mut self) {
if self
.is_initialized
Expand Down
119 changes: 34 additions & 85 deletions iceoryx2-bb/container/src/slotmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,11 @@ 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::owning_pointer::OwningPointer;
use iceoryx2_bb_elementary::owning_pointer::GenericOwningPointer;
use iceoryx2_bb_elementary::placement_default::PlacementDefault;
use iceoryx2_bb_elementary::pointer_trait::PointerTrait;
use iceoryx2_bb_elementary::relocatable_container::RelocatableContainer;
use iceoryx2_bb_elementary::relocatable_ptr::RelocatablePointer;
use iceoryx2_bb_elementary::relocatable_ptr::GenericRelocatablePointer;
use iceoryx2_bb_log::fail;
use std::mem::MaybeUninit;

/// A key of a [`SlotMap`], [`RelocatableSlotMap`] or [`FixedSizeSlotMap`] that identifies a
/// value.
Expand All @@ -70,52 +68,32 @@ struct FreeListEntry {

/// A runtime fixed-size, non-shared memory compatible [`SlotMap`]. The [`SlotMap`]s memory resides
/// in the heap.
pub type SlotMap<T> = details::MetaSlotMap<
T,
OwningPointer<MaybeUninit<Option<T>>>,
OwningPointer<MaybeUninit<usize>>,
>;
pub type SlotMap<T> = details::MetaSlotMap<T, GenericOwningPointer>;

/// A runtime fixed-size, shared-memory compatible [`RelocatableSlotMap`].
pub type RelocatableSlotMap<T> = details::MetaSlotMap<
T,
RelocatablePointer<MaybeUninit<Option<T>>>,
RelocatablePointer<MaybeUninit<usize>>,
>;
pub type RelocatableSlotMap<T> = details::MetaSlotMap<T, GenericRelocatablePointer>;

const INVALID_KEY: usize = usize::MAX;

#[doc(hidden)]
pub mod details {
use iceoryx2_bb_elementary::{
generic_pointer::GenericPointer, owning_pointer::GenericOwningPointer,
relocatable_ptr::GenericRelocatablePointer,
};

use super::*;

/// The iterator of a [`SlotMap`], [`RelocatableSlotMap`] or [`FixedSizeSlotMap`].
pub struct Iter<
'slotmap,
T,
DataPtrType: PointerTrait<MaybeUninit<Option<T>>>,
IdxPtrType: PointerTrait<MaybeUninit<usize>>,
> {
slotmap: &'slotmap MetaSlotMap<T, DataPtrType, IdxPtrType>,
pub struct Iter<'slotmap, T, Ptr: GenericPointer> {
slotmap: &'slotmap MetaSlotMap<T, Ptr>,
key: SlotMapKey,
}

pub type OwningIter<'slotmap, T> =
Iter<'slotmap, T, OwningPointer<MaybeUninit<Option<T>>>, OwningPointer<MaybeUninit<usize>>>;
pub type RelocatableIter<'slotmap, T> = Iter<
'slotmap,
T,
RelocatablePointer<MaybeUninit<Option<T>>>,
RelocatablePointer<MaybeUninit<usize>>,
>;

impl<
'slotmap,
T,
DataPtrType: PointerTrait<MaybeUninit<Option<T>>>,
IdxPtrType: PointerTrait<MaybeUninit<usize>>,
> Iterator for Iter<'slotmap, T, DataPtrType, IdxPtrType>
{
pub type OwningIter<'slotmap, T> = Iter<'slotmap, T, GenericOwningPointer>;
pub type RelocatableIter<'slotmap, T> = Iter<'slotmap, T, GenericRelocatablePointer>;

impl<'slotmap, T, Ptr: GenericPointer> Iterator for Iter<'slotmap, T, Ptr> {
type Item = (SlotMapKey, &'slotmap T);

fn next(&mut self) -> Option<Self::Item> {
Expand All @@ -130,26 +108,17 @@ pub mod details {

#[repr(C)]
#[derive(Debug)]
pub struct MetaSlotMap<
T,
DataPtrType: PointerTrait<MaybeUninit<Option<T>>>,
IdxPtrType: PointerTrait<MaybeUninit<usize>>,
> {
idx_to_data: MetaVec<usize, IdxPtrType>,
idx_to_data_free_list: MetaVec<usize, IdxPtrType>,
data: MetaVec<Option<T>, DataPtrType>,
data_next_free_index: MetaQueue<usize, IdxPtrType>,
pub struct MetaSlotMap<T, Ptr: GenericPointer> {
idx_to_data: MetaVec<usize, Ptr>,
idx_to_data_free_list: MetaVec<usize, Ptr>,
data: MetaVec<Option<T>, Ptr>,
data_next_free_index: MetaQueue<usize, Ptr>,

idx_to_data_free_list_head: usize,
len: usize,
}

impl<
T,
DataPtrType: PointerTrait<MaybeUninit<Option<T>>>,
IdxPtrType: PointerTrait<MaybeUninit<usize>>,
> MetaSlotMap<T, DataPtrType, IdxPtrType>
{
impl<T, Ptr: GenericPointer> MetaSlotMap<T, Ptr> {
fn next(&self, start: SlotMapKey) -> Option<(SlotMapKey, &T)> {
let idx_to_data = &self.idx_to_data;

Expand Down Expand Up @@ -177,7 +146,7 @@ pub mod details {
}
}

pub(crate) unsafe fn iter_impl(&self) -> Iter<T, DataPtrType, IdxPtrType> {
pub(crate) unsafe fn iter_impl(&self) -> Iter<T, Ptr> {
Iter {
slotmap: self,
key: SlotMapKey(0),
Expand Down Expand Up @@ -294,13 +263,7 @@ pub mod details {
}
}

impl<T> RelocatableContainer
for MetaSlotMap<
T,
RelocatablePointer<MaybeUninit<Option<T>>>,
RelocatablePointer<MaybeUninit<usize>>,
>
{
impl<T> RelocatableContainer for MetaSlotMap<T, GenericRelocatablePointer> {
unsafe fn new_uninit(capacity: usize) -> Self {
Self {
len: 0,
Expand Down Expand Up @@ -339,24 +302,7 @@ pub mod details {
}
}

impl<T>
MetaSlotMap<
T,
RelocatablePointer<MaybeUninit<Option<T>>>,
RelocatablePointer<MaybeUninit<usize>>,
>
{
/// Returns how many memory the [`RelocatableSlotMap`] will allocate from the allocator
/// in [`RelocatableSlotMap::init()`].
pub const fn const_memory_size(capacity: usize) -> usize {
RelocatableVec::<usize>::const_memory_size(capacity)
+ RelocatableVec::<usize>::const_memory_size(capacity)
+ RelocatableVec::<Option<T>>::const_memory_size(capacity)
+ RelocatableQueue::<usize>::const_memory_size(capacity)
}
}

impl<T> MetaSlotMap<T, OwningPointer<MaybeUninit<Option<T>>>, OwningPointer<MaybeUninit<usize>>> {
impl<T> MetaSlotMap<T, GenericOwningPointer> {
/// Creates a new runtime-fixed size [`SlotMap`] on the heap with the given capacity.
pub fn new(capacity: usize) -> Self {
let mut new_self = Self {
Expand Down Expand Up @@ -433,13 +379,16 @@ pub mod details {
}
}

impl<T>
MetaSlotMap<
T,
RelocatablePointer<MaybeUninit<Option<T>>>,
RelocatablePointer<MaybeUninit<usize>>,
>
{
impl<T> MetaSlotMap<T, GenericRelocatablePointer> {
/// Returns how many memory the [`RelocatableSlotMap`] will allocate from the allocator
/// in [`RelocatableSlotMap::init()`].
pub const fn const_memory_size(capacity: usize) -> usize {
RelocatableVec::<usize>::const_memory_size(capacity)
+ RelocatableVec::<usize>::const_memory_size(capacity)
+ RelocatableVec::<Option<T>>::const_memory_size(capacity)
+ RelocatableQueue::<usize>::const_memory_size(capacity)
}

/// Returns the [`Iter`]ator to iterate over all entries.
///
/// # Safety
Expand Down
Loading

0 comments on commit d4321bc

Please sign in to comment.