cycle_ptr 0.1.0

Smart pointers, with cycles
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
//! The object module declares the [Object] type.
//!
//! These objects are an abstraction of the actual objects used in a program.
//! Each such holds on to the actual object, and a control-block.
pub(crate) mod metadata;
mod ptr;

use crate::generation::{ControlBlock, GenerationPtr, ObjectState};
use crate::util::NonNull;
use std::backtrace::Backtrace;
use std::cell::RefCell;
use std::cell::UnsafeCell;
use std::pin::Pin;
use std::sync::Arc;

#[cfg(feature = "weak_pointer")]
use crate::generation::Colour;
#[cfg(all(feature = "multi_thread", feature = "weak_pointer"))]
use crate::generation::MTRefCount;
#[cfg(feature = "weak_pointer")]
use crate::generation::RefCount;
#[cfg(feature = "multi_thread")]
use crate::generation::{MTControlBlock, MTGenerationPtr};
#[cfg(feature = "weak_pointer")]
use std::fmt;
#[cfg(all(feature = "multi_thread", feature = "weak_pointer"))]
use std::sync::atomic::AtomicBool;
#[cfg(feature = "multi_thread")]
use std::sync::atomic::{AtomicUsize, Ordering};

#[cfg(feature = "multi_thread")]
pub(crate) use ptr::{DynMTObjectPtr, MTObjectPtr};
pub(crate) use ptr::{DynObjectPtr, ObjectPtr};

/// Convenience function, that invokes [Box::leak], but maintains the [Pin].
fn leak_pinned_box<T>(ptr: Pin<Box<T>>) -> Pin<&'static T>
where
    T: ?Sized,
{
    unsafe { Pin::new_unchecked(Box::leak(Pin::into_inner_unchecked(ptr))) }
}

/// An garbage-collectible object.
///
/// The object holds on to the actual object `T`, as well as book-keeping information ([ControlBlock]).
pub(crate) struct Object<T>
where
    T: 'static,
{
    /// The control block for the object.
    control_block: ControlBlock,
    /// The object itself.
    data: UnsafeCell<Option<T>>,
    /// Reference counter for use by [ObjectPtr]/[DynObjectPtr].
    ///
    /// In [Rc][std::rc::Rc]/[Arc][std::sync::Arc] terms, this would be the weak reference count.
    refcount: RefCell<usize>,
}

/// An garbage-collectible object.
///
/// The object holds on to the actual object `T`, as well as book-keeping information ([MTControlBlock]).
#[cfg(feature = "multi_thread")]
pub(crate) struct MTObject<T>
where
    T: 'static,
{
    /// The control block for the object.
    control_block: MTControlBlock,
    /// The object itself.
    data: UnsafeCell<Option<T>>,
    /// Reference counter for use by [MTObjectPtr]/[DynMTObjectPtr].
    ///
    /// In [Rc][std::rc::Rc]/[Arc][std::sync::Arc] terms, this would be the weak reference count.
    refcount: AtomicUsize,
    /// Flag that indicates if `data` has been initialized.
    ///
    /// Weak pointers can be created before `data` has been initialized,
    /// which means `data` will be written to at some point,
    /// while the [Weak::upgrade][crate::Weak::upgrade] will want to read to confirm the presence of `data`.
    /// Since that leads to undefined behaviour, we'll use an extra flag instead.
    #[cfg(feature = "weak_pointer")]
    initialized: AtomicBool,
}

/// Describes the methods of [Object] for type-erasure.
///
/// This interface is defined so that [DynObjectPtr] can point at an object, without requiring dynamic typing.
pub(crate) trait ObjectIntf {
    /// Release the object.
    fn release_object(&self);
    /// Try to acquire a reference to the object.
    ///
    /// Only succeeds if there is at least one active reference.
    fn try_refcount_inc(&self) -> Result<(), ()>;
    /// Acquire a reference to the object.
    ///
    /// As long as there are references, it will remain valid.
    fn refcount_inc(&self);
    /// Release a reference to the object.
    ///
    /// This function will destroy and free `Self`, if the reference count reaches zero.
    fn refcount_dec(&self);
    /// Gain access to the control block.
    fn get_control_block(&self) -> &ControlBlock;
    /// Get access to the generation pointer.
    ///
    /// Access is not protected.
    fn get_generation_ptr(&self) -> GenerationPtr {
        self.get_control_block().generation.borrow().clone()
    }
    /// Check the object state.
    fn object_state(&self) -> ObjectState {
        self.get_control_block().object_state()
    }
    /// Check if the object is initialized.
    fn has_data(&self) -> bool;
    /// Retrieve the backtrace of when this object was created.
    fn created_backtrace(&self) -> &Option<Arc<Backtrace>> {
        &self.get_control_block().created_backtrace
    }
}

