Skip to content

Commit

Permalink
[eclipse-iceoryx#224] Implement PlacementDefault for Vec, Queue and S…
Browse files Browse the repository at this point in the history
…emanticString
  • Loading branch information
elfenpiff committed Jun 5, 2024
1 parent a9410ce commit c4d8e26
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 12 deletions.
24 changes: 18 additions & 6 deletions iceoryx2-bb/container/src/queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -397,15 +398,17 @@ pub struct FixedSizeQueue<T, const CAPACITY: usize> {
_data: [MaybeUninit<T>; CAPACITY],
}

impl<T, const CAPACITY: usize> PlacementDefault for FixedSizeQueue<T, CAPACITY> {
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<T, const CAPACITY: usize> Default for FixedSizeQueue<T, CAPACITY> {
fn default() -> Self {
Self {
state: unsafe {
RelocatableQueue::new(
CAPACITY,
align_to::<MaybeUninit<T>>(std::mem::size_of::<RelocatableQueue<T>>()) as isize,
)
},
state: Self::initialize_state(),
_data: unsafe { MaybeUninit::uninit().assume_init() },
}
}
Expand All @@ -415,6 +418,15 @@ unsafe impl<T: Send, const CAPACITY: usize> Send for FixedSizeQueue<T, CAPACITY>
unsafe impl<T: Sync, const CAPACITY: usize> Sync for FixedSizeQueue<T, CAPACITY> {}

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

/// Creates a new queue.
pub fn new() -> Self {
Self::default()
Expand Down
9 changes: 9 additions & 0 deletions iceoryx2-bb/container/src/semantic_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
24 changes: 18 additions & 6 deletions iceoryx2-bb/container/src/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -330,15 +331,17 @@ pub struct FixedSizeVec<T, const CAPACITY: usize> {
_data: [MaybeUninit<T>; CAPACITY],
}

impl<T, const CAPACITY: usize> PlacementDefault for FixedSizeVec<T, CAPACITY> {
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<T, const CAPACITY: usize> Default for FixedSizeVec<T, CAPACITY> {
fn default() -> Self {
Self {
state: unsafe {
RelocatableVec::new(
CAPACITY,
align_to::<MaybeUninit<T>>(std::mem::size_of::<RelocatableVec<T>>()) as isize,
)
},
state: Self::initialize_state(),
_data: core::array::from_fn(|_| MaybeUninit::uninit()),
}
}
Expand Down Expand Up @@ -377,6 +380,15 @@ impl<T: Clone, const CAPACITY: usize> Clone for FixedSizeVec<T, CAPACITY> {
unsafe impl<T: Send, const CAPACITY: usize> Send for FixedSizeVec<T, CAPACITY> {}

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

/// Creates a new vector.
pub fn new() -> Self {
Self::default()
Expand Down

0 comments on commit c4d8e26

Please sign in to comment.