Skip to content

Commit

Permalink
[eclipse-iceoryx#129] Introduce unaligned_mem_size; simplify const_me…
Browse files Browse the repository at this point in the history
…mory_size functions
  • Loading branch information
elfenpiff committed Mar 6, 2024
1 parent cc5fca6 commit b787bf5
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 13 deletions.
4 changes: 3 additions & 1 deletion iceoryx2-bb/container/src/queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ pub type RelocatableQueue<T> = details::Queue<T, RelocatablePointer<MaybeUninit<
mod details {
use std::marker::PhantomData;

use iceoryx2_bb_elementary::math::unaligned_mem_size;

use super::*;
/// **Non-movable** relocatable queue with runtime fixed size capacity.
#[repr(C)]
Expand Down Expand Up @@ -196,7 +198,7 @@ mod details {

/// Returns the required memory size for a queue with a specified capacity
pub const fn const_memory_size(capacity: usize) -> usize {
std::mem::size_of::<T>() * capacity + std::mem::align_of::<T>() - 1
unaligned_mem_size::<T>(capacity)
}

/// Returns true if the queue is empty, otherwise false
Expand Down
6 changes: 4 additions & 2 deletions iceoryx2-bb/container/src/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ use std::{
};

use iceoryx2_bb_elementary::{
math::align_to, pointer_trait::PointerTrait, relocatable_container::RelocatableContainer,
math::{align_to, unaligned_mem_size},
pointer_trait::PointerTrait,
relocatable_container::RelocatableContainer,
relocatable_ptr::RelocatablePointer,
};
use iceoryx2_bb_log::{fail, fatal_panic};
Expand Down Expand Up @@ -187,7 +189,7 @@ impl<T> Vec<T> {

/// Returns the required memory size for a vec with a specified capacity
pub const fn const_memory_size(capacity: usize) -> usize {
std::mem::size_of::<T>() * capacity + std::mem::align_of::<T>() - 1
unaligned_mem_size::<T>(capacity)
}

/// Returns the capacity of the vector
Expand Down
5 changes: 5 additions & 0 deletions iceoryx2-bb/elementary/src/math.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@

//! Contains simplistic math functions.
/// Returns the required memory size when alignment adjustments are taken into account
pub const fn unaligned_mem_size<T>(array_capacity: usize) -> usize {
core::mem::size_of::<T>() * array_capacity + core::mem::align_of::<T>() - 1
}

/// Aligns value to alignment. It increments value to the next multiple of alignment.
pub const fn align(value: usize, alignment: usize) -> usize {
if value % alignment == 0 {
Expand Down
8 changes: 4 additions & 4 deletions iceoryx2-bb/lock-free/src/mpmc/container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
//! ```
use iceoryx2_bb_elementary::allocator::AllocationError;
use iceoryx2_bb_elementary::math::unaligned_mem_size;
use iceoryx2_bb_elementary::pointer_trait::PointerTrait;
use iceoryx2_bb_elementary::relocatable_container::RelocatableContainer;
use iceoryx2_bb_elementary::relocatable_ptr::RelocatablePointer;
Expand Down Expand Up @@ -235,12 +236,11 @@ impl<T: Copy + Debug> Container<T> {

/// Returns the required memory size of the data segment of the [`Container`].
pub const fn const_memory_size(capacity: usize) -> usize {
// UniqueIndexSet
(std::mem::size_of::<u32>() * (capacity + 1) + std::mem::align_of::<u32>() - 1)
UniqueIndexSet::const_memory_size(capacity)
// ActiveIndexPtr
+ (std::mem::size_of::<AtomicU64>() * capacity + std::mem::align_of::<u64>() - 1)
+ unaligned_mem_size::<AtomicU64>(capacity)
// data ptr
+ (std::mem::size_of::<T>() * capacity + std::mem::align_of::<T>() - 1)
+ unaligned_mem_size::<T>(capacity)
}

/// Returns the capacity of the container.
Expand Down
4 changes: 3 additions & 1 deletion iceoryx2-bb/lock-free/src/spsc/index_queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ pub type RelocatableIndexQueue = details::IndexQueue<RelocatablePointer<UnsafeCe
pub mod details {
use std::fmt::Debug;

use iceoryx2_bb_elementary::math::unaligned_mem_size;

use super::*;

/// A threadsafe lock-free index queue with a capacity which can be set up at runtime, when the
Expand Down Expand Up @@ -207,7 +209,7 @@ pub mod details {
/// Returns the amount of memory required to create a [`IndexQueue`] with the provided
/// capacity.
pub const fn const_memory_size(capacity: usize) -> usize {
std::mem::size_of::<UnsafeCell<u64>>() * capacity + std::mem::align_of::<u64>() - 1
unaligned_mem_size::<UnsafeCell<u64>>(capacity)
}

unsafe fn at(&self, position: usize) -> *mut usize {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ pub type RelocatableSafelyOverflowingIndexQueue =
details::SafelyOverflowingIndexQueue<RelocatablePointer<UnsafeCell<usize>>>;

pub mod details {
use iceoryx2_bb_elementary::math::unaligned_mem_size;

use super::*;

/// A threadsafe lock-free safely overflowing index queue with a capacity which can be set up at runtime,
Expand Down Expand Up @@ -222,9 +224,7 @@ pub mod details {
/// Returns the amount of memory required to create a [`SafelyOverflowingIndexQueue`] with
/// the provided capacity.
pub const fn const_memory_size(capacity: usize) -> usize {
std::mem::size_of::<UnsafeCell<usize>>() * (capacity + 1)
+ std::mem::align_of::<usize>()
- 1
unaligned_mem_size::<UnsafeCell<usize>>(capacity + 1)
}

fn at(&self, position: usize) -> *mut usize {
Expand Down
4 changes: 2 additions & 2 deletions iceoryx2-cal/src/zero_copy_connection/used_chunk_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub type RelocatableUsedChunkList = details::UsedChunkList<RelocatablePointer<At
pub mod details {
use std::fmt::Debug;

use iceoryx2_bb_elementary::owning_pointer::OwningPointer;
use iceoryx2_bb_elementary::{math::unaligned_mem_size, owning_pointer::OwningPointer};

use super::*;

Expand Down Expand Up @@ -119,7 +119,7 @@ pub mod details {

impl<PointerType: PointerTrait<AtomicBool> + Debug> UsedChunkList<PointerType> {
pub const fn const_memory_size(capacity: usize) -> usize {
std::mem::size_of::<AtomicBool>() * capacity + std::mem::align_of::<AtomicBool>() - 1
unaligned_mem_size::<AtomicBool>(capacity)
}

pub fn capacity(&self) -> usize {
Expand Down

0 comments on commit b787bf5

Please sign in to comment.