Skip to content

Commit 6d17b9e

Browse files
committed
[eclipse-iceoryx#139] Adjust test; define BitsetElement an perform all calculation on its memory size
1 parent eb0babd commit 6d17b9e

File tree

4 files changed

+32
-29
lines changed

4 files changed

+32
-29
lines changed

iceoryx2-bb/lock-free/src/mpmc/bit_set.rs

+29-26
Original file line numberDiff line numberDiff line change
@@ -49,29 +49,33 @@ use iceoryx2_bb_elementary::{
4949
relocatable_container::RelocatableContainer,
5050
relocatable_ptr::{PointerTrait, RelocatablePointer},
5151
};
52+
5253
use iceoryx2_bb_log::{fail, fatal_panic};
5354

55+
type BitsetElement = AtomicU8;
56+
const BITSET_ELEMENT_SIZE: usize = core::mem::size_of::<BitsetElement>();
57+
5458
/// This BitSet variant's data is stored in the heap.
55-
pub type BitSet = details::BitSet<OwningPointer<AtomicU8>>;
59+
pub type BitSet = details::BitSet<OwningPointer<BitsetElement>>;
5660
/// This BitSet variant can be stored inside shared memory.
57-
pub type RelocatableBitSet = details::BitSet<RelocatablePointer<AtomicU8>>;
61+
pub type RelocatableBitSet = details::BitSet<RelocatablePointer<BitsetElement>>;
5862

5963
pub mod details {
6064
use super::*;
6165

6266
#[derive(Debug)]
6367
#[repr(C)]
64-
pub struct BitSet<PointerType: PointerTrait<AtomicU8>> {
68+
pub struct BitSet<PointerType: PointerTrait<BitsetElement>> {
6569
data_ptr: PointerType,
6670
capacity: usize,
6771
array_capacity: usize,
6872
is_memory_initialized: AtomicBool,
6973
}
7074

71-
unsafe impl<PointerType: PointerTrait<AtomicU8>> Send for BitSet<PointerType> {}
72-
unsafe impl<PointerType: PointerTrait<AtomicU8>> Sync for BitSet<PointerType> {}
75+
unsafe impl<PointerType: PointerTrait<BitsetElement>> Send for BitSet<PointerType> {}
76+
unsafe impl<PointerType: PointerTrait<BitsetElement>> Sync for BitSet<PointerType> {}
7377

74-
impl BitSet<OwningPointer<AtomicU8>> {
78+
impl BitSet<OwningPointer<BitsetElement>> {
7579
/// Create a new [`BitSet`] with data located in the heap.
7680
///
7781
/// ```
@@ -80,10 +84,10 @@ pub mod details {
8084
/// ```
8185
pub fn new(capacity: usize) -> Self {
8286
let array_capacity = Self::array_capacity(capacity);
83-
let mut data_ptr = OwningPointer::<AtomicU8>::new_with_alloc(array_capacity);
87+
let mut data_ptr = OwningPointer::<BitsetElement>::new_with_alloc(array_capacity);
8488

8589
for i in 0..array_capacity {
86-
unsafe { data_ptr.as_mut_ptr().add(i).write(AtomicU8::new(0u8)) };
90+
unsafe { data_ptr.as_mut_ptr().add(i).write(BitsetElement::new(0)) };
8791
}
8892

8993
Self {
@@ -95,7 +99,7 @@ pub mod details {
9599
}
96100
}
97101

98-
impl RelocatableContainer for BitSet<RelocatablePointer<AtomicU8>> {
102+
impl RelocatableContainer for BitSet<RelocatablePointer<BitsetElement>> {
99103
unsafe fn new_uninit(capacity: usize) -> Self {
100104
Self {
101105
data_ptr: RelocatablePointer::new_uninit(),
@@ -116,17 +120,17 @@ pub mod details {
116120

117121
let memory = fail!(from self, when allocator
118122
.allocate(Layout::from_size_align_unchecked(
119-
std::mem::size_of::<AtomicU8>() * self.array_capacity,
120-
std::mem::align_of::<AtomicU8>())),
123+
std::mem::size_of::<BitsetElement>() * self.array_capacity,
124+
std::mem::align_of::<BitsetElement>())),
121125
"Failed to initialize since the allocation of the data memory failed.");
122126

123127
self.data_ptr.init(memory);
124128

125129
for i in 0..self.array_capacity {
126130
unsafe {
127-
(self.data_ptr.as_ptr() as *mut AtomicU8)
131+
(self.data_ptr.as_ptr() as *mut BitsetElement)
128132
.add(i)
129-
.write(AtomicU8::new(0u8))
133+
.write(BitsetElement::new(0))
130134
};
131135
}
132136

@@ -152,14 +156,14 @@ pub mod details {
152156
}
153157
}
154158

155-
impl<PointerType: PointerTrait<AtomicU8> + Debug> BitSet<PointerType> {
159+
impl<PointerType: PointerTrait<BitsetElement> + Debug> BitSet<PointerType> {
156160
pub(super) const fn array_capacity(capacity: usize) -> usize {
157-
capacity.div_ceil(8)
161+
capacity.div_ceil(BITSET_ELEMENT_SIZE)
158162
}
159163

160164
/// Returns the required memory size for a BitSet with a specified capacity.
161165
pub const fn const_memory_size(capacity: usize) -> usize {
162-
unaligned_mem_size::<AtomicU8>(Self::array_capacity(capacity))
166+
unaligned_mem_size::<BitsetElement>(Self::array_capacity(capacity))
163167
}
164168

165169
/// Returns the capacity of the BitSet
@@ -209,8 +213,8 @@ pub mod details {
209213
id
210214
);
211215

212-
let bitset_index = id / 8;
213-
let bit = id % 8;
216+
let bitset_index = id / BITSET_ELEMENT_SIZE;
217+
let bit = id % BITSET_ELEMENT_SIZE;
214218
self.set_bit(bitset_index, bit)
215219
}
216220

@@ -219,11 +223,10 @@ pub mod details {
219223
pub fn reset<F: FnMut(usize)>(&self, mut callback: F) {
220224
self.verify_init("set");
221225
for i in 0..self.array_capacity {
222-
let value =
223-
unsafe { (*self.data_ptr.as_ptr().add(i)).swap(0u8, Ordering::Relaxed) };
224-
for b in 0..8 {
226+
let value = unsafe { (*self.data_ptr.as_ptr().add(i)).swap(0, Ordering::Relaxed) };
227+
for b in 0..BITSET_ELEMENT_SIZE {
225228
if value & (1 << b) != 0 {
226-
let index = i * 8 + b;
229+
let index = i * BITSET_ELEMENT_SIZE + b;
227230
callback(index);
228231
}
229232
}
@@ -239,9 +242,9 @@ pub struct FixedSizeBitSet<const CAPACITY: usize> {
239242
bitset: RelocatableBitSet,
240243
// TODO: we waste here some memory since rust does us not allow to perform const operations
241244
// on generic parameters. Whenever this is supported, change this line into
242-
// data: [AtomicU8; Self::array_capacity(CAPACITY)]
245+
// data: [BitsetElement; Self::array_capacity(CAPACITY)]
243246
// For now we can live with it, since the bitsets are usually rather small
244-
data: [AtomicU8; CAPACITY],
247+
data: [BitsetElement; CAPACITY],
245248
}
246249

247250
unsafe impl<const CAPACITY: usize> Send for FixedSizeBitSet<CAPACITY> {}
@@ -253,10 +256,10 @@ impl<const CAPACITY: usize> Default for FixedSizeBitSet<CAPACITY> {
253256
bitset: unsafe {
254257
RelocatableBitSet::new(
255258
CAPACITY,
256-
align_to::<AtomicU8>(std::mem::size_of::<RelocatableBitSet>()) as _,
259+
align_to::<BitsetElement>(std::mem::size_of::<RelocatableBitSet>()) as _,
257260
)
258261
},
259-
data: core::array::from_fn(|_| AtomicU8::new(0u8)),
262+
data: core::array::from_fn(|_| BitsetElement::new(0)),
260263
}
261264
}
262265
}

iceoryx2-bb/lock-free/tests/bitset_tests.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ fn bit_set_set_bit_outside_of_bitset_leads_to_panic() {
110110
const CAPACITY: usize = 1551;
111111
let sut = BitSet::new(CAPACITY);
112112

113-
sut.set(CAPACITY + 1);
113+
sut.set(CAPACITY);
114114
}
115115

116116
#[test]

internal/docker/archlinux-base-devel

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ RUN echo "#### Installing dependencies" \
1919
&& rustup toolchain add \
2020
beta \
2121
nightly \
22-
1.70.0 \
22+
1.73.0 \
2323
&& echo "#### Adding more components like 'clippy', 'rustfmt', etc." \
2424
&& rustup component add \
2525
clippy \

internal/docker/ubuntu-22.04

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ RUN echo "#### Installing dependencies" \
2525
&& rustup toolchain add \
2626
beta \
2727
nightly \
28-
1.70.0 \
28+
1.73.0 \
2929
&& echo "#### Adding more components like 'clippy', 'rustfmt', etc." \
3030
&& rustup component add \
3131
clippy \

0 commit comments

Comments
 (0)