/// Describes the methods of [Object] for type-erasure.
///
/// This interface is defined so that [DynObjectPtr] can point at an object, without requiring dynamic typing.
///
/// This is the counterpart of [ObjectIntf] for use in multi-thread context.
#[cfg(feature = "multi_thread")]
pub(crate) trait MTObjectIntf {
    /// Release the object.
    fn release_object(&self);
    /// Try to acquire a reference to the object.
    ///
    /// Only succeeds if there is at least one active reference.
    fn try_refcount_inc(&self) -> Result<(), ()>;
    /// Acquire a reference to the object.
    ///
    /// As long as there are references, it will remain valid.
    fn refcount_inc(&self);
    /// Release a reference to the object.
    ///
    /// This function will destroy and free `Self`, if the reference count reaches zero.
    fn refcount_dec(&self);
    /// Gain access to the control block.
    fn get_control_block(&self) -> &MTControlBlock;
    /// Get access to the generation pointer.
    ///
    /// Access is not protected.
    fn get_generation_ptr(&self) -> MTGenerationPtr {
        self.get_control_block().generation.read().unwrap().clone()
    }
    /// Check the object state.
    fn object_state(&self) -> ObjectState {
        self.get_control_block().object_state()
    }
    /// Check if the object is initialized.
    fn has_data(&self) -> bool;
    /// Retrieve the backtrace of when this object was created.
    fn created_backtrace(&self) -> &Option<Arc<Backtrace>> {
        &self.get_control_block().created_backtrace
    }
}

impl<T> Object<T>
where
    T: 'static,
{
    /// Create a new [Object].
    pub(crate) fn new_ptr<Factory>(
        initial_refcount: usize,
        factory: Factory,
        generation: Option<GenerationPtr>,
    ) -> Pin<ObjectPtr<T>>
    where
        Factory: FnOnce(metadata::Metadata) -> T,
    {
        let mut p = Box::pin(Object {
            control_block: ControlBlock::new(initial_refcount, generation),
            data: UnsafeCell::new(None),
            refcount: RefCell::new(0),
        });

        unsafe {
            // We need to publish the destructor of ourself.
            let destructor = NonNull::<dyn ObjectIntf>::from_ref(&*p);
            p.as_mut()
                .map_unchecked_mut(|p| &mut p.control_block)
                .register(destructor);
        }

        // Now that we have our pointer, we can initialize the data.
        let p = ObjectPtr::new(leak_pinned_box(p));
        unsafe {
            *p.data.get() = Some(factory(metadata::Metadata::new(p.clone().into())));
        }
        p
    }

    /// Create a new [Object], which will be initialized with a non-upgradable [Weak][crate::sync::Weak] pointer, allowing for a cyclic weak reference.
    #[cfg(feature = "weak_pointer")]
    pub(crate) fn new_cyclic_ptr<Factory>(
        initial_refcount: usize,
        factory: Factory,
        generation: Option<GenerationPtr>,
    ) -> Pin<ObjectPtr<T>>
    where
        Factory: FnOnce(crate::Metadata, crate::Weak<T>) -> T,
    {
        let mut p = Box::pin(Object {
            control_block: ControlBlock::new(initial_refcount, generation),
            data: UnsafeCell::new(None),
            refcount: RefCell::new(0),
        });

        unsafe {
            // We need to publish the destructor of ourself.
            let destructor = NonNull::<dyn ObjectIntf>::from_ref(&*p);
            p.as_mut()
                .map_unchecked_mut(|p| &mut p.control_block)
                .register(destructor);
        }

        // Now that we have our pointer, we can initialize the data.
        let p = ObjectPtr::new(leak_pinned_box(p));
        let weak = crate::Weak::new_from_raw(p.clone());
        unsafe {
            *p.data.get() = Some(factory(metadata::Metadata::new(p.clone().into()), weak));
        }
        p
    }

    /// Read the data helf in an object.
    ///
    /// # Panics
    ///
    /// Will panic if the object isn't dereferencable,
    /// which may be due to the object not having been initialized,
    /// or because the object has expired.
    pub(crate) fn get_data(&self) -> &T {
        unsafe {
            let opt_data_ref = &*self.data.get();
            opt_data_ref
                .as_ref()
                .expect("live object should be dereferencable")
        }
    }
}

