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
// immutable_arena: a Rust crate for arenas of immutable-once-built objects
// with references to other objects in the arena.
//
// Copyright (c) Chris Fallin <cfallin@c1f.net>. Released under the MIT
// license.

//! `immutable_arena` provides a type `Arena<T>` for objects that are immutable
//! once allocated, and a smart pointer type `Ref<'arena, T>` that may be set
//! exactly once, allowing the user to create cycles among objects in the
//! arena.
//!
//! Example usage:
//!
//! ```
//! use immutable_arena::{Arena, Ref};
//!
//! struct S<'arena> {
//!     id: u32,
//!     next: Ref<'arena, S<'arena>>,
//! }
//!
//! fn alloc_cycle<'arena>(arena: &'arena Arena<S<'arena>>)
//!     -> &'arena S<'arena> {
//!     let s1 = arena.alloc(S { id: 1, next: Ref::empty() });
//!     let s2 = arena.alloc(S { id: 2, next: Ref::empty() });
//!     s1.next.set(s2);
//!     s2.next.set(s1);
//!     s1
//! }
//!
//! fn test_cycle() {
//!     let arena = Arena::new();
//!     let s1 = alloc_cycle(&arena);
//!     assert!(s1.next.next.id == s1.id);
//! }
//! ```

extern crate typed_arena;

use std::fmt;
use std::mem;
use std::marker::PhantomData;
use std::ops::Deref;
use std::sync::atomic::{AtomicPtr, Ordering};

/// An `Arena<T>` is a container of objects of type `T` that, once allocated,
/// live as long as the containing arena. Within the arena, objects may refer
/// to other objects using the `Ref<'arena, T>` smart-pointer type. These
/// object references are allowed to form cycles. Once created, an object is
/// immutable. However, any `Ref<'arena, T>` instances within the object may be
/// set *exactly once*. The common usage pattern is to create objects and set
/// all their refs before returning them to user code; the objects are
/// subsequently completely immutable.
pub struct Arena<T> {
    arena: typed_arena::Arena<T>,
}

impl<T> Arena<T> {
    /// Create a new immutable-object arena.
    pub fn new() -> Arena<T> {
        Arena { arena: typed_arena::Arena::new() }
    }

    /// Allocate a new immutable object on the arena.
    pub fn alloc<'arena>(&'arena self, t: T) -> &'arena T
        where T: 'arena
    {
        self.arena.alloc(t)
    }
}

/// A `Ref<'arena, T>` is a smart pointer type that may be used within an
/// arena-allocated type to hold a reference to another object within that arena.
/// It may be set exactly once, and is immutable thereafter. It dereferences only
/// to a read-only borrow, never a mutable one.
pub struct Ref<'arena, T> {
    ptr: AtomicPtr<T>,
    _lifetime: PhantomData<&'arena ()>,
}

impl<'arena, T> Ref<'arena, T>
    where T: 'arena
{
    /// Create a new empty `Ref`. Dereferencing this reference before it is set
    /// will panic. The reference may be set exactly once.
    pub fn empty() -> Ref<'arena, T> {
        Ref {
            ptr: AtomicPtr::new(0 as *mut T),
            _lifetime: PhantomData,
        }
    }

    /// Create a new `Ref` from an existing ordinary borrow with the
    /// appropriate lifetime. The resulting `Ref` may not be re-set to any
    /// other value.
    pub fn new(r: &'arena T) -> Ref<'arena, T> {
        Ref {
            ptr: AtomicPtr::new(r as *const T as *mut T),
            _lifetime: PhantomData,
        }
    }

    /// Set the `Ref`. This may be done only once.
    pub fn set(&'arena self, to: &'arena T) {
        let ptr = to as *const T as *mut T;
        assert!(!ptr.is_null());
        if self.ptr.compare_and_swap(0 as *mut T, ptr, Ordering::Relaxed) != 0 as *mut T {
            panic!("Attempt to re-set a Ref that has already been set.");
        }
    }
}

impl<'arena, T> Deref for Ref<'arena, T>
    where T: 'arena
{
    type Target = T;
    fn deref(&self) -> &T {
        unsafe { mem::transmute(self.ptr.load(Ordering::Relaxed)) }
    }
}

impl<'arena, T> Clone for Ref<'arena, T>
    where T: 'arena
{
    fn clone(&self) -> Ref<'arena, T> {
        Ref {
            ptr: AtomicPtr::new(self.ptr.load(Ordering::Relaxed)),
            _lifetime: PhantomData,
        }
    }
}

impl<'arena, T> fmt::Debug for Ref<'arena, T>
    where T: 'arena + fmt::Debug
{
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        self.deref().fmt(f)
    }
}

#[cfg(test)]
mod test {
    use super::*;

    struct BasicTest<'arena> {
        id: u32,
        a: Ref<'arena, BasicTest<'arena>>,
        b: Ref<'arena, BasicTest<'arena>>,
    }

    #[test]
    fn basic_test() {
        let arena = Arena::new();

        let x = arena.alloc(BasicTest {
            id: 0,
            a: Ref::empty(),
            b: Ref::empty(),
        });
        let y = arena.alloc(BasicTest {
            id: 1,
            a: Ref::empty(),
            b: Ref::empty(),
        });
        let z = arena.alloc(BasicTest {
            id: 2,
            a: Ref::empty(),
            b: Ref::empty(),
        });
        x.a.set(y);
        x.b.set(z);
        y.a.set(x);
        y.b.set(z);
        z.a.set(x);
        z.b.set(y);

        assert!(x.a.id == 1);
        assert!(x.b.id == 2);
        assert!(y.a.id == 0);
        assert!(y.b.id == 2);
        assert!(z.a.id == 0);
        assert!(z.b.id == 1);
    }

    #[test]
    fn ref_from_borrow_test() {
        let arena = Arena::new();
        let x = arena.alloc(BasicTest {
            id: 0,
            a: Ref::empty(),
            b: Ref::empty(),
        });
        let y = arena.alloc(BasicTest {
            id: 1,
            a: Ref::new(x),
            b: Ref::empty(),
        });

        assert!(y.a.id == 0);
    }
}