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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
use std::alloc::{alloc, dealloc, Layout};
use std::marker::PhantomData;
use std::ptr::NonNull;

#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct BorrowMutError;

struct ScopedCellGuard<'a, T: ?Sized>(&'a ScopedCell<T>);
impl<'a, T: ?Sized> Drop for ScopedCellGuard<'a, T> {
    fn drop(&mut self) {
        unsafe {
            let inner = self.0.inner_mut();
            debug_assert!(inner.active);
            inner.active = false;
        }
    }
}

pub struct ScopedCell<T: ?Sized> {
    ptr: NonNull<ScopedCellInner<T>>,
    //phantom: PhantomData<ScopedCellInner<T>>,
}

impl<T: ?Sized> ScopedCell<T> {
    pub fn borrow_mut<F, R>(&self, fun: F) -> R
    where
        F: FnOnce(&mut T) -> R,
    {
        unsafe {
            let inner_ptr;
            {
                let inner = self.inner_mut();
                if inner.active {
                    panic!("ScopedCell already borrowed");
                }
                if inner.inner.is_none() {
                    panic!("Tried to borrow ScopedCell outside of creating scope");
                }
                inner.active = true;
                inner_ptr = inner.inner.unwrap();
            }

            // This guard will take care of setting active to false when we
            // leave the inner scope.
            let _guard = ScopedCellGuard(self);

            fun(&mut *inner_ptr.as_ptr())
        }
    }

    pub fn try_borrow_mut<F, R>(&self, fun: F) -> Result<R, BorrowMutError>
    where
        F: FnOnce(&mut T) -> R,
    {
        unsafe {
            let inner_ptr;
            {
                let inner = self.inner_mut();
                if inner.active {
                    return Err(BorrowMutError);
                }
                if inner.inner.is_none() {
                    return Err(BorrowMutError);
                }
                inner.active = true;
                inner_ptr = inner.inner.unwrap();
            }

            // This guard will take care of setting active to false when we
            // leave the inner scope.
            let _guard = ScopedCellGuard(self);

            Ok(fun(&mut *inner_ptr.as_ptr()))
        }
    }

    /// # Safety
    /// This may only be called by internal functions.
    /// A reference to this may only be held while control flow is controlled by the scoped cell.
    /// Only one reference to inner may be obtained at the time.
    unsafe fn inner_mut(&self) -> &mut ScopedCellInner<T> {
        &mut *(self.ptr.as_ptr() as *mut _)
    }
}

impl<T: ?Sized> Clone for ScopedCell<T> {
    fn clone(&self) -> Self {
        unsafe {
            let inner = self.inner_mut();
            inner.references += 1;
        }
        ScopedCell {
            ptr: self.ptr,
            //phantom: PhantomData,
        }
    }
}
impl<T: ?Sized> Drop for ScopedCell<T> {
    fn drop(&mut self) {
        unsafe {
            let is_zero;
            let active;
            {
                let inner = self.inner_mut();
                inner.references -= 1;
                is_zero = inner.references == 0;
                active = inner.active;
            }

            if is_zero {
                debug_assert!(!active);
                dealloc(
                    self.ptr.as_ptr().cast(),
                    Layout::for_value(self.ptr.as_ref()),
                )
            }
        }
    }
}

pub struct ScopedCellInner<T: ?Sized> {
    /// Number of `ScopedCell`s that exist at any given time.
    /// This is incremented on creation/cloning of a new `ScopedCell`, and
    /// decremented in the drop implementation.
    /// When this reaches 0, this struct is deallocated.
    references: usize,
    /// When this is true, a `&mut T` exists
    active: bool,
    /// Pointer to the thing this ScopedCell references.
    /// If this is `Some`, the creator guard of this scoped cell is still in
    /// scope.
    /// If this is `None`, the creator guard has been dropped and any future
    /// borrow attempts will fail.
    inner: Option<NonNull<T>>,
}