#[cfg(feature = "multi_thread")]
impl<T> MTObject<T>
where
    T: 'static + Send + Sync,
{
    /// Create a new [Object].
    pub(crate) fn new_ptr<Factory>(
        initial_refcount: usize,
        factory: Factory,
        generation: Option<MTGenerationPtr>,
    ) -> Pin<MTObjectPtr<T>>
    where
        Factory: FnOnce(metadata::sync::Metadata) -> T,
    {
        let mut p = Box::pin(MTObject {
            control_block: MTControlBlock::new(initial_refcount, generation),
            data: UnsafeCell::new(None),
            refcount: AtomicUsize::new(0),
            #[cfg(feature = "weak_pointer")]
            initialized: AtomicBool::new(false),
        });

        unsafe {
            // We need to publish the destructor of ourself.
            let destructor = NonNull::<dyn Send + Sync + MTObjectIntf>::from_ref(&*p);
            p.as_mut()
                .map_unchecked_mut(|p| &mut p.control_block)
                .register(destructor);
        }

        // Now that we have our pointer, we can initialize the data.
        let p = MTObjectPtr::new(leak_pinned_box(p));
        unsafe {
            *p.data.get() = Some(factory(metadata::sync::Metadata::new(p.clone().into())));
        }
        #[cfg(feature = "weak_pointer")]
        p.initialized.store(true, Ordering::Release);
        p
    }

    /// Create a new [Object], which will be initialized with a non-upgradable [Weak][crate::sync::Weak] pointer, allowing for a cyclic weak reference.
    #[cfg(feature = "weak_pointer")]
    pub(crate) fn new_cyclic_ptr<Factory>(
        initial_refcount: usize,
        factory: Factory,
        generation: Option<MTGenerationPtr>,
    ) -> Pin<MTObjectPtr<T>>
    where
        Factory: FnOnce(metadata::sync::Metadata, crate::sync::Weak<T>) -> T,
    {
        let mut p = Box::pin(MTObject {
            control_block: MTControlBlock::new(initial_refcount, generation),
            data: UnsafeCell::new(None),
            refcount: AtomicUsize::new(0),
            #[cfg(feature = "weak_pointer")]
            initialized: AtomicBool::new(false),
        });

        unsafe {
            // We need to publish the destructor of ourself.
            let destructor = NonNull::<dyn Send + Sync + MTObjectIntf>::from_ref(&*p);
            p.as_mut()
                .map_unchecked_mut(|p| &mut p.control_block)
                .register(destructor);
        }

        // Now that we have our pointer, we can initialize the data.
        let p = MTObjectPtr::new(leak_pinned_box(p));
        let weak = crate::sync::Weak::new_from_raw(p.clone());
        unsafe {
            *p.data.get() = Some(factory(
                metadata::sync::Metadata::new(p.clone().into()),
                weak,
            ));
        }
        #[cfg(feature = "weak_pointer")]
        p.initialized.store(true, Ordering::Release);
        p
    }
}

#[cfg(feature = "multi_thread")]
impl<T> MTObject<T>
where
    T: 'static,
{
    /// Read the data helf in an object.
    ///
    /// # Panics
    ///
    /// Will panic if the object isn't dereferencable,
    /// which may be due to the object not having been initialized,
    /// or because the object has expired.
    #[inline]
    pub(crate) fn get_data(&self) -> &T {
        unsafe {
            let opt_data_ref = &*self.data.get();
            opt_data_ref
                .as_ref()
                .expect("live object should be dereferencable")
        }
    }
}

