From 9826285c51f8af2205d043f52f4965248be1bf93 Mon Sep 17 00:00:00 2001 From: Christian Eltzschig Date: Wed, 5 Jun 2024 16:52:49 +0200 Subject: [PATCH] [#224] Implement PlacementDefault for Vec, Queue and SemanticString --- iceoryx2-bb/container/src/queue.rs | 24 +++++++++++++++----- iceoryx2-bb/container/src/semantic_string.rs | 9 ++++++++ iceoryx2-bb/container/src/vec.rs | 24 +++++++++++++++----- 3 files changed, 45 insertions(+), 12 deletions(-) diff --git a/iceoryx2-bb/container/src/queue.rs b/iceoryx2-bb/container/src/queue.rs index ba616c7eb..20c6448c7 100644 --- a/iceoryx2-bb/container/src/queue.rs +++ b/iceoryx2-bb/container/src/queue.rs @@ -99,6 +99,7 @@ use iceoryx2_bb_elementary::allocator::{AllocationError, BaseAllocator}; use iceoryx2_bb_elementary::math::align_to; use iceoryx2_bb_elementary::math::unaligned_mem_size; use iceoryx2_bb_elementary::owning_pointer::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; @@ -397,15 +398,17 @@ pub struct FixedSizeQueue { _data: [MaybeUninit; CAPACITY], } +impl PlacementDefault for FixedSizeQueue { + unsafe fn placement_default(ptr: *mut Self) { + let state_ptr = core::ptr::addr_of_mut!((&mut *ptr).state); + state_ptr.write(Self::initialize_state()); + } +} + impl Default for FixedSizeQueue { fn default() -> Self { Self { - state: unsafe { - RelocatableQueue::new( - CAPACITY, - align_to::>(std::mem::size_of::>()) as isize, - ) - }, + state: Self::initialize_state(), _data: unsafe { MaybeUninit::uninit().assume_init() }, } } @@ -415,6 +418,15 @@ unsafe impl Send for FixedSizeQueue unsafe impl Sync for FixedSizeQueue {} impl FixedSizeQueue { + fn initialize_state() -> RelocatableQueue { + unsafe { + RelocatableQueue::new( + CAPACITY, + align_to::>(std::mem::size_of::>()) as isize, + ) + } + } + /// Creates a new queue. pub fn new() -> Self { Self::default() diff --git a/iceoryx2-bb/container/src/semantic_string.rs b/iceoryx2-bb/container/src/semantic_string.rs index 87df5a91d..a363f3f33 100644 --- a/iceoryx2-bb/container/src/semantic_string.rs +++ b/iceoryx2-bb/container/src/semantic_string.rs @@ -60,6 +60,8 @@ //! } //! ``` +pub use iceoryx2_bb_elementary::placement_default::PlacementDefault; + use crate::byte_string::FixedSizeByteStringModificationError; use crate::byte_string::{as_escaped_string, strnlen, FixedSizeByteString}; use iceoryx2_bb_elementary::enum_gen; @@ -379,6 +381,13 @@ macro_rules! semantic_string { value: iceoryx2_bb_container::byte_string::FixedSizeByteString<$capacity> } + impl iceoryx2_bb_container::semantic_string::PlacementDefault for $string_name { + unsafe fn placement_default(ptr: *mut Self) { + let ptr = core::ptr::addr_of_mut!((&mut *ptr).value); + iceoryx2_bb_container::semantic_string::PlacementDefault::placement_default(ptr) + } + } + impl iceoryx2_bb_container::semantic_string::SemanticString<$capacity> for $string_name { fn as_string(&self) -> &iceoryx2_bb_container::byte_string::FixedSizeByteString<$capacity> { &self.value diff --git a/iceoryx2-bb/container/src/vec.rs b/iceoryx2-bb/container/src/vec.rs index 0bb7614d2..b663f57f6 100644 --- a/iceoryx2-bb/container/src/vec.rs +++ b/iceoryx2-bb/container/src/vec.rs @@ -79,6 +79,7 @@ use std::{alloc::Layout, mem::MaybeUninit, ops::Deref, ops::DerefMut, sync::atom use iceoryx2_bb_elementary::{ math::{align_to, unaligned_mem_size}, + placement_default::PlacementDefault, pointer_trait::PointerTrait, relocatable_container::RelocatableContainer, relocatable_ptr::RelocatablePointer, @@ -330,15 +331,17 @@ pub struct FixedSizeVec { _data: [MaybeUninit; CAPACITY], } +impl PlacementDefault for FixedSizeVec { + unsafe fn placement_default(ptr: *mut Self) { + let state_ptr = core::ptr::addr_of_mut!((&mut *ptr).state); + state_ptr.write(Self::initialize_state()) + } +} + impl Default for FixedSizeVec { fn default() -> Self { Self { - state: unsafe { - RelocatableVec::new( - CAPACITY, - align_to::>(std::mem::size_of::>()) as isize, - ) - }, + state: Self::initialize_state(), _data: core::array::from_fn(|_| MaybeUninit::uninit()), } } @@ -377,6 +380,15 @@ impl Clone for FixedSizeVec { unsafe impl Send for FixedSizeVec {} impl FixedSizeVec { + fn initialize_state() -> RelocatableVec { + unsafe { + RelocatableVec::new( + CAPACITY, + align_to::>(std::mem::size_of::>()) as isize, + ) + } + } + /// Creates a new vector. pub fn new() -> Self { Self::default()