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