impl<T> ObjectIntf for Object<T>
where
    T: 'static,
{
    fn release_object(&self) {
        unsafe {
            let data = &mut *self.data.get();
            assert!(data.is_some());
            data.take();
        }
    }

    fn try_refcount_inc(&self) -> Result<(), ()> {
        let mut v = self.refcount.borrow_mut();
        match *v {
            0 => Err(()),
            _ => {
                *v += 1;
                Ok(())
            }
        }
    }

    fn refcount_inc(&self) {
        self.refcount.replace_with(|v| *v + 1);
    }

    fn refcount_dec(&self) {
        let old_v = self.refcount.replace_with(|v| {
            assert_ne!(*v, 0);
            *v - 1
        });
        if old_v == 1 {
            // Last reference was dropped.
            //
            // Deregister from generation prior to destroying.
            self.control_block.invalidate_and_deregister();

            // SAFETY: we own the only reference to self,
            // and UnsafeCell<Self> has the same memory layout as <Self>.
            unsafe {
                let this = &*(self as *const Self as *const UnsafeCell<Self>);
                let _ = self;
                drop(Box::from_raw(this.get()));
            }
        }
    }

    fn get_control_block(&self) -> &ControlBlock {
        &self.control_block
    }

    fn has_data(&self) -> bool {
        unsafe { (*self.data.get()).is_some() }
    }
}

#[cfg(feature = "multi_thread")]
impl<T> MTObjectIntf for MTObject<T>
where
    T: 'static,
{
    fn release_object(&self) {
        unsafe {
            let data = &mut *self.data.get();
            assert!(data.is_some());
            data.take();
        }
    }

    fn try_refcount_inc(&self) -> Result<(), ()> {
        let mut v = 1;
        loop {
            match self.refcount.compare_exchange_weak(
                v,
                v + 1,
                Ordering::Relaxed,
                Ordering::Relaxed,
            ) {
                Ok(_) => {
                    return Ok(());
                }
                Err(x) => v = x,
            }

            if v == 0 {
                return Err(());
            }
        }
    }

    fn refcount_inc(&self) {
        self.refcount.fetch_add(1, Ordering::Relaxed);
    }

    fn refcount_dec(&self) {
        let old_v = self.refcount.fetch_sub(1, Ordering::Relaxed);
        assert_ne!(old_v, 0);
        if old_v == 1 {
            // Last reference was dropped.
            //
            // ThreadSanitizer does not support memory fences.
            // Yeah, I copied this from Arc.
            // Note that Arc uses `#[cfg(sanitize)]`, but the compiler won't permit us to do that,
            // because it's experimental.
            let _ = self.refcount.load(Ordering::Acquire);

            // Deregister from generation prior to destroying.
            self.control_block.invalidate_and_deregister();

            // SAFETY: we own the only reference to self,
            // and UnsafeCell<Self> has the same memory layout as <Self>.
            unsafe {
                let this = &*(self as *const Self as *const UnsafeCell<Self>);
                let _ = self;
                drop(Box::from_raw(this.get()));
            }
        }
    }

    fn get_control_block(&self) -> &MTControlBlock {
        &self.control_block
    }

    fn has_data(&self) -> bool {
        #[cfg(feature = "weak_pointer")]
        if !self.initialized.load(Ordering::Relaxed) {
            return false;
        }

        unsafe { (*self.data.get()).is_some() }
    }
}

impl<T> Drop for Object<T>
where
    T: 'static,
{
    fn drop(&mut self) {
        assert_eq!(*self.refcount.borrow(), 0);
    }
}

#[cfg(feature = "multi_thread")]
impl<T> Drop for MTObject<T>
where
    T: 'static,
{
    fn drop(&mut self) {
        assert_eq!(self.refcount.load(Ordering::Relaxed), 0);
    }
}

