From d4321bcf21bc33c2bd3ce23a2679529c375d14ff Mon Sep 17 00:00:00 2001 From: Christian Eltzschig Date: Tue, 12 Nov 2024 19:14:29 +0100 Subject: [PATCH] [#504] Integrate GenericPointer --- iceoryx2-bb/container/src/queue.rs | 28 +++-- iceoryx2-bb/container/src/slotmap.rs | 119 +++++------------- iceoryx2-bb/container/src/vec.rs | 37 +++--- iceoryx2-bb/elementary/src/generic_pointer.rs | 4 +- iceoryx2-bb/elementary/src/owning_pointer.rs | 4 +- iceoryx2-bb/elementary/src/relocatable_ptr.rs | 5 +- 6 files changed, 78 insertions(+), 119 deletions(-) diff --git a/iceoryx2-bb/container/src/queue.rs b/iceoryx2-bb/container/src/queue.rs index b8d57e208..29efa6757 100644 --- a/iceoryx2-bb/container/src/queue.rs +++ b/iceoryx2-bb/container/src/queue.rs @@ -104,11 +104,11 @@ 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; @@ -116,18 +116,20 @@ 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 = details::MetaQueue>>; +pub type Queue = details::MetaQueue; /// **Non-movable** relocatable queue with runtime fixed size capacity. -pub type RelocatableQueue = details::MetaQueue>>; +pub type RelocatableQueue = details::MetaQueue; #[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>> { - data_ptr: PointerType, + pub struct MetaQueue { + data_ptr: Ptr::Type>, start: usize, len: usize, capacity: usize, @@ -135,9 +137,9 @@ pub mod details { _phantom_data: PhantomData, } - unsafe impl>> Send for MetaQueue {} + unsafe impl Send for MetaQueue {} - impl MetaQueue>> { + impl MetaQueue { /// Creates a new [`Queue`] with the provided capacity pub fn new(capacity: usize) -> Self { Self { @@ -184,7 +186,7 @@ pub mod details { } } - impl> + Debug> MetaQueue { + impl MetaQueue { /// Returns a copy of the element stored at index. The index is starting by 0 for the first /// element until [`Queue::len()`]. /// @@ -212,7 +214,7 @@ pub mod details { } } - impl RelocatableContainer for MetaQueue>> { + impl RelocatableContainer for MetaQueue { unsafe fn new_uninit(capacity: usize) -> Self { Self { data_ptr: RelocatablePointer::new_uninit(), @@ -255,7 +257,7 @@ pub mod details { } } - impl MetaQueue>> { + impl MetaQueue { /// Returns the required memory size for a queue with a specified capacity pub const fn const_memory_size(capacity: usize) -> usize { unaligned_mem_size::(capacity) @@ -325,7 +327,7 @@ pub mod details { } } - impl>> MetaQueue { + impl MetaQueue { #[inline(always)] fn verify_init(&self, source: &str) { debug_assert!( @@ -438,7 +440,7 @@ pub mod details { } } - impl>> Drop for MetaQueue { + impl Drop for MetaQueue { fn drop(&mut self) { if self .is_initialized diff --git a/iceoryx2-bb/container/src/slotmap.rs b/iceoryx2-bb/container/src/slotmap.rs index 0759bff29..970a91547 100644 --- a/iceoryx2-bb/container/src/slotmap.rs +++ b/iceoryx2-bb/container/src/slotmap.rs @@ -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. @@ -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 = details::MetaSlotMap< - T, - OwningPointer>>, - OwningPointer>, ->; +pub type SlotMap = details::MetaSlotMap; /// A runtime fixed-size, shared-memory compatible [`RelocatableSlotMap`]. -pub type RelocatableSlotMap = details::MetaSlotMap< - T, - RelocatablePointer>>, - RelocatablePointer>, ->; +pub type RelocatableSlotMap = details::MetaSlotMap; 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>>, - IdxPtrType: PointerTrait>, - > { - slotmap: &'slotmap MetaSlotMap, + pub struct Iter<'slotmap, T, Ptr: GenericPointer> { + slotmap: &'slotmap MetaSlotMap, key: SlotMapKey, } - pub type OwningIter<'slotmap, T> = - Iter<'slotmap, T, OwningPointer>>, OwningPointer>>; - pub type RelocatableIter<'slotmap, T> = Iter< - 'slotmap, - T, - RelocatablePointer>>, - RelocatablePointer>, - >; - - impl< - 'slotmap, - T, - DataPtrType: PointerTrait>>, - IdxPtrType: PointerTrait>, - > 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 { @@ -130,26 +108,17 @@ pub mod details { #[repr(C)] #[derive(Debug)] - pub struct MetaSlotMap< - T, - DataPtrType: PointerTrait>>, - IdxPtrType: PointerTrait>, - > { - idx_to_data: MetaVec, - idx_to_data_free_list: MetaVec, - data: MetaVec, DataPtrType>, - data_next_free_index: MetaQueue, + pub struct MetaSlotMap { + idx_to_data: MetaVec, + idx_to_data_free_list: MetaVec, + data: MetaVec, Ptr>, + data_next_free_index: MetaQueue, idx_to_data_free_list_head: usize, len: usize, } - impl< - T, - DataPtrType: PointerTrait>>, - IdxPtrType: PointerTrait>, - > MetaSlotMap - { + impl MetaSlotMap { fn next(&self, start: SlotMapKey) -> Option<(SlotMapKey, &T)> { let idx_to_data = &self.idx_to_data; @@ -177,7 +146,7 @@ pub mod details { } } - pub(crate) unsafe fn iter_impl(&self) -> Iter { + pub(crate) unsafe fn iter_impl(&self) -> Iter { Iter { slotmap: self, key: SlotMapKey(0), @@ -294,13 +263,7 @@ pub mod details { } } - impl RelocatableContainer - for MetaSlotMap< - T, - RelocatablePointer>>, - RelocatablePointer>, - > - { + impl RelocatableContainer for MetaSlotMap { unsafe fn new_uninit(capacity: usize) -> Self { Self { len: 0, @@ -339,24 +302,7 @@ pub mod details { } } - impl - MetaSlotMap< - T, - RelocatablePointer>>, - RelocatablePointer>, - > - { - /// Returns how many memory the [`RelocatableSlotMap`] will allocate from the allocator - /// in [`RelocatableSlotMap::init()`]. - pub const fn const_memory_size(capacity: usize) -> usize { - RelocatableVec::::const_memory_size(capacity) - + RelocatableVec::::const_memory_size(capacity) - + RelocatableVec::>::const_memory_size(capacity) - + RelocatableQueue::::const_memory_size(capacity) - } - } - - impl MetaSlotMap>>, OwningPointer>> { + impl MetaSlotMap { /// 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 { @@ -433,13 +379,16 @@ pub mod details { } } - impl - MetaSlotMap< - T, - RelocatablePointer>>, - RelocatablePointer>, - > - { + impl MetaSlotMap { + /// Returns how many memory the [`RelocatableSlotMap`] will allocate from the allocator + /// in [`RelocatableSlotMap::init()`]. + pub const fn const_memory_size(capacity: usize) -> usize { + RelocatableVec::::const_memory_size(capacity) + + RelocatableVec::::const_memory_size(capacity) + + RelocatableVec::>::const_memory_size(capacity) + + RelocatableQueue::::const_memory_size(capacity) + } + /// Returns the [`Iter`]ator to iterate over all entries. /// /// # Safety diff --git a/iceoryx2-bb/container/src/vec.rs b/iceoryx2-bb/container/src/vec.rs index 75493493c..13e1c92f6 100644 --- a/iceoryx2-bb/container/src/vec.rs +++ b/iceoryx2-bb/container/src/vec.rs @@ -91,22 +91,27 @@ use std::{ sync::atomic::Ordering, }; -use iceoryx2_bb_elementary::bump_allocator::BumpAllocator; +use iceoryx2_bb_elementary::generic_pointer::GenericPointer; +use iceoryx2_bb_elementary::{ + bump_allocator::BumpAllocator, owning_pointer::GenericOwningPointer, + relocatable_ptr::GenericRelocatablePointer, +}; use iceoryx2_bb_elementary::{ math::unaligned_mem_size, owning_pointer::OwningPointer, placement_default::PlacementDefault, pointer_trait::PointerTrait, relocatable_container::RelocatableContainer, relocatable_ptr::RelocatablePointer, }; + use iceoryx2_bb_log::{fail, fatal_panic}; use iceoryx2_pal_concurrency_sync::iox_atomic::IoxAtomicBool; use serde::{de::Visitor, Deserialize, Serialize}; /// Vector with run-time fixed size capacity. In contrast to its counterpart the /// [`RelocatableVec`] it is movable but is not shared memory compatible. -pub type Vec = details::MetaVec>>; +pub type Vec = details::MetaVec; /// **Non-movable** relocatable vector with runtime fixed size capacity. -pub type RelocatableVec = details::MetaVec>>; +pub type RelocatableVec = details::MetaVec; #[doc(hidden)] pub mod details { @@ -115,17 +120,17 @@ pub mod details { /// **Non-movable** relocatable vector with runtime fixed size capacity. #[repr(C)] #[derive(Debug)] - pub struct MetaVec>> { - data_ptr: PointerType, + pub struct MetaVec { + data_ptr: Ptr::Type>, capacity: usize, len: usize, is_initialized: IoxAtomicBool, _phantom_data: PhantomData, } - unsafe impl>> Send for MetaVec {} + unsafe impl Send for MetaVec {} - impl>> Drop for MetaVec { + impl Drop for MetaVec { fn drop(&mut self) { if self .is_initialized @@ -136,7 +141,7 @@ pub mod details { } } - impl RelocatableContainer for MetaVec>> { + impl RelocatableContainer for MetaVec { unsafe fn new_uninit(capacity: usize) -> Self { Self { data_ptr: RelocatablePointer::new_uninit(), @@ -172,7 +177,7 @@ pub mod details { } } - impl>> Deref for MetaVec { + impl Deref for MetaVec { type Target = [T]; fn deref(&self) -> &Self::Target { @@ -181,7 +186,7 @@ pub mod details { } } - impl>> DerefMut for MetaVec { + impl DerefMut for MetaVec { fn deref_mut(&mut self) -> &mut Self::Target { self.verify_init(&format!("Vec<{}>::push()", std::any::type_name::())); unsafe { @@ -193,9 +198,7 @@ pub mod details { } } - impl>> PartialEq - for MetaVec - { + impl PartialEq for MetaVec { fn eq(&self, other: &Self) -> bool { if other.len() != self.len() { return false; @@ -211,9 +214,9 @@ pub mod details { } } - impl>> Eq for MetaVec {} + impl Eq for MetaVec {} - impl>> MetaVec { + impl MetaVec { #[inline(always)] fn verify_init(&self, source: &str) { debug_assert!( @@ -329,7 +332,7 @@ pub mod details { } } - impl MetaVec>> { + impl MetaVec { /// Creates a new [`Queue`] with the provided capacity pub fn new(capacity: usize) -> Self { Self { @@ -390,7 +393,7 @@ pub mod details { } } - impl MetaVec>> { + impl MetaVec { /// Returns the required memory size for a vec with a specified capacity pub const fn const_memory_size(capacity: usize) -> usize { unaligned_mem_size::(capacity) diff --git a/iceoryx2-bb/elementary/src/generic_pointer.rs b/iceoryx2-bb/elementary/src/generic_pointer.rs index 7d166e16c..b24946b59 100644 --- a/iceoryx2-bb/elementary/src/generic_pointer.rs +++ b/iceoryx2-bb/elementary/src/generic_pointer.rs @@ -10,10 +10,12 @@ // // SPDX-License-Identifier: Apache-2.0 OR MIT +use std::fmt::Debug; + use crate::pointer_trait::PointerTrait; /// Trait that allows to use typed pointers as generic arguments for structs. pub trait GenericPointer { /// The underlying pointer type. - type Type: PointerTrait; + type Type: PointerTrait + Debug; } diff --git a/iceoryx2-bb/elementary/src/owning_pointer.rs b/iceoryx2-bb/elementary/src/owning_pointer.rs index f1f6f5f13..bc3966091 100644 --- a/iceoryx2-bb/elementary/src/owning_pointer.rs +++ b/iceoryx2-bb/elementary/src/owning_pointer.rs @@ -15,10 +15,12 @@ use std::alloc::Layout; use std::alloc::{alloc, dealloc}; +use std::fmt::Debug; use crate::generic_pointer::GenericPointer; use crate::pointer_trait::PointerTrait; +#[derive(Debug)] pub struct GenericOwningPointer; /// Representation of a pointer which owns its memory. @@ -68,5 +70,5 @@ impl PointerTrait for OwningPointer { } impl GenericPointer for GenericOwningPointer { - type Type = OwningPointer; + type Type = OwningPointer; } diff --git a/iceoryx2-bb/elementary/src/relocatable_ptr.rs b/iceoryx2-bb/elementary/src/relocatable_ptr.rs index d4e48d6a4..4529f613a 100644 --- a/iceoryx2-bb/elementary/src/relocatable_ptr.rs +++ b/iceoryx2-bb/elementary/src/relocatable_ptr.rs @@ -67,8 +67,9 @@ use crate::generic_pointer::GenericPointer; pub use crate::pointer_trait::PointerTrait; use iceoryx2_pal_concurrency_sync::iox_atomic::IoxAtomicIsize; -use std::{marker::PhantomData, ptr::NonNull}; +use std::{fmt::Debug, marker::PhantomData, ptr::NonNull}; +#[derive(Debug)] pub struct GenericRelocatablePointer; /// A [`RelocatablePointer`] stores only the distance from its memory starting position to the @@ -143,5 +144,5 @@ impl PointerTrait for RelocatablePointer { } impl GenericPointer for GenericRelocatablePointer { - type Type = RelocatablePointer; + type Type = RelocatablePointer; }