]> gitweb.factorcode.org Git - factor.git/blob - vm/local_roots.hpp
Merge branch 'master' of git://factorcode.org/git/factor
[factor.git] / vm / local_roots.hpp
1 namespace factor
2 {
3
4 template<typename Type>
5 struct gc_root : public tagged<Type>
6 {
7         factor_vm *parent;
8
9         void push() { parent->check_tagged_pointer(tagged<Type>::value()); parent->gc_locals.push_back((cell)this); }
10         
11         explicit gc_root(cell value_,factor_vm *vm) : tagged<Type>(value_),parent(vm) { push(); }
12         explicit gc_root(Type *value_, factor_vm *vm) : tagged<Type>(value_),parent(vm) { push(); }
13
14         const gc_root<Type>& operator=(const Type *x) { tagged<Type>::operator=(x); return *this; }
15         const gc_root<Type>& operator=(const cell &x) { tagged<Type>::operator=(x); return *this; }
16
17         ~gc_root() {
18 #ifdef FACTOR_DEBUG
19                 assert(parent->gc_locals.back() == (cell)this);
20 #endif
21                 parent->gc_locals.pop_back();
22         }
23 };
24
25 /* A similar hack for the bignum implementation */
26 struct gc_bignum
27 {
28         bignum **addr;
29         factor_vm *parent;
30         gc_bignum(bignum **addr_, factor_vm *vm) : addr(addr_), parent(vm) {
31                 if(*addr_)
32                         parent->check_data_pointer(*addr_);
33                 parent->gc_bignums.push_back((cell)addr);
34         }
35
36         ~gc_bignum() {
37 #ifdef FACTOR_DEBUG
38                 assert(parent->gc_bignums.back() == (cell)addr);
39 #endif
40                 parent->gc_bignums.pop_back();
41         }
42 };
43
44 #define GC_BIGNUM(x) gc_bignum x##__gc_root(&x,this)
45
46 }