/// The guard must not be dropped while a borrow of a related cell is in
/// progress. If this happens, the whole process will be aborted.
pub fn new<'a, T: ?Sized + 'a>(value: &'a mut T) -> ScopedCellCreatorGuard<'a, T> {
    // Because we are using the value reference in a `PhantomData`, the
    // reference will be concidered used, while not actually existing in a
    // usable form until this guard stuct is dropped.
    //
    // This should make things sound when we create a mutable reference from
    // the pointer we have stored in `ScopedCellInner`, because that can only
    // be done while the actual value reference is tied up in the PhantomData
    // of this guard.

    unsafe {
        let inner_layout = Layout::new::<ScopedCellInner<T>>();
        let inner_ptr_u8 = alloc(inner_layout);

        let inner_ptr = NonNull::new(inner_ptr_u8 as *mut _).unwrap();
        std::ptr::write(
            inner_ptr.as_ptr(),
            ScopedCellInner {
                references: 1,
                active: false,
                inner: Some(NonNull::new(value as *mut _).unwrap()),
            },
        );

        let cell = ScopedCell {
            ptr: inner_ptr,
            //phantom: PhantomData,
        };

        ScopedCellCreatorGuard {
            cell,
            life: PhantomData,
        }
    }
}

pub struct ScopedCellCreatorGuard<'a, T: ?Sized> {
    cell: ScopedCell<T>,
    life: PhantomData<&'a mut T>,
}
impl<'a, T: ?Sized> Drop for ScopedCellCreatorGuard<'a, T> {
    fn drop(&mut self) {
        unsafe {
            let inner = self.cell.inner_mut();
            if inner.active {
                println!(
                    "FATAL: ScopedCell borrow active while ScopedCellCreatorGuard was dropped"
                );
                std::process::abort();
            }
            inner.inner = None;
        }
    }
}
impl<'a, T: ?Sized> ScopedCellCreatorGuard<'a, T> {
    pub fn clone_cell(&self) -> ScopedCell<T> {
        self.cell.clone()
    }
}

pub fn scoped_cell<T: ?Sized, F, R>(value: &mut T, fun: F) -> R
where
    F: FnOnce(ScopedCell<T>) -> R,
{
    let guard = new(value);

    let cell = guard.clone_cell();
    fun(cell)
}

#[cfg(test)]
mod tests {
    use super::{new, scoped_cell};

    #[test]
    fn creation() {
        let mut a: u32 = 0;
        scoped_cell(&mut a, |_ac| {});
    }

    #[test]
    fn basic_usage() {
        let mut a: u32 = 0;

        fn inner(a: &mut u32) {
            scoped_cell(a, |ac| {
                let ac2 = ac.clone();

                ac.borrow_mut(|v| {
                    *v += 1;
                });

                ac2.borrow_mut(|v| {
                    *v += 1;
                });
            });
        }

        inner(&mut a);

        assert!(a == 2);
    }

    //#[test]
    //fn miri_fail() {
    //    use std::marker::PhantomData;

    //    struct Guard<'a>(PhantomData<&'a u8>);
    //    fn new<'a>(_val: &'a mut u8) -> Guard<'a> {
    //        Guard(PhantomData)
    //    }

    //    let mut a = 0u8;
    //    let a_ptr = &mut a as *mut _;

    //    // Should be sound:
    //    {
    //        let guard = new(&mut a);

    //        // Compiler is aware that `a` is borrowed mutably at this point, but
    //        // no actual reference exists. Therefore there will only be one active
    //        // mutable reference when we create one from the pointer.

    //        let inner_ref = unsafe { &mut *a_ptr };
    //        *inner_ref += 1u8;
    //        std::mem::drop(inner_ref);

    //        std::mem::drop(guard);
    //    }
    //}

    #[test]
    fn raw_usage() {
        let mut a: u32 = 0;
        let aco;
        {
            let sc = new(&mut a);
            let ac1 = sc.clone_cell();
            let ac2 = sc.clone_cell();
            aco = ac2.clone();
            ac1.borrow_mut(|_i| {
                assert!(ac2.try_borrow_mut(|_i| ()).is_err());
            });
        }
        assert!(aco.try_borrow_mut(|_i| ()).is_err());
    }

    #[test]
    fn double_borrow_fails() {
        let mut a: u32 = 0;

        scoped_cell(&mut a, |ac| {
            let ac2 = ac.clone();
            ac.borrow_mut(|_i| {
                assert!(ac2.try_borrow_mut(|_i| ()).is_err());
            });
        });
    }
}