@@ -49,29 +49,33 @@ use iceoryx2_bb_elementary::{
49
49
relocatable_container:: RelocatableContainer ,
50
50
relocatable_ptr:: { PointerTrait , RelocatablePointer } ,
51
51
} ;
52
+
52
53
use iceoryx2_bb_log:: { fail, fatal_panic} ;
53
54
55
+ type BitsetElement = AtomicU8 ;
56
+ const BITSET_ELEMENT_SIZE : usize = core:: mem:: size_of :: < BitsetElement > ( ) ;
57
+
54
58
/// 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 > > ;
56
60
/// 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 > > ;
58
62
59
63
pub mod details {
60
64
use super :: * ;
61
65
62
66
#[ derive( Debug ) ]
63
67
#[ repr( C ) ]
64
- pub struct BitSet < PointerType : PointerTrait < AtomicU8 > > {
68
+ pub struct BitSet < PointerType : PointerTrait < BitsetElement > > {
65
69
data_ptr : PointerType ,
66
70
capacity : usize ,
67
71
array_capacity : usize ,
68
72
is_memory_initialized : AtomicBool ,
69
73
}
70
74
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 > { }
73
77
74
- impl BitSet < OwningPointer < AtomicU8 > > {
78
+ impl BitSet < OwningPointer < BitsetElement > > {
75
79
/// Create a new [`BitSet`] with data located in the heap.
76
80
///
77
81
/// ```
@@ -80,10 +84,10 @@ pub mod details {
80
84
/// ```
81
85
pub fn new ( capacity : usize ) -> Self {
82
86
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) ;
84
88
85
89
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 ) ) } ;
87
91
}
88
92
89
93
Self {
@@ -95,7 +99,7 @@ pub mod details {
95
99
}
96
100
}
97
101
98
- impl RelocatableContainer for BitSet < RelocatablePointer < AtomicU8 > > {
102
+ impl RelocatableContainer for BitSet < RelocatablePointer < BitsetElement > > {
99
103
unsafe fn new_uninit ( capacity : usize ) -> Self {
100
104
Self {
101
105
data_ptr : RelocatablePointer :: new_uninit ( ) ,
@@ -116,17 +120,17 @@ pub mod details {
116
120
117
121
let memory = fail ! ( from self , when allocator
118
122
. 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 >( ) ) ) ,
121
125
"Failed to initialize since the allocation of the data memory failed." ) ;
122
126
123
127
self . data_ptr . init ( memory) ;
124
128
125
129
for i in 0 ..self . array_capacity {
126
130
unsafe {
127
- ( self . data_ptr . as_ptr ( ) as * mut AtomicU8 )
131
+ ( self . data_ptr . as_ptr ( ) as * mut BitsetElement )
128
132
. add ( i)
129
- . write ( AtomicU8 :: new ( 0u8 ) )
133
+ . write ( BitsetElement :: new ( 0 ) )
130
134
} ;
131
135
}
132
136
@@ -152,14 +156,14 @@ pub mod details {
152
156
}
153
157
}
154
158
155
- impl < PointerType : PointerTrait < AtomicU8 > + Debug > BitSet < PointerType > {
159
+ impl < PointerType : PointerTrait < BitsetElement > + Debug > BitSet < PointerType > {
156
160
pub ( super ) const fn array_capacity ( capacity : usize ) -> usize {
157
- capacity. div_ceil ( 8 )
161
+ capacity. div_ceil ( BITSET_ELEMENT_SIZE )
158
162
}
159
163
160
164
/// Returns the required memory size for a BitSet with a specified capacity.
161
165
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) )
163
167
}
164
168
165
169
/// Returns the capacity of the BitSet
@@ -209,8 +213,8 @@ pub mod details {
209
213
id
210
214
) ;
211
215
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 ;
214
218
self . set_bit ( bitset_index, bit)
215
219
}
216
220
@@ -219,11 +223,10 @@ pub mod details {
219
223
pub fn reset < F : FnMut ( usize ) > ( & self , mut callback : F ) {
220
224
self . verify_init ( "set" ) ;
221
225
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 {
225
228
if value & ( 1 << b) != 0 {
226
- let index = i * 8 + b;
229
+ let index = i * BITSET_ELEMENT_SIZE + b;
227
230
callback ( index) ;
228
231
}
229
232
}
@@ -239,9 +242,9 @@ pub struct FixedSizeBitSet<const CAPACITY: usize> {
239
242
bitset : RelocatableBitSet ,
240
243
// TODO: we waste here some memory since rust does us not allow to perform const operations
241
244
// 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)]
243
246
// For now we can live with it, since the bitsets are usually rather small
244
- data : [ AtomicU8 ; CAPACITY ] ,
247
+ data : [ BitsetElement ; CAPACITY ] ,
245
248
}
246
249
247
250
unsafe impl < const CAPACITY : usize > Send for FixedSizeBitSet < CAPACITY > { }
@@ -253,10 +256,10 @@ impl<const CAPACITY: usize> Default for FixedSizeBitSet<CAPACITY> {
253
256
bitset : unsafe {
254
257
RelocatableBitSet :: new (
255
258
CAPACITY ,
256
- align_to :: < AtomicU8 > ( std:: mem:: size_of :: < RelocatableBitSet > ( ) ) as _ ,
259
+ align_to :: < BitsetElement > ( std:: mem:: size_of :: < RelocatableBitSet > ( ) ) as _ ,
257
260
)
258
261
} ,
259
- data : core:: array:: from_fn ( |_| AtomicU8 :: new ( 0u8 ) ) ,
262
+ data : core:: array:: from_fn ( |_| BitsetElement :: new ( 0 ) ) ,
260
263
}
261
264
}
262
265
}
0 commit comments