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