]> gitweb.factorcode.org Git - factor.git/blob - vm/data_roots.hpp
vm: restructure data_roots so that its a sequence of handle/len pairs rather than...
[factor.git] / vm / data_roots.hpp
1 namespace factor
2 {
3
4 template<typename Type>
5 struct data_root : public tagged<Type> {
6         factor_vm *parent;
7
8         void push()
9         {
10                 parent->data_roots.push_back((cell)this);
11                 parent->data_roots.push_back(1);
12         }
13
14         explicit data_root(cell value_, factor_vm *parent_)
15                 : tagged<Type>(value_), parent(parent_)
16         {
17                 push();
18         }
19
20         explicit data_root(Type *value_, factor_vm *parent_) :
21                 tagged<Type>(value_), parent(parent_)
22         {
23                 push();
24         }
25
26         const data_root<Type>& operator=(const Type *x) { tagged<Type>::operator=(x); return *this; }
27         const data_root<Type>& operator=(const cell &x) { tagged<Type>::operator=(x); return *this; }
28
29         ~data_root()
30         {
31 #ifdef FACTOR_DEBUG
32                 assert(parent->data_roots.back() == 1);
33 #endif
34                 parent->data_roots.pop_back();
35 #ifdef FACTOR_DEBUG
36                 assert(parent->data_roots.back() == (cell)this);
37 #endif
38                 parent->data_roots.pop_back();
39         }
40 };
41
42 /* A similar hack for the bignum implementation */
43 struct gc_bignum {
44         bignum **addr;
45         factor_vm *parent;
46
47         gc_bignum(bignum **addr_, factor_vm *parent_) : addr(addr_), parent(parent_)
48         {
49                 if(*addr_) parent->check_data_pointer(*addr_);
50                 parent->bignum_roots.push_back((cell)addr);
51         }
52
53         ~gc_bignum()
54         {
55 #ifdef FACTOR_DEBUG
56                 assert(parent->bignum_roots.back() == (cell)addr);
57 #endif
58                 parent->bignum_roots.pop_back();
59         }
60 };
61
62 #define GC_BIGNUM(x) gc_bignum x##__data_root(&x,this)
63
64 }