| 1 | |
| 2 |
|
| 3 | |
| 4 | |
| 5 | |
| 6 |
|
| 7 | use crate::{
|
| 8 | alloc, bindings, build_assert,
|
| 9 | error::{Error, Result},
|
| 10 | types::{ForeignOwnable, NotThreadSafe, Opaque},
|
| 11 | };
|
| 12 | use core::{iter, marker::PhantomData, mem, pin::Pin, ptr::NonNull};
|
| 13 | use pin_init::{pin_data, pin_init, pinned_drop, PinInit};
|
| 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 | #[pin_data(PinnedDrop)]
|
| 55 | pub struct XArray<T: ForeignOwnable> {
|
| 56 | #[pin]
|
| 57 | xa: Opaque<bindings::xarray>,
|
| 58 | _p: PhantomData<T>,
|
| 59 | }
|
| 60 |
|
| 61 | #[pinned_drop]
|
| 62 | impl<T: ForeignOwnable> PinnedDrop for XArray<T> {
|
| 63 | fn drop(self: Pin<&mut Self>) {
|
| 64 | self.iter().for_each(|ptr| {
|
| 65 | let ptr = ptr.as_ptr();
|
| 66 | |
| 67 | |
| 68 | |
| 69 | |
| 70 | drop(unsafe { T::from_foreign(ptr) })
|
| 71 | });
|
| 72 |
|
| 73 | |
| 74 | unsafe { bindings::xa_destroy(self.xa.get()) };
|
| 75 | }
|
| 76 | }
|
| 77 |
|
| 78 | |
| 79 | pub enum AllocKind {
|
| 80 | |
| 81 | Alloc,
|
| 82 | |
| 83 | Alloc1,
|
| 84 | }
|
| 85 |
|
| 86 | impl<T: ForeignOwnable> XArray<T> {
|
| 87 | |
| 88 | pub fn new(kind: AllocKind) -> impl PinInit<Self> {
|
| 89 | let flags = match kind {
|
| 90 | AllocKind::Alloc => bindings::XA_FLAGS_ALLOC,
|
| 91 | AllocKind::Alloc1 => bindings::XA_FLAGS_ALLOC1,
|
| 92 | };
|
| 93 | pin_init!(Self {
|
| 94 | |
| 95 | |
| 96 | |
| 97 | xa <- Opaque::ffi_init(|xa| unsafe {
|
| 98 | bindings::xa_init_flags(xa, flags)
|
| 99 | }),
|
| 100 | _p: PhantomData,
|
| 101 | })
|
| 102 | }
|
| 103 |
|
| 104 | fn iter(&self) -> impl Iterator<Item = NonNull<T::PointedTo>> + '_ {
|
| 105 | let mut index = 0;
|
| 106 |
|
| 107 | |
| 108 | iter::once(unsafe {
|
| 109 | bindings::xa_find(self.xa.get(), &mut index, usize::MAX, bindings::XA_PRESENT)
|
| 110 | })
|
| 111 | .chain(iter::from_fn(move || {
|
| 112 | |
| 113 | Some(unsafe {
|
| 114 | bindings::xa_find_after(self.xa.get(), &mut index, usize::MAX, bindings::XA_PRESENT)
|
| 115 | })
|
| 116 | }))
|
| 117 | .map_while(|ptr| NonNull::new(ptr.cast()))
|
| 118 | }
|
| 119 |
|
| 120 | |
| 121 | pub fn try_lock(&self) -> Option<Guard<'_, T>> {
|
| 122 | |
| 123 | if (unsafe { bindings::xa_trylock(self.xa.get()) } != 0) {
|
| 124 | Some(Guard {
|
| 125 | xa: self,
|
| 126 | _not_send: NotThreadSafe,
|
| 127 | })
|
| 128 | } else {
|
| 129 | None
|
| 130 | }
|
| 131 | }
|
| 132 |
|
| 133 | |
| 134 | pub fn lock(&self) -> Guard<'_, T> {
|
| 135 | |
| 136 | unsafe { bindings::xa_lock(self.xa.get()) };
|
| 137 |
|
| 138 | Guard {
|
| 139 | xa: self,
|
| 140 | _not_send: NotThreadSafe,
|
| 141 | }
|
| 142 | }
|
| 143 | }
|
| 144 |
|
| 145 | |
| 146 | |
| 147 | |
| 148 | #[must_use = "the lock unlocks immediately when the guard is unused"]
|
| 149 | pub struct Guard<'a, T: ForeignOwnable> {
|
| 150 | xa: &'a XArray<T>,
|
| 151 | _not_send: NotThreadSafe,
|
| 152 | }
|
| 153 |
|
| 154 | impl<T: ForeignOwnable> Drop for Guard<'_, T> {
|
| 155 | fn drop(&mut self) {
|
| 156 | |
| 157 | |
| 158 | |
| 159 | unsafe { bindings::xa_unlock(self.xa.xa.get()) };
|
| 160 | }
|
| 161 | }
|
| 162 |
|
| 163 | |
| 164 | |
| 165 | |
| 166 | pub struct StoreError<T> {
|
| 167 | |
| 168 | pub error: Error,
|
| 169 | |
| 170 | pub value: T,
|
| 171 | }
|
| 172 |
|
| 173 | impl<T> From<StoreError<T>> for Error {
|
| 174 | fn from(value: StoreError<T>) -> Self {
|
| 175 | value.error
|
| 176 | }
|
| 177 | }
|
| 178 |
|
| 179 | impl<'a, T: ForeignOwnable> Guard<'a, T> {
|
| 180 | fn load<F, U>(&self, index: usize, f: F) -> Option<U>
|
| 181 | where
|
| 182 | F: FnOnce(NonNull<T::PointedTo>) -> U,
|
| 183 | {
|
| 184 | |
| 185 | let ptr = unsafe { bindings::xa_load(self.xa.xa.get(), index) };
|
| 186 | let ptr = NonNull::new(ptr.cast())?;
|
| 187 | Some(f(ptr))
|
| 188 | }
|
| 189 |
|
| 190 | |
| 191 | pub fn get(&self, index: usize) -> Option<T::Borrowed<'_>> {
|
| 192 | self.load(index, |ptr| {
|
| 193 | |
| 194 | unsafe { T::borrow(ptr.as_ptr()) }
|
| 195 | })
|
| 196 | }
|
| 197 |
|
| 198 | |
| 199 | pub fn get_mut(&mut self, index: usize) -> Option<T::BorrowedMut<'_>> {
|
| 200 | self.load(index, |ptr| {
|
| 201 | |
| 202 | unsafe { T::borrow_mut(ptr.as_ptr()) }
|
| 203 | })
|
| 204 | }
|
| 205 |
|
| 206 | |
| 207 | pub fn remove(&mut self, index: usize) -> Option<T> {
|
| 208 | |
| 209 | |
| 210 | |
| 211 | let ptr = unsafe { bindings::__xa_erase(self.xa.xa.get(), index) }.cast();
|
| 212 | |
| 213 | |
| 214 | |
| 215 | |
| 216 | unsafe { T::try_from_foreign(ptr) }
|
| 217 | }
|
| 218 |
|
| 219 | |
| 220 | |
| 221 | |
| 222 | |
| 223 | |
| 224 | |
| 225 | |
| 226 | pub fn store(
|
| 227 | &mut self,
|
| 228 | index: usize,
|
| 229 | value: T,
|
| 230 | gfp: alloc::Flags,
|
| 231 | ) -> Result<Option<T>, StoreError<T>> {
|
| 232 | build_assert!(
|
| 233 | mem::align_of::<T::PointedTo>() >= 4,
|
| 234 | "pointers stored in XArray must be 4-byte aligned"
|
| 235 | );
|
| 236 | let new = value.into_foreign();
|
| 237 |
|
| 238 | let old = {
|
| 239 | let new = new.cast();
|
| 240 | |
| 241 | |
| 242 | |
| 243 | |
| 244 | |
| 245 | unsafe { bindings::__xa_store(self.xa.xa.get(), index, new, gfp.as_raw()) }
|
| 246 | };
|
| 247 |
|
| 248 | |
| 249 | |
| 250 | let errno = unsafe { bindings::xa_err(old) };
|
| 251 | if errno != 0 {
|
| 252 | |
| 253 | |
| 254 | let value = unsafe { T::from_foreign(new) };
|
| 255 | Err(StoreError {
|
| 256 | value,
|
| 257 | error: Error::from_errno(errno),
|
| 258 | })
|
| 259 | } else {
|
| 260 | let old = old.cast();
|
| 261 | |
| 262 | |
| 263 | |
| 264 | |
| 265 | Ok(unsafe { T::try_from_foreign(old) })
|
| 266 | }
|
| 267 | }
|
| 268 | }
|
| 269 |
|
| 270 | |
| 271 | unsafe impl<T: ForeignOwnable + Send> Send for XArray<T> {}
|
| 272 |
|
| 273 | |
| 274 | |
| 275 | unsafe impl<T: ForeignOwnable + Send> Sync for XArray<T> {}
|