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
#![deny(missing_docs)]
#![allow(unknown_lints)]
#![allow(mut_from_ref)]

//! When a value is put into the Arena, it will stay there for the whole
//! lifetime of the arena, and never move.
use std::cell::UnsafeCell;
use std::{fmt, mem};

const BASE: usize = 32;
const NUM_ALLOCATIONS: usize = 32;
const USIZE_BITS: usize = mem::size_of::<usize>() * 8;

/// A reference into the arena that can be used for lookup
#[derive(Clone, Copy, Debug)]
pub struct ArenaRef(usize);

/// An arena that can hold values of type `T`.
pub struct Arena<T> {
    len: UnsafeCell<usize>,
    arenas: UnsafeCell<[Vec<T>; NUM_ALLOCATIONS]>,
}

impl<T> Default for Arena<T> {
    fn default() -> Self {
        Arena {
            len: Default::default(),
            arenas: Default::default(),
        }
    }
}

impl<T> fmt::Debug for Arena<T>
where
    T: fmt::Debug,
{
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "[")?;
        let len = unsafe { *self.len.get() };
        for i in 0..len.saturating_sub(1) {
            write!(f, "{:?}, ", self.get(&ArenaRef(i)))?;
        }
        if len > 0 {
            write!(f, "{:?}, ", self.get(&ArenaRef(len - 1)))?;
        }
        write!(f, "]")
    }
}

impl<T> Clone for Arena<T>
where
    T: Clone,
{
    fn clone(&self) -> Self {
        unsafe {
            let arenas = (*self.arenas.get()).clone();
            let len = *self.len.get();
            Arena {
                len: UnsafeCell::new(len),
                arenas: UnsafeCell::new(arenas),
            }
        }
    }
}

/// An iterator over all elements in the Arena
pub struct ArenaIter<'a, T: 'a> {
    ofs: usize,
    arena: &'a Arena<T>,
}

impl<'a, T> Iterator for ArenaIter<'a, T> {
    type Item = &'a T;

    fn next(&mut self) -> Option<Self::Item> {
        unsafe {
            let len = *self.arena.len.get();
            let index = self.ofs;
            if index == len {
                None
            } else {
                self.ofs += 1;
                Some(self.arena.get(&ArenaRef(index)))
            }
        }
    }
}

impl<T> Arena<T> {
    /// Creates a new empty arena.
    pub fn new() -> Self {
        Arena::default()
    }

    /// Get a reference into the arena.
    ///
    /// Panics on out-of bound access.
    pub fn get(&self, arena_ref: &ArenaRef) -> &T {
        let i = arena_ref.0;
        if i >= unsafe { *self.len.get() } {
            panic!("Index out of bounds")
        }
        let (row, col) = Self::index(i);
        let arenas = unsafe { &*self.arenas.get() };
        &arenas[row][col]
    }

    /// Get a mutable reference into the arena.
    /// this is unsafe, since you could easily alias mutable references.
    pub unsafe fn get_mut(&self, owned: &ArenaRef) -> &mut T {
        let i = owned.0;
        let (row, col) = Self::index(i);
        let arenas = &mut *self.arenas.get();
        &mut arenas[row][col]
    }

    /// Puts a value into the arena, returning an owned reference
    pub fn append(&self, t: T) -> ArenaRef {
        let i = unsafe { *self.len.get() };
        let (row, col) = Self::index(i);
        if row > 31 {
            panic!("Arena out of space!");
        }
        let arenas = unsafe { &mut *self.arenas.get() };
        if col == 0 {
            // allocate new memory
            arenas[row] = Vec::with_capacity(BASE << row);
        }
        arenas[row].push(t);
        unsafe {
            *self.len.get() += 1;
        }
        ArenaRef(i)
    }

    /// Returns an iterator over all elements in the Arena
    pub fn iter(&self) -> ArenaIter<T> {
        ArenaIter {
            arena: self,
            ofs: 0,
        }
    }

    fn index(i: usize) -> (usize, usize) {
        let j = i / BASE + 1;
        let row = USIZE_BITS - j.leading_zeros() as usize - 1;
        (row, i - (2usize.pow(row as u32) - 1) * BASE)
    }

    #[cfg(test)]
    fn _debug_check_lengths(&self) {
        let arenas = unsafe { &mut *self.arenas.get() };

        for i in 0..NUM_ALLOCATIONS {
            assert!(arenas[i].len() <= BASE << i);
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::rc::Rc;

    #[test]
    fn simple() {
        unsafe {
            let arena = Arena::new();
            let a = arena.append(13);

            assert!(arena.get(&a) == &13);
            *arena.get_mut(&a) += 1;

            assert!(arena.get(&a) == &14);
        }
    }

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

        for i in 0..32 {
            arena.append(i);
        }

        let mut count = 0;

        let mut iter = arena.iter();

        while let Some(i) = iter.next() {
            assert_eq!(i, &count);
            count += 1;
        }
        assert_eq!(count, 32);
    }

    #[test]
    fn should_drop() {
        let rcs: Vec<_> = (0..1024).map(|_| Rc::new(0)).collect();
        {
            let arena = Arena::new();

            for rc in rcs.iter() {
                arena.append(rc.clone());
            }

            arena._debug_check_lengths();

            for rc in rcs.iter() {
                assert!(Rc::strong_count(rc) == 2);
            }
            // drop arena
        }
        for rc in rcs.iter() {
            assert!(Rc::strong_count(rc) == 1);
        }
    }

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

        let a = arena.append(0);
        let b = arena.append(1);

        assert_eq!(*arena.get(&a), 0);

        unsafe {
            *arena.get_mut(&b) += 1;
        }

        assert_eq!(*arena.get(&b), 2);
    }
}