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
/*
 * @Copyright 2020 Jason Carr
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

mod types;
mod collect;

pub use types::*;

use alloc::vec::Vec;
use crate::types::{Ix, HasIx, Error, Root, Weak};
use crate::entry::{Spot, Entry};

impl <T> Region<T> {
    #[inline]
    #[allow(unused)]
    pub(crate) fn check_ix(&self, ix: Ix<T>) -> Result<(), Error> {
        #[cfg(feature = "debug-arena")]
        {
            if ix.nonce != self.nonce {
                Err(Error::IncorrectRegion)?;
            } else if ix.generation < self.generation {
                Err(Error::EntryExpired)?;
            } else if ix.generation > self.generation {
                Err(Error::UnexpectedInternalState)?;
            }
        }
        Ok(())
    }

    pub(crate) fn entry_raw_mut(&mut self, ix: Ix<T>) -> MutEntry<T> {
        MutEntry {
            ix,
            entry: self.data.get_mut(ix.ix()).unwrap().get_mut().unwrap(),
            roots: &self.roots,
        }
    }

    pub(crate) fn entry(&self, ix: Ix<T>) -> Result<&Entry<T>, Error> {
        Ok(self.data.get(ix.ix())
            .ok_or(Error::Indeterminable)?
            .get()
            .ok_or(Error::Indeterminable)?)
    }

    pub(crate) fn weak(&self, ix: Ix<T>) -> Result<Weak<T>, Error> {
        Ok(self.entry(ix)?.weak(ix))
    }

    pub(crate) fn root(&self, ix: Ix<T>) -> Result<Root<T>, Error> {
        let e = self.entry(ix)?;
        let r = e.root(ix);
        // there will be two copies of the roots,
        // there is a strong reference inside the entry
        // and one in this method.
        // The entry will handle clearing its version if there
        // are no weak references

        // This is one copy, so it's already in the vec
        // only this method considers it already rooted
        if !r.is_rooted() { // new root, push to list
            self.roots.borrow_mut().push(r.clone());
        }
        Ok(r)
    }

    pub(crate) fn try_get_mut(&mut self, ix: Ix<T>) -> Result<&mut T, Error> {
        self.check_ix(ix)?;
        Ok(self.data.get_mut(ix.ix())
            .ok_or(Error::Indeterminable)?
            .get_mut()
            .ok_or(Error::Indeterminable)?
            .get_mut())
    }

    pub(crate) fn try_get(&self, ix: Ix<T>) -> Result<&T, Error> {
        self.check_ix(ix)?;
        Ok(self.data.get(ix.ix())
            .ok_or(Error::Indeterminable)?
            .get()
            .ok_or(Error::Indeterminable)?
            .get())
    }
}

impl <T> Region<T> {
    /**
     * Return the current capacity of this region. A collection won't
     * be triggered by allocation unless the desired amount exceeds the capacity.
     */
    #[inline]
    pub fn capacity(&self) -> usize {
        self.data.capacity()
    }
    /**
     * Return the current number of entries in the region.
     */
    #[inline]
    pub fn len(&self) -> usize {
        self.data.len()
    }
    /**
     * Returns true if there are currently no entries in this region.
     */
    #[inline]
    pub fn is_empty(&self) -> bool {
        self.data.is_empty()
    }
}

impl <T: 'static + HasIx<T>> Region<T> {

    /**
     * Ensure that the capacity supports new_elems more
     * elements, collecting garbage if necessary.
     */
    pub fn ensure(&mut self, additional: usize) {
        let len = self.data.len();
        let cap = self.data.capacity();
        if cap >= len + additional { return }
        let mut dst = Vec::with_capacity(len + core::cmp::max(len, additional));

        #[cfg(feature = "debug-arena")]
        let new_gen = (self.nonce, self.generation+1);

        let roots = self.roots.get_mut();
        Self::prim_gc_to(&mut self.data, &mut dst, roots,
            #[cfg(feature = "debug-arena")]
            (self.nonce, self.generation),
            #[cfg(feature = "debug-arena")]
            new_gen);
        *roots = roots.drain(..).filter(Root::is_rooted).collect();
        self.data = dst;

        #[cfg(feature = "debug-arena")]
        {
            self.generation = new_gen.1;
        }
    }

    /**
     * Allocate a new object, returning a temporary handle,
     * which can be used to mutate the object and to get
     * roots, weak pointers, and internal pointers to
     * the object.
     *
     * This may trigger a garbage collection and invalidate
     * raw indices.  As such, a function is used to
     * generate the new value, which
     * can query the state of the world post-collection.
     */
    pub fn alloc<F>(&mut self, make_t: F) -> MutEntry<T> where
        F: FnOnce(&Self) -> T
    {
        //else the index could be incorrect
        self.ensure(1);
        let n = self.data.len();
        self.data.push(Spot::new(make_t(&self)));
        self.entry_raw_mut(Ix::new(
            n,
            #[cfg(feature = "debug-arena")]
            self.nonce,
            #[cfg(feature = "debug-arena")]
            self.generation,
            ))
    }


    /**
     * Immediately trigger a standard garbage collection.
     *
     * This invalidates raw indices.
     *
     * ```rust
     * use moving_gc_arena as gc;
     * let mut r = gc::Region::new();
     *
     * r.alloc(|_|{()});
     * r.gc();
     * assert!(r.is_empty());
     * ```
     */
    pub fn gc(&mut self) {
        let mut dst = Vec::with_capacity(self.data.len());
        let roots = self.roots.get_mut();
        Self::prim_gc_to(&mut self.data, &mut dst, roots,
            #[cfg(feature = "debug-arena")]
            (self.nonce, self.generation),
            #[cfg(feature = "debug-arena")]
            (self.nonce, self.generation+1));
        let new_roots = self.take_valid_roots().collect();
        self.roots.replace(new_roots);
        self.data = dst;
        #[cfg(feature = "debug-arena")]
        {
            self.generation = self.generation+1;
        }
    }
    /**
     * Move the elements of this region onto the end of another Region.
     * This can trigger a collection in the other region if it
     * must be re-allocated.
     */
    pub fn gc_into(mut self, other: &mut Region<T>) {
        other.ensure(self.data.len());
        Self::prim_gc_to(&mut self.data, &mut other.data, self.roots.get_mut(),
            #[cfg(feature = "debug-arena")]
            (self.nonce, self.generation),
            #[cfg(feature = "debug-arena")]
            (other.nonce, other.generation));
        other.roots.get_mut().extend(self.take_valid_roots());
    }
    fn take_valid_roots(&mut self) -> impl Iterator<Item=Root<T>> + '_ {
        self.roots.get_mut().drain(..).filter(Root::is_rooted)
    }

    #[inline(always)]
    /**
     * Perform a depth-first search, applying a mutating function
     * to each of the values passed over. This traversal
     * uses the C stack, and so is subject to stack overflow,
     * but can use any state for the traversal.
     *
     * In order to ensure that this function performs well, the
     * HasIx implementation for T must be deterministic: it
     * should always return the same references. This is so
     * that we can correctly maintain marking information.
     *
     * There is currently no immutable variation on this function.
     */
    pub fn dfs_mut_cstack<'a, I, F>(&'a mut self, start: I, process: F)
    where
        T: HasIx<T>,
        F: FnMut(Ix<T>, &mut T),
        I: IntoIterator<Item=&'a Ix<T>>,
        I::IntoIter : Clone,
    {
        Self::prim_traverse_mut_cstack(&mut self.data, start.into_iter(), process)
    }
}