#[cfg(feature = "weak_pointer")]
impl<T> Object<T>
where
    T: 'static + fmt::Debug,
{
    /// Compute the summary of a reference counter.
    ///
    /// Returns a string representing the object state (stronly-reachable/weakly-reachable/expired/unreachable),
    /// and a boolean indicating if it is safe to dereference the object.
    fn weak_debug_summary(refcount: &RefCount) -> (&'static str, bool) {
        let (refs, colour, state) = refcount.load();
        if refs > 0 {
            ("strongly-reachable", true)
        } else if colour != Colour::White {
            ("weakly-reachable", true)
        } else if state == ObjectState::Expired {
            ("expired", false)
        } else {
            ("unreachable", false)
        }
    }

    /// Print debugging information about a weak reference to this object.
    pub(crate) fn weak_debug_fmt(
        &self,
        typename: &str,
        f: &mut fmt::Formatter<'_>,
    ) -> Result<(), fmt::Error> {
        self.control_block.try_with_weak_lockout(|refcount| {
            let (summary, dereferencable) = Self::weak_debug_summary(refcount);
            if dereferencable {
                match unsafe { &*self.data.get() } {
                    None => write!(f, "{typename}(uninitialized object)", typename = typename),
                    Some(value) => {
                        write!(f, "{summary} {value:?}", summary = summary, value = value)
                    }
                }
            } else {
                write!(
                    f,
                    "{typename}({summary})",
                    typename = typename,
                    summary = summary
                )
            }
        })
    }
}

#[cfg(all(feature = "multi_thread", feature = "weak_pointer"))]
impl<T> MTObject<T>
where
    T: 'static + fmt::Debug,
{
    /// Compute the summary of a reference counter.
    ///
    /// Returns a string representing the object state (stronly-reachable/weakly-reachable/expired/unreachable),
    /// and a boolean indicating if it is safe to dereference the object if the `weak_lockout` is engaged.
    fn weak_debug_summary(refcount: &MTRefCount) -> (&'static str, bool) {
        let (refs, colour, state) = refcount.load();
        if refs > 0 {
            ("strongly-reachable", true)
        } else if colour != Colour::White {
            ("weakly-reachable", true)
        } else if state == ObjectState::Expired {
            ("expired", false)
        } else {
            ("unreachable", false)
        }
    }

    /// Print debugging information about a weak reference to this object.
    ///
    /// This method won't attempt to acquire any weak locks, and thus can only report on if an object is reachable.
    fn weak_debug_fmt_no_lock(
        &self,
        typename: &str,
        f: &mut fmt::Formatter<'_>,
    ) -> Result<(), fmt::Error> {
        write!(
            f,
            "{typename}({summary})",
            typename = typename,
            summary = Self::weak_debug_summary(self.control_block.borrow_refcount_for_debug()).0
        )
    }

    /// Print debugging information about a weak reference to this object.
    ///
    /// Attempts to print the pointee object, if it can do so.
    pub(crate) fn weak_debug_fmt(
        &self,
        typename: &str,
        f: &mut fmt::Formatter<'_>,
    ) -> Result<(), fmt::Error> {
        if !self.initialized.load(Ordering::Relaxed) {
            return write!(f, "{typename}(uninitialized object)", typename = typename);
        }

        // Prevent weak pointers from being collected from under us.
        let attempt = self.control_block.try_with_weak_lockout(|refcount| {
            let (summary, dereferencable) = Self::weak_debug_summary(refcount);
            if dereferencable {
                match unsafe { &*self.data.get() } {
                    None => write!(
                        f,
                        "{typename}({summary} yet unintialized (BUG!))",
                        typename = typename,
                        summary = summary
                    ),
                    Some(value) => {
                        write!(f, "{summary} {value:?}", summary = summary, value = value)
                    }
                }
            } else {
                write!(
                    f,
                    "{typename}({summary})",
                    typename = typename,
                    summary = summary
                )
            }
        });

        match attempt {
            Ok(result) => result,
            Err(_) => self.weak_debug_fmt_no_lock(typename, f),
        }
    }
}

#[cfg(feature = "multi_thread")]
unsafe impl<T> Send for MTObject<T> where T: 'static + Send + Sync {}

#[cfg(feature = "multi_thread")]
unsafe impl<T> Sync for MTObject<T> where T: 'static + Send + Sync {}