1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
use core::{ops::{Shl, Shr, BitAnd, Not}, sync::atomic::Ordering};
use alloc::{boxed::Box};
use bytemuck::Zeroable;
use num_traits::{One, Zero, Num, WrappingSub};
use crate::{traits::{AtomicInt}, AllocError};
use crate::InnerAtomicFlag;
#[cfg(feature = "alloc_api")]
use {alloc::alloc::Global, core::alloc::*};
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
#[repr(transparent)]
pub struct AtomicBitBox<T: AtomicInt = InnerAtomicFlag, #[cfg(feature = "alloc_api")] A: Allocator = Global> {
#[cfg(feature = "alloc_api")]
bits: Box<[T], A>,
#[cfg(not(feature = "alloc_api"))]
bits: Box<[T]>,
}
impl<T: AtomicInt> AtomicBitBox<T> where T::Primitive: BitFieldAble {
#[inline(always)]
pub fn new (bits: usize) -> Self {
Self::try_new(bits).unwrap()
}
#[inline(always)]
pub fn try_new (bits: usize) -> Result<Self, AllocError> {
let bytes = bits.div_ceil(core::mem::size_of::<T>());
let bits = unsafe {
let uninit = Box::<[T]>::new_zeroed_slice(bytes);
uninit.assume_init()
};
Ok(Self { bits })
}
}
cfg_if::cfg_if! {
if #[cfg(feature = "alloc_api")] {
impl<T: AtomicInt, A: Allocator> AtomicBitBox<T, A> where T::Primitive: BitFieldAble {
#[inline(always)]
pub fn new_in (bits: usize, alloc: A) -> Self {
Self::try_new_in(bits, alloc).unwrap()
}
#[inline]
pub fn try_new_in (bits: usize, alloc: A) -> Result<Self, AllocError> {
let bytes = bits.div_ceil(core::mem::size_of::<T>());
let bits = unsafe {
let uninit = Box::<[T], _>::new_zeroed_slice_in(bytes, alloc);
uninit.assume_init()
};
Ok(Self { bits })
}
pub fn get (&self, idx: usize, order: Ordering) -> Option<bool> {
let byte = idx / core::mem::size_of::<T>();
let idx = idx % core::mem::size_of::<T>();
if let Some(byte) = <[T]>::get(&self.bits, byte) {
let v = byte.load(order);
let mask = T::Primitive::one() << idx;
return Some((v & mask) != T::Primitive::zero())
}
None
}
#[inline(always)]
pub fn set (&self, v: bool, idx: usize, order: Ordering) -> Option<bool> {
if v { return self.set_true(idx, order) }
self.set_false(idx, order)
}
#[inline]
pub fn set_true (&self, idx: usize, order: Ordering) -> Option<bool> {
let byte = idx / core::mem::size_of::<T>();
let idx = idx % core::mem::size_of::<T>();
if let Some(byte) = <[T]>::get(&self.bits, byte) {
let mask = T::Primitive::one() << idx;
let prev = byte.fetch_or(mask, order);
return Some((prev & mask) != T::Primitive::zero())
}
None
}
#[inline]
pub fn set_false (&self, idx: usize, order: Ordering) -> Option<bool> {
let byte = idx / core::mem::size_of::<T>();
let idx = idx % core::mem::size_of::<T>();
if let Some(byte) = <[T]>::get(&self.bits, byte) {
let zero = T::Primitive::zero();
let mask = T::Primitive::one() << idx;
let prev = byte.fetch_and((!zero).wrapping_sub(&mask), order);
return Some((prev & mask) != zero)
}
None
}
}
} else {
impl<T: AtomicInt> AtomicBitBox<T> where T::Primitive: BitFieldAble {
pub fn get (&self, idx: usize, order: Ordering) -> Option<bool> {
let byte = idx / core::mem::size_of::<T>();
let idx = idx % core::mem::size_of::<T>();
if let Some(byte) = <[T]>::get(&self.bits, byte) {
let v = byte.load(order);
let mask = T::Primitive::one() << idx;
return Some((v & mask) != T::Primitive::zero())
}
None
}
#[inline(always)]
pub fn set (&self, v: bool, idx: usize, order: Ordering) -> Option<bool> {
if v { return self.set_true(idx, order) }
self.set_false(idx, order)
}
#[inline]
pub fn set_true (&self, idx: usize, order: Ordering) -> Option<bool> {
let byte = idx / core::mem::size_of::<T>();
let idx = idx % core::mem::size_of::<T>();
if let Some(byte) = <[T]>::get(&self.bits, byte) {
let mask = T::Primitive::one() << idx;
let prev = byte.fetch_or(mask, order);
return Some((prev & mask) != T::Primitive::zero())
}
None
}
#[inline]
pub fn set_false (&self, idx: usize, order: Ordering) -> Option<bool> {
let byte = idx / core::mem::size_of::<T>();
let idx = idx % core::mem::size_of::<T>();
if let Some(byte) = <[T]>::get(&self.bits, byte) {
let zero = T::Primitive::zero();
let mask = T::Primitive::one() << idx;
let prev = byte.fetch_and((!zero).wrapping_sub(&mask), order);
return Some((prev & mask) != zero)
}
None
}
}
}
}
pub trait BitFieldAble: Num + Copy + Zeroable + Eq + WrappingSub + BitAnd<Output = Self> + Shl<usize, Output = Self> + Shr<usize, Output = Self> + Not<Output = Self> {}
impl<T> BitFieldAble for T where T: Num + Copy + Zeroable + Eq + WrappingSub + BitAnd<Output = Self> + Shl<usize, Output = Self> + Shr<usize, Output = Self> + Not<Output = Self> {}