1/*
2    Title:  save_vec.h - The save vector holds temporary values that may move as
3    the result of a garbage collection.
4
5    Copyright (c) 2006, 2010 David C.J. Matthews
6
7    This library is free software; you can redistribute it and/or
8    modify it under the terms of the GNU Lesser General Public
9    License as published by the Free Software Foundation; either
10    version 2.1 of the License, or (at your option) any later version.
11
12    This library is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15    Lesser General Public License for more details.
16
17    You should have received a copy of the GNU Lesser General Public
18    License along with this library; if not, write to the Free Software
19    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20
21*/
22
23#ifndef SAVE_VEC_H_DEFINED
24#define SAVE_VEC_H_DEFINED
25
26#include "globals.h" // For PolyWord
27
28/* A handle is the address of an element of save_vec  */
29/* This element points at an element of the Poly heap */
30/* The element is currently represented as a (PolyWord *) */
31
32class SaveVecEntry {
33public:
34    SaveVecEntry(PolyWord w): m_Handle(w) {}
35    SaveVecEntry(): m_Handle(PolyWord::FromUnsigned(0)) {} // Just used when initialising the vec
36
37    PolyWord Word() { return m_Handle; }
38    PolyObject *WordP() { return m_Handle.AsObjPtr(); }
39
40private:
41    PolyWord m_Handle;
42
43    friend class SaveVec;
44};
45
46typedef SaveVecEntry *Handle;
47
48#define DEREFWORD(_x)            ((_x)->Word())
49#define DEREFHANDLE(_x)          ((_x)->WordP())
50#define DEREFWORDHANDLE(_x)      ((_x)->WordP())
51
52
53#define DEREFBYTEHANDLE(_x)      ((byte *)DEREFHANDLE(_x))
54#define DEREFLISTHANDLE(_x)      ((ML_Cons_Cell *)DEREFHANDLE(_x))
55#define DEREFSTREAMHANDLE(_x)    ((StreamToken*)DEREFHANDLE(_x))
56
57class ScanAddress;
58
59class SaveVec
60{
61public:
62    SaveVec();
63    ~SaveVec();
64
65    // Clear the save vec at the start of an RTS call
66    void init(void) { save_vec_addr = save_vec; }
67
68    // Add a word to the save vec
69    Handle push(PolyWord valu);
70
71    // Mark a position
72    Handle mark(void) { return save_vec_addr; }
73
74    // Reset to the mark
75    void reset(Handle mark);
76
77    bool isValidHandle(Handle h) { return h >= save_vec && h < save_vec_addr; } // Check it is in the range.
78
79    // Called by the garbage collector to scan and then update the addresses in the
80    // vector.
81    void gcScan(ScanAddress *process);
82
83private:
84    SaveVecEntry *save_vec;
85    SaveVecEntry *save_vec_addr;
86};
87
88#endif